blob: 6817aa72619b07d9465c9e8cec792bc392ece2d5 [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 Moolenaar6a8958d2017-06-22 21:33:20 +020050/*
51 * Quickfix/Location list definition
52 * Contains a list of entries (qfline_T). qf_start points to the first entry
53 * and qf_last points to the last entry. qf_count contains the list size.
54 *
55 * Usually the list contains one or more entries. But an empty list can be
56 * created using setqflist()/setloclist() with a title and/or user context
57 * information and entries can be added later using setqflist()/setloclist().
58 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000059typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000060{
Bram Moolenaara539f4f2017-08-30 20:33:55 +020061 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000062 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020063 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020065 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 int qf_index; /* current index in the error list */
67 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020068 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020069 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020070 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020071
72 struct dir_stack_T *qf_dir_stack;
73 char_u *qf_directory;
74 struct dir_stack_T *qf_file_stack;
75 char_u *qf_currfile;
76 int qf_multiline;
77 int qf_multiignore;
78 int qf_multiscan;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000079} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020081/*
82 * Quickfix/Location list stack definition
83 * Contains a list of quickfix/location lists (qf_list_T)
84 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000085struct qf_info_S
86{
87 /*
88 * Count of references to this list. Used only for location lists.
89 * When a location list window reference this list, qf_refcount
90 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
91 * reaches 0, the list is freed.
92 */
93 int qf_refcount;
94 int qf_listcount; /* current number of lists */
95 int qf_curlist; /* current error list */
96 qf_list_T qf_lists[LISTCOUNT];
97};
98
99static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200100static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000102#define FMT_PATTERNS 10 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103
104/*
105 * Structure used to hold the info of one part of 'errorformat'
106 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000107typedef struct efm_S efm_T;
108struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109{
110 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000111 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
113 char_u prefix; /* prefix of this format line: */
114 /* 'D' enter directory */
115 /* 'X' leave directory */
116 /* 'A' start of multi-line message */
117 /* 'E' error message */
118 /* 'W' warning message */
119 /* 'I' informational message */
120 /* 'C' continuation line */
121 /* 'Z' end of multi-line message */
122 /* 'G' general, unspecific message */
123 /* 'P' push file (partial) message */
124 /* 'Q' pop/quit file (partial) message */
125 /* 'O' overread (partial) message */
126 char_u flags; /* additional flags given in prefix */
127 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000128 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000129 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130};
131
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100132static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
133
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200134static int qf_init_ext(qf_info_T *qi, int qf_idx, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title, char_u *enc);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200135static void qf_store_title(qf_info_T *qi, int qf_idx, char_u *title);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void qf_new_list(qf_info_T *qi, char_u *qf_title);
137static void ll_free_all(qf_info_T **pqi);
Bram Moolenaara3921f42017-06-04 15:30:34 +0200138static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static qf_info_T *ll_new_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void qf_free(qf_info_T *qi, int idx);
141static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200142static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200143static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200145static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
147static void qf_clean_dir_stack(struct dir_stack_T **);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
149static int is_qf_win(win_T *win, qf_info_T *qi);
150static win_T *qf_find_win(qf_info_T *qi);
151static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200152static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200154static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static char_u *get_mef_name(void);
156static void restore_start_dir(char_u *dirname_start);
157static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
158static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
159static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
160static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000162/* Quickfix window check helper macro */
163#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
164/* Location list window check helper macro */
165#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
166/*
167 * Return location list for window 'wp'
168 * For location list window, return the referenced location list
169 */
170#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200173 * Looking up a buffer can be slow if there are many. Remember the last one
174 * to make this a lot faster if there are multiple matches in the same file.
175 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200176static char_u *qf_last_bufname = NULL;
177static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200178
179/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000180 * Read the errorfile "efile" into memory, line by line, building the error
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200181 * list. Set the error list's title to qf_title.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 * Return -1 for error, number of errors for success.
183 */
184 int
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200185qf_init(win_T *wp,
186 char_u *efile,
187 char_u *errorformat,
188 int newlist, /* TRUE: start a new error list */
189 char_u *qf_title,
190 char_u *enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191{
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000192 qf_info_T *qi = &ql_info;
193
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000194 if (wp != NULL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000195 {
196 qi = ll_get_or_alloc_list(wp);
197 if (qi == NULL)
198 return FAIL;
199 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000200
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200201 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
202 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000203}
204
205/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200206 * Maximum number of bytes allowed per line while reading a errorfile.
207 */
208#define LINE_MAXLEN 4096
209
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200210static struct fmtpattern
211{
212 char_u convchar;
213 char *pattern;
214} fmt_pat[FMT_PATTERNS] =
215 {
216 {'f', ".\\+"}, /* only used when at end */
217 {'n', "\\d\\+"},
218 {'l', "\\d\\+"},
219 {'c', "\\d\\+"},
220 {'t', "."},
221 {'m', ".\\+"},
222 {'r', ".*"},
223 {'p', "[- .]*"},
224 {'v', "\\d\\+"},
225 {'s', ".\\+"}
226 };
227
228/*
229 * Converts a 'errorformat' string to regular expression pattern
230 */
231 static int
232efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200233 char_u *efm,
234 int len,
235 efm_T *fmt_ptr,
236 char_u *regpat,
237 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200238{
239 char_u *ptr;
240 char_u *efmp;
241 char_u *srcptr;
242 int round;
243 int idx = 0;
244
245 /*
246 * Build regexp pattern from current 'errorformat' option
247 */
248 ptr = regpat;
249 *ptr++ = '^';
250 round = 0;
251 for (efmp = efm; efmp < efm + len; ++efmp)
252 {
253 if (*efmp == '%')
254 {
255 ++efmp;
256 for (idx = 0; idx < FMT_PATTERNS; ++idx)
257 if (fmt_pat[idx].convchar == *efmp)
258 break;
259 if (idx < FMT_PATTERNS)
260 {
261 if (fmt_ptr->addr[idx])
262 {
263 sprintf((char *)errmsg,
264 _("E372: Too many %%%c in format string"), *efmp);
265 EMSG(errmsg);
266 return -1;
267 }
268 if ((idx
269 && idx < 6
270 && vim_strchr((char_u *)"DXOPQ",
271 fmt_ptr->prefix) != NULL)
272 || (idx == 6
273 && vim_strchr((char_u *)"OPQ",
274 fmt_ptr->prefix) == NULL))
275 {
276 sprintf((char *)errmsg,
277 _("E373: Unexpected %%%c in format string"), *efmp);
278 EMSG(errmsg);
279 return -1;
280 }
281 fmt_ptr->addr[idx] = (char_u)++round;
282 *ptr++ = '\\';
283 *ptr++ = '(';
284#ifdef BACKSLASH_IN_FILENAME
285 if (*efmp == 'f')
286 {
287 /* Also match "c:" in the file name, even when
288 * checking for a colon next: "%f:".
289 * "\%(\a:\)\=" */
290 STRCPY(ptr, "\\%(\\a:\\)\\=");
291 ptr += 10;
292 }
293#endif
294 if (*efmp == 'f' && efmp[1] != NUL)
295 {
296 if (efmp[1] != '\\' && efmp[1] != '%')
297 {
298 /* A file name may contain spaces, but this isn't
299 * in "\f". For "%f:%l:%m" there may be a ":" in
300 * the file name. Use ".\{-1,}x" instead (x is
301 * the next character), the requirement that :999:
302 * follows should work. */
303 STRCPY(ptr, ".\\{-1,}");
304 ptr += 7;
305 }
306 else
307 {
308 /* File name followed by '\\' or '%': include as
309 * many file name chars as possible. */
310 STRCPY(ptr, "\\f\\+");
311 ptr += 4;
312 }
313 }
314 else
315 {
316 srcptr = (char_u *)fmt_pat[idx].pattern;
317 while ((*ptr = *srcptr++) != NUL)
318 ++ptr;
319 }
320 *ptr++ = '\\';
321 *ptr++ = ')';
322 }
323 else if (*efmp == '*')
324 {
325 if (*++efmp == '[' || *efmp == '\\')
326 {
327 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
328 {
329 if (efmp[1] == '^')
330 *ptr++ = *++efmp;
331 if (efmp < efm + len)
332 {
333 *ptr++ = *++efmp; /* could be ']' */
334 while (efmp < efm + len
335 && (*ptr++ = *++efmp) != ']')
336 /* skip */;
337 if (efmp == efm + len)
338 {
339 EMSG(_("E374: Missing ] in format string"));
340 return -1;
341 }
342 }
343 }
344 else if (efmp < efm + len) /* %*\D, %*\s etc. */
345 *ptr++ = *++efmp;
346 *ptr++ = '\\';
347 *ptr++ = '+';
348 }
349 else
350 {
351 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
352 sprintf((char *)errmsg,
353 _("E375: Unsupported %%%c in format string"), *efmp);
354 EMSG(errmsg);
355 return -1;
356 }
357 }
358 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
359 *ptr++ = *efmp; /* regexp magic characters */
360 else if (*efmp == '#')
361 *ptr++ = '*';
362 else if (*efmp == '>')
363 fmt_ptr->conthere = TRUE;
364 else if (efmp == efm + 1) /* analyse prefix */
365 {
366 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
367 fmt_ptr->flags = *efmp++;
368 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
369 fmt_ptr->prefix = *efmp;
370 else
371 {
372 sprintf((char *)errmsg,
373 _("E376: Invalid %%%c in format string prefix"), *efmp);
374 EMSG(errmsg);
375 return -1;
376 }
377 }
378 else
379 {
380 sprintf((char *)errmsg,
381 _("E377: Invalid %%%c in format string"), *efmp);
382 EMSG(errmsg);
383 return -1;
384 }
385 }
386 else /* copy normal character */
387 {
388 if (*efmp == '\\' && efmp + 1 < efm + len)
389 ++efmp;
390 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
391 *ptr++ = '\\'; /* escape regexp atoms */
392 if (*efmp)
393 *ptr++ = *efmp;
394 }
395 }
396 *ptr++ = '$';
397 *ptr = NUL;
398
399 return 0;
400}
401
402 static void
403free_efm_list(efm_T **efm_first)
404{
405 efm_T *efm_ptr;
406
407 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
408 {
409 *efm_first = efm_ptr->next;
410 vim_regfree(efm_ptr->prog);
411 vim_free(efm_ptr);
412 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100413 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200414}
415
416/* Parse 'errorformat' option */
417 static efm_T *
418parse_efm_option(char_u *efm)
419{
420 char_u *errmsg = NULL;
421 int errmsglen;
422 efm_T *fmt_ptr = NULL;
423 efm_T *fmt_first = NULL;
424 efm_T *fmt_last = NULL;
425 char_u *fmtstr = NULL;
426 int len;
427 int i;
428 int round;
429
430 errmsglen = CMDBUFFSIZE + 1;
431 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
432 if (errmsg == NULL)
433 goto parse_efm_end;
434
435 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200436 * Each part of the format string is copied and modified from errorformat
437 * to regex prog. Only a few % characters are allowed.
438 */
439
440 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200441 * Get some space to modify the format string into.
442 */
443 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
444 for (round = FMT_PATTERNS; round > 0; )
445 i += (int)STRLEN(fmt_pat[--round].pattern);
Bram Moolenaar0b05e492017-09-24 19:39:09 +0200446#ifdef BACKSLASH_IN_FILENAME
447 i += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200448#else
449 i += 2; /* "%f" can become two chars longer */
450#endif
451 if ((fmtstr = alloc(i)) == NULL)
452 goto parse_efm_error;
453
454 while (efm[0] != NUL)
455 {
456 /*
457 * Allocate a new eformat structure and put it at the end of the list
458 */
459 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
460 if (fmt_ptr == NULL)
461 goto parse_efm_error;
462 if (fmt_first == NULL) /* first one */
463 fmt_first = fmt_ptr;
464 else
465 fmt_last->next = fmt_ptr;
466 fmt_last = fmt_ptr;
467
468 /*
469 * Isolate one part in the 'errorformat' option
470 */
471 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
472 if (efm[len] == '\\' && efm[len + 1] != NUL)
473 ++len;
474
475 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
476 goto parse_efm_error;
477 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
478 goto parse_efm_error;
479 /*
480 * Advance to next part
481 */
482 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
483 }
484
485 if (fmt_first == NULL) /* nothing found */
486 EMSG(_("E378: 'errorformat' contains no pattern"));
487
488 goto parse_efm_end;
489
490parse_efm_error:
491 free_efm_list(&fmt_first);
492
493parse_efm_end:
494 vim_free(fmtstr);
495 vim_free(errmsg);
496
497 return fmt_first;
498}
499
Bram Moolenaare0d37972016-07-15 22:36:01 +0200500enum {
501 QF_FAIL = 0,
502 QF_OK = 1,
503 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200504 QF_NOMEM = 3,
505 QF_IGNORE_LINE = 4
Bram Moolenaare0d37972016-07-15 22:36:01 +0200506};
507
508typedef struct {
509 char_u *linebuf;
510 int linelen;
511 char_u *growbuf;
512 int growbufsiz;
513 FILE *fd;
514 typval_T *tv;
515 char_u *p_str;
516 listitem_T *p_li;
517 buf_T *buf;
518 linenr_T buflnum;
519 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100520 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200521} qfstate_T;
522
523 static char_u *
524qf_grow_linebuf(qfstate_T *state, int newsz)
525{
526 /*
527 * If the line exceeds LINE_MAXLEN exclude the last
528 * byte since it's not a NL character.
529 */
530 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
531 if (state->growbuf == NULL)
532 {
533 state->growbuf = alloc(state->linelen + 1);
534 if (state->growbuf == NULL)
535 return NULL;
536 state->growbufsiz = state->linelen;
537 }
538 else if (state->linelen > state->growbufsiz)
539 {
540 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1);
541 if (state->growbuf == NULL)
542 return NULL;
543 state->growbufsiz = state->linelen;
544 }
545 return state->growbuf;
546}
547
548/*
549 * Get the next string (separated by newline) from state->p_str.
550 */
551 static int
552qf_get_next_str_line(qfstate_T *state)
553{
554 /* Get the next line from the supplied string */
555 char_u *p_str = state->p_str;
556 char_u *p;
557 int len;
558
559 if (*p_str == NUL) /* Reached the end of the string */
560 return QF_END_OF_INPUT;
561
562 p = vim_strchr(p_str, '\n');
563 if (p != NULL)
564 len = (int)(p - p_str) + 1;
565 else
566 len = (int)STRLEN(p_str);
567
568 if (len > IOSIZE - 2)
569 {
570 state->linebuf = qf_grow_linebuf(state, len);
571 if (state->linebuf == NULL)
572 return QF_NOMEM;
573 }
574 else
575 {
576 state->linebuf = IObuff;
577 state->linelen = len;
578 }
579 vim_strncpy(state->linebuf, p_str, state->linelen);
580
581 /*
582 * Increment using len in order to discard the rest of the
583 * line if it exceeds LINE_MAXLEN.
584 */
585 p_str += len;
586 state->p_str = p_str;
587
588 return QF_OK;
589}
590
591/*
592 * Get the next string from state->p_Li.
593 */
594 static int
595qf_get_next_list_line(qfstate_T *state)
596{
597 listitem_T *p_li = state->p_li;
598 int len;
599
600 while (p_li != NULL
601 && (p_li->li_tv.v_type != VAR_STRING
602 || p_li->li_tv.vval.v_string == NULL))
603 p_li = p_li->li_next; /* Skip non-string items */
604
605 if (p_li == NULL) /* End of the list */
606 {
607 state->p_li = NULL;
608 return QF_END_OF_INPUT;
609 }
610
611 len = (int)STRLEN(p_li->li_tv.vval.v_string);
612 if (len > IOSIZE - 2)
613 {
614 state->linebuf = qf_grow_linebuf(state, len);
615 if (state->linebuf == NULL)
616 return QF_NOMEM;
617 }
618 else
619 {
620 state->linebuf = IObuff;
621 state->linelen = len;
622 }
623
624 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
625
626 state->p_li = p_li->li_next; /* next item */
627 return QF_OK;
628}
629
630/*
631 * Get the next string from state->buf.
632 */
633 static int
634qf_get_next_buf_line(qfstate_T *state)
635{
636 char_u *p_buf = NULL;
637 int len;
638
639 /* Get the next line from the supplied buffer */
640 if (state->buflnum > state->lnumlast)
641 return QF_END_OF_INPUT;
642
643 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
644 state->buflnum += 1;
645
646 len = (int)STRLEN(p_buf);
647 if (len > IOSIZE - 2)
648 {
649 state->linebuf = qf_grow_linebuf(state, len);
650 if (state->linebuf == NULL)
651 return QF_NOMEM;
652 }
653 else
654 {
655 state->linebuf = IObuff;
656 state->linelen = len;
657 }
658 vim_strncpy(state->linebuf, p_buf, state->linelen);
659
660 return QF_OK;
661}
662
663/*
664 * Get the next string from file state->fd.
665 */
666 static int
667qf_get_next_file_line(qfstate_T *state)
668{
669 int discard;
670 int growbuflen;
671
672 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
673 return QF_END_OF_INPUT;
674
675 discard = FALSE;
676 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200677 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200678 {
679 /*
680 * The current line exceeds IObuff, continue reading using
681 * growbuf until EOL or LINE_MAXLEN bytes is read.
682 */
683 if (state->growbuf == NULL)
684 {
685 state->growbufsiz = 2 * (IOSIZE - 1);
686 state->growbuf = alloc(state->growbufsiz);
687 if (state->growbuf == NULL)
688 return QF_NOMEM;
689 }
690
691 /* Copy the read part of the line, excluding null-terminator */
692 memcpy(state->growbuf, IObuff, IOSIZE - 1);
693 growbuflen = state->linelen;
694
695 for (;;)
696 {
697 if (fgets((char *)state->growbuf + growbuflen,
698 state->growbufsiz - growbuflen, state->fd) == NULL)
699 break;
700 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
701 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200702 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200703 break;
704 if (state->growbufsiz == LINE_MAXLEN)
705 {
706 discard = TRUE;
707 break;
708 }
709
710 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
711 ? 2 * state->growbufsiz : LINE_MAXLEN;
712 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz);
713 if (state->growbuf == NULL)
714 return QF_NOMEM;
715 }
716
717 while (discard)
718 {
719 /*
720 * The current line is longer than LINE_MAXLEN, continue
721 * reading but discard everything until EOL or EOF is
722 * reached.
723 */
724 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
725 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200726 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200727 break;
728 }
729
730 state->linebuf = state->growbuf;
731 state->linelen = growbuflen;
732 }
733 else
734 state->linebuf = IObuff;
735
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100736#ifdef FEAT_MBYTE
737 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200738 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
739 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100740 char_u *line;
741
742 line = string_convert(&state->vc, state->linebuf, &state->linelen);
743 if (line != NULL)
744 {
745 if (state->linelen < IOSIZE)
746 {
747 STRCPY(state->linebuf, line);
748 vim_free(line);
749 }
750 else
751 {
752 vim_free(state->growbuf);
753 state->linebuf = state->growbuf = line;
754 state->growbufsiz = state->linelen < LINE_MAXLEN
755 ? state->linelen : LINE_MAXLEN;
756 }
757 }
758 }
759#endif
760
Bram Moolenaare0d37972016-07-15 22:36:01 +0200761 return QF_OK;
762}
763
764/*
765 * Get the next string from a file/buffer/list/string.
766 */
767 static int
768qf_get_nextline(qfstate_T *state)
769{
770 int status = QF_FAIL;
771
772 if (state->fd == NULL)
773 {
774 if (state->tv != NULL)
775 {
776 if (state->tv->v_type == VAR_STRING)
777 /* Get the next line from the supplied string */
778 status = qf_get_next_str_line(state);
779 else if (state->tv->v_type == VAR_LIST)
780 /* Get the next line from the supplied list */
781 status = qf_get_next_list_line(state);
782 }
783 else
784 /* Get the next line from the supplied buffer */
785 status = qf_get_next_buf_line(state);
786 }
787 else
788 /* Get the next line from the supplied file */
789 status = qf_get_next_file_line(state);
790
791 if (status != QF_OK)
792 return status;
793
794 /* remove newline/CR from the line */
795 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200796 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200797 state->linebuf[state->linelen - 1] = NUL;
798#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200799 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
800 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200801#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200802 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200803
804#ifdef FEAT_MBYTE
805 remove_bom(state->linebuf);
806#endif
807
808 return QF_OK;
809}
810
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200811typedef struct {
812 char_u *namebuf;
813 char_u *errmsg;
814 int errmsglen;
815 long lnum;
816 int col;
817 char_u use_viscol;
818 char_u *pattern;
819 int enr;
820 int type;
821 int valid;
822} qffields_T;
823
824/*
825 * Parse a line and get the quickfix fields.
826 * Return the QF_ status.
827 */
828 static int
829qf_parse_line(
830 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200831 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200832 char_u *linebuf,
833 int linelen,
834 efm_T *fmt_first,
835 qffields_T *fields)
836{
837 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200838 char_u *ptr;
839 int len;
840 int i;
841 int idx = 0;
842 char_u *tail = NULL;
843 regmatch_T regmatch;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200844 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200845
846 /* Always ignore case when looking for a matching error. */
847 regmatch.rm_ic = TRUE;
848
849 /* If there was no %> item start at the first pattern */
850 if (fmt_start == NULL)
851 fmt_ptr = fmt_first;
852 else
853 {
854 fmt_ptr = fmt_start;
855 fmt_start = NULL;
856 }
857
858 /*
859 * Try to match each part of 'errorformat' until we find a complete
860 * match or no match.
861 */
862 fields->valid = TRUE;
863restofline:
864 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
865 {
866 int r;
867
868 idx = fmt_ptr->prefix;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200869 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200870 continue;
871 fields->namebuf[0] = NUL;
872 fields->pattern[0] = NUL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200873 if (!qfl->qf_multiscan)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200874 fields->errmsg[0] = NUL;
875 fields->lnum = 0;
876 fields->col = 0;
877 fields->use_viscol = FALSE;
878 fields->enr = -1;
879 fields->type = 0;
880 tail = NULL;
881
882 regmatch.regprog = fmt_ptr->prog;
883 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
884 fmt_ptr->prog = regmatch.regprog;
885 if (r)
886 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200887 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200888 continue;
889 if (vim_strchr((char_u *)"EWI", idx) != NULL)
890 fields->type = idx;
891 else
892 fields->type = 0;
893 /*
894 * Extract error message data from matched line.
895 * We check for an actual submatch, because "\[" and "\]" in
896 * the 'errorformat' may cause the wrong submatch to be used.
897 */
898 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
899 {
900 int c;
901
902 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
903 continue;
904
905 /* Expand ~/file and $HOME/file to full path. */
906 c = *regmatch.endp[i];
907 *regmatch.endp[i] = NUL;
908 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE);
909 *regmatch.endp[i] = c;
910
911 if (vim_strchr((char_u *)"OPQ", idx) != NULL
912 && mch_getperm(fields->namebuf) == -1)
913 continue;
914 }
915 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
916 {
917 if (regmatch.startp[i] == NULL)
918 continue;
919 fields->enr = (int)atol((char *)regmatch.startp[i]);
920 }
921 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
922 {
923 if (regmatch.startp[i] == NULL)
924 continue;
925 fields->lnum = atol((char *)regmatch.startp[i]);
926 }
927 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
928 {
929 if (regmatch.startp[i] == NULL)
930 continue;
931 fields->col = (int)atol((char *)regmatch.startp[i]);
932 }
933 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
934 {
935 if (regmatch.startp[i] == NULL)
936 continue;
937 fields->type = *regmatch.startp[i];
938 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200939 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200940 {
Bram Moolenaar253f9122017-05-15 08:45:13 +0200941 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200942 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200943 /* linelen + null terminator */
944 if ((fields->errmsg = vim_realloc(fields->errmsg,
945 linelen + 1)) == NULL)
946 return QF_NOMEM;
947 fields->errmsglen = linelen + 1;
948 }
949 vim_strncpy(fields->errmsg, linebuf, linelen);
950 }
951 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
952 {
953 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
954 continue;
955 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
Bram Moolenaar253f9122017-05-15 08:45:13 +0200956 if (len >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200957 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200958 /* len + null terminator */
959 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1))
960 == NULL)
961 return QF_NOMEM;
962 fields->errmsglen = len + 1;
963 }
964 vim_strncpy(fields->errmsg, regmatch.startp[i], len);
965 }
966 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
967 {
968 if (regmatch.startp[i] == NULL)
969 continue;
970 tail = regmatch.startp[i];
971 }
972 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
973 {
974 char_u *match_ptr;
975
976 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
977 continue;
978 fields->col = 0;
979 for (match_ptr = regmatch.startp[i];
980 match_ptr != regmatch.endp[i]; ++match_ptr)
981 {
982 ++fields->col;
983 if (*match_ptr == TAB)
984 {
985 fields->col += 7;
986 fields->col -= fields->col % 8;
987 }
988 }
989 ++fields->col;
990 fields->use_viscol = TRUE;
991 }
992 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
993 {
994 if (regmatch.startp[i] == NULL)
995 continue;
996 fields->col = (int)atol((char *)regmatch.startp[i]);
997 fields->use_viscol = TRUE;
998 }
999 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
1000 {
1001 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
1002 continue;
1003 len = (int)(regmatch.endp[i] - regmatch.startp[i]);
1004 if (len > CMDBUFFSIZE - 5)
1005 len = CMDBUFFSIZE - 5;
1006 STRCPY(fields->pattern, "^\\V");
1007 STRNCAT(fields->pattern, regmatch.startp[i], len);
1008 fields->pattern[len + 3] = '\\';
1009 fields->pattern[len + 4] = '$';
1010 fields->pattern[len + 5] = NUL;
1011 }
1012 break;
1013 }
1014 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001015 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001016
1017 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1018 {
1019 if (fmt_ptr != NULL)
1020 {
1021 if (idx == 'D') /* enter directory */
1022 {
1023 if (*fields->namebuf == NUL)
1024 {
1025 EMSG(_("E379: Missing or empty directory name"));
1026 return QF_FAIL;
1027 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001028 qfl->qf_directory =
1029 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1030 if (qfl->qf_directory == NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001031 return QF_FAIL;
1032 }
1033 else if (idx == 'X') /* leave directory */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001034 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001035 }
1036 fields->namebuf[0] = NUL; /* no match found, remove file name */
1037 fields->lnum = 0; /* don't jump to this line */
1038 fields->valid = FALSE;
Bram Moolenaar253f9122017-05-15 08:45:13 +02001039 if (linelen >= fields->errmsglen)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001040 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001041 /* linelen + null terminator */
1042 if ((fields->errmsg = vim_realloc(fields->errmsg,
1043 linelen + 1)) == NULL)
1044 return QF_NOMEM;
1045 fields->errmsglen = linelen + 1;
1046 }
1047 /* copy whole line to error message */
1048 vim_strncpy(fields->errmsg, linebuf, linelen);
1049 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001050 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001051 }
1052 else if (fmt_ptr != NULL)
1053 {
1054 /* honor %> item */
1055 if (fmt_ptr->conthere)
1056 fmt_start = fmt_ptr;
1057
1058 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1059 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001060 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1061 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001062 }
1063 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1064 { /* continuation of multi-line msg */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001065 if (!qfl->qf_multiignore)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001066 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001067 qfline_T *qfprev = qfl->qf_last;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001068
Bram Moolenaar9b457942016-10-09 16:10:05 +02001069 if (qfprev == NULL)
1070 return QF_FAIL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001071 if (*fields->errmsg && !qfl->qf_multiignore)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001072 {
1073 len = (int)STRLEN(qfprev->qf_text);
1074 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1075 == NULL)
1076 return QF_FAIL;
1077 STRCPY(ptr, qfprev->qf_text);
1078 vim_free(qfprev->qf_text);
1079 qfprev->qf_text = ptr;
1080 *(ptr += len) = '\n';
1081 STRCPY(++ptr, fields->errmsg);
1082 }
1083 if (qfprev->qf_nr == -1)
1084 qfprev->qf_nr = fields->enr;
1085 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1086 /* only printable chars allowed */
1087 qfprev->qf_type = fields->type;
1088
1089 if (!qfprev->qf_lnum)
1090 qfprev->qf_lnum = fields->lnum;
1091 if (!qfprev->qf_col)
1092 qfprev->qf_col = fields->col;
1093 qfprev->qf_viscol = fields->use_viscol;
1094 if (!qfprev->qf_fnum)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001095 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1096 qfl->qf_directory,
1097 *fields->namebuf || qfl->qf_directory != NULL
Bram Moolenaar9b457942016-10-09 16:10:05 +02001098 ? fields->namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001099 : qfl->qf_currfile != NULL && fields->valid
1100 ? qfl->qf_currfile : 0);
Bram Moolenaar9b457942016-10-09 16:10:05 +02001101 }
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001102 if (idx == 'Z')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001103 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001104 line_breakcheck();
1105 return QF_IGNORE_LINE;
1106 }
1107 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
1108 {
1109 /* global file names */
1110 fields->valid = FALSE;
1111 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1112 {
1113 if (*fields->namebuf && idx == 'P')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001114 qfl->qf_currfile =
1115 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001116 else if (idx == 'Q')
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001117 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001118 *fields->namebuf = NUL;
1119 if (tail && *tail)
1120 {
1121 STRMOVE(IObuff, skipwhite(tail));
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001122 qfl->qf_multiscan = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001123 goto restofline;
1124 }
1125 }
1126 }
1127 if (fmt_ptr->flags == '-') /* generally exclude this line */
1128 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001129 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001130 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001131 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001132 return QF_IGNORE_LINE;
1133 }
1134 }
1135
1136 return QF_OK;
1137}
1138
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001139/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001140 * Read the errorfile "efile" into memory, line by line, building the error
1141 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001142 * Alternative: when "efile" is NULL read errors from buffer "buf".
1143 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001144 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001145 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1146 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001147 * Return -1 for error, number of errors for success.
1148 */
1149 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001150qf_init_ext(
1151 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001152 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001153 char_u *efile,
1154 buf_T *buf,
1155 typval_T *tv,
1156 char_u *errorformat,
1157 int newlist, /* TRUE: start a new error list */
1158 linenr_T lnumfirst, /* first line number to use */
1159 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001160 char_u *qf_title,
1161 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001162{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001163 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001164 qfstate_T state;
1165 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001166 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001167 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001168 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001170 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001172 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001174 /* Do not used the cached buffer, it may have been wiped out. */
1175 vim_free(qf_last_bufname);
1176 qf_last_bufname = NULL;
1177
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001178 vim_memset(&state, 0, sizeof(state));
1179 vim_memset(&fields, 0, sizeof(fields));
1180#ifdef FEAT_MBYTE
1181 state.vc.vc_type = CONV_NONE;
1182 if (enc != NULL && *enc != NUL)
1183 convert_setup(&state.vc, enc, p_enc);
1184#endif
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001185 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1186 fields.errmsglen = CMDBUFFSIZE + 1;
1187 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
1188 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
Bram Moolenaar55b69262017-08-13 13:42:01 +02001189 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 goto qf_init_end;
1191
Bram Moolenaare0d37972016-07-15 22:36:01 +02001192 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 {
1194 EMSG2(_(e_openerrf), efile);
1195 goto qf_init_end;
1196 }
1197
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001198 if (newlist || qf_idx == qi->qf_listcount)
1199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001201 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001202 qf_idx = qi->qf_curlist;
1203 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001204 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001205 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001206 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001207 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001208 if (qi->qf_lists[qf_idx].qf_count > 0)
1209 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001212 qfl = &qi->qf_lists[qf_idx];
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001215 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001216 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 else
1218 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001220 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001221 * If the errorformat didn't change between calls, then reuse the
1222 * previously parsed values.
1223 */
1224 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1225 {
1226 /* free the previously parsed data */
1227 vim_free(last_efm);
1228 last_efm = NULL;
1229 free_efm_list(&fmt_first);
1230
1231 /* parse the current 'efm' */
1232 fmt_first = parse_efm_option(efm);
1233 if (fmt_first != NULL)
1234 last_efm = vim_strsave(efm);
1235 }
1236
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
1240 /*
1241 * got_int is reset here, because it was probably set when killing the
1242 * ":make" command, but we still want to read the errorfile then.
1243 */
1244 got_int = FALSE;
1245
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001246 if (tv != NULL)
1247 {
1248 if (tv->v_type == VAR_STRING)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001249 state.p_str = tv->vval.v_string;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001250 else if (tv->v_type == VAR_LIST)
Bram Moolenaare0d37972016-07-15 22:36:01 +02001251 state.p_li = tv->vval.v_list->lv_first;
1252 state.tv = tv;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001253 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001254 state.buf = buf;
Bram Moolenaarbfafb4c2016-07-16 14:20:45 +02001255 state.buflnum = lnumfirst;
1256 state.lnumlast = lnumlast;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 /*
1259 * Read the lines in the error file one by one.
1260 * Try to recognize one of the error formats in each line.
1261 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001262 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001264 /* Get the next line from a file/buffer/list/string */
1265 status = qf_get_nextline(&state);
1266 if (status == QF_NOMEM) /* memory alloc failure */
1267 goto qf_init_end;
1268 if (status == QF_END_OF_INPUT) /* end of input */
1269 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001271 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1272 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001273 if (status == QF_FAIL)
1274 goto error2;
1275 if (status == QF_NOMEM)
1276 goto qf_init_end;
1277 if (status == QF_IGNORE_LINE)
1278 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001280 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001281 qf_idx,
1282 qfl->qf_directory,
1283 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001284 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001285 : ((qfl->qf_currfile != NULL && fields.valid)
1286 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001287 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001288 fields.errmsg,
1289 fields.lnum,
1290 fields.col,
1291 fields.use_viscol,
1292 fields.pattern,
1293 fields.enr,
1294 fields.type,
1295 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 goto error2;
1297 line_breakcheck();
1298 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001299 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001301 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001303 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001304 qfl->qf_ptr = qfl->qf_start;
1305 qfl->qf_index = 1;
1306 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
1308 else
1309 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001310 qfl->qf_nonevalid = FALSE;
1311 if (qfl->qf_ptr == NULL)
1312 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001314 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001315 retval = qfl->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 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001322 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001323 qf_free(qi, qi->qf_curlist);
1324 qi->qf_listcount--;
1325 if (qi->qf_curlist > 0)
1326 --qi->qf_curlist;
1327 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001328qf_init_end:
Bram Moolenaare0d37972016-07-15 22:36:01 +02001329 if (state.fd != NULL)
1330 fclose(state.fd);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001331 vim_free(fields.namebuf);
1332 vim_free(fields.errmsg);
1333 vim_free(fields.pattern);
Bram Moolenaare0d37972016-07-15 22:36:01 +02001334 vim_free(state.growbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001336 if (qf_idx == qi->qf_curlist)
1337 qf_update_buffer(qi, old_last);
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 Moolenaara3921f42017-06-04 15:30:34 +02001347qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001348{
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001349 vim_free(qi->qf_lists[qf_idx].qf_title);
1350 qi->qf_lists[qf_idx].qf_title = NULL;
1351
Bram Moolenaarfb604092014-07-23 15:55:00 +02001352 if (title != NULL)
1353 {
1354 char_u *p = alloc((int)STRLEN(title) + 2);
1355
Bram Moolenaara3921f42017-06-04 15:30:34 +02001356 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001357 if (p != NULL)
1358 sprintf((char *)p, ":%s", (char *)title);
1359 }
1360}
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001363 * Prepare for adding a new quickfix list. If the current list is in the
1364 * middle of the stack, then all the following lists are freed and then
1365 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 */
1367 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001368qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
1370 int i;
1371
1372 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001373 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 * the current entry. This makes it possible to browse in a tree-like
1375 * way with ":grep'.
1376 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001377 while (qi->qf_listcount > qi->qf_curlist + 1)
1378 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
1380 /*
1381 * When the stack is full, remove to oldest entry
1382 * Otherwise, add a new entry.
1383 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001384 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001386 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001388 qi->qf_lists[i - 1] = qi->qf_lists[i];
1389 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001392 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001393 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001394 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001395 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396}
1397
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001398/*
1399 * Free a location list
1400 */
1401 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001402ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001403{
1404 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001405 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001406
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001407 qi = *pqi;
1408 if (qi == NULL)
1409 return;
1410 *pqi = NULL; /* Remove reference to this list */
1411
1412 qi->qf_refcount--;
1413 if (qi->qf_refcount < 1)
1414 {
1415 /* No references to this location list */
1416 for (i = 0; i < qi->qf_listcount; ++i)
1417 qf_free(qi, i);
1418 vim_free(qi);
1419 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001420}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001421
1422 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001423qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001424{
1425 int i;
1426 qf_info_T *qi = &ql_info;
1427
1428 if (wp != NULL)
1429 {
1430 /* location list */
1431 ll_free_all(&wp->w_llist);
1432 ll_free_all(&wp->w_llist_ref);
1433 }
1434 else
1435 /* quickfix list */
1436 for (i = 0; i < qi->qf_listcount; ++i)
1437 qf_free(qi, i);
1438}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440/*
1441 * Add an entry to the end of the list of errors.
1442 * Returns OK or FAIL.
1443 */
1444 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001445qf_add_entry(
1446 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001447 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001448 char_u *dir, /* optional directory name */
1449 char_u *fname, /* file name or NULL */
1450 int bufnum, /* buffer number or zero */
1451 char_u *mesg, /* message */
1452 long lnum, /* line number */
1453 int col, /* column */
1454 int vis_col, /* using visual column */
1455 char_u *pattern, /* search pattern */
1456 int nr, /* error number */
1457 int type, /* type character */
1458 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001460 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001461 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001463 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001465 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001466 {
1467 buf_T *buf = buflist_findnr(bufnum);
1468
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001469 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001470 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001471 buf->b_has_qf_entry |=
1472 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001473 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001474 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001475 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1477 {
1478 vim_free(qfp);
1479 return FAIL;
1480 }
1481 qfp->qf_lnum = lnum;
1482 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001483 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001484 if (pattern == NULL || *pattern == NUL)
1485 qfp->qf_pattern = NULL;
1486 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1487 {
1488 vim_free(qfp->qf_text);
1489 vim_free(qfp);
1490 return FAIL;
1491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 qfp->qf_nr = nr;
1493 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1494 type = 0;
1495 qfp->qf_type = type;
1496 qfp->qf_valid = valid;
1497
Bram Moolenaara3921f42017-06-04 15:30:34 +02001498 lastp = &qi->qf_lists[qf_idx].qf_last;
1499 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001500 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001502 qi->qf_lists[qf_idx].qf_start = qfp;
1503 qi->qf_lists[qf_idx].qf_ptr = qfp;
1504 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001505 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
1507 else
1508 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001509 qfp->qf_prev = *lastp;
1510 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001512 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001514 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001515 ++qi->qf_lists[qf_idx].qf_count;
1516 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001517 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001519 qi->qf_lists[qf_idx].qf_index =
1520 qi->qf_lists[qf_idx].qf_count;
1521 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 }
1523
1524 return OK;
1525}
1526
1527/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001528 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001529 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001530 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001531ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001532{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001533 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001534
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001535 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1536 if (qi != NULL)
1537 {
1538 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1539 qi->qf_refcount++;
1540 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001541
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001542 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001543}
1544
1545/*
1546 * Return the location list for window 'wp'.
1547 * If not present, allocate a location list
1548 */
1549 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001550ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001551{
1552 if (IS_LL_WINDOW(wp))
1553 /* For a location list window, use the referenced location list */
1554 return wp->w_llist_ref;
1555
1556 /*
1557 * For a non-location list window, w_llist_ref should not point to a
1558 * location list.
1559 */
1560 ll_free_all(&wp->w_llist_ref);
1561
1562 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001563 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001564 return wp->w_llist;
1565}
1566
1567/*
1568 * Copy the location list from window "from" to window "to".
1569 */
1570 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001571copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001572{
1573 qf_info_T *qi;
1574 int idx;
1575 int i;
1576
1577 /*
1578 * When copying from a location list window, copy the referenced
1579 * location list. For other windows, copy the location list for
1580 * that window.
1581 */
1582 if (IS_LL_WINDOW(from))
1583 qi = from->w_llist_ref;
1584 else
1585 qi = from->w_llist;
1586
1587 if (qi == NULL) /* no location list to copy */
1588 return;
1589
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001590 /* allocate a new location list */
1591 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001592 return;
1593
1594 to->w_llist->qf_listcount = qi->qf_listcount;
1595
1596 /* Copy the location lists one at a time */
1597 for (idx = 0; idx < qi->qf_listcount; idx++)
1598 {
1599 qf_list_T *from_qfl;
1600 qf_list_T *to_qfl;
1601
1602 to->w_llist->qf_curlist = idx;
1603
1604 from_qfl = &qi->qf_lists[idx];
1605 to_qfl = &to->w_llist->qf_lists[idx];
1606
1607 /* Some of the fields are populated by qf_add_entry() */
1608 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1609 to_qfl->qf_count = 0;
1610 to_qfl->qf_index = 0;
1611 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001612 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001613 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001614 if (from_qfl->qf_title != NULL)
1615 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1616 else
1617 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001618 if (from_qfl->qf_ctx != NULL)
1619 {
1620 to_qfl->qf_ctx = alloc_tv();
1621 if (to_qfl->qf_ctx != NULL)
1622 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1623 }
1624 else
1625 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001626
1627 if (from_qfl->qf_count)
1628 {
1629 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001630 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001631
1632 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001633 for (i = 0, from_qfp = from_qfl->qf_start;
1634 i < from_qfl->qf_count && from_qfp != NULL;
1635 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001636 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001637 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001638 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001639 NULL,
1640 NULL,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001641 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001642 from_qfp->qf_text,
1643 from_qfp->qf_lnum,
1644 from_qfp->qf_col,
1645 from_qfp->qf_viscol,
1646 from_qfp->qf_pattern,
1647 from_qfp->qf_nr,
1648 0,
1649 from_qfp->qf_valid) == FAIL)
1650 {
1651 qf_free_all(to);
1652 return;
1653 }
1654 /*
1655 * qf_add_entry() will not set the qf_num field, as the
1656 * directory and file names are not supplied. So the qf_fnum
1657 * field is copied here.
1658 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001659 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001660 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1661 prevp->qf_type = from_qfp->qf_type; /* error type */
1662 if (from_qfl->qf_ptr == from_qfp)
1663 to_qfl->qf_ptr = prevp; /* current location */
1664 }
1665 }
1666
1667 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1668
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001669 /* Assign a new ID for the location list */
1670 to_qfl->qf_id = ++last_qf_id;
1671
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001672 /* When no valid entries are present in the list, qf_ptr points to
1673 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001674 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001675 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001676 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001677 to_qfl->qf_index = 1;
1678 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001679 }
1680
1681 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1682}
1683
1684/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001685 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001686 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
1688 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001689qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
Bram Moolenaar82404332016-07-10 17:00:38 +02001691 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001692 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001693 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001694
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (fname == NULL || *fname == NUL) /* no file name */
1696 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
Bram Moolenaare60acc12011-05-10 16:41:25 +02001698#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001699 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001700#endif
1701#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001702 if (directory != NULL)
1703 slash_adjust(directory);
1704 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001705#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001706 if (directory != NULL && !vim_isAbsName(fname)
1707 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1708 {
1709 /*
1710 * Here we check if the file really exists.
1711 * This should normally be true, but if make works without
1712 * "leaving directory"-messages we might have missed a
1713 * directory change.
1714 */
1715 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001718 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001719 if (directory)
1720 ptr = concat_fnames(directory, fname, TRUE);
1721 else
1722 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001724 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02001725 bufname = ptr;
1726 }
1727 else
1728 bufname = fname;
1729
1730 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001731 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02001732 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001733 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001734 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001736 else
Bram Moolenaar82404332016-07-10 17:00:38 +02001737 {
1738 vim_free(qf_last_bufname);
1739 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
1740 if (bufname == ptr)
1741 qf_last_bufname = bufname;
1742 else
1743 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001744 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001745 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001746 if (buf == NULL)
1747 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02001748
Bram Moolenaarc1542742016-07-20 21:44:37 +02001749 buf->b_has_qf_entry =
1750 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001751 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752}
1753
1754/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001755 * Push dirbuf onto the directory stack and return pointer to actual dir or
1756 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 */
1758 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001759qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 struct dir_stack_T *ds_new;
1762 struct dir_stack_T *ds_ptr;
1763
1764 /* allocate new stack element and hook it in */
1765 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
1766 if (ds_new == NULL)
1767 return NULL;
1768
1769 ds_new->next = *stackptr;
1770 *stackptr = ds_new;
1771
1772 /* store directory on the stack */
1773 if (vim_isAbsName(dirbuf)
1774 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001775 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 (*stackptr)->dirname = vim_strsave(dirbuf);
1777 else
1778 {
1779 /* Okay we don't have an absolute path.
1780 * dirbuf must be a subdir of one of the directories on the stack.
1781 * Let's search...
1782 */
1783 ds_new = (*stackptr)->next;
1784 (*stackptr)->dirname = NULL;
1785 while (ds_new)
1786 {
1787 vim_free((*stackptr)->dirname);
1788 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
1789 TRUE);
1790 if (mch_isdir((*stackptr)->dirname) == TRUE)
1791 break;
1792
1793 ds_new = ds_new->next;
1794 }
1795
1796 /* clean up all dirs we already left */
1797 while ((*stackptr)->next != ds_new)
1798 {
1799 ds_ptr = (*stackptr)->next;
1800 (*stackptr)->next = (*stackptr)->next->next;
1801 vim_free(ds_ptr->dirname);
1802 vim_free(ds_ptr);
1803 }
1804
1805 /* Nothing found -> it must be on top level */
1806 if (ds_new == NULL)
1807 {
1808 vim_free((*stackptr)->dirname);
1809 (*stackptr)->dirname = vim_strsave(dirbuf);
1810 }
1811 }
1812
1813 if ((*stackptr)->dirname != NULL)
1814 return (*stackptr)->dirname;
1815 else
1816 {
1817 ds_ptr = *stackptr;
1818 *stackptr = (*stackptr)->next;
1819 vim_free(ds_ptr);
1820 return NULL;
1821 }
1822}
1823
1824
1825/*
1826 * pop dirbuf from the directory stack and return previous directory or NULL if
1827 * stack is empty
1828 */
1829 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001830qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
1832 struct dir_stack_T *ds_ptr;
1833
1834 /* TODO: Should we check if dirbuf is the directory on top of the stack?
1835 * What to do if it isn't? */
1836
1837 /* pop top element and free it */
1838 if (*stackptr != NULL)
1839 {
1840 ds_ptr = *stackptr;
1841 *stackptr = (*stackptr)->next;
1842 vim_free(ds_ptr->dirname);
1843 vim_free(ds_ptr);
1844 }
1845
1846 /* return NEW top element as current dir or NULL if stack is empty*/
1847 return *stackptr ? (*stackptr)->dirname : NULL;
1848}
1849
1850/*
1851 * clean up directory stack
1852 */
1853 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001854qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
1856 struct dir_stack_T *ds_ptr;
1857
1858 while ((ds_ptr = *stackptr) != NULL)
1859 {
1860 *stackptr = (*stackptr)->next;
1861 vim_free(ds_ptr->dirname);
1862 vim_free(ds_ptr);
1863 }
1864}
1865
1866/*
1867 * Check in which directory of the directory stack the given file can be
1868 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02001869 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 * Cleans up intermediate directory entries.
1871 *
1872 * TODO: How to solve the following problem?
1873 * If we have the this directory tree:
1874 * ./
1875 * ./aa
1876 * ./aa/bb
1877 * ./bb
1878 * ./bb/x.c
1879 * and make says:
1880 * making all in aa
1881 * making all in bb
1882 * x.c:9: Error
1883 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
1884 * qf_guess_filepath will return NULL.
1885 */
1886 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001887qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888{
1889 struct dir_stack_T *ds_ptr;
1890 struct dir_stack_T *ds_tmp;
1891 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001892 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
1894 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001895 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 return NULL;
1897
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001898 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 fullname = NULL;
1900 while (ds_ptr)
1901 {
1902 vim_free(fullname);
1903 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
1904
1905 /* If concat_fnames failed, just go on. The worst thing that can happen
1906 * is that we delete the entire stack.
1907 */
1908 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
1909 break;
1910
1911 ds_ptr = ds_ptr->next;
1912 }
1913
1914 vim_free(fullname);
1915
1916 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001917 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001919 ds_tmp = qfl->qf_dir_stack->next;
1920 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 vim_free(ds_tmp->dirname);
1922 vim_free(ds_tmp);
1923 }
1924
1925 return ds_ptr==NULL? NULL: ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926}
1927
1928/*
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001929 * When loading a file from the quickfix, the auto commands may modify it.
1930 * This may invalidate the current quickfix entry. This function checks
1931 * whether a entry is still present in the quickfix.
1932 * Similar to location list.
1933 */
1934 static int
1935is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
1936{
1937 qf_list_T *qfl;
1938 qfline_T *qfp;
1939 int i;
1940
1941 qfl = &qi->qf_lists[qi->qf_curlist];
1942
1943 /* Search for the entry in the current list */
1944 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
1945 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001946 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001947 break;
1948
1949 if (i == qfl->qf_count) /* Entry is not found */
1950 return FALSE;
1951
1952 return TRUE;
1953}
1954
1955/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001956 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02001957 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001958 */
1959 static qfline_T *
1960get_next_valid_entry(
1961 qf_info_T *qi,
1962 qfline_T *qf_ptr,
1963 int *qf_index,
1964 int dir)
1965{
1966 int idx;
1967 int old_qf_fnum;
1968
1969 idx = *qf_index;
1970 old_qf_fnum = qf_ptr->qf_fnum;
1971
1972 do
1973 {
1974 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
1975 || qf_ptr->qf_next == NULL)
1976 return NULL;
1977 ++idx;
1978 qf_ptr = qf_ptr->qf_next;
1979 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
1980 && !qf_ptr->qf_valid)
1981 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
1982
1983 *qf_index = idx;
1984 return qf_ptr;
1985}
1986
1987/*
1988 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02001989 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02001990 */
1991 static qfline_T *
1992get_prev_valid_entry(
1993 qf_info_T *qi,
1994 qfline_T *qf_ptr,
1995 int *qf_index,
1996 int dir)
1997{
1998 int idx;
1999 int old_qf_fnum;
2000
2001 idx = *qf_index;
2002 old_qf_fnum = qf_ptr->qf_fnum;
2003
2004 do
2005 {
2006 if (idx == 1 || qf_ptr->qf_prev == NULL)
2007 return NULL;
2008 --idx;
2009 qf_ptr = qf_ptr->qf_prev;
2010 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2011 && !qf_ptr->qf_valid)
2012 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2013
2014 *qf_index = idx;
2015 return qf_ptr;
2016}
2017
2018/*
2019 * Get the n'th (errornr) previous/next valid entry from the current entry in
2020 * the quickfix list.
2021 * dir == FORWARD or FORWARD_FILE: next valid entry
2022 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2023 */
2024 static qfline_T *
2025get_nth_valid_entry(
2026 qf_info_T *qi,
2027 int errornr,
2028 qfline_T *qf_ptr,
2029 int *qf_index,
2030 int dir)
2031{
2032 qfline_T *prev_qf_ptr;
2033 int prev_index;
2034 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2035 char_u *err = e_no_more_items;
2036
2037 while (errornr--)
2038 {
2039 prev_qf_ptr = qf_ptr;
2040 prev_index = *qf_index;
2041
2042 if (dir == FORWARD || dir == FORWARD_FILE)
2043 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2044 else
2045 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2046 if (qf_ptr == NULL)
2047 {
2048 qf_ptr = prev_qf_ptr;
2049 *qf_index = prev_index;
2050 if (err != NULL)
2051 {
2052 EMSG(_(err));
2053 return NULL;
2054 }
2055 break;
2056 }
2057
2058 err = NULL;
2059 }
2060
2061 return qf_ptr;
2062}
2063
2064/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002065 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002066 */
2067 static qfline_T *
2068get_nth_entry(
2069 qf_info_T *qi,
2070 int errornr,
2071 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002072 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002073{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002074 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002075
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002076 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002077 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2078 {
2079 --qf_idx;
2080 qf_ptr = qf_ptr->qf_prev;
2081 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002082 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002083 while (errornr > qf_idx &&
2084 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2085 qf_ptr->qf_next != NULL)
2086 {
2087 ++qf_idx;
2088 qf_ptr = qf_ptr->qf_next;
2089 }
2090
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002091 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002092 return qf_ptr;
2093}
2094
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002095/*
2096 * Find a help window or open one.
2097 */
2098 static int
2099jump_to_help_window(qf_info_T *qi, int *opened_window)
2100{
2101 win_T *wp;
2102 int flags;
2103
2104 if (cmdmod.tab != 0)
2105 wp = NULL;
2106 else
2107 FOR_ALL_WINDOWS(wp)
2108 if (bt_help(wp->w_buffer))
2109 break;
2110 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2111 win_enter(wp, TRUE);
2112 else
2113 {
2114 /*
2115 * Split off help window; put it at far top if no position
2116 * specified, the current window is vertically split and narrow.
2117 */
2118 flags = WSP_HELP;
2119 if (cmdmod.split == 0 && curwin->w_width != Columns
2120 && curwin->w_width < 80)
2121 flags |= WSP_TOP;
2122 if (qi != &ql_info)
2123 flags |= WSP_NEWLOC; /* don't copy the location list */
2124
2125 if (win_split(0, flags) == FAIL)
2126 return FAIL;
2127
2128 *opened_window = TRUE;
2129
2130 if (curwin->w_height < p_hh)
2131 win_setheight((int)p_hh);
2132
2133 if (qi != &ql_info) /* not a quickfix list */
2134 {
2135 /* The new window should use the supplied location list */
2136 curwin->w_llist = qi;
2137 qi->qf_refcount++;
2138 }
2139 }
2140
2141 if (!p_im)
2142 restart_edit = 0; /* don't want insert mode in help file */
2143
2144 return OK;
2145}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002146
2147/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002148 * Find a suitable window for opening a file (qf_fnum) and jump to it.
2149 * If the file is already opened in a window, jump to it.
2150 */
2151 static int
2152qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2153{
2154 win_T *usable_win_ptr = NULL;
2155 int usable_win;
2156 qf_info_T *ll_ref;
2157 int flags;
2158 win_T *win;
2159 win_T *altwin;
2160
2161 usable_win = 0;
2162
2163 ll_ref = curwin->w_llist_ref;
2164 if (ll_ref != NULL)
2165 {
2166 /* Find a window using the same location list that is not a
2167 * quickfix window. */
2168 FOR_ALL_WINDOWS(usable_win_ptr)
2169 if (usable_win_ptr->w_llist == ll_ref
2170 && !bt_quickfix(usable_win_ptr->w_buffer))
2171 {
2172 usable_win = 1;
2173 break;
2174 }
2175 }
2176
2177 if (!usable_win)
2178 {
2179 /* Locate a window showing a normal buffer */
2180 FOR_ALL_WINDOWS(win)
2181 if (win->w_buffer->b_p_bt[0] == NUL)
2182 {
2183 usable_win = 1;
2184 break;
2185 }
2186 }
2187
2188 /*
2189 * If no usable window is found and 'switchbuf' contains "usetab"
2190 * then search in other tabs.
2191 */
2192 if (!usable_win && (swb_flags & SWB_USETAB))
2193 {
2194 tabpage_T *tp;
2195 win_T *wp;
2196
2197 FOR_ALL_TAB_WINDOWS(tp, wp)
2198 {
2199 if (wp->w_buffer->b_fnum == qf_fnum)
2200 {
2201 goto_tabpage_win(tp, wp);
2202 usable_win = 1;
2203 goto win_found;
2204 }
2205 }
2206 }
2207win_found:
2208
2209 /*
2210 * If there is only one window and it is the quickfix window, create a
2211 * new one above the quickfix window.
2212 */
2213 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2214 {
2215 flags = WSP_ABOVE;
2216 if (ll_ref != NULL)
2217 flags |= WSP_NEWLOC;
2218 if (win_split(0, flags) == FAIL)
2219 return FAIL; /* not enough room for window */
2220 *opened_window = TRUE; /* close it when fail */
2221 p_swb = empty_option; /* don't split again */
2222 swb_flags = 0;
2223 RESET_BINDING(curwin);
2224 if (ll_ref != NULL)
2225 {
2226 /* The new window should use the location list from the
2227 * location list window */
2228 curwin->w_llist = ll_ref;
2229 ll_ref->qf_refcount++;
2230 }
2231 }
2232 else
2233 {
2234 if (curwin->w_llist_ref != NULL)
2235 {
2236 /* In a location window */
2237 win = usable_win_ptr;
2238 if (win == NULL)
2239 {
2240 /* Find the window showing the selected file */
2241 FOR_ALL_WINDOWS(win)
2242 if (win->w_buffer->b_fnum == qf_fnum)
2243 break;
2244 if (win == NULL)
2245 {
2246 /* Find a previous usable window */
2247 win = curwin;
2248 do
2249 {
2250 if (win->w_buffer->b_p_bt[0] == NUL)
2251 break;
2252 if (win->w_prev == NULL)
2253 win = lastwin; /* wrap around the top */
2254 else
2255 win = win->w_prev; /* go to previous window */
2256 } while (win != curwin);
2257 }
2258 }
2259 win_goto(win);
2260
2261 /* If the location list for the window is not set, then set it
2262 * to the location list from the location window */
2263 if (win->w_llist == NULL)
2264 {
2265 win->w_llist = ll_ref;
2266 ll_ref->qf_refcount++;
2267 }
2268 }
2269 else
2270 {
2271
2272 /*
2273 * Try to find a window that shows the right buffer.
2274 * Default to the window just above the quickfix buffer.
2275 */
2276 win = curwin;
2277 altwin = NULL;
2278 for (;;)
2279 {
2280 if (win->w_buffer->b_fnum == qf_fnum)
2281 break;
2282 if (win->w_prev == NULL)
2283 win = lastwin; /* wrap around the top */
2284 else
2285 win = win->w_prev; /* go to previous window */
2286
2287 if (IS_QF_WINDOW(win))
2288 {
2289 /* Didn't find it, go to the window before the quickfix
2290 * window. */
2291 if (altwin != NULL)
2292 win = altwin;
2293 else if (curwin->w_prev != NULL)
2294 win = curwin->w_prev;
2295 else
2296 win = curwin->w_next;
2297 break;
2298 }
2299
2300 /* Remember a usable window. */
2301 if (altwin == NULL && !win->w_p_pvw
2302 && win->w_buffer->b_p_bt[0] == NUL)
2303 altwin = win;
2304 }
2305
2306 win_goto(win);
2307 }
2308 }
2309
2310 return OK;
2311}
2312
2313/*
2314 * Edit the selected file or help file.
2315 */
2316 static int
2317qf_jump_edit_buffer(
2318 qf_info_T *qi,
2319 qfline_T *qf_ptr,
2320 int forceit,
2321 win_T *oldwin,
2322 int *opened_window,
2323 int *abort)
2324{
2325 int retval = OK;
2326
2327 if (qf_ptr->qf_type == 1)
2328 {
2329 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2330 * set b_p_ro flag). */
2331 if (!can_abandon(curbuf, forceit))
2332 {
2333 no_write_message();
2334 retval = FALSE;
2335 }
2336 else
2337 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2338 ECMD_HIDE + ECMD_SET_HELP,
2339 oldwin == curwin ? curwin : NULL);
2340 }
2341 else
2342 {
2343 int old_qf_curlist = qi->qf_curlist;
2344
2345 retval = buflist_getfile(qf_ptr->qf_fnum,
2346 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
2347 if (qi != &ql_info && !win_valid_any_tab(oldwin))
2348 {
2349 EMSG(_("E924: Current window was closed"));
2350 *abort = TRUE;
2351 *opened_window = FALSE;
2352 }
2353 else if (old_qf_curlist != qi->qf_curlist
2354 || !is_qf_entry_present(qi, qf_ptr))
2355 {
2356 if (qi == &ql_info)
2357 EMSG(_("E925: Current quickfix was changed"));
2358 else
2359 EMSG(_("E926: Current location list was changed"));
2360 *abort = TRUE;
2361 }
2362
2363 if (*abort)
2364 retval = FALSE;
2365 }
2366
2367 return retval;
2368}
2369
2370/*
2371 * Goto the error line in the current file using either line/column number or a
2372 * search pattern.
2373 */
2374 static void
2375qf_jump_goto_line(
2376 linenr_T qf_lnum,
2377 int qf_col,
2378 char_u qf_viscol,
2379 char_u *qf_pattern)
2380{
2381 linenr_T i;
2382 char_u *line;
2383 colnr_T screen_col;
2384 colnr_T char_col;
2385
2386 if (qf_pattern == NULL)
2387 {
2388 /*
2389 * Go to line with error, unless qf_lnum is 0.
2390 */
2391 i = qf_lnum;
2392 if (i > 0)
2393 {
2394 if (i > curbuf->b_ml.ml_line_count)
2395 i = curbuf->b_ml.ml_line_count;
2396 curwin->w_cursor.lnum = i;
2397 }
2398 if (qf_col > 0)
2399 {
2400 curwin->w_cursor.col = qf_col - 1;
2401#ifdef FEAT_VIRTUALEDIT
2402 curwin->w_cursor.coladd = 0;
2403#endif
2404 if (qf_viscol == TRUE)
2405 {
2406 /*
2407 * Check each character from the beginning of the error
2408 * line up to the error column. For each tab character
2409 * found, reduce the error column value by the length of
2410 * a tab character.
2411 */
2412 line = ml_get_curline();
2413 screen_col = 0;
2414 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2415 {
2416 if (*line == NUL)
2417 break;
2418 if (*line++ == '\t')
2419 {
2420 curwin->w_cursor.col -= 7 - (screen_col % 8);
2421 screen_col += 8 - (screen_col % 8);
2422 }
2423 else
2424 ++screen_col;
2425 }
2426 }
2427 check_cursor();
2428 }
2429 else
2430 beginline(BL_WHITE | BL_FIX);
2431 }
2432 else
2433 {
2434 pos_T save_cursor;
2435
2436 /* Move the cursor to the first line in the buffer */
2437 save_cursor = curwin->w_cursor;
2438 curwin->w_cursor.lnum = 0;
2439 if (!do_search(NULL, '/', qf_pattern, (long)1,
2440 SEARCH_KEEP, NULL, NULL))
2441 curwin->w_cursor = save_cursor;
2442 }
2443}
2444
2445/*
2446 * Display quickfix list index and size message
2447 */
2448 static void
2449qf_jump_print_msg(
2450 qf_info_T *qi,
2451 int qf_index,
2452 qfline_T *qf_ptr,
2453 buf_T *old_curbuf,
2454 linenr_T old_lnum)
2455{
2456 linenr_T i;
2457 int len;
2458
2459 /* Update the screen before showing the message, unless the screen
2460 * scrolled up. */
2461 if (!msg_scrolled)
2462 update_topline_redraw();
2463 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2464 qi->qf_lists[qi->qf_curlist].qf_count,
2465 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2466 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2467 /* Add the message, skipping leading whitespace and newlines. */
2468 len = (int)STRLEN(IObuff);
2469 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2470
2471 /* Output the message. Overwrite to avoid scrolling when the 'O'
2472 * flag is present in 'shortmess'; But when not jumping, print the
2473 * whole message. */
2474 i = msg_scroll;
2475 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2476 msg_scroll = TRUE;
2477 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2478 msg_scroll = FALSE;
2479 msg_attr_keep(IObuff, 0, TRUE);
2480 msg_scroll = i;
2481}
2482
2483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 * jump to a quickfix line
2485 * if dir == FORWARD go "errornr" valid entries forward
2486 * if dir == BACKWARD go "errornr" valid entries backward
2487 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2488 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2489 * else if "errornr" is zero, redisplay the same line
2490 * else go to entry "errornr"
2491 */
2492 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002493qf_jump(qf_info_T *qi,
2494 int dir,
2495 int errornr,
2496 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002498 qfline_T *qf_ptr;
2499 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 buf_T *old_curbuf;
2503 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002504 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002505 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002507 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509#ifdef FEAT_FOLDING
2510 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2511#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002512 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002514 if (qi == NULL)
2515 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002516
2517 if (qi->qf_curlist >= qi->qf_listcount
2518 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
2520 EMSG(_(e_quickfix));
2521 return;
2522 }
2523
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002524 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002526 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002528 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002530 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2531 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002533 qf_ptr = old_qf_ptr;
2534 qf_index = old_qf_index;
2535 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
2537 }
2538 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002539 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002541 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2542 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 /* No need to print the error message if it's visible in the error
2544 * window */
2545 print_message = FALSE;
2546
2547 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002548 * For ":helpgrep" find a help window or open one.
2549 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002550 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002551 if (jump_to_help_window(qi, &opened_window) == FAIL)
2552 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002553
2554 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 * If currently in the quickfix window, find another window to show the
2556 * file in.
2557 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002558 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
2560 /*
2561 * If there is no file specified, we don't know where to go.
2562 * But do advance, otherwise ":cn" gets stuck.
2563 */
2564 if (qf_ptr->qf_fnum == 0)
2565 goto theend;
2566
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002567 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2568 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
2571 /*
2572 * If there is a file name,
2573 * read the wanted file if needed, and check autowrite etc.
2574 */
2575 old_curbuf = curbuf;
2576 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002577
2578 if (qf_ptr->qf_fnum != 0)
2579 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002580 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002581
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002582 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2583 &opened_window, &abort);
2584 if (abort)
2585 {
2586 qi = NULL;
2587 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002588 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002589 }
2590
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002591 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
2593 /* When not switched to another buffer, still need to set pc mark */
2594 if (curbuf == old_curbuf)
2595 setpcmark();
2596
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002597 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
2598 qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600#ifdef FEAT_FOLDING
2601 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2602 foldOpenCursor();
2603#endif
2604 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002605 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 }
2607 else
2608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (opened_window)
2610 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01002611 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
2613 /*
2614 * Couldn't open file, so put index back where it was. This could
2615 * happen if the file was readonly and we changed something.
2616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 qf_ptr = old_qf_ptr;
2619 qf_index = old_qf_index;
2620 }
2621 }
2622theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01002623 if (qi != NULL)
2624 {
2625 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
2626 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (p_swb != old_swb && opened_window)
2629 {
2630 /* Restore old 'switchbuf' value, but not when an autocommand or
2631 * modeline has changed the value. */
2632 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00002633 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002635 swb_flags = old_swb_flags;
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 else
2638 free_string_option(old_swb);
2639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640}
2641
2642/*
2643 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002644 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 */
2646 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002647qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002649 buf_T *buf;
2650 char_u *fname;
2651 qfline_T *qfp;
2652 int i;
2653 int idx1 = 1;
2654 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002655 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02002656 int plus = FALSE;
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002657 int qfFileAttr;
2658 int qfSepAttr;
2659 int qfLineAttr;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002660 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002662 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002664 if (eap->cmdidx == CMD_llist)
2665 {
2666 qi = GET_LOC_LIST(curwin);
2667 if (qi == NULL)
2668 {
2669 EMSG(_(e_loclist));
2670 return;
2671 }
2672 }
2673
2674 if (qi->qf_curlist >= qi->qf_listcount
2675 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
2677 EMSG(_(e_quickfix));
2678 return;
2679 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002680 if (*arg == '+')
2681 {
2682 ++arg;
2683 plus = TRUE;
2684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
2686 {
2687 EMSG(_(e_trailing));
2688 return;
2689 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02002690 if (plus)
2691 {
2692 i = qi->qf_lists[qi->qf_curlist].qf_index;
2693 idx2 = i + idx1;
2694 idx1 = i;
2695 }
2696 else
2697 {
2698 i = qi->qf_lists[qi->qf_curlist].qf_count;
2699 if (idx1 < 0)
2700 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
2701 if (idx2 < 0)
2702 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
2703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002705 /*
2706 * Get the attributes for the different quickfix highlight items. Note
2707 * that this depends on syntax items defined in the qf.vim syntax file
2708 */
2709 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
2710 if (qfFileAttr == 0)
2711 qfFileAttr = HL_ATTR(HLF_D);
2712 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
2713 if (qfSepAttr == 0)
2714 qfSepAttr = HL_ATTR(HLF_D);
2715 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
2716 if (qfLineAttr == 0)
2717 qfLineAttr = HL_ATTR(HLF_N);
2718
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002719 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002721 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
2722 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
2724 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
2725 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002726 msg_putchar('\n');
2727 if (got_int)
2728 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002729
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002730 fname = NULL;
2731 if (qfp->qf_fnum != 0
2732 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
2733 {
2734 fname = buf->b_fname;
2735 if (qfp->qf_type == 1) /* :helpgrep */
2736 fname = gettail(fname);
2737 }
2738 if (fname == NULL)
2739 sprintf((char *)IObuff, "%2d", i);
2740 else
2741 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
2742 i, (char *)fname);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002743 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002744 ? HL_ATTR(HLF_QFL) : qfFileAttr);
2745
2746 if (qfp->qf_lnum != 0)
2747 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002748 if (qfp->qf_lnum == 0)
2749 IObuff[0] = NUL;
2750 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002751 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002752 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002753 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002754 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002755 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002756 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002757 msg_puts_attr(IObuff, qfLineAttr);
2758 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002759 if (qfp->qf_pattern != NULL)
2760 {
2761 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002762 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01002763 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002764 }
2765 msg_puts((char_u *)" ");
2766
2767 /* Remove newlines and leading whitespace from the text. For an
2768 * unrecognized line keep the indent, the compiler may mark a word
2769 * with ^^^^. */
2770 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 ? skipwhite(qfp->qf_text) : qfp->qf_text,
2772 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002773 msg_prt_line(IObuff, FALSE);
2774 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002776
2777 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002778 if (qfp == NULL)
2779 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002780 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 ui_breakcheck();
2782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783}
2784
2785/*
2786 * Remove newlines and leading whitespace from an error message.
2787 * Put the result in "buf[bufsize]".
2788 */
2789 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002790qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
2792 int i;
2793 char_u *p = text;
2794
2795 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
2796 {
2797 if (*p == '\n')
2798 {
2799 buf[i] = ' ';
2800 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002801 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 break;
2803 }
2804 else
2805 buf[i] = *p++;
2806 }
2807 buf[i] = NUL;
2808}
2809
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002810 static void
2811qf_msg(qf_info_T *qi, int which, char *lead)
2812{
2813 char *title = (char *)qi->qf_lists[which].qf_title;
2814 int count = qi->qf_lists[which].qf_count;
2815 char_u buf[IOSIZE];
2816
2817 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
2818 lead,
2819 which + 1,
2820 qi->qf_listcount,
2821 count);
2822
2823 if (title != NULL)
2824 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02002825 size_t len = STRLEN(buf);
2826
2827 if (len < 34)
2828 {
2829 vim_memset(buf + len, ' ', 34 - len);
2830 buf[34] = NUL;
2831 }
2832 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002833 }
2834 trunc_string(buf, buf, Columns - 1, IOSIZE);
2835 msg(buf);
2836}
2837
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838/*
2839 * ":colder [count]": Up in the quickfix stack.
2840 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002841 * ":lolder [count]": Up in the location list stack.
2842 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 */
2844 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002845qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002847 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 int count;
2849
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002850 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
2851 {
2852 qi = GET_LOC_LIST(curwin);
2853 if (qi == NULL)
2854 {
2855 EMSG(_(e_loclist));
2856 return;
2857 }
2858 }
2859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 if (eap->addr_count != 0)
2861 count = eap->line2;
2862 else
2863 count = 1;
2864 while (count--)
2865 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002866 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002868 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
2870 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002871 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002873 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
2875 else
2876 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002877 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
2879 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02002880 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002882 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002885 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02002886 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887}
2888
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002889 void
2890qf_history(exarg_T *eap)
2891{
2892 qf_info_T *qi = &ql_info;
2893 int i;
2894
2895 if (eap->cmdidx == CMD_lhistory)
2896 qi = GET_LOC_LIST(curwin);
2897 if (qi == NULL || (qi->qf_listcount == 0
2898 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
2899 MSG(_("No entries"));
2900 else
2901 for (i = 0; i < qi->qf_listcount; ++i)
2902 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
2903}
2904
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002906 * Free all the entries in the error list "idx". Note that other information
2907 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 */
2909 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002910qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002912 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002913 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01002914 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002915 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002917 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002919 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002920 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002921 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002922 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002923 vim_free(qfp->qf_text);
2924 stop = (qfp == qfpnext);
2925 vim_free(qfp->qf_pattern);
2926 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01002927 if (stop)
2928 /* Somehow qf_count may have an incorrect value, set it to 1
2929 * to avoid crashing when it's wrong.
2930 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002931 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01002932 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002933 qfl->qf_start = qfpnext;
2934 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002936
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002937 qfl->qf_index = 0;
2938 qfl->qf_start = NULL;
2939 qfl->qf_last = NULL;
2940 qfl->qf_ptr = NULL;
2941 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002942
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002943 qf_clean_dir_stack(&qfl->qf_dir_stack);
2944 qfl->qf_directory = NULL;
2945 qf_clean_dir_stack(&qfl->qf_file_stack);
2946 qfl->qf_currfile = NULL;
2947 qfl->qf_multiline = FALSE;
2948 qfl->qf_multiignore = FALSE;
2949 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950}
2951
2952/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002953 * Free error list "idx". Frees all the entries in the quickfix list,
2954 * associated context information and the title.
2955 */
2956 static void
2957qf_free(qf_info_T *qi, int idx)
2958{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002959 qf_list_T *qfl = &qi->qf_lists[idx];
2960
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002961 qf_free_items(qi, idx);
2962
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002963 vim_free(qfl->qf_title);
2964 qfl->qf_title = NULL;
2965 free_tv(qfl->qf_ctx);
2966 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002967 qfl->qf_id = 0;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002968}
2969
2970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 * qf_mark_adjust: adjust marks
2972 */
2973 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002974qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002975 win_T *wp,
2976 linenr_T line1,
2977 linenr_T line2,
2978 long amount,
2979 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002981 int i;
2982 qfline_T *qfp;
2983 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002984 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002985 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02002986 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987
Bram Moolenaarc1542742016-07-20 21:44:37 +02002988 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002989 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002990 if (wp != NULL)
2991 {
2992 if (wp->w_llist == NULL)
2993 return;
2994 qi = wp->w_llist;
2995 }
2996
2997 for (idx = 0; idx < qi->qf_listcount; ++idx)
2998 if (qi->qf_lists[idx].qf_count)
2999 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003000 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3001 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 if (qfp->qf_fnum == curbuf->b_fnum)
3003 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003004 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3006 {
3007 if (amount == MAXLNUM)
3008 qfp->qf_cleared = TRUE;
3009 else
3010 qfp->qf_lnum += amount;
3011 }
3012 else if (amount_after && qfp->qf_lnum > line2)
3013 qfp->qf_lnum += amount_after;
3014 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003015
3016 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003017 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018}
3019
3020/*
3021 * Make a nice message out of the error character and the error number:
3022 * char number message
3023 * e or E 0 " error"
3024 * w or W 0 " warning"
3025 * i or I 0 " info"
3026 * 0 0 ""
3027 * other 0 " c"
3028 * e or E n " error n"
3029 * w or W n " warning n"
3030 * i or I n " info n"
3031 * 0 n " error n"
3032 * other n " c n"
3033 * 1 x "" :helpgrep
3034 */
3035 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003036qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037{
3038 static char_u buf[20];
3039 static char_u cc[3];
3040 char_u *p;
3041
3042 if (c == 'W' || c == 'w')
3043 p = (char_u *)" warning";
3044 else if (c == 'I' || c == 'i')
3045 p = (char_u *)" info";
3046 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3047 p = (char_u *)" error";
3048 else if (c == 0 || c == 1)
3049 p = (char_u *)"";
3050 else
3051 {
3052 cc[0] = ' ';
3053 cc[1] = c;
3054 cc[2] = NUL;
3055 p = cc;
3056 }
3057
3058 if (nr <= 0)
3059 return p;
3060
3061 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3062 return buf;
3063}
3064
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065/*
3066 * ":cwindow": open the quickfix window if we have errors to display,
3067 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003068 * ":lwindow": open the location list window if we have locations to display,
3069 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 */
3071 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003072ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003074 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 win_T *win;
3076
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003077 if (eap->cmdidx == CMD_lwindow)
3078 {
3079 qi = GET_LOC_LIST(curwin);
3080 if (qi == NULL)
3081 return;
3082 }
3083
3084 /* Look for an existing quickfix window. */
3085 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
3087 /*
3088 * If a quickfix window is open but we have no errors to display,
3089 * close the window. If a quickfix window is not open, then open
3090 * it if we have errors; otherwise, leave it closed.
3091 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003092 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003093 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003094 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 {
3096 if (win != NULL)
3097 ex_cclose(eap);
3098 }
3099 else if (win == NULL)
3100 ex_copen(eap);
3101}
3102
3103/*
3104 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003105 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003108ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003110 win_T *win = NULL;
3111 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003113 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3114 {
3115 qi = GET_LOC_LIST(curwin);
3116 if (qi == NULL)
3117 return;
3118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003120 /* Find existing quickfix window and close it. */
3121 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 if (win != NULL)
3123 win_close(win, FALSE);
3124}
3125
3126/*
3127 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003128 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 */
3130 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003131ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003133 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003136 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003137 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003138 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003140 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3141 {
3142 qi = GET_LOC_LIST(curwin);
3143 if (qi == NULL)
3144 {
3145 EMSG(_(e_loclist));
3146 return;
3147 }
3148 }
3149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (eap->addr_count != 0)
3151 height = eap->line2;
3152 else
3153 height = QF_WINHEIGHT;
3154
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156#ifdef FEAT_GUI
3157 need_mouse_correct = TRUE;
3158#endif
3159
3160 /*
3161 * Find existing quickfix window, or open a new one.
3162 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003163 win = qf_find_win(qi);
3164
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003165 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003168 if (eap->addr_count != 0)
3169 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003170 if (cmdmod.split & WSP_VERT)
3171 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003172 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003173 win_setwidth(height);
3174 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003175 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003176 win_setheight(height);
3177 }
3178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 else
3180 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00003181 qf_buf = qf_find_buf(qi);
3182
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 /* The current window becomes the previous window afterwards. */
3184 win = curwin;
3185
Bram Moolenaar77642c02012-11-20 17:55:10 +01003186 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3187 && cmdmod.split == 0)
3188 /* Create the new window at the very bottom, except when
3189 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003190 win_goto(lastwin);
Bram Moolenaar884ae642009-02-22 01:37:59 +00003191 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003193 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003195 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003197 /*
3198 * For the location list window, create a reference to the
3199 * location list from the window 'win'.
3200 */
3201 curwin->w_llist_ref = win->w_llist;
3202 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003204
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003205 if (oldwin != curwin)
3206 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003207 if (qf_buf != NULL)
3208 /* Use the existing quickfix buffer */
3209 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003210 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003211 else
3212 {
3213 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003214 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003215 /* switch off 'swapfile' */
3216 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3217 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003218 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003219 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003220 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003221#ifdef FEAT_DIFF
3222 curwin->w_p_diff = FALSE;
3223#endif
3224#ifdef FEAT_FOLDING
3225 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3226 OPT_LOCAL);
3227#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003230 /* Only set the height when still in the same tab page and there is no
3231 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003232 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 win_setheight(height);
3234 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3235 if (win_valid(win))
3236 prevwin = win;
3237 }
3238
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003239 qf_set_title_var(qi);
3240
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 /*
3242 * Fill the buffer with the quickfix list.
3243 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003244 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003246 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 curwin->w_cursor.col = 0;
3248 check_cursor();
3249 update_topline(); /* scroll to show the line */
3250}
3251
3252/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003253 * Move the cursor in the quickfix window to "lnum".
3254 */
3255 static void
3256qf_win_goto(win_T *win, linenr_T lnum)
3257{
3258 win_T *old_curwin = curwin;
3259
3260 curwin = win;
3261 curbuf = win->w_buffer;
3262 curwin->w_cursor.lnum = lnum;
3263 curwin->w_cursor.col = 0;
3264#ifdef FEAT_VIRTUALEDIT
3265 curwin->w_cursor.coladd = 0;
3266#endif
3267 curwin->w_curswant = 0;
3268 update_topline(); /* scroll to show the line */
3269 redraw_later(VALID);
3270 curwin->w_redr_status = TRUE; /* update ruler */
3271 curwin = old_curwin;
3272 curbuf = curwin->w_buffer;
3273}
3274
3275/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003276 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003277 */
3278 void
3279ex_cbottom(exarg_T *eap UNUSED)
3280{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003281 qf_info_T *qi = &ql_info;
3282 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003283
Bram Moolenaar537ef082016-07-09 17:56:19 +02003284 if (eap->cmdidx == CMD_lbottom)
3285 {
3286 qi = GET_LOC_LIST(curwin);
3287 if (qi == NULL)
3288 {
3289 EMSG(_(e_loclist));
3290 return;
3291 }
3292 }
3293
3294 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003295 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3296 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3297}
3298
3299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 * Return the number of the current entry (line number in the quickfix
3301 * window).
3302 */
3303 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003304qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003306 qf_info_T *qi = &ql_info;
3307
3308 if (IS_LL_WINDOW(wp))
3309 /* In the location list window, use the referenced location list */
3310 qi = wp->w_llist_ref;
3311
3312 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313}
3314
3315/*
3316 * Update the cursor position in the quickfix window to the current error.
3317 * Return TRUE if there is a quickfix window.
3318 */
3319 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003320qf_win_pos_update(
3321 qf_info_T *qi,
3322 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
3324 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003325 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
3327 /*
3328 * Put the cursor on the current error in the quickfix window, so that
3329 * it's viewable.
3330 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003331 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 if (win != NULL
3333 && qf_index <= win->w_buffer->b_ml.ml_line_count
3334 && old_qf_index != qf_index)
3335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 if (qf_index > old_qf_index)
3337 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003338 win->w_redraw_top = old_qf_index;
3339 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 }
3341 else
3342 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003343 win->w_redraw_top = qf_index;
3344 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003346 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 }
3348 return win != NULL;
3349}
3350
3351/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003352 * Check whether the given window is displaying the specified quickfix/location
3353 * list buffer
3354 */
3355 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003356is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003357{
3358 /*
3359 * A window displaying the quickfix buffer will have the w_llist_ref field
3360 * set to NULL.
3361 * A window displaying a location list buffer will have the w_llist_ref
3362 * pointing to the location list.
3363 */
3364 if (bt_quickfix(win->w_buffer))
3365 if ((qi == &ql_info && win->w_llist_ref == NULL)
3366 || (qi != &ql_info && win->w_llist_ref == qi))
3367 return TRUE;
3368
3369 return FALSE;
3370}
3371
3372/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003373 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar9c102382006-05-03 21:26:49 +00003374 * Searches in only the windows opened in the current tab.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003375 */
3376 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003377qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003378{
3379 win_T *win;
3380
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003381 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003382 if (is_qf_win(win, qi))
3383 break;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003384
3385 return win;
3386}
3387
3388/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003389 * Find a quickfix buffer.
3390 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 */
3392 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003393qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003395 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003396 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaar9c102382006-05-03 21:26:49 +00003398 FOR_ALL_TAB_WINDOWS(tp, win)
3399 if (is_qf_win(win, qi))
3400 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003401
Bram Moolenaar9c102382006-05-03 21:26:49 +00003402 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403}
3404
3405/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003406 * Update the w:quickfix_title variable in the quickfix/location list window
3407 */
3408 static void
3409qf_update_win_titlevar(qf_info_T *qi)
3410{
3411 win_T *win;
3412 win_T *curwin_save;
3413
3414 if ((win = qf_find_win(qi)) != NULL)
3415 {
3416 curwin_save = curwin;
3417 curwin = win;
3418 qf_set_title_var(qi);
3419 curwin = curwin_save;
3420 }
3421}
3422
3423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 * Find the quickfix buffer. If it exists, update the contents.
3425 */
3426 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003427qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
3429 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003430 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
3433 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003434 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 if (buf != NULL)
3436 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003437 linenr_T old_line_count = buf->b_ml.ml_line_count;
3438
3439 if (old_last == NULL)
3440 /* set curwin/curbuf to buf and save a few things */
3441 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaard823fa92016-08-12 16:29:27 +02003443 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003444
Bram Moolenaar864293a2016-06-02 13:40:04 +02003445 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003446 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003447
Bram Moolenaar864293a2016-06-02 13:40:04 +02003448 if (old_last == NULL)
3449 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003450 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003451
3452 /* restore curwin/curbuf and a few other things */
3453 aucmd_restbuf(&aco);
3454 }
3455
3456 /* Only redraw when added lines are visible. This avoids flickering
3457 * when the added lines are not visible. */
3458 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3459 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 }
3461}
3462
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003463/*
3464 * Set "w:quickfix_title" if "qi" has a title.
3465 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003466 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003467qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003468{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003469 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3470 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003471 qi->qf_lists[qi->qf_curlist].qf_title);
3472}
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474/*
3475 * Fill current buffer with quickfix errors, replacing any previous contents.
3476 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02003477 * If "old_last" is not NULL append the items after this one.
3478 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
3479 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 */
3481 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003482qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003484 linenr_T lnum;
3485 qfline_T *qfp;
3486 buf_T *errbuf;
3487 int len;
3488 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaar864293a2016-06-02 13:40:04 +02003490 if (old_last == NULL)
3491 {
3492 if (buf != curbuf)
3493 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003494 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003495 return;
3496 }
3497
3498 /* delete all existing lines */
3499 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
3500 (void)ml_delete((linenr_T)1, FALSE);
3501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
3503 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003504 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003507 if (old_last == NULL)
3508 {
3509 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3510 lnum = 0;
3511 }
3512 else
3513 {
3514 qfp = old_last->qf_next;
3515 lnum = buf->b_ml.ml_line_count;
3516 }
3517 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
3519 if (qfp->qf_fnum != 0
3520 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3521 && errbuf->b_fname != NULL)
3522 {
3523 if (qfp->qf_type == 1) /* :helpgrep */
3524 STRCPY(IObuff, gettail(errbuf->b_fname));
3525 else
3526 STRCPY(IObuff, errbuf->b_fname);
3527 len = (int)STRLEN(IObuff);
3528 }
3529 else
3530 len = 0;
3531 IObuff[len++] = '|';
3532
3533 if (qfp->qf_lnum > 0)
3534 {
3535 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3536 len += (int)STRLEN(IObuff + len);
3537
3538 if (qfp->qf_col > 0)
3539 {
3540 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3541 len += (int)STRLEN(IObuff + len);
3542 }
3543
3544 sprintf((char *)IObuff + len, "%s",
3545 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3546 len += (int)STRLEN(IObuff + len);
3547 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003548 else if (qfp->qf_pattern != NULL)
3549 {
3550 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3551 len += (int)STRLEN(IObuff + len);
3552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 IObuff[len++] = '|';
3554 IObuff[len++] = ' ';
3555
3556 /* Remove newlines and leading whitespace from the text.
3557 * For an unrecognized line keep the indent, the compiler may
3558 * mark a word with ^^^^. */
3559 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3560 IObuff + len, IOSIZE - len);
3561
Bram Moolenaar864293a2016-06-02 13:40:04 +02003562 if (ml_append_buf(buf, lnum, IObuff,
3563 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 break;
Bram Moolenaar864293a2016-06-02 13:40:04 +02003565 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003567 if (qfp == NULL)
3568 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02003570
3571 if (old_last == NULL)
3572 /* Delete the empty line which is now at the end */
3573 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575
3576 /* correct cursor position */
3577 check_lnums(TRUE);
3578
Bram Moolenaar864293a2016-06-02 13:40:04 +02003579 if (old_last == NULL)
3580 {
3581 /* Set the 'filetype' to "qf" each time after filling the buffer.
3582 * This resembles reading a file into a buffer, it's more logical when
3583 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02003584#ifdef FEAT_AUTOCMD
3585 ++curbuf_lock;
3586#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003587 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
3588 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
3590#ifdef FEAT_AUTOCMD
Bram Moolenaar864293a2016-06-02 13:40:04 +02003591 keep_filetype = TRUE; /* don't detect 'filetype' */
3592 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003594 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003596 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02003597 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598#endif
Bram Moolenaar864293a2016-06-02 13:40:04 +02003599 /* make sure it will be redrawn */
3600 redraw_curbuf_later(NOT_VALID);
3601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
3603 /* Restore KeyTyped, setting 'filetype' may reset it. */
3604 KeyTyped = old_KeyTyped;
3605}
3606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003608 * Return TRUE when using ":vimgrep" for ":grep".
3609 */
3610 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003611grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003612{
Bram Moolenaar754b5602006-02-09 23:53:20 +00003613 return ((cmdidx == CMD_grep
3614 || cmdidx == CMD_lgrep
3615 || cmdidx == CMD_grepadd
3616 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003617 && STRCMP("internal",
3618 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
3619}
3620
3621/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003622 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 */
3624 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003625ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
Bram Moolenaar7c626922005-02-07 22:01:03 +00003627 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003629 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00003631 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003632 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003633 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003634#ifdef FEAT_AUTOCMD
3635 char_u *au_name = NULL;
3636
Bram Moolenaard88e02d2011-04-28 17:27:09 +02003637 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
3638 if (grep_internal(eap->cmdidx))
3639 {
3640 ex_vimgrep(eap);
3641 return;
3642 }
3643
Bram Moolenaar7c626922005-02-07 22:01:03 +00003644 switch (eap->cmdidx)
3645 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00003646 case CMD_make: au_name = (char_u *)"make"; break;
3647 case CMD_lmake: au_name = (char_u *)"lmake"; break;
3648 case CMD_grep: au_name = (char_u *)"grep"; break;
3649 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
3650 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
3651 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003652 default: break;
3653 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01003654 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
3655 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003656 {
Bram Moolenaar1e015462005-09-25 22:16:38 +00003657# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01003658 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00003659 return;
Bram Moolenaar1e015462005-09-25 22:16:38 +00003660# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00003661 }
3662#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003663#ifdef FEAT_MBYTE
3664 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
3665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
Bram Moolenaara6557602006-02-04 22:43:20 +00003667 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
3668 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00003669 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00003670
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003672 fname = get_mef_name();
3673 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003675 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
3677 /*
3678 * If 'shellpipe' empty: don't redirect to 'errorfile'.
3679 */
3680 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
3681 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003682 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 cmd = alloc(len);
3684 if (cmd == NULL)
3685 return;
3686 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
3687 (char *)p_shq);
3688 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003689 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 /*
3691 * Output a newline if there's something else than the :make command that
3692 * was typed (in which case the cursor is in column 0).
3693 */
3694 if (msg_col == 0)
3695 msg_didout = FALSE;
3696 msg_start();
3697 MSG_PUTS(":!");
3698 msg_outtrans(cmd); /* show what we are doing */
3699
3700 /* let the shell know if we are redirecting output or not */
3701 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
3702
3703#ifdef AMIGA
3704 out_flush();
3705 /* read window status report and redraw before message */
3706 (void)char_avail();
3707#endif
3708
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003709 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00003710 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
3711 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003712 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003713 *eap->cmdlinep, enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003714 if (wp != NULL)
3715 qi = GET_LOC_LIST(wp);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003716#ifdef FEAT_AUTOCMD
3717 if (au_name != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003718 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003719 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
3720 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02003721 if (qi->qf_curlist < qi->qf_listcount)
3722 res = qi->qf_lists[qi->qf_curlist].qf_count;
3723 else
3724 res = 0;
3725 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003726#endif
3727 if (res > 0 && !eap->forceit)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003728 qf_jump(qi, 0, 0, FALSE); /* display first error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729
Bram Moolenaar7c626922005-02-07 22:01:03 +00003730 mch_remove(fname);
3731 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 vim_free(cmd);
3733}
3734
3735/*
3736 * Return the name for the errorfile, in allocated memory.
3737 * Find a new unique name when 'makeef' contains "##".
3738 * Returns NULL for error.
3739 */
3740 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003741get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
3743 char_u *p;
3744 char_u *name;
3745 static int start = -1;
3746 static int off = 0;
3747#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02003748 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749#endif
3750
3751 if (*p_mef == NUL)
3752 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02003753 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 if (name == NULL)
3755 EMSG(_(e_notmp));
3756 return name;
3757 }
3758
3759 for (p = p_mef; *p; ++p)
3760 if (p[0] == '#' && p[1] == '#')
3761 break;
3762
3763 if (*p == NUL)
3764 return vim_strsave(p_mef);
3765
3766 /* Keep trying until the name doesn't exist yet. */
3767 for (;;)
3768 {
3769 if (start == -1)
3770 start = mch_get_pid();
3771 else
3772 off += 19;
3773
3774 name = alloc((unsigned)STRLEN(p_mef) + 30);
3775 if (name == NULL)
3776 break;
3777 STRCPY(name, p_mef);
3778 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
3779 STRCAT(name, p + 2);
3780 if (mch_getperm(name) < 0
3781#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02003782 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 && mch_lstat((char *)name, &sb) < 0
3784#endif
3785 )
3786 break;
3787 vim_free(name);
3788 }
3789 return name;
3790}
3791
3792/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003793 * Returns the number of valid entries in the current quickfix/location list.
3794 */
3795 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003796qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003797{
3798 qf_info_T *qi = &ql_info;
3799 qfline_T *qfp;
3800 int i, sz = 0;
3801 int prev_fnum = 0;
3802
3803 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3804 {
3805 /* Location list */
3806 qi = GET_LOC_LIST(curwin);
3807 if (qi == NULL)
3808 return 0;
3809 }
3810
3811 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003812 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003813 ++i, qfp = qfp->qf_next)
3814 {
3815 if (qfp->qf_valid)
3816 {
3817 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
3818 sz++; /* Count all valid entries */
3819 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3820 {
3821 /* Count the number of files */
3822 sz++;
3823 prev_fnum = qfp->qf_fnum;
3824 }
3825 }
3826 }
3827
3828 return sz;
3829}
3830
3831/*
3832 * Returns the current index of the quickfix/location list.
3833 * Returns 0 if there is an error.
3834 */
3835 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003836qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003837{
3838 qf_info_T *qi = &ql_info;
3839
3840 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3841 {
3842 /* Location list */
3843 qi = GET_LOC_LIST(curwin);
3844 if (qi == NULL)
3845 return 0;
3846 }
3847
3848 return qi->qf_lists[qi->qf_curlist].qf_index;
3849}
3850
3851/*
3852 * Returns the current index in the quickfix/location list (counting only valid
3853 * entries). If no valid entries are in the list, then returns 1.
3854 */
3855 int
Bram Moolenaar05540972016-01-30 20:31:25 +01003856qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003857{
3858 qf_info_T *qi = &ql_info;
3859 qf_list_T *qfl;
3860 qfline_T *qfp;
3861 int i, eidx = 0;
3862 int prev_fnum = 0;
3863
3864 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
3865 {
3866 /* Location list */
3867 qi = GET_LOC_LIST(curwin);
3868 if (qi == NULL)
3869 return 1;
3870 }
3871
3872 qfl = &qi->qf_lists[qi->qf_curlist];
3873 qfp = qfl->qf_start;
3874
3875 /* check if the list has valid errors */
3876 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3877 return 1;
3878
3879 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
3880 {
3881 if (qfp->qf_valid)
3882 {
3883 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
3884 {
3885 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3886 {
3887 /* Count the number of files */
3888 eidx++;
3889 prev_fnum = qfp->qf_fnum;
3890 }
3891 }
3892 else
3893 eidx++;
3894 }
3895 }
3896
3897 return eidx ? eidx : 1;
3898}
3899
3900/*
3901 * Get the 'n'th valid error entry in the quickfix or location list.
3902 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
3903 * For :cdo and :ldo returns the 'n'th valid error entry.
3904 * For :cfdo and :lfdo returns the 'n'th valid file entry.
3905 */
3906 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003907qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003908{
3909 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
3910 qfline_T *qfp = qfl->qf_start;
3911 int i, eidx;
3912 int prev_fnum = 0;
3913
3914 /* check if the list has valid errors */
3915 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
3916 return 1;
3917
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003918 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003919 i++, qfp = qfp->qf_next)
3920 {
3921 if (qfp->qf_valid)
3922 {
3923 if (fdo)
3924 {
3925 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
3926 {
3927 /* Count the number of files */
3928 eidx++;
3929 prev_fnum = qfp->qf_fnum;
3930 }
3931 }
3932 else
3933 eidx++;
3934 }
3935
3936 if (eidx == n)
3937 break;
3938 }
3939
3940 if (i <= qfl->qf_count)
3941 return i;
3942 else
3943 return 1;
3944}
3945
3946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003948 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003949 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 */
3951 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003952ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003954 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003955 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003956
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003957 if (eap->cmdidx == CMD_ll
3958 || eap->cmdidx == CMD_lrewind
3959 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003960 || eap->cmdidx == CMD_llast
3961 || eap->cmdidx == CMD_ldo
3962 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003963 {
3964 qi = GET_LOC_LIST(curwin);
3965 if (qi == NULL)
3966 {
3967 EMSG(_(e_loclist));
3968 return;
3969 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003970 }
3971
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003972 if (eap->addr_count > 0)
3973 errornr = (int)eap->line2;
3974 else
3975 {
3976 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
3977 errornr = 0;
3978 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
3979 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
3980 errornr = 1;
3981 else
3982 errornr = 32767;
3983 }
3984
3985 /* For cdo and ldo commands, jump to the nth valid error.
3986 * For cfdo and lfdo commands, jump to the nth valid file entry.
3987 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02003988 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
3989 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003990 errornr = qf_get_nth_valid_entry(qi,
3991 eap->addr_count > 0 ? (int)eap->line1 : 1,
3992 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
3993
3994 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995}
3996
3997/*
3998 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003999 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004000 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 */
4002 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004003ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004005 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004006 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004007
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004008 if (eap->cmdidx == CMD_lnext
4009 || eap->cmdidx == CMD_lNext
4010 || eap->cmdidx == CMD_lprevious
4011 || eap->cmdidx == CMD_lnfile
4012 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004013 || eap->cmdidx == CMD_lpfile
4014 || eap->cmdidx == CMD_ldo
4015 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004016 {
4017 qi = GET_LOC_LIST(curwin);
4018 if (qi == NULL)
4019 {
4020 EMSG(_(e_loclist));
4021 return;
4022 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004023 }
4024
Bram Moolenaar55b69262017-08-13 13:42:01 +02004025 if (eap->addr_count > 0
4026 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4027 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004028 errornr = (int)eap->line2;
4029 else
4030 errornr = 1;
4031
4032 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4033 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004035 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4036 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004038 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4039 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 ? BACKWARD_FILE
4041 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004042 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043}
4044
4045/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004046 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004047 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 */
4049 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004050ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004052 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004053 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004054 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004055#ifdef FEAT_AUTOCMD
4056 char_u *au_name = NULL;
4057#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004058 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004059
4060 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004061 || eap->cmdidx == CMD_laddfile)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004062 wp = curwin;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004063
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004064#ifdef FEAT_AUTOCMD
4065 switch (eap->cmdidx)
4066 {
4067 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4068 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4069 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4070 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4071 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4072 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4073 default: break;
4074 }
4075 if (au_name != NULL)
4076 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
4077#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004078#ifdef FEAT_MBYTE
4079 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4080#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004081#ifdef FEAT_BROWSE
4082 if (cmdmod.browse)
4083 {
4084 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
4085 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
4086 if (browse_file == NULL)
4087 return;
4088 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4089 vim_free(browse_file);
4090 }
4091 else
4092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004094 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004095
4096 /*
4097 * This function is used by the :cfile, :cgetfile and :caddfile
4098 * commands.
4099 * :cfile always creates a new quickfix list and jumps to the
4100 * first error.
4101 * :cgetfile creates a new quickfix list but doesn't jump to the
4102 * first error.
4103 * :caddfile adds to an existing quickfix list. If there is no
4104 * quickfix list then a new list is created.
4105 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004106 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
4107 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc);
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004108#ifdef FEAT_AUTOCMD
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004109 if (au_name != NULL)
4110 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004111#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004112 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile))
4113 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004114 if (wp != NULL)
4115 qi = GET_LOC_LIST(wp);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004116 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118}
4119
4120/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004121 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004122 * ":vimgrepadd {pattern} file(s)"
4123 * ":lvimgrep {pattern} file(s)"
4124 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004125 */
4126 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004127ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004128{
Bram Moolenaar81695252004-12-29 20:58:21 +00004129 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004130 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004131 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004132 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004133 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004134 char_u *s;
4135 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004136 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004137 qf_info_T *qi = &ql_info;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004138#ifdef FEAT_AUTOCMD
4139 qfline_T *cur_qf_start;
4140#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004141 long lnum;
Bram Moolenaar81695252004-12-29 20:58:21 +00004142 buf_T *buf;
4143 int duplicate_name = FALSE;
4144 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004145 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004146 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004147 buf_T *first_match_buf = NULL;
4148 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004149 int save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004150#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4151 char_u *save_ei = NULL;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004152#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004153 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004154 int flags = 0;
4155 colnr_T col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004156 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004157 char_u *dirname_start = NULL;
4158 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004159 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004160#ifdef FEAT_AUTOCMD
4161 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004162
4163 switch (eap->cmdidx)
4164 {
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004165 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break;
4166 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break;
4167 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break;
Bram Moolenaara6557602006-02-04 22:43:20 +00004168 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004169 case CMD_grep: au_name = (char_u *)"grep"; break;
4170 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4171 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4172 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004173 default: break;
4174 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004175 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4176 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004177 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01004178# ifdef FEAT_EVAL
4179 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004180 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01004181# endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00004182 }
4183#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00004184
Bram Moolenaar754b5602006-02-09 23:53:20 +00004185 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004186 || eap->cmdidx == CMD_lvimgrep
4187 || eap->cmdidx == CMD_lgrepadd
4188 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004189 {
4190 qi = ll_get_or_alloc_list(curwin);
4191 if (qi == NULL)
4192 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00004193 }
4194
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004195 if (eap->addr_count > 0)
4196 tomatch = eap->line2;
4197 else
4198 tomatch = MAXLNUM;
4199
Bram Moolenaar81695252004-12-29 20:58:21 +00004200 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004201 regmatch.regprog = NULL;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004202 title = vim_strsave(*eap->cmdlinep);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004203 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004204 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004205 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004206 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004207 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004208 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004209
4210 if (s != NULL && *s == NUL)
4211 {
4212 /* Pattern is empty, use last search pattern. */
4213 if (last_search_pat() == NULL)
4214 {
4215 EMSG(_(e_noprevre));
4216 goto theend;
4217 }
4218 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4219 }
4220 else
4221 regmatch.regprog = vim_regcomp(s, RE_MAGIC);
4222
Bram Moolenaar86b68352004-12-27 21:59:20 +00004223 if (regmatch.regprog == NULL)
4224 goto theend;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004225 regmatch.rmm_ic = p_ic;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004226 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004227
4228 p = skipwhite(p);
4229 if (*p == NUL)
4230 {
4231 EMSG(_("E683: File name missing or invalid pattern"));
4232 goto theend;
4233 }
4234
Bram Moolenaar55b69262017-08-13 13:42:01 +02004235 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
4236 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004237 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004238 /* make place for a new list */
Bram Moolenaar5584df62016-03-18 21:00:51 +01004239 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004240
4241 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004242 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004243 goto theend;
4244 if (fcount == 0)
4245 {
4246 EMSG(_(e_nomatch));
4247 goto theend;
4248 }
4249
Bram Moolenaarb86a3432016-01-10 16:00:53 +01004250 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
4251 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004252 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004253 {
4254 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004255 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01004256 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02004257
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004258 /* Remember the current directory, because a BufRead autocommand that does
4259 * ":lcd %:p:h" changes the meaning of short path names. */
4260 mch_dirname(dirname_start, MAXPATHL);
4261
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004262#ifdef FEAT_AUTOCMD
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004263 /* Remember the value of qf_start, so that we can check for autocommands
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004264 * changing the current quickfix list. */
4265 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4266#endif
4267
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004268 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004269 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004270 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004271 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004272 if (time(NULL) > seconds)
4273 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004274 /* Display the file name every second or so, show the user we are
4275 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004276 seconds = time(NULL);
4277 msg_start();
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004278 p = msg_strtrunc(fname, TRUE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004279 if (p == NULL)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004280 msg_outtrans(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004281 else
4282 {
4283 msg_outtrans(p);
4284 vim_free(p);
4285 }
4286 msg_clr_eos();
4287 msg_didout = FALSE; /* overwrite this message */
4288 msg_nowait = TRUE; /* don't wait for this message */
4289 msg_col = 0;
4290 out_flush();
4291 }
4292
Bram Moolenaar81695252004-12-29 20:58:21 +00004293 buf = buflist_findname_exp(fnames[fi]);
4294 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
4295 {
4296 /* Remember that a buffer with this name already exists. */
4297 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004298 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004299 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004300
4301#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4302 /* Don't do Filetype autocommands to avoid loading syntax and
4303 * indent scripts, a great speed improvement. */
4304 save_ei = au_event_disable(",Filetype");
4305#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004306 /* Don't use modelines here, it's useless. */
4307 save_mls = p_mls;
4308 p_mls = 0;
Bram Moolenaar81695252004-12-29 20:58:21 +00004309
4310 /* Load file into a buffer, so that 'fileencoding' is detected,
4311 * autocommands applied, etc. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004312 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004313
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004314 p_mls = save_mls;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004315#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
4316 au_event_restore(save_ei);
4317#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004318 }
4319 else
4320 /* Use existing, loaded buffer. */
4321 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004322
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004323#ifdef FEAT_AUTOCMD
4324 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4325 {
4326 int idx;
4327
4328 /* Autocommands changed the quickfix list. Find the one we were
4329 * using and restore it. */
4330 for (idx = 0; idx < LISTCOUNT; ++idx)
4331 if (cur_qf_start == qi->qf_lists[idx].qf_start)
4332 {
4333 qi->qf_curlist = idx;
4334 break;
4335 }
4336 if (idx == LISTCOUNT)
4337 {
4338 /* List cannot be found, create a new one. */
4339 qf_new_list(qi, *eap->cmdlinep);
4340 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4341 }
4342 }
4343#endif
4344
Bram Moolenaar81695252004-12-29 20:58:21 +00004345 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004346 {
4347 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004348 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004349 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004350 else
4351 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004352 /* Try for a match in all lines of the buffer.
4353 * For ":1vimgrep" look for first match only. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004354 found_match = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004355 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0;
4356 ++lnum)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004357 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004358 col = 0;
4359 while (vim_regexec_multi(&regmatch, curwin, buf, lnum,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004360 col, NULL, NULL) > 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004361 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004362 /* Pass the buffer number so that it gets used even for a
4363 * dummy buffer, unless duplicate_name is set, then the
4364 * buffer will be wiped out below. */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004365 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02004366 qi->qf_curlist,
Bram Moolenaar86b68352004-12-27 21:59:20 +00004367 NULL, /* dir */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004368 fname,
Bram Moolenaar015102e2016-07-16 18:24:56 +02004369 duplicate_name ? 0 : buf->b_fnum,
Bram Moolenaar81695252004-12-29 20:58:21 +00004370 ml_get_buf(buf,
4371 regmatch.startpos[0].lnum + lnum, FALSE),
4372 regmatch.startpos[0].lnum + lnum,
4373 regmatch.startpos[0].col + 1,
Bram Moolenaar05159a02005-02-26 23:04:13 +00004374 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004375 NULL, /* search pattern */
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004376 0, /* nr */
4377 0, /* type */
4378 TRUE /* valid */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004379 ) == FAIL)
4380 {
4381 got_int = TRUE;
4382 break;
4383 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004384 found_match = TRUE;
4385 if (--tomatch == 0)
4386 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004387 if ((flags & VGR_GLOBAL) == 0
4388 || regmatch.endpos[0].lnum > 0)
4389 break;
4390 col = regmatch.endpos[0].col
4391 + (col == regmatch.endpos[0].col);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004392 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00004393 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004394 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004395 line_breakcheck();
Bram Moolenaar81695252004-12-29 20:58:21 +00004396 if (got_int)
4397 break;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004398 }
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01004399#ifdef FEAT_AUTOCMD
4400 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4401#endif
Bram Moolenaar81695252004-12-29 20:58:21 +00004402
4403 if (using_dummy)
4404 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004405 if (found_match && first_match_buf == NULL)
4406 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00004407 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004408 {
Bram Moolenaar81695252004-12-29 20:58:21 +00004409 /* Never keep a dummy buffer if there is another buffer
4410 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004411 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004412 buf = NULL;
4413 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00004414 else if (!cmdmod.hide
4415 || buf->b_p_bh[0] == 'u' /* "unload" */
4416 || buf->b_p_bh[0] == 'w' /* "wipe" */
4417 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00004418 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00004419 /* When no match was found we don't need to remember the
4420 * buffer, wipe it out. If there was a match and it
4421 * wasn't the first one or we won't jump there: only
4422 * unload the buffer.
4423 * Ignore 'hidden' here, because it may lead to having too
4424 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00004425 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004426 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004427 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004428 buf = NULL;
4429 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004430 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004431 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004432 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02004433 /* Keeping the buffer, remove the dummy flag. */
4434 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004435 buf = NULL;
4436 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004437 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004438
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004439 if (buf != NULL)
4440 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02004441 /* Keeping the buffer, remove the dummy flag. */
4442 buf->b_flags &= ~BF_DUMMY;
4443
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004444 /* If the buffer is still loaded we need to use the
4445 * directory we jumped to below. */
4446 if (buf == first_match_buf
4447 && target_dir == NULL
4448 && STRCMP(dirname_start, dirname_now) != 0)
4449 target_dir = vim_strsave(dirname_now);
4450
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004451 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004452 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00004453 * need to be done (again). But not the window-local
4454 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004455 aucmd_prepbuf(&aco, buf);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004456#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004457 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
4458 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004459#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004460 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004461 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004462 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004463 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004464 }
4465 }
4466
4467 FreeWild(fcount, fnames);
4468
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004469 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
4470 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
4471 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004472
Bram Moolenaar864293a2016-06-02 13:40:04 +02004473 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004474
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004475#ifdef FEAT_AUTOCMD
4476 if (au_name != NULL)
4477 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4478 curbuf->b_fname, TRUE, curbuf);
4479#endif
4480
Bram Moolenaar86b68352004-12-27 21:59:20 +00004481 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004482 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004483 {
4484 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004485 {
4486 buf = curbuf;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004487 qf_jump(qi, 0, 0, eap->forceit);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004488 if (buf != curbuf)
4489 /* If we jumped to another buffer redrawing will already be
4490 * taken care of. */
4491 redraw_for_dummy = FALSE;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004492
4493 /* Jump to the directory used after loading the buffer. */
4494 if (curbuf == first_match_buf && target_dir != NULL)
4495 {
4496 exarg_T ea;
4497
4498 ea.arg = target_dir;
4499 ea.cmdidx = CMD_lcd;
4500 ex_cd(&ea);
4501 }
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004502 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00004503 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004504 else
4505 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004506
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004507 /* If we loaded a dummy buffer into the current window, the autocommands
4508 * may have messed up things, need to redraw and recompute folds. */
4509 if (redraw_for_dummy)
4510 {
4511#ifdef FEAT_FOLDING
4512 foldUpdateAll(curwin);
4513#else
4514 redraw_later(NOT_VALID);
4515#endif
4516 }
4517
Bram Moolenaar86b68352004-12-27 21:59:20 +00004518theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01004519 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02004520 vim_free(dirname_now);
4521 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004522 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02004523 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004524}
4525
4526/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004527 * Restore current working directory to "dirname_start" if they differ, taking
4528 * into account whether it is set locally or globally.
4529 */
4530 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004531restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004532{
4533 char_u *dirname_now = alloc(MAXPATHL);
4534
4535 if (NULL != dirname_now)
4536 {
4537 mch_dirname(dirname_now, MAXPATHL);
4538 if (STRCMP(dirname_start, dirname_now) != 0)
4539 {
4540 /* If the directory has changed, change it back by building up an
4541 * appropriate ex command and executing it. */
4542 exarg_T ea;
4543
4544 ea.arg = dirname_start;
4545 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
4546 ex_cd(&ea);
4547 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01004548 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004549 }
4550}
4551
4552/*
4553 * Load file "fname" into a dummy buffer and return the buffer pointer,
4554 * placing the directory resulting from the buffer load into the
4555 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
4556 * prior to calling this function. Restores directory to "dirname_start" prior
4557 * to returning, if autocmds or the 'autochdir' option have changed it.
4558 *
4559 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
4560 * or wipe_dummy_buffer() later!
4561 *
Bram Moolenaar81695252004-12-29 20:58:21 +00004562 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00004563 */
4564 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004565load_dummy_buffer(
4566 char_u *fname,
4567 char_u *dirname_start, /* in: old directory */
4568 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00004569{
4570 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004571 bufref_T newbufref;
4572 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00004573 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004574 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004575 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00004576
4577 /* Allocate a buffer without putting it in the buffer list. */
4578 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4579 if (newbuf == NULL)
4580 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004581 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004582
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00004583 /* Init the options. */
4584 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
4585
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004586 /* need to open the memfile before putting the buffer in a window */
4587 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00004588 {
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004589 /* Make sure this buffer isn't wiped out by auto commands. */
4590 ++newbuf->b_locked;
4591
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004592 /* set curwin/curbuf to buf and save a few things */
4593 aucmd_prepbuf(&aco, newbuf);
4594
4595 /* Need to set the filename for autocommands. */
4596 (void)setfname(curbuf, fname, NULL, FALSE);
4597
Bram Moolenaar81695252004-12-29 20:58:21 +00004598 /* Create swap file now to avoid the ATTENTION message. */
4599 check_need_swap(TRUE);
4600
4601 /* Remove the "dummy" flag, otherwise autocommands may not
4602 * work. */
4603 curbuf->b_flags &= ~BF_DUMMY;
4604
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004605 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004606 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00004607 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01004608 NULL, READ_NEW | READ_DUMMY);
4609 --newbuf->b_locked;
4610 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00004611 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00004612 && !(curbuf->b_flags & BF_NEW))
4613 {
4614 failed = FALSE;
4615 if (curbuf != newbuf)
4616 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01004617 /* Bloody autocommands changed the buffer! Can happen when
4618 * using netrw and editing a remote file. Use the current
4619 * buffer instead, delete the dummy one after restoring the
4620 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004621 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00004622 newbuf = curbuf;
4623 }
4624 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004625
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004626 /* restore curwin/curbuf and a few other things */
4627 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004628 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
4629 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02004630
4631 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
4632 * skip it. */
4633 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004634 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004635
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004636 /*
4637 * When autocommands/'autochdir' option changed directory: go back.
4638 * Let the caller know what the resulting dir was first, in case it is
4639 * important.
4640 */
4641 mch_dirname(resulting_dir, MAXPATHL);
4642 restore_start_dir(dirname_start);
4643
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004644 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00004645 return NULL;
4646 if (failed)
4647 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004648 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00004649 return NULL;
4650 }
4651 return newbuf;
4652}
4653
4654/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004655 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
4656 * directory to "dirname_start" prior to returning, if autocmds or the
4657 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004658 */
4659 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004660wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004661{
4662 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00004663 {
4664#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4665 cleanup_T cs;
4666
4667 /* Reset the error/interrupt/exception state here so that aborting()
4668 * returns FALSE when wiping out the buffer. Otherwise it doesn't
4669 * work when got_int is set. */
4670 enter_cleanup(&cs);
4671#endif
4672
Bram Moolenaar81695252004-12-29 20:58:21 +00004673 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004674
4675#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4676 /* Restore the error/interrupt/exception state if not discarded by a
4677 * new aborting error, interrupt, or uncaught exception. */
4678 leave_cleanup(&cs);
4679#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004680 /* When autocommands/'autochdir' option changed directory: go back. */
4681 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00004682 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004683}
4684
4685/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004686 * Unload the dummy buffer that load_dummy_buffer() created. Restores
4687 * directory to "dirname_start" prior to returning, if autocmds or the
4688 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00004689 */
4690 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004691unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00004692{
4693 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004694 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01004695 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02004696
4697 /* When autocommands/'autochdir' option changed directory: go back. */
4698 restore_start_dir(dirname_start);
4699 }
Bram Moolenaar81695252004-12-29 20:58:21 +00004700}
4701
Bram Moolenaar05159a02005-02-26 23:04:13 +00004702#if defined(FEAT_EVAL) || defined(PROTO)
4703/*
4704 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004705 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004706 */
4707 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004708get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004709{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004710 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004711 dict_T *dict;
4712 char_u buf[2];
4713 qfline_T *qfp;
4714 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004715 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004716
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004717 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004718 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004719 qi = &ql_info;
4720 if (wp != NULL)
4721 {
4722 qi = GET_LOC_LIST(wp);
4723 if (qi == NULL)
4724 return FAIL;
4725 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00004726 }
4727
Bram Moolenaard823fa92016-08-12 16:29:27 +02004728 if (qf_idx == -1)
4729 qf_idx = qi->qf_curlist;
4730
4731 if (qf_idx >= qi->qf_listcount
4732 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004733 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004734
Bram Moolenaard823fa92016-08-12 16:29:27 +02004735 qfp = qi->qf_lists[qf_idx].qf_start;
4736 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004737 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004738 /* Handle entries with a non-existing buffer number. */
4739 bufnum = qfp->qf_fnum;
4740 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
4741 bufnum = 0;
4742
Bram Moolenaar05159a02005-02-26 23:04:13 +00004743 if ((dict = dict_alloc()) == NULL)
4744 return FAIL;
4745 if (list_append_dict(list, dict) == FAIL)
4746 return FAIL;
4747
4748 buf[0] = qfp->qf_type;
4749 buf[1] = NUL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00004750 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004751 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL
4752 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL
4753 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL
4754 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL
Bram Moolenaar53ed1922006-09-05 13:37:47 +00004755 || dict_add_nr_str(dict, "pattern", 0L,
4756 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL
4757 || dict_add_nr_str(dict, "text", 0L,
4758 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL
Bram Moolenaar05159a02005-02-26 23:04:13 +00004759 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL
4760 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL)
4761 return FAIL;
4762
4763 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004764 if (qfp == NULL)
4765 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004766 }
4767 return OK;
4768}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004769
4770/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004771 * Flags used by getqflist()/getloclist() to determine which fields to return.
4772 */
4773enum {
4774 QF_GETLIST_NONE = 0x0,
4775 QF_GETLIST_TITLE = 0x1,
4776 QF_GETLIST_ITEMS = 0x2,
4777 QF_GETLIST_NR = 0x4,
4778 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004779 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004780 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004781 QF_GETLIST_IDX = 0x40,
4782 QF_GETLIST_SIZE = 0x80,
Bram Moolenaard823fa92016-08-12 16:29:27 +02004783 QF_GETLIST_ALL = 0xFF
4784};
4785
4786/*
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004787 * Parse text from 'di' and return the quickfix list items
4788 */
4789 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02004790qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004791{
4792 int status = FAIL;
4793 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02004794 char_u *errorformat = p_efm;
4795 dictitem_T *efm_di;
4796 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004797
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004798 /* Only a List value is supported */
4799 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004800 {
Bram Moolenaar36538222017-09-02 19:51:44 +02004801 /* If errorformat is supplied then use it, otherwise use the 'efm'
4802 * option setting
4803 */
4804 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
4805 {
4806 if (efm_di->di_tv.v_type != VAR_STRING ||
4807 efm_di->di_tv.vval.v_string == NULL)
4808 return FAIL;
4809 errorformat = efm_di->di_tv.vval.v_string;
4810 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004811
Bram Moolenaar36538222017-09-02 19:51:44 +02004812 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02004813 if (l == NULL)
4814 return FAIL;
4815
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004816 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
4817 if (qi != NULL)
4818 {
4819 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
4820 qi->qf_refcount++;
4821
Bram Moolenaar36538222017-09-02 19:51:44 +02004822 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004823 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
4824 {
Bram Moolenaarda732532017-08-31 20:58:02 +02004825 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004826 qf_free(qi, 0);
4827 }
4828 free(qi);
4829 }
Bram Moolenaarda732532017-08-31 20:58:02 +02004830 dict_add_list(retdict, "items", l);
4831 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004832 }
4833
4834 return status;
4835}
4836
4837/*
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004838 * Return the quickfix/location list number with the given identifier.
4839 * Returns -1 if list is not found.
4840 */
4841 static int
4842qf_id2nr(qf_info_T *qi, int_u qfid)
4843{
4844 int qf_idx;
4845
4846 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4847 if (qi->qf_lists[qf_idx].qf_id == qfid)
4848 return qf_idx;
4849 return -1;
4850}
4851
4852/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004853 * Return quickfix/location list details (title) as a
4854 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
4855 * then current list is used. Otherwise the specified list is used.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004856 */
4857 int
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004858qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004859{
4860 qf_info_T *qi = &ql_info;
4861 int status = OK;
4862 int qf_idx;
4863 dictitem_T *di;
4864 int flags = QF_GETLIST_NONE;
4865
Bram Moolenaar2c809b72017-09-01 18:34:02 +02004866 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaar36538222017-09-02 19:51:44 +02004867 return qf_get_list_from_lines(what, di, retdict);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004868
Bram Moolenaard823fa92016-08-12 16:29:27 +02004869 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004870 qi = GET_LOC_LIST(wp);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004871
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004872 if (dict_find(what, (char_u *)"all", -1) != NULL)
4873 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004874
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004875 if (dict_find(what, (char_u *)"title", -1) != NULL)
4876 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004877
Bram Moolenaara6d48492017-12-12 22:45:31 +01004878 if (dict_find(what, (char_u *)"nr", -1) != NULL)
4879 flags |= QF_GETLIST_NR;
4880
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004881 if (dict_find(what, (char_u *)"winid", -1) != NULL)
4882 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004883
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004884 if (dict_find(what, (char_u *)"context", -1) != NULL)
4885 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004886
Bram Moolenaara6d48492017-12-12 22:45:31 +01004887 if (dict_find(what, (char_u *)"id", -1) != NULL)
4888 flags |= QF_GETLIST_ID;
4889
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004890 if (dict_find(what, (char_u *)"items", -1) != NULL)
4891 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004892
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02004893 if (dict_find(what, (char_u *)"idx", -1) != NULL)
4894 flags |= QF_GETLIST_IDX;
4895
4896 if (dict_find(what, (char_u *)"size", -1) != NULL)
4897 flags |= QF_GETLIST_SIZE;
4898
Bram Moolenaara6d48492017-12-12 22:45:31 +01004899 if (qi != NULL && qi->qf_listcount != 0)
4900 {
4901 qf_idx = qi->qf_curlist; /* default is the current list */
4902 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4903 {
4904 /* Use the specified quickfix/location list */
4905 if (di->di_tv.v_type == VAR_NUMBER)
4906 {
4907 /* for zero use the current list */
4908 if (di->di_tv.vval.v_number != 0)
4909 {
4910 qf_idx = di->di_tv.vval.v_number - 1;
4911 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4912 qf_idx = -1;
4913 }
4914 }
4915 else if ((di->di_tv.v_type == VAR_STRING)
4916 && (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4917 /* Get the last quickfix list number */
4918 qf_idx = qi->qf_listcount - 1;
4919 else
4920 qf_idx = -1;
4921 flags |= QF_GETLIST_NR;
4922 }
4923
4924 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
4925 {
4926 /* Look for a list with the specified id */
4927 if (di->di_tv.v_type == VAR_NUMBER)
4928 {
4929 /*
4930 * For zero, use the current list or the list specifed by 'nr'
4931 */
4932 if (di->di_tv.vval.v_number != 0)
4933 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
4934 flags |= QF_GETLIST_ID;
4935 }
4936 else
4937 qf_idx = -1;
4938 }
4939 }
4940
4941 /* List is not present or is empty */
4942 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == -1)
4943 {
4944 if (flags & QF_GETLIST_TITLE)
4945 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
4946 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
4947 {
4948 list_T *l = list_alloc();
4949 if (l != NULL)
4950 status = dict_add_list(retdict, "items", l);
4951 else
4952 status = FAIL;
4953 }
4954 if ((status == OK) && (flags & QF_GETLIST_NR))
4955 status = dict_add_nr_str(retdict, "nr", 0L, NULL);
4956 if ((status == OK) && (flags & QF_GETLIST_WINID))
4957 status = dict_add_nr_str(retdict, "winid", 0L, NULL);
4958 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
4959 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4960 if ((status == OK) && (flags & QF_GETLIST_ID))
4961 status = dict_add_nr_str(retdict, "id", 0L, NULL);
4962 if ((status == OK) && (flags & QF_GETLIST_IDX))
4963 status = dict_add_nr_str(retdict, "idx", 0L, NULL);
4964 if ((status == OK) && (flags & QF_GETLIST_SIZE))
4965 status = dict_add_nr_str(retdict, "size", 0L, NULL);
4966
4967 return status;
4968 }
4969
Bram Moolenaard823fa92016-08-12 16:29:27 +02004970 if (flags & QF_GETLIST_TITLE)
4971 {
4972 char_u *t;
4973 t = qi->qf_lists[qf_idx].qf_title;
4974 if (t == NULL)
4975 t = (char_u *)"";
4976 status = dict_add_nr_str(retdict, "title", 0L, t);
4977 }
4978 if ((status == OK) && (flags & QF_GETLIST_NR))
4979 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
4980 if ((status == OK) && (flags & QF_GETLIST_WINID))
4981 {
4982 win_T *win;
Bram Moolenaar74240d32017-12-10 15:26:15 +01004983 int win_id = 0;
4984
Bram Moolenaard823fa92016-08-12 16:29:27 +02004985 win = qf_find_win(qi);
4986 if (win != NULL)
Bram Moolenaar74240d32017-12-10 15:26:15 +01004987 win_id = win->w_id;
4988 status = dict_add_nr_str(retdict, "winid", win_id, NULL);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004989 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004990 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
4991 {
4992 list_T *l = list_alloc();
4993 if (l != NULL)
4994 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004995 (void)get_errorlist(qi, NULL, qf_idx, l);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004996 dict_add_list(retdict, "items", l);
4997 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02004998 else
4999 status = FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005000 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005001
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005002 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5003 {
5004 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5005 {
5006 di = dictitem_alloc((char_u *)"context");
5007 if (di != NULL)
5008 {
5009 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005010 status = dict_add(retdict, di);
5011 if (status == FAIL)
Bram Moolenaarbeb9cb12017-05-01 14:14:04 +02005012 dictitem_free(di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005013 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005014 else
5015 status = FAIL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005016 }
5017 else
5018 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5019 }
5020
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005021 if ((status == OK) && (flags & QF_GETLIST_ID))
5022 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
5023 NULL);
5024
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005025 if ((status == OK) && (flags & QF_GETLIST_IDX))
5026 {
5027 int idx = qi->qf_lists[qf_idx].qf_index;
5028 if (qi->qf_lists[qf_idx].qf_count == 0)
5029 /* For empty lists, qf_index is set to 1 */
5030 idx = 0;
5031 status = dict_add_nr_str(retdict, "idx", idx, NULL);
5032 }
5033
5034 if ((status == OK) && (flags & QF_GETLIST_SIZE))
5035 status = dict_add_nr_str(retdict, "size",
5036 qi->qf_lists[qf_idx].qf_count, NULL);
5037
Bram Moolenaard823fa92016-08-12 16:29:27 +02005038 return status;
5039}
5040
5041/*
5042 * Add list of entries to quickfix/location list. Each list entry is
5043 * a dictionary with item information.
5044 */
5045 static int
5046qf_add_entries(
5047 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005048 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005049 list_T *list,
5050 char_u *title,
5051 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005052{
5053 listitem_T *li;
5054 dict_T *d;
5055 char_u *filename, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005056 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005057 long lnum;
5058 int col, nr;
5059 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005060 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005061 int valid, status;
5062 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005063 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005064
Bram Moolenaara3921f42017-06-04 15:30:34 +02005065 if (action == ' ' || qf_idx == qi->qf_listcount)
5066 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005067 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005068 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005069 qf_idx = qi->qf_curlist;
5070 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005071 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005072 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005073 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005074 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005075 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005076 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005077 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005078 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005079
5080 for (li = list->lv_first; li != NULL; li = li->li_next)
5081 {
5082 if (li->li_tv.v_type != VAR_DICT)
5083 continue; /* Skip non-dict items */
5084
5085 d = li->li_tv.vval.v_dict;
5086 if (d == NULL)
5087 continue;
5088
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005089 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005090 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5091 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5092 col = (int)get_dict_number(d, (char_u *)"col");
5093 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5094 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005095 type = get_dict_string(d, (char_u *)"type", TRUE);
5096 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5097 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005098 if (text == NULL)
5099 text = vim_strsave((char_u *)"");
5100
5101 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005102 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005103 valid = FALSE;
5104
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005105 /* Mark entries with non-existing buffer number as not valid. Give the
5106 * error message only once. */
5107 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5108 {
5109 if (!did_bufnr_emsg)
5110 {
5111 did_bufnr_emsg = TRUE;
5112 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5113 }
5114 valid = FALSE;
5115 bufnum = 0;
5116 }
5117
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005118 /* If the 'valid' field is present it overrules the detected value. */
5119 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5120 valid = (int)get_dict_number(d, (char_u *)"valid");
5121
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005122 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005123 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005124 NULL, /* dir */
5125 filename,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005126 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005127 text,
5128 lnum,
5129 col,
5130 vcol, /* vis_col */
5131 pattern, /* search pattern */
5132 nr,
5133 type == NULL ? NUL : *type,
5134 valid);
5135
5136 vim_free(filename);
5137 vim_free(pattern);
5138 vim_free(text);
5139 vim_free(type);
5140
5141 if (status == FAIL)
5142 {
5143 retval = FAIL;
5144 break;
5145 }
5146 }
5147
Bram Moolenaara3921f42017-06-04 15:30:34 +02005148 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005149 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005150 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005151 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005152 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005153 if (action != 'a')
5154 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005155 qi->qf_lists[qf_idx].qf_ptr =
5156 qi->qf_lists[qf_idx].qf_start;
5157 if (qi->qf_lists[qf_idx].qf_count > 0)
5158 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005159 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005160
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005161 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005162 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005163
5164 return retval;
5165}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005166
5167 static int
Bram Moolenaarae338332017-08-11 20:25:26 +02005168qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005169{
5170 dictitem_T *di;
5171 int retval = FAIL;
5172 int qf_idx;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005173 int newlist = FALSE;
Bram Moolenaar36538222017-09-02 19:51:44 +02005174 char_u *errorformat = p_efm;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005175
5176 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
5177 newlist = TRUE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005178
5179 qf_idx = qi->qf_curlist; /* default is the current list */
5180 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5181 {
5182 /* Use the specified quickfix/location list */
5183 if (di->di_tv.v_type == VAR_NUMBER)
5184 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005185 /* for zero use the current list */
5186 if (di->di_tv.vval.v_number != 0)
5187 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005188
Bram Moolenaar55b69262017-08-13 13:42:01 +02005189 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5190 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005191 /*
5192 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005193 * non-available list and add the new list at the end of the
5194 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005195 */
5196 newlist = TRUE;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005197 qf_idx = qi->qf_listcount - 1;
5198 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005199 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005200 return FAIL;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005201 else if (action != ' ')
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005202 newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005203 }
5204 else if (di->di_tv.v_type == VAR_STRING
5205 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005206 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005207 if (qi->qf_listcount > 0)
5208 qf_idx = qi->qf_listcount - 1;
5209 else if (newlist)
5210 qf_idx = 0;
5211 else
5212 return FAIL;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005213 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005214 else
5215 return FAIL;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005216 }
5217
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005218 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
5219 {
5220 /* Use the quickfix/location list with the specified id */
5221 if (di->di_tv.v_type == VAR_NUMBER)
5222 {
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005223 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5224 if (qf_idx == -1)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005225 return FAIL; /* List not found */
5226 }
5227 else
5228 return FAIL;
5229 }
5230
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005231 if (newlist)
5232 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005233 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02005234 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005235 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005236 }
5237
5238 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
5239 {
5240 if (di->di_tv.v_type == VAR_STRING)
5241 {
5242 vim_free(qi->qf_lists[qf_idx].qf_title);
5243 qi->qf_lists[qf_idx].qf_title =
5244 get_dict_string(what, (char_u *)"title", TRUE);
5245 if (qf_idx == qi->qf_curlist)
5246 qf_update_win_titlevar(qi);
5247 retval = OK;
5248 }
5249 }
Bram Moolenaar36538222017-09-02 19:51:44 +02005250
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005251 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
5252 {
5253 if (di->di_tv.v_type == VAR_LIST)
5254 {
5255 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5256
5257 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5258 title_save, action == ' ' ? 'a' : action);
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005259 if (action == 'r')
5260 {
5261 /*
5262 * When replacing the quickfix list entries using
5263 * qf_add_entries(), the title is set with a ':' prefix.
5264 * Restore the title with the saved title.
5265 */
5266 vim_free(qi->qf_lists[qf_idx].qf_title);
5267 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save);
5268 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005269 vim_free(title_save);
5270 }
5271 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005272
Bram Moolenaar36538222017-09-02 19:51:44 +02005273 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5274 {
5275 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
5276 return FAIL;
5277 errorformat = di->di_tv.vval.v_string;
5278 }
5279
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005280 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005281 {
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005282 /* Only a List value is supported */
5283 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005284 {
5285 if (action == 'r')
5286 qf_free_items(qi, qf_idx);
Bram Moolenaar36538222017-09-02 19:51:44 +02005287 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaarae338332017-08-11 20:25:26 +02005288 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5289 retval = OK;
5290 }
5291 else
5292 return FAIL;
5293 }
5294
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005295 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
5296 {
5297 typval_T *ctx;
Bram Moolenaar875feea2017-06-11 16:07:51 +02005298
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005299 free_tv(qi->qf_lists[qf_idx].qf_ctx);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005300 ctx = alloc_tv();
5301 if (ctx != NULL)
5302 copy_tv(&di->di_tv, ctx);
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005303 qi->qf_lists[qf_idx].qf_ctx = ctx;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02005304 retval = OK;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005305 }
5306
Bram Moolenaard823fa92016-08-12 16:29:27 +02005307 return retval;
5308}
5309
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005310/*
5311 * Find the non-location list window with the specified location list.
5312 */
5313 static win_T *
5314find_win_with_ll(qf_info_T *qi)
5315{
5316 win_T *wp = NULL;
5317
5318 FOR_ALL_WINDOWS(wp)
5319 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
5320 return wp;
5321
5322 return NULL;
5323}
5324
5325/*
5326 * Free the entire quickfix/location list stack.
5327 * If the quickfix/location list window is open, then clear it.
5328 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005329 static void
5330qf_free_stack(win_T *wp, qf_info_T *qi)
5331{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005332 win_T *qfwin = qf_find_win(qi);
5333 win_T *llwin = NULL;
5334 win_T *orig_wp = wp;
5335
5336 if (qfwin != NULL)
5337 {
5338 /* If the quickfix/location list window is open, then clear it */
5339 if (qi->qf_curlist < qi->qf_listcount)
5340 qf_free(qi, qi->qf_curlist);
5341 qf_update_buffer(qi, NULL);
5342 }
5343
5344 if (wp != NULL && IS_LL_WINDOW(wp))
5345 {
5346 /* If in the location list window, then use the non-location list
5347 * window with this location list (if present)
5348 */
5349 llwin = find_win_with_ll(qi);
5350 if (llwin != NULL)
5351 wp = llwin;
5352 }
5353
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005354 qf_free_all(wp);
5355 if (wp == NULL)
5356 {
5357 /* quickfix list */
5358 qi->qf_curlist = 0;
5359 qi->qf_listcount = 0;
5360 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005361 else if (IS_LL_WINDOW(orig_wp))
5362 {
5363 /* If the location list window is open, then create a new empty
5364 * location list */
5365 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02005366
Bram Moolenaard788f6f2017-04-23 17:19:43 +02005367 /* first free the list reference in the location list window */
5368 ll_free_all(&orig_wp->w_llist_ref);
5369
Bram Moolenaar69f40be2017-04-02 15:15:49 +02005370 orig_wp->w_llist_ref = new_ll;
5371 if (llwin != NULL)
5372 {
5373 llwin->w_llist = new_ll;
5374 new_ll->qf_refcount++;
5375 }
5376 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005377}
5378
Bram Moolenaard823fa92016-08-12 16:29:27 +02005379/*
5380 * Populate the quickfix list with the items supplied in the list
5381 * of dictionaries. "title" will be copied to w:quickfix_title.
5382 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
5383 */
5384 int
5385set_errorlist(
5386 win_T *wp,
5387 list_T *list,
5388 int action,
5389 char_u *title,
5390 dict_T *what)
5391{
5392 qf_info_T *qi = &ql_info;
5393 int retval = OK;
5394
5395 if (wp != NULL)
5396 {
5397 qi = ll_get_or_alloc_list(wp);
5398 if (qi == NULL)
5399 return FAIL;
5400 }
5401
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005402 if (action == 'f')
5403 {
5404 /* Free the entire quickfix or location list stack */
5405 qf_free_stack(wp, qi);
5406 }
5407 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02005408 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005409 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005410 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005411
5412 return retval;
5413}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005414
5415 static int
5416mark_quickfix_ctx(qf_info_T *qi, int copyID)
5417{
5418 int i;
5419 int abort = FALSE;
5420 typval_T *ctx;
5421
5422 for (i = 0; i < LISTCOUNT && !abort; ++i)
5423 {
5424 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005425 if (ctx != NULL && ctx->v_type != VAR_NUMBER
5426 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005427 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
5428 }
5429
5430 return abort;
5431}
5432
5433/*
5434 * Mark the context of the quickfix list and the location lists (if present) as
5435 * "in use". So that garabage collection doesn't free the context.
5436 */
5437 int
5438set_ref_in_quickfix(int copyID)
5439{
5440 int abort = FALSE;
5441 tabpage_T *tp;
5442 win_T *win;
5443
5444 abort = mark_quickfix_ctx(&ql_info, copyID);
5445 if (abort)
5446 return abort;
5447
5448 FOR_ALL_TAB_WINDOWS(tp, win)
5449 {
5450 if (win->w_llist != NULL)
5451 {
5452 abort = mark_quickfix_ctx(win->w_llist, copyID);
5453 if (abort)
5454 return abort;
5455 }
5456 }
5457
5458 return abort;
5459}
Bram Moolenaar05159a02005-02-26 23:04:13 +00005460#endif
5461
Bram Moolenaar81695252004-12-29 20:58:21 +00005462/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005463 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005464 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005465 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005466 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00005467 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00005468 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00005469 */
5470 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005471ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005472{
5473 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005474 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005475#ifdef FEAT_AUTOCMD
5476 char_u *au_name = NULL;
5477#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005478 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005479
Bram Moolenaardb552d602006-03-23 22:59:57 +00005480 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer
5481 || eap->cmdidx == CMD_laddbuffer)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005482 {
5483 qi = ll_get_or_alloc_list(curwin);
5484 if (qi == NULL)
5485 return;
5486 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005487
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005488#ifdef FEAT_AUTOCMD
5489 switch (eap->cmdidx)
5490 {
5491 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
5492 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
5493 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
5494 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
5495 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
5496 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
5497 default: break;
5498 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005499 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5500 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005501 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005502# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005503 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005504 return;
5505# endif
5506 }
5507#endif
5508
Bram Moolenaar86b68352004-12-27 21:59:20 +00005509 if (*eap->arg == NUL)
5510 buf = curbuf;
5511 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
5512 buf = buflist_findnr(atoi((char *)eap->arg));
5513 if (buf == NULL)
5514 EMSG(_(e_invarg));
5515 else if (buf->b_ml.ml_mfp == NULL)
5516 EMSG(_("E681: Buffer is not loaded"));
5517 else
5518 {
5519 if (eap->addr_count == 0)
5520 {
5521 eap->line1 = 1;
5522 eap->line2 = buf->b_ml.ml_line_count;
5523 }
5524 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
5525 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
5526 EMSG(_(e_invrange));
5527 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00005528 {
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005529 char_u *qf_title = *eap->cmdlinep;
5530
5531 if (buf->b_sfname)
5532 {
5533 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
5534 (char *)qf_title, (char *)buf->b_sfname);
5535 qf_title = IObuff;
5536 }
5537
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005538 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005539 (eap->cmdidx != CMD_caddbuffer
5540 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005541 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005542 qf_title, NULL);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005543#ifdef FEAT_AUTOCMD
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005544 if (au_name != NULL)
5545 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5546 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005547#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005548 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
5549 eap->cmdidx == CMD_lbuffer))
5550 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar754b5602006-02-09 23:53:20 +00005551 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005552 }
5553}
5554
Bram Moolenaar1e015462005-09-25 22:16:38 +00005555#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005556/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00005557 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
5558 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005559 */
5560 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005561ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005562{
5563 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005564 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005565#ifdef FEAT_AUTOCMD
5566 char_u *au_name = NULL;
5567#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005568 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005569
Bram Moolenaardb552d602006-03-23 22:59:57 +00005570 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr
5571 || eap->cmdidx == CMD_laddexpr)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005572 {
5573 qi = ll_get_or_alloc_list(curwin);
5574 if (qi == NULL)
5575 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005576 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005577
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005578#ifdef FEAT_AUTOCMD
5579 switch (eap->cmdidx)
5580 {
5581 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
5582 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
5583 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
5584 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
5585 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
5586 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
5587 default: break;
5588 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005589 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5590 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005591 {
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005592# ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005593 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005594 return;
5595# endif
5596 }
5597#endif
5598
Bram Moolenaar4770d092006-01-12 23:22:24 +00005599 /* Evaluate the expression. When the result is a string or a list we can
5600 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005601 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005602 if (tv != NULL)
5603 {
5604 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
5605 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
5606 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005607 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00005608 (eap->cmdidx != CMD_caddexpr
5609 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005610 (linenr_T)0, (linenr_T)0, *eap->cmdlinep,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005611 NULL);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005612#ifdef FEAT_AUTOCMD
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005613 if (au_name != NULL)
5614 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5615 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02005616#endif
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005617 if (res > 0 && (eap->cmdidx == CMD_cexpr ||
5618 eap->cmdidx == CMD_lexpr))
5619 qf_jump(qi, 0, 0, eap->forceit); /* display first error */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005620 }
5621 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00005622 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005623 free_tv(tv);
5624 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005625}
Bram Moolenaar1e015462005-09-25 22:16:38 +00005626#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005627
5628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 * ":helpgrep {pattern}"
5630 */
5631 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005632ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
5634 regmatch_T regmatch;
5635 char_u *save_cpo;
5636 char_u *p;
5637 int fcount;
5638 char_u **fnames;
5639 FILE *fd;
5640 int fi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 long lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005642#ifdef FEAT_MULTI_LANG
5643 char_u *lang;
5644#endif
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005645 qf_info_T *qi = &ql_info;
Bram Moolenaaree85df32017-03-19 14:19:50 +01005646 qf_info_T *save_qi;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005647 int new_qi = FALSE;
5648 win_T *wp;
Bram Moolenaar73633f82012-01-20 13:39:07 +01005649#ifdef FEAT_AUTOCMD
5650 char_u *au_name = NULL;
5651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005653#ifdef FEAT_MULTI_LANG
5654 /* Check for a specified language */
5655 lang = check_help_lang(eap->arg);
5656#endif
5657
Bram Moolenaar73633f82012-01-20 13:39:07 +01005658#ifdef FEAT_AUTOCMD
5659 switch (eap->cmdidx)
5660 {
5661 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
5662 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
5663 default: break;
5664 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01005665 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5666 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01005667 {
Bram Moolenaar21662be2016-11-06 14:46:44 +01005668# ifdef FEAT_EVAL
5669 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01005670 return;
Bram Moolenaar21662be2016-11-06 14:46:44 +01005671# endif
Bram Moolenaar73633f82012-01-20 13:39:07 +01005672 }
5673#endif
5674
5675 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
5676 save_cpo = p_cpo;
5677 p_cpo = empty_option;
5678
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005679 if (eap->cmdidx == CMD_lhelpgrep)
5680 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005681 /* If the current window is a help window, then use it */
5682 if (bt_help(curwin->w_buffer))
5683 wp = curwin;
5684 else
5685 /* Find an existing help window */
5686 FOR_ALL_WINDOWS(wp)
5687 if (bt_help(wp->w_buffer))
5688 break;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005689
5690 if (wp == NULL) /* Help window not found */
5691 qi = NULL;
5692 else
5693 qi = wp->w_llist;
5694
5695 if (qi == NULL)
5696 {
5697 /* Allocate a new location list for help text matches */
5698 if ((qi = ll_new_list()) == NULL)
5699 return;
5700 new_qi = TRUE;
5701 }
5702 }
5703
Bram Moolenaaree85df32017-03-19 14:19:50 +01005704 /* Autocommands may change the list. Save it for later comparison */
5705 save_qi = qi;
5706
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
5708 regmatch.rm_ic = FALSE;
5709 if (regmatch.regprog != NULL)
5710 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005711#ifdef FEAT_MBYTE
5712 vimconv_T vc;
5713
5714 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
5715 * differs. */
5716 vc.vc_type = CONV_NONE;
5717 if (!enc_utf8)
5718 convert_setup(&vc, (char_u *)"utf-8", p_enc);
5719#endif
5720
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 /* create a new quickfix list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005722 qf_new_list(qi, *eap->cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
5724 /* Go through all directories in 'runtimepath' */
5725 p = p_rtp;
5726 while (*p != NUL && !got_int)
5727 {
5728 copy_option_part(&p, NameBuff, MAXPATHL, ",");
5729
5730 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
5731 add_pathsep(NameBuff);
5732 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)");
5733 if (gen_expand_wildcards(1, &NameBuff, &fcount,
5734 &fnames, EW_FILE|EW_SILENT) == OK
5735 && fcount > 0)
5736 {
5737 for (fi = 0; fi < fcount && !got_int; ++fi)
5738 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005739#ifdef FEAT_MULTI_LANG
5740 /* Skip files for a different language. */
5741 if (lang != NULL
5742 && STRNICMP(lang, fnames[fi]
5743 + STRLEN(fnames[fi]) - 3, 2) != 0
5744 && !(STRNICMP(lang, "en", 2) == 0
5745 && STRNICMP("txt", fnames[fi]
5746 + STRLEN(fnames[fi]) - 3, 3) == 0))
5747 continue;
5748#endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005749 fd = mch_fopen((char *)fnames[fi], "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 if (fd != NULL)
5751 {
5752 lnum = 1;
5753 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
5754 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005755 char_u *line = IObuff;
5756#ifdef FEAT_MBYTE
5757 /* Convert a line if 'encoding' is not utf-8 and
5758 * the line contains a non-ASCII character. */
5759 if (vc.vc_type != CONV_NONE
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005760 && has_non_ascii(IObuff))
5761 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005762 line = string_convert(&vc, IObuff, NULL);
5763 if (line == NULL)
5764 line = IObuff;
5765 }
5766#endif
5767
5768 if (vim_regexec(&regmatch, line, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005770 int l = (int)STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
5772 /* remove trailing CR, LF, spaces, etc. */
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005773 while (l > 0 && line[l - 1] <= ' ')
5774 line[--l] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005776 if (qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005777 qi->qf_curlist,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 NULL, /* dir */
5779 fnames[fi],
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005780 0,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005781 line,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 lnum,
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005783 (int)(regmatch.startp[0] - line)
Bram Moolenaar81695252004-12-29 20:58:21 +00005784 + 1, /* col */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005785 FALSE, /* vis_col */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005786 NULL, /* search pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 0, /* nr */
5788 1, /* type */
5789 TRUE /* valid */
5790 ) == FAIL)
5791 {
5792 got_int = TRUE;
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005793#ifdef FEAT_MBYTE
5794 if (line != IObuff)
5795 vim_free(line);
5796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 break;
5798 }
5799 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005800#ifdef FEAT_MBYTE
5801 if (line != IObuff)
5802 vim_free(line);
5803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 ++lnum;
5805 line_breakcheck();
5806 }
5807 fclose(fd);
5808 }
5809 }
5810 FreeWild(fcount, fnames);
5811 }
5812 }
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005813
Bram Moolenaar473de612013-06-08 18:19:48 +02005814 vim_regfree(regmatch.regprog);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01005815#ifdef FEAT_MBYTE
5816 if (vc.vc_type != CONV_NONE)
5817 convert_setup(&vc, NULL, NULL);
5818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005820 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5821 qi->qf_lists[qi->qf_curlist].qf_ptr =
5822 qi->qf_lists[qi->qf_curlist].qf_start;
5823 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 }
5825
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005826 if (p_cpo == empty_option)
5827 p_cpo = save_cpo;
5828 else
5829 /* Darn, some plugin changed the value. */
5830 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaar864293a2016-06-02 13:40:04 +02005832 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
Bram Moolenaar73633f82012-01-20 13:39:07 +01005834#ifdef FEAT_AUTOCMD
5835 if (au_name != NULL)
5836 {
5837 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5838 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaaree85df32017-03-19 14:19:50 +01005839 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01005840 /* autocommands made "qi" invalid */
5841 return;
5842 }
5843#endif
5844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005846 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005847 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005848 else
5849 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005850
5851 if (eap->cmdidx == CMD_lhelpgrep)
5852 {
5853 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00005854 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005855 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005856 {
5857 if (new_qi)
5858 ll_free_all(&qi);
5859 }
5860 else if (curwin->w_llist == NULL)
5861 curwin->w_llist = qi;
5862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863}
5864
5865#endif /* FEAT_QUICKFIX */