blob: 6108b36be3dbd402ee1eaa09740e68be7f7e8c91 [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 */
Bram Moolenaard76ce852018-05-01 15:02:04 +020036 char_u *qf_module; /* module name for this error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000037 char_u *qf_pattern; /* search pattern for the error */
38 char_u *qf_text; /* description of the error */
39 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
40 char_u qf_cleared; /* set to TRUE if line has been deleted */
41 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000043 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044};
45
46/*
47 * There is a stack of error lists.
48 */
49#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020050#define INVALID_QFIDX (-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020052/*
53 * Quickfix/Location list definition
54 * Contains a list of entries (qfline_T). qf_start points to the first entry
55 * and qf_last points to the last entry. qf_count contains the list size.
56 *
57 * Usually the list contains one or more entries. But an empty list can be
58 * created using setqflist()/setloclist() with a title and/or user context
59 * information and entries can be added later using setqflist()/setloclist().
60 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000062{
Bram Moolenaara539f4f2017-08-30 20:33:55 +020063 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020065 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020067 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000068 int qf_index; /* current index in the error list */
69 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020070 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020071 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020072 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020073
74 struct dir_stack_T *qf_dir_stack;
75 char_u *qf_directory;
76 struct dir_stack_T *qf_file_stack;
77 char_u *qf_currfile;
78 int qf_multiline;
79 int qf_multiignore;
80 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010081 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000082} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020084/*
85 * Quickfix/Location list stack definition
86 * Contains a list of quickfix/location lists (qf_list_T)
87 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000088struct qf_info_S
89{
90 /*
91 * Count of references to this list. Used only for location lists.
92 * When a location list window reference this list, qf_refcount
93 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
94 * reaches 0, the list is freed.
95 */
96 int qf_refcount;
97 int qf_listcount; /* current number of lists */
98 int qf_curlist; /* current error list */
99 qf_list_T qf_lists[LISTCOUNT];
100};
101
102static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200103static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
Bram Moolenaard76ce852018-05-01 15:02:04 +0200105#define FMT_PATTERNS 11 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
107/*
108 * Structure used to hold the info of one part of 'errorformat'
109 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000110typedef struct efm_S efm_T;
111struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112{
113 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
116 char_u prefix; /* prefix of this format line: */
117 /* 'D' enter directory */
118 /* 'X' leave directory */
119 /* 'A' start of multi-line message */
120 /* 'E' error message */
121 /* 'W' warning message */
122 /* 'I' informational message */
123 /* 'C' continuation line */
124 /* 'Z' end of multi-line message */
125 /* 'G' general, unspecific message */
126 /* 'P' push file (partial) message */
127 /* 'Q' pop/quit file (partial) message */
128 /* 'O' overread (partial) message */
129 char_u flags; /* additional flags given in prefix */
130 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000131 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000132 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133};
134
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100135static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
136
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaard76ce852018-05-01 15:02:04 +0200138static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, char_u *module, 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 void qf_free(qf_info_T *qi, int idx);
140static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200141static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200142static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200144static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static win_T *qf_find_win(qf_info_T *qi);
148static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200149static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200151static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static char_u *get_mef_name(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
154static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
155static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
156static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000158/* Quickfix window check helper macro */
159#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
160/* Location list window check helper macro */
161#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
162/*
163 * Return location list for window 'wp'
164 * For location list window, return the referenced location list
165 */
166#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200169 * Looking up a buffer can be slow if there are many. Remember the last one
170 * to make this a lot faster if there are multiple matches in the same file.
171 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200172static char_u *qf_last_bufname = NULL;
173static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200174
Bram Moolenaar3c097222017-12-21 20:54:49 +0100175static char *e_loc_list_changed =
176 N_("E926: Current location list was changed");
177
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200178/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200179 * Maximum number of bytes allowed per line while reading a errorfile.
180 */
181#define LINE_MAXLEN 4096
182
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200183static struct fmtpattern
184{
185 char_u convchar;
186 char *pattern;
187} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200188 {
189 {'f', ".\\+"}, /* only used when at end */
190 {'n', "\\d\\+"},
191 {'l', "\\d\\+"},
192 {'c', "\\d\\+"},
193 {'t', "."},
194 {'m', ".\\+"},
195 {'r', ".*"},
196 {'p', "[- .]*"},
197 {'v', "\\d\\+"},
198 {'s', ".\\+"},
199 {'o', ".\\+"}
200 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200201
202/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200203 * Convert an errorformat pattern to a regular expression pattern.
204 * See fmt_pat definition above for the list of supported patterns.
205 */
206 static char_u *
207fmtpat_to_regpat(
208 char_u *efmp,
209 efm_T *fmt_ptr,
210 int idx,
211 int round,
212 char_u *ptr,
213 char_u *errmsg)
214{
215 char_u *srcptr;
216
217 if (fmt_ptr->addr[idx])
218 {
219 /* Each errorformat pattern can occur only once */
220 sprintf((char *)errmsg,
221 _("E372: Too many %%%c in format string"), *efmp);
222 EMSG(errmsg);
223 return NULL;
224 }
225 if ((idx && idx < 6
226 && vim_strchr((char_u *)"DXOPQ", fmt_ptr->prefix) != NULL)
227 || (idx == 6
228 && vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL))
229 {
230 sprintf((char *)errmsg,
231 _("E373: Unexpected %%%c in format string"), *efmp);
232 EMSG(errmsg);
233 return NULL;
234 }
235 fmt_ptr->addr[idx] = (char_u)++round;
236 *ptr++ = '\\';
237 *ptr++ = '(';
238#ifdef BACKSLASH_IN_FILENAME
239 if (*efmp == 'f')
240 {
241 /* Also match "c:" in the file name, even when
242 * checking for a colon next: "%f:".
243 * "\%(\a:\)\=" */
244 STRCPY(ptr, "\\%(\\a:\\)\\=");
245 ptr += 10;
246 }
247#endif
248 if (*efmp == 'f' && efmp[1] != NUL)
249 {
250 if (efmp[1] != '\\' && efmp[1] != '%')
251 {
252 /* A file name may contain spaces, but this isn't
253 * in "\f". For "%f:%l:%m" there may be a ":" in
254 * the file name. Use ".\{-1,}x" instead (x is
255 * the next character), the requirement that :999:
256 * follows should work. */
257 STRCPY(ptr, ".\\{-1,}");
258 ptr += 7;
259 }
260 else
261 {
262 /* File name followed by '\\' or '%': include as
263 * many file name chars as possible. */
264 STRCPY(ptr, "\\f\\+");
265 ptr += 4;
266 }
267 }
268 else
269 {
270 srcptr = (char_u *)fmt_pat[idx].pattern;
271 while ((*ptr = *srcptr++) != NUL)
272 ++ptr;
273 }
274 *ptr++ = '\\';
275 *ptr++ = ')';
276
277 return ptr;
278}
279
280/*
281 * Convert a scanf like format in 'errorformat' to a regular expression.
282 */
283 static char_u *
284scanf_fmt_to_regpat(
285 char_u *efm,
286 int len,
287 char_u **pefmp,
288 char_u *ptr,
289 char_u *errmsg)
290{
291 char_u *efmp = *pefmp;
292
293 if (*++efmp == '[' || *efmp == '\\')
294 {
295 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
296 {
297 if (efmp[1] == '^')
298 *ptr++ = *++efmp;
299 if (efmp < efm + len)
300 {
301 *ptr++ = *++efmp; /* could be ']' */
302 while (efmp < efm + len
303 && (*ptr++ = *++efmp) != ']')
304 /* skip */;
305 if (efmp == efm + len)
306 {
307 EMSG(_("E374: Missing ] in format string"));
308 return NULL;
309 }
310 }
311 }
312 else if (efmp < efm + len) /* %*\D, %*\s etc. */
313 *ptr++ = *++efmp;
314 *ptr++ = '\\';
315 *ptr++ = '+';
316 }
317 else
318 {
319 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
320 sprintf((char *)errmsg,
321 _("E375: Unsupported %%%c in format string"), *efmp);
322 EMSG(errmsg);
323 return NULL;
324 }
325
326 *pefmp = efmp;
327
328 return ptr;
329}
330
331/*
332 * Analyze/parse an errorformat prefix.
333 */
334 static int
335efm_analyze_prefix(char_u **pefmp, efm_T *fmt_ptr, char_u *errmsg)
336{
337 char_u *efmp = *pefmp;
338
339 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
340 fmt_ptr->flags = *efmp++;
341 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
342 fmt_ptr->prefix = *efmp;
343 else
344 {
345 sprintf((char *)errmsg,
346 _("E376: Invalid %%%c in format string prefix"), *efmp);
347 EMSG(errmsg);
348 return FAIL;
349 }
350
351 *pefmp = efmp;
352
353 return OK;
354}
355
356/*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200357 * Converts a 'errorformat' string to regular expression pattern
358 */
359 static int
360efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200361 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200362 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200363 efm_T *fmt_ptr,
364 char_u *regpat,
365 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200366{
367 char_u *ptr;
368 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200369 int round;
370 int idx = 0;
371
372 /*
373 * Build regexp pattern from current 'errorformat' option
374 */
375 ptr = regpat;
376 *ptr++ = '^';
377 round = 0;
378 for (efmp = efm; efmp < efm + len; ++efmp)
379 {
380 if (*efmp == '%')
381 {
382 ++efmp;
383 for (idx = 0; idx < FMT_PATTERNS; ++idx)
384 if (fmt_pat[idx].convchar == *efmp)
385 break;
386 if (idx < FMT_PATTERNS)
387 {
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200388 ptr = fmtpat_to_regpat(efmp, fmt_ptr, idx, round, ptr,
389 errmsg);
390 if (ptr == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200391 return -1;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200392 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200393 }
394 else if (*efmp == '*')
395 {
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200396 ptr = scanf_fmt_to_regpat(efm, len, &efmp, ptr, errmsg);
397 if (ptr == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200398 return -1;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200399 }
400 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
401 *ptr++ = *efmp; /* regexp magic characters */
402 else if (*efmp == '#')
403 *ptr++ = '*';
404 else if (*efmp == '>')
405 fmt_ptr->conthere = TRUE;
406 else if (efmp == efm + 1) /* analyse prefix */
407 {
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200408 if (efm_analyze_prefix(&efmp, fmt_ptr, errmsg) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200409 return -1;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200410 }
411 else
412 {
413 sprintf((char *)errmsg,
414 _("E377: Invalid %%%c in format string"), *efmp);
415 EMSG(errmsg);
416 return -1;
417 }
418 }
419 else /* copy normal character */
420 {
421 if (*efmp == '\\' && efmp + 1 < efm + len)
422 ++efmp;
423 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
424 *ptr++ = '\\'; /* escape regexp atoms */
425 if (*efmp)
426 *ptr++ = *efmp;
427 }
428 }
429 *ptr++ = '$';
430 *ptr = NUL;
431
432 return 0;
433}
434
435 static void
436free_efm_list(efm_T **efm_first)
437{
438 efm_T *efm_ptr;
439
440 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
441 {
442 *efm_first = efm_ptr->next;
443 vim_regfree(efm_ptr->prog);
444 vim_free(efm_ptr);
445 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100446 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200447}
448
449/* Parse 'errorformat' option */
450 static efm_T *
451parse_efm_option(char_u *efm)
452{
453 char_u *errmsg = NULL;
454 int errmsglen;
455 efm_T *fmt_ptr = NULL;
456 efm_T *fmt_first = NULL;
457 efm_T *fmt_last = NULL;
458 char_u *fmtstr = NULL;
459 int len;
460 int i;
461 int round;
462
463 errmsglen = CMDBUFFSIZE + 1;
464 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
465 if (errmsg == NULL)
466 goto parse_efm_end;
467
468 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200469 * Each part of the format string is copied and modified from errorformat
470 * to regex prog. Only a few % characters are allowed.
471 */
472
473 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200474 * Get some space to modify the format string into.
475 */
476 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
477 for (round = FMT_PATTERNS; round > 0; )
478 i += (int)STRLEN(fmt_pat[--round].pattern);
Bram Moolenaar0b05e492017-09-24 19:39:09 +0200479#ifdef BACKSLASH_IN_FILENAME
480 i += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200481#else
482 i += 2; /* "%f" can become two chars longer */
483#endif
484 if ((fmtstr = alloc(i)) == NULL)
485 goto parse_efm_error;
486
487 while (efm[0] != NUL)
488 {
489 /*
490 * Allocate a new eformat structure and put it at the end of the list
491 */
492 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
493 if (fmt_ptr == NULL)
494 goto parse_efm_error;
495 if (fmt_first == NULL) /* first one */
496 fmt_first = fmt_ptr;
497 else
498 fmt_last->next = fmt_ptr;
499 fmt_last = fmt_ptr;
500
501 /*
502 * Isolate one part in the 'errorformat' option
503 */
504 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
505 if (efm[len] == '\\' && efm[len + 1] != NUL)
506 ++len;
507
508 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1)
509 goto parse_efm_error;
510 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
511 goto parse_efm_error;
512 /*
513 * Advance to next part
514 */
515 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
516 }
517
518 if (fmt_first == NULL) /* nothing found */
519 EMSG(_("E378: 'errorformat' contains no pattern"));
520
521 goto parse_efm_end;
522
523parse_efm_error:
524 free_efm_list(&fmt_first);
525
526parse_efm_end:
527 vim_free(fmtstr);
528 vim_free(errmsg);
529
530 return fmt_first;
531}
532
Bram Moolenaare0d37972016-07-15 22:36:01 +0200533enum {
534 QF_FAIL = 0,
535 QF_OK = 1,
536 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200537 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200538 QF_IGNORE_LINE = 4,
539 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200540};
541
542typedef struct {
543 char_u *linebuf;
544 int linelen;
545 char_u *growbuf;
546 int growbufsiz;
547 FILE *fd;
548 typval_T *tv;
549 char_u *p_str;
550 listitem_T *p_li;
551 buf_T *buf;
552 linenr_T buflnum;
553 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100554 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200555} qfstate_T;
556
557 static char_u *
558qf_grow_linebuf(qfstate_T *state, int newsz)
559{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200560 char_u *p;
561
Bram Moolenaare0d37972016-07-15 22:36:01 +0200562 /*
563 * If the line exceeds LINE_MAXLEN exclude the last
564 * byte since it's not a NL character.
565 */
566 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
567 if (state->growbuf == NULL)
568 {
569 state->growbuf = alloc(state->linelen + 1);
570 if (state->growbuf == NULL)
571 return NULL;
572 state->growbufsiz = state->linelen;
573 }
574 else if (state->linelen > state->growbufsiz)
575 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200576 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200577 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200578 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200579 state->growbufsiz = state->linelen;
580 }
581 return state->growbuf;
582}
583
584/*
585 * Get the next string (separated by newline) from state->p_str.
586 */
587 static int
588qf_get_next_str_line(qfstate_T *state)
589{
590 /* Get the next line from the supplied string */
591 char_u *p_str = state->p_str;
592 char_u *p;
593 int len;
594
595 if (*p_str == NUL) /* Reached the end of the string */
596 return QF_END_OF_INPUT;
597
598 p = vim_strchr(p_str, '\n');
599 if (p != NULL)
600 len = (int)(p - p_str) + 1;
601 else
602 len = (int)STRLEN(p_str);
603
604 if (len > IOSIZE - 2)
605 {
606 state->linebuf = qf_grow_linebuf(state, len);
607 if (state->linebuf == NULL)
608 return QF_NOMEM;
609 }
610 else
611 {
612 state->linebuf = IObuff;
613 state->linelen = len;
614 }
615 vim_strncpy(state->linebuf, p_str, state->linelen);
616
617 /*
618 * Increment using len in order to discard the rest of the
619 * line if it exceeds LINE_MAXLEN.
620 */
621 p_str += len;
622 state->p_str = p_str;
623
624 return QF_OK;
625}
626
627/*
628 * Get the next string from state->p_Li.
629 */
630 static int
631qf_get_next_list_line(qfstate_T *state)
632{
633 listitem_T *p_li = state->p_li;
634 int len;
635
636 while (p_li != NULL
637 && (p_li->li_tv.v_type != VAR_STRING
638 || p_li->li_tv.vval.v_string == NULL))
639 p_li = p_li->li_next; /* Skip non-string items */
640
641 if (p_li == NULL) /* End of the list */
642 {
643 state->p_li = NULL;
644 return QF_END_OF_INPUT;
645 }
646
647 len = (int)STRLEN(p_li->li_tv.vval.v_string);
648 if (len > IOSIZE - 2)
649 {
650 state->linebuf = qf_grow_linebuf(state, len);
651 if (state->linebuf == NULL)
652 return QF_NOMEM;
653 }
654 else
655 {
656 state->linebuf = IObuff;
657 state->linelen = len;
658 }
659
660 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
661
662 state->p_li = p_li->li_next; /* next item */
663 return QF_OK;
664}
665
666/*
667 * Get the next string from state->buf.
668 */
669 static int
670qf_get_next_buf_line(qfstate_T *state)
671{
672 char_u *p_buf = NULL;
673 int len;
674
675 /* Get the next line from the supplied buffer */
676 if (state->buflnum > state->lnumlast)
677 return QF_END_OF_INPUT;
678
679 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
680 state->buflnum += 1;
681
682 len = (int)STRLEN(p_buf);
683 if (len > IOSIZE - 2)
684 {
685 state->linebuf = qf_grow_linebuf(state, len);
686 if (state->linebuf == NULL)
687 return QF_NOMEM;
688 }
689 else
690 {
691 state->linebuf = IObuff;
692 state->linelen = len;
693 }
694 vim_strncpy(state->linebuf, p_buf, state->linelen);
695
696 return QF_OK;
697}
698
699/*
700 * Get the next string from file state->fd.
701 */
702 static int
703qf_get_next_file_line(qfstate_T *state)
704{
705 int discard;
706 int growbuflen;
707
708 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
709 return QF_END_OF_INPUT;
710
711 discard = FALSE;
712 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200713 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200714 {
715 /*
716 * The current line exceeds IObuff, continue reading using
717 * growbuf until EOL or LINE_MAXLEN bytes is read.
718 */
719 if (state->growbuf == NULL)
720 {
721 state->growbufsiz = 2 * (IOSIZE - 1);
722 state->growbuf = alloc(state->growbufsiz);
723 if (state->growbuf == NULL)
724 return QF_NOMEM;
725 }
726
727 /* Copy the read part of the line, excluding null-terminator */
728 memcpy(state->growbuf, IObuff, IOSIZE - 1);
729 growbuflen = state->linelen;
730
731 for (;;)
732 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200733 char_u *p;
734
Bram Moolenaare0d37972016-07-15 22:36:01 +0200735 if (fgets((char *)state->growbuf + growbuflen,
736 state->growbufsiz - growbuflen, state->fd) == NULL)
737 break;
738 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
739 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200740 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200741 break;
742 if (state->growbufsiz == LINE_MAXLEN)
743 {
744 discard = TRUE;
745 break;
746 }
747
748 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
749 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200750 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200751 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200752 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200753 }
754
755 while (discard)
756 {
757 /*
758 * The current line is longer than LINE_MAXLEN, continue
759 * reading but discard everything until EOL or EOF is
760 * reached.
761 */
762 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
763 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200764 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200765 break;
766 }
767
768 state->linebuf = state->growbuf;
769 state->linelen = growbuflen;
770 }
771 else
772 state->linebuf = IObuff;
773
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100774#ifdef FEAT_MBYTE
775 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200776 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
777 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100778 char_u *line;
779
780 line = string_convert(&state->vc, state->linebuf, &state->linelen);
781 if (line != NULL)
782 {
783 if (state->linelen < IOSIZE)
784 {
785 STRCPY(state->linebuf, line);
786 vim_free(line);
787 }
788 else
789 {
790 vim_free(state->growbuf);
791 state->linebuf = state->growbuf = line;
792 state->growbufsiz = state->linelen < LINE_MAXLEN
793 ? state->linelen : LINE_MAXLEN;
794 }
795 }
796 }
797#endif
798
Bram Moolenaare0d37972016-07-15 22:36:01 +0200799 return QF_OK;
800}
801
802/*
803 * Get the next string from a file/buffer/list/string.
804 */
805 static int
806qf_get_nextline(qfstate_T *state)
807{
808 int status = QF_FAIL;
809
810 if (state->fd == NULL)
811 {
812 if (state->tv != NULL)
813 {
814 if (state->tv->v_type == VAR_STRING)
815 /* Get the next line from the supplied string */
816 status = qf_get_next_str_line(state);
817 else if (state->tv->v_type == VAR_LIST)
818 /* Get the next line from the supplied list */
819 status = qf_get_next_list_line(state);
820 }
821 else
822 /* Get the next line from the supplied buffer */
823 status = qf_get_next_buf_line(state);
824 }
825 else
826 /* Get the next line from the supplied file */
827 status = qf_get_next_file_line(state);
828
829 if (status != QF_OK)
830 return status;
831
832 /* remove newline/CR from the line */
833 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200834 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200835 state->linebuf[state->linelen - 1] = NUL;
836#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200837 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
838 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200839#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200840 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200841
842#ifdef FEAT_MBYTE
843 remove_bom(state->linebuf);
844#endif
845
846 return QF_OK;
847}
848
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200849typedef struct {
850 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200851 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200852 char_u *errmsg;
853 int errmsglen;
854 long lnum;
855 int col;
856 char_u use_viscol;
857 char_u *pattern;
858 int enr;
859 int type;
860 int valid;
861} qffields_T;
862
863/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200864 * Parse the error format matches in 'regmatch' and set the values in 'fields'.
865 * fmt_ptr contains the 'efm' format specifiers/prefixes that have a match.
866 * Returns QF_OK if all the matches are successfully parsed. On failure,
867 * returns QF_FAIL or QF_NOMEM.
868 */
869 static int
870qf_parse_match(
871 char_u *linebuf,
872 int linelen,
873 efm_T *fmt_ptr,
874 regmatch_T *regmatch,
875 qffields_T *fields,
876 int qf_multiline,
877 int qf_multiscan,
878 char_u **tail)
879{
880 char_u *p;
881 int idx = fmt_ptr->prefix;
882 int i;
883 int len;
884
885 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
886 return QF_FAIL;
887 if (vim_strchr((char_u *)"EWI", idx) != NULL)
888 fields->type = idx;
889 else
890 fields->type = 0;
891 /*
892 * Extract error message data from matched line.
893 * We check for an actual submatch, because "\[" and "\]" in
894 * the 'errorformat' may cause the wrong submatch to be used.
895 */
896 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
897 {
898 int c;
899
900 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
901 return QF_FAIL;
902
903 /* Expand ~/file and $HOME/file to full path. */
904 c = *regmatch->endp[i];
905 *regmatch->endp[i] = NUL;
906 expand_env(regmatch->startp[i], fields->namebuf, CMDBUFFSIZE);
907 *regmatch->endp[i] = c;
908
909 if (vim_strchr((char_u *)"OPQ", idx) != NULL
910 && mch_getperm(fields->namebuf) == -1)
911 return QF_FAIL;
912 }
913 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
914 {
915 if (regmatch->startp[i] == NULL)
916 return QF_FAIL;
917 fields->enr = (int)atol((char *)regmatch->startp[i]);
918 }
919 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
920 {
921 if (regmatch->startp[i] == NULL)
922 return QF_FAIL;
923 fields->lnum = atol((char *)regmatch->startp[i]);
924 }
925 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
926 {
927 if (regmatch->startp[i] == NULL)
928 return QF_FAIL;
929 fields->col = (int)atol((char *)regmatch->startp[i]);
930 }
931 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
932 {
933 if (regmatch->startp[i] == NULL)
934 return QF_FAIL;
935 fields->type = *regmatch->startp[i];
936 }
937 if (fmt_ptr->flags == '+' && !qf_multiscan) /* %+ */
938 {
939 if (linelen >= fields->errmsglen)
940 {
941 /* linelen + null terminator */
942 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
943 return QF_NOMEM;
944 fields->errmsg = p;
945 fields->errmsglen = linelen + 1;
946 }
947 vim_strncpy(fields->errmsg, linebuf, linelen);
948 }
949 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
950 {
951 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
952 return QF_FAIL;
953 len = (int)(regmatch->endp[i] - regmatch->startp[i]);
954 if (len >= fields->errmsglen)
955 {
956 /* len + null terminator */
957 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
958 return QF_NOMEM;
959 fields->errmsg = p;
960 fields->errmsglen = len + 1;
961 }
962 vim_strncpy(fields->errmsg, regmatch->startp[i], len);
963 }
964 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
965 {
966 if (regmatch->startp[i] == NULL)
967 return QF_FAIL;
968 *tail = regmatch->startp[i];
969 }
970 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
971 {
972 char_u *match_ptr;
973
974 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
975 return QF_FAIL;
976 fields->col = 0;
977 for (match_ptr = regmatch->startp[i];
978 match_ptr != regmatch->endp[i]; ++match_ptr)
979 {
980 ++fields->col;
981 if (*match_ptr == TAB)
982 {
983 fields->col += 7;
984 fields->col -= fields->col % 8;
985 }
986 }
987 ++fields->col;
988 fields->use_viscol = TRUE;
989 }
990 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
991 {
992 if (regmatch->startp[i] == NULL)
993 return QF_FAIL;
994 fields->col = (int)atol((char *)regmatch->startp[i]);
995 fields->use_viscol = TRUE;
996 }
997 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
998 {
999 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
1000 return QF_FAIL;
1001 len = (int)(regmatch->endp[i] - regmatch->startp[i]);
1002 if (len > CMDBUFFSIZE - 5)
1003 len = CMDBUFFSIZE - 5;
1004 STRCPY(fields->pattern, "^\\V");
1005 STRNCAT(fields->pattern, regmatch->startp[i], len);
1006 fields->pattern[len + 3] = '\\';
1007 fields->pattern[len + 4] = '$';
1008 fields->pattern[len + 5] = NUL;
1009 }
1010 if ((i = (int)fmt_ptr->addr[10]) > 0) /* %o */
1011 {
1012 if (regmatch->startp[i] == NULL || regmatch->endp[i] == NULL)
1013 return QF_FAIL;
1014 len = (int)(regmatch->endp[i] - regmatch->startp[i]);
1015 if (len > CMDBUFFSIZE)
1016 len = CMDBUFFSIZE;
1017 STRNCAT(fields->module, regmatch->startp[i], len);
1018 }
1019
1020 return QF_OK;
1021}
1022
1023/*
1024 * Parse an error line in 'linebuf' using a single error format string in
1025 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1026 * Returns QF_OK if the efm format matches completely and the fields are
1027 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1028 */
1029 static int
1030qf_parse_get_fields(
1031 char_u *linebuf,
1032 int linelen,
1033 efm_T *fmt_ptr,
1034 qffields_T *fields,
1035 int qf_multiline,
1036 int qf_multiscan,
1037 char_u **tail)
1038{
1039 regmatch_T regmatch;
1040 int status = QF_FAIL;
1041 int r;
1042
1043 if (qf_multiscan &&
1044 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1045 return QF_FAIL;
1046
1047 fields->namebuf[0] = NUL;
1048 fields->module[0] = NUL;
1049 fields->pattern[0] = NUL;
1050 if (!qf_multiscan)
1051 fields->errmsg[0] = NUL;
1052 fields->lnum = 0;
1053 fields->col = 0;
1054 fields->use_viscol = FALSE;
1055 fields->enr = -1;
1056 fields->type = 0;
1057 *tail = NULL;
1058
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001059 /* Always ignore case when looking for a matching error. */
1060 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001061 regmatch.regprog = fmt_ptr->prog;
1062 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1063 fmt_ptr->prog = regmatch.regprog;
1064 if (r)
1065 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1066 fields, qf_multiline, qf_multiscan, tail);
1067
1068 return status;
1069}
1070
1071/*
1072 * Parse directory error format prefixes (%D and %X).
1073 * Push and pop directories from the directory stack when scanning directory
1074 * names.
1075 */
1076 static int
1077qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1078{
1079 if (idx == 'D') /* enter directory */
1080 {
1081 if (*fields->namebuf == NUL)
1082 {
1083 EMSG(_("E379: Missing or empty directory name"));
1084 return QF_FAIL;
1085 }
1086 qfl->qf_directory =
1087 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1088 if (qfl->qf_directory == NULL)
1089 return QF_FAIL;
1090 }
1091 else if (idx == 'X') /* leave directory */
1092 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1093
1094 return QF_OK;
1095}
1096
1097/*
1098 * Parse global file name error format prefixes (%O, %P and %Q).
1099 */
1100 static int
1101qf_parse_file_pfx(
1102 int idx,
1103 qffields_T *fields,
1104 qf_list_T *qfl,
1105 char_u *tail)
1106{
1107 fields->valid = FALSE;
1108 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1109 {
1110 if (*fields->namebuf && idx == 'P')
1111 qfl->qf_currfile =
1112 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1113 else if (idx == 'Q')
1114 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1115 *fields->namebuf = NUL;
1116 if (tail && *tail)
1117 {
1118 STRMOVE(IObuff, skipwhite(tail));
1119 qfl->qf_multiscan = TRUE;
1120 return QF_MULTISCAN;
1121 }
1122 }
1123
1124 return QF_OK;
1125}
1126
1127/*
1128 * Parse a non-error line (a line which doesn't match any of the error
1129 * format in 'efm').
1130 */
1131 static int
1132qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1133{
1134 char_u *p;
1135
1136 fields->namebuf[0] = NUL; /* no match found, remove file name */
1137 fields->lnum = 0; /* don't jump to this line */
1138 fields->valid = FALSE;
1139 if (linelen >= fields->errmsglen)
1140 {
1141 /* linelen + null terminator */
1142 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1143 return QF_NOMEM;
1144 fields->errmsg = p;
1145 fields->errmsglen = linelen + 1;
1146 }
1147 /* copy whole line to error message */
1148 vim_strncpy(fields->errmsg, linebuf, linelen);
1149
1150 return QF_OK;
1151}
1152
1153/*
1154 * Parse multi-line error format prefixes (%C and %Z)
1155 */
1156 static int
1157qf_parse_multiline_pfx(
1158 qf_info_T *qi,
1159 int qf_idx,
1160 int idx,
1161 qf_list_T *qfl,
1162 qffields_T *fields)
1163{
1164 char_u *ptr;
1165 int len;
1166
1167 if (!qfl->qf_multiignore)
1168 {
1169 qfline_T *qfprev = qfl->qf_last;
1170
1171 if (qfprev == NULL)
1172 return QF_FAIL;
1173 if (*fields->errmsg && !qfl->qf_multiignore)
1174 {
1175 len = (int)STRLEN(qfprev->qf_text);
1176 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1177 == NULL)
1178 return QF_FAIL;
1179 STRCPY(ptr, qfprev->qf_text);
1180 vim_free(qfprev->qf_text);
1181 qfprev->qf_text = ptr;
1182 *(ptr += len) = '\n';
1183 STRCPY(++ptr, fields->errmsg);
1184 }
1185 if (qfprev->qf_nr == -1)
1186 qfprev->qf_nr = fields->enr;
1187 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1188 /* only printable chars allowed */
1189 qfprev->qf_type = fields->type;
1190
1191 if (!qfprev->qf_lnum)
1192 qfprev->qf_lnum = fields->lnum;
1193 if (!qfprev->qf_col)
1194 qfprev->qf_col = fields->col;
1195 qfprev->qf_viscol = fields->use_viscol;
1196 if (!qfprev->qf_fnum)
1197 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1198 qfl->qf_directory,
1199 *fields->namebuf || qfl->qf_directory != NULL
1200 ? fields->namebuf
1201 : qfl->qf_currfile != NULL && fields->valid
1202 ? qfl->qf_currfile : 0);
1203 }
1204 if (idx == 'Z')
1205 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1206 line_breakcheck();
1207
1208 return QF_IGNORE_LINE;
1209}
1210
1211/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001212 * Parse a line and get the quickfix fields.
1213 * Return the QF_ status.
1214 */
1215 static int
1216qf_parse_line(
1217 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001218 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001219 char_u *linebuf,
1220 int linelen,
1221 efm_T *fmt_first,
1222 qffields_T *fields)
1223{
1224 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001225 int idx = 0;
1226 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001227 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001228 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001229
Bram Moolenaare333e792018-04-08 13:27:39 +02001230restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001231 /* If there was no %> item start at the first pattern */
1232 if (fmt_start == NULL)
1233 fmt_ptr = fmt_first;
1234 else
1235 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001236 /* Otherwise start from the last used pattern */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001237 fmt_ptr = fmt_start;
1238 fmt_start = NULL;
1239 }
1240
1241 /*
1242 * Try to match each part of 'errorformat' until we find a complete
1243 * match or no match.
1244 */
1245 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001246 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1247 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001248 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001249 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1250 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1251 if (status == QF_NOMEM)
1252 return status;
1253 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001254 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001255 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001256 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001257
1258 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1259 {
1260 if (fmt_ptr != NULL)
1261 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001262 /* 'D' and 'X' directory specifiers */
1263 status = qf_parse_dir_pfx(idx, fields, qfl);
1264 if (status != QF_OK)
1265 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001266 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001267
1268 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1269 if (status != QF_OK)
1270 return status;
1271
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001272 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001273 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001274 }
1275 else if (fmt_ptr != NULL)
1276 {
1277 /* honor %> item */
1278 if (fmt_ptr->conthere)
1279 fmt_start = fmt_ptr;
1280
1281 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1282 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001283 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1284 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001285 }
1286 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1287 { /* continuation of multi-line msg */
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001288 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1289 if (status != QF_OK)
1290 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001291 }
1292 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001293 { /* global file names */
1294 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1295 if (status == QF_MULTISCAN)
1296 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001297 }
1298 if (fmt_ptr->flags == '-') /* generally exclude this line */
1299 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001300 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001301 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001302 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001303 return QF_IGNORE_LINE;
1304 }
1305 }
1306
1307 return QF_OK;
1308}
1309
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001310/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001311 * Allocate the fields used for parsing lines and populating a quickfix list.
1312 */
1313 static int
1314qf_alloc_fields(qffields_T *pfields)
1315{
1316 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1317 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1318 pfields->errmsglen = CMDBUFFSIZE + 1;
1319 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1320 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1321 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1322 || pfields->pattern == NULL || pfields->module == NULL)
1323 return FAIL;
1324
1325 return OK;
1326}
1327
1328/*
1329 * Free the fields used for parsing lines and populating a quickfix list.
1330 */
1331 static void
1332qf_free_fields(qffields_T *pfields)
1333{
1334 vim_free(pfields->namebuf);
1335 vim_free(pfields->module);
1336 vim_free(pfields->errmsg);
1337 vim_free(pfields->pattern);
1338}
1339
1340/*
1341 * Setup the state information used for parsing lines and populating a
1342 * quickfix list.
1343 */
1344 static int
1345qf_setup_state(
1346 qfstate_T *pstate,
1347 char_u *enc,
1348 char_u *efile,
1349 typval_T *tv,
1350 buf_T *buf,
1351 linenr_T lnumfirst,
1352 linenr_T lnumlast)
1353{
1354#ifdef FEAT_MBYTE
1355 pstate->vc.vc_type = CONV_NONE;
1356 if (enc != NULL && *enc != NUL)
1357 convert_setup(&pstate->vc, enc, p_enc);
1358#endif
1359
1360 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1361 {
1362 EMSG2(_(e_openerrf), efile);
1363 return FAIL;
1364 }
1365
1366 if (tv != NULL)
1367 {
1368 if (tv->v_type == VAR_STRING)
1369 pstate->p_str = tv->vval.v_string;
1370 else if (tv->v_type == VAR_LIST)
1371 pstate->p_li = tv->vval.v_list->lv_first;
1372 pstate->tv = tv;
1373 }
1374 pstate->buf = buf;
1375 pstate->buflnum = lnumfirst;
1376 pstate->lnumlast = lnumlast;
1377
1378 return OK;
1379}
1380
1381/*
1382 * Cleanup the state information used for parsing lines and populating a
1383 * quickfix list.
1384 */
1385 static void
1386qf_cleanup_state(qfstate_T *pstate)
1387{
1388 if (pstate->fd != NULL)
1389 fclose(pstate->fd);
1390
1391 vim_free(pstate->growbuf);
1392#ifdef FEAT_MBYTE
1393 if (pstate->vc.vc_type != CONV_NONE)
1394 convert_setup(&pstate->vc, NULL, NULL);
1395#endif
1396}
1397
1398/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001399 * Read the errorfile "efile" into memory, line by line, building the error
1400 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001401 * Alternative: when "efile" is NULL read errors from buffer "buf".
1402 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001403 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001404 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1405 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001406 * Return -1 for error, number of errors for success.
1407 */
1408 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001409qf_init_ext(
1410 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001411 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001412 char_u *efile,
1413 buf_T *buf,
1414 typval_T *tv,
1415 char_u *errorformat,
1416 int newlist, /* TRUE: start a new error list */
1417 linenr_T lnumfirst, /* first line number to use */
1418 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001419 char_u *qf_title,
1420 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001421{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001422 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001423 qfstate_T state;
1424 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001425 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001426 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001427 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001429 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001431 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001433 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001434 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001435
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001436 vim_memset(&state, 0, sizeof(state));
1437 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001438 if ((qf_alloc_fields(&fields) == FAIL) ||
1439 (qf_setup_state(&state, enc, efile, tv, buf,
1440 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 goto qf_init_end;
1442
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001443 if (newlist || qf_idx == qi->qf_listcount)
1444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001446 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001447 qf_idx = qi->qf_curlist;
1448 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001449 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001450 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001451 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001452 adding = TRUE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001453 if (qi->qf_lists[qf_idx].qf_count > 0)
1454 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001457 qfl = &qi->qf_lists[qf_idx];
1458
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001460 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001461 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 else
1463 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001465 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001466 * If the errorformat didn't change between calls, then reuse the
1467 * previously parsed values.
1468 */
1469 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1470 {
1471 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001472 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001473 free_efm_list(&fmt_first);
1474
1475 /* parse the current 'efm' */
1476 fmt_first = parse_efm_option(efm);
1477 if (fmt_first != NULL)
1478 last_efm = vim_strsave(efm);
1479 }
1480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 /*
1485 * got_int is reset here, because it was probably set when killing the
1486 * ":make" command, but we still want to read the errorfile then.
1487 */
1488 got_int = FALSE;
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 /*
1491 * Read the lines in the error file one by one.
1492 * Try to recognize one of the error formats in each line.
1493 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001494 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001496 /* Get the next line from a file/buffer/list/string */
1497 status = qf_get_nextline(&state);
1498 if (status == QF_NOMEM) /* memory alloc failure */
1499 goto qf_init_end;
1500 if (status == QF_END_OF_INPUT) /* end of input */
1501 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001503 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1504 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001505 if (status == QF_FAIL)
1506 goto error2;
1507 if (status == QF_NOMEM)
1508 goto qf_init_end;
1509 if (status == QF_IGNORE_LINE)
1510 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001512 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001513 qf_idx,
1514 qfl->qf_directory,
1515 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001516 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001517 : ((qfl->qf_currfile != NULL && fields.valid)
1518 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001519 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001520 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001521 fields.errmsg,
1522 fields.lnum,
1523 fields.col,
1524 fields.use_viscol,
1525 fields.pattern,
1526 fields.enr,
1527 fields.type,
1528 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 goto error2;
1530 line_breakcheck();
1531 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001532 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001534 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001536 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001537 qfl->qf_ptr = qfl->qf_start;
1538 qfl->qf_index = 1;
1539 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 }
1541 else
1542 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001543 qfl->qf_nonevalid = FALSE;
1544 if (qfl->qf_ptr == NULL)
1545 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001547 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001548 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001549 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 }
1551 EMSG(_(e_readerrf));
1552error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001553 if (!adding)
1554 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001555 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001556 qf_free(qi, qi->qf_curlist);
1557 qi->qf_listcount--;
1558 if (qi->qf_curlist > 0)
1559 --qi->qf_curlist;
1560 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001561qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001562 if (qf_idx == qi->qf_curlist)
1563 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001564 qf_cleanup_state(&state);
1565 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
1567 return retval;
1568}
1569
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001570/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001571 * Read the errorfile "efile" into memory, line by line, building the error
1572 * list. Set the error list's title to qf_title.
1573 * Return -1 for error, number of errors for success.
1574 */
1575 int
1576qf_init(win_T *wp,
1577 char_u *efile,
1578 char_u *errorformat,
1579 int newlist, /* TRUE: start a new error list */
1580 char_u *qf_title,
1581 char_u *enc)
1582{
1583 qf_info_T *qi = &ql_info;
1584
1585 if (wp != NULL)
1586 {
1587 qi = ll_get_or_alloc_list(wp);
1588 if (qi == NULL)
1589 return FAIL;
1590 }
1591
1592 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1593 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1594}
1595
1596/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001597 * Set the title of the specified quickfix list. Frees the previous title.
1598 * Prepends ':' to the title.
1599 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001600 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001601qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001602{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001603 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001604
Bram Moolenaarfb604092014-07-23 15:55:00 +02001605 if (title != NULL)
1606 {
1607 char_u *p = alloc((int)STRLEN(title) + 2);
1608
Bram Moolenaara3921f42017-06-04 15:30:34 +02001609 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001610 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001611 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001612 }
1613}
1614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001616 * The title of a quickfix/location list is set, by default, to the command
1617 * that created the quickfix list with the ":" prefix.
1618 * Create a quickfix list title string by prepending ":" to a user command.
1619 * Returns a pointer to a static buffer with the title.
1620 */
1621 static char_u *
1622qf_cmdtitle(char_u *cmd)
1623{
1624 static char_u qftitle_str[IOSIZE];
1625
1626 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1627 return qftitle_str;
1628}
1629
1630/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001631 * Prepare for adding a new quickfix list. If the current list is in the
1632 * middle of the stack, then all the following lists are freed and then
1633 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 */
1635 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001636qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 int i;
1639
1640 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001641 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 * the current entry. This makes it possible to browse in a tree-like
1643 * way with ":grep'.
1644 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001645 while (qi->qf_listcount > qi->qf_curlist + 1)
1646 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
1648 /*
1649 * When the stack is full, remove to oldest entry
1650 * Otherwise, add a new entry.
1651 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001652 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001654 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001656 qi->qf_lists[i - 1] = qi->qf_lists[i];
1657 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 }
1659 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001660 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001661 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001662 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001663 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664}
1665
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001666/*
1667 * Free a location list
1668 */
1669 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001670ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001671{
1672 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001673 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001674
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001675 qi = *pqi;
1676 if (qi == NULL)
1677 return;
1678 *pqi = NULL; /* Remove reference to this list */
1679
1680 qi->qf_refcount--;
1681 if (qi->qf_refcount < 1)
1682 {
1683 /* No references to this location list */
1684 for (i = 0; i < qi->qf_listcount; ++i)
1685 qf_free(qi, i);
1686 vim_free(qi);
1687 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001688}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001689
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001690/*
1691 * Free all the quickfix/location lists in the stack.
1692 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001693 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001694qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001695{
1696 int i;
1697 qf_info_T *qi = &ql_info;
1698
1699 if (wp != NULL)
1700 {
1701 /* location list */
1702 ll_free_all(&wp->w_llist);
1703 ll_free_all(&wp->w_llist_ref);
1704 }
1705 else
1706 /* quickfix list */
1707 for (i = 0; i < qi->qf_listcount; ++i)
1708 qf_free(qi, i);
1709}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711/*
1712 * Add an entry to the end of the list of errors.
1713 * Returns OK or FAIL.
1714 */
1715 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001716qf_add_entry(
1717 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001718 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001719 char_u *dir, /* optional directory name */
1720 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001721 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001722 int bufnum, /* buffer number or zero */
1723 char_u *mesg, /* message */
1724 long lnum, /* line number */
1725 int col, /* column */
1726 int vis_col, /* using visual column */
1727 char_u *pattern, /* search pattern */
1728 int nr, /* error number */
1729 int type, /* type character */
1730 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001732 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001733 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001735 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001737 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001738 {
1739 buf_T *buf = buflist_findnr(bufnum);
1740
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001741 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001742 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001743 buf->b_has_qf_entry |=
1744 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001745 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001746 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001747 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1749 {
1750 vim_free(qfp);
1751 return FAIL;
1752 }
1753 qfp->qf_lnum = lnum;
1754 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001755 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001756 if (pattern == NULL || *pattern == NUL)
1757 qfp->qf_pattern = NULL;
1758 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1759 {
1760 vim_free(qfp->qf_text);
1761 vim_free(qfp);
1762 return FAIL;
1763 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001764 if (module == NULL || *module == NUL)
1765 qfp->qf_module = NULL;
1766 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1767 {
1768 vim_free(qfp->qf_text);
1769 vim_free(qfp->qf_pattern);
1770 vim_free(qfp);
1771 return FAIL;
1772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 qfp->qf_nr = nr;
1774 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1775 type = 0;
1776 qfp->qf_type = type;
1777 qfp->qf_valid = valid;
1778
Bram Moolenaara3921f42017-06-04 15:30:34 +02001779 lastp = &qi->qf_lists[qf_idx].qf_last;
1780 if (qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001781 /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001783 qi->qf_lists[qf_idx].qf_start = qfp;
1784 qi->qf_lists[qf_idx].qf_ptr = qfp;
1785 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001786 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
1788 else
1789 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001790 qfp->qf_prev = *lastp;
1791 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001793 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001795 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001796 ++qi->qf_lists[qf_idx].qf_count;
1797 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001798 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001800 qi->qf_lists[qf_idx].qf_index =
1801 qi->qf_lists[qf_idx].qf_count;
1802 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804
1805 return OK;
1806}
1807
1808/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001809 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001810 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001811 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001812ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001813{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001814 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001815
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001816 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
1817 if (qi != NULL)
1818 {
1819 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
1820 qi->qf_refcount++;
1821 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001822
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001823 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001824}
1825
1826/*
1827 * Return the location list for window 'wp'.
1828 * If not present, allocate a location list
1829 */
1830 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001831ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001832{
1833 if (IS_LL_WINDOW(wp))
1834 /* For a location list window, use the referenced location list */
1835 return wp->w_llist_ref;
1836
1837 /*
1838 * For a non-location list window, w_llist_ref should not point to a
1839 * location list.
1840 */
1841 ll_free_all(&wp->w_llist_ref);
1842
1843 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001844 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001845 return wp->w_llist;
1846}
1847
1848/*
1849 * Copy the location list from window "from" to window "to".
1850 */
1851 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001852copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001853{
1854 qf_info_T *qi;
1855 int idx;
1856 int i;
1857
1858 /*
1859 * When copying from a location list window, copy the referenced
1860 * location list. For other windows, copy the location list for
1861 * that window.
1862 */
1863 if (IS_LL_WINDOW(from))
1864 qi = from->w_llist_ref;
1865 else
1866 qi = from->w_llist;
1867
1868 if (qi == NULL) /* no location list to copy */
1869 return;
1870
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001871 /* allocate a new location list */
1872 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001873 return;
1874
1875 to->w_llist->qf_listcount = qi->qf_listcount;
1876
1877 /* Copy the location lists one at a time */
1878 for (idx = 0; idx < qi->qf_listcount; idx++)
1879 {
1880 qf_list_T *from_qfl;
1881 qf_list_T *to_qfl;
1882
1883 to->w_llist->qf_curlist = idx;
1884
1885 from_qfl = &qi->qf_lists[idx];
1886 to_qfl = &to->w_llist->qf_lists[idx];
1887
1888 /* Some of the fields are populated by qf_add_entry() */
1889 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
1890 to_qfl->qf_count = 0;
1891 to_qfl->qf_index = 0;
1892 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001893 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001894 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001895 if (from_qfl->qf_title != NULL)
1896 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
1897 else
1898 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001899 if (from_qfl->qf_ctx != NULL)
1900 {
1901 to_qfl->qf_ctx = alloc_tv();
1902 if (to_qfl->qf_ctx != NULL)
1903 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
1904 }
1905 else
1906 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001907
1908 if (from_qfl->qf_count)
1909 {
1910 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001911 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001912
1913 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001914 for (i = 0, from_qfp = from_qfl->qf_start;
1915 i < from_qfl->qf_count && from_qfp != NULL;
1916 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001917 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001918 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02001919 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001920 NULL,
1921 NULL,
Bram Moolenaard76ce852018-05-01 15:02:04 +02001922 from_qfp->qf_module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001923 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001924 from_qfp->qf_text,
1925 from_qfp->qf_lnum,
1926 from_qfp->qf_col,
1927 from_qfp->qf_viscol,
1928 from_qfp->qf_pattern,
1929 from_qfp->qf_nr,
1930 0,
1931 from_qfp->qf_valid) == FAIL)
1932 {
1933 qf_free_all(to);
1934 return;
1935 }
1936 /*
1937 * qf_add_entry() will not set the qf_num field, as the
1938 * directory and file names are not supplied. So the qf_fnum
1939 * field is copied here.
1940 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001941 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001942 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
1943 prevp->qf_type = from_qfp->qf_type; /* error type */
1944 if (from_qfl->qf_ptr == from_qfp)
1945 to_qfl->qf_ptr = prevp; /* current location */
1946 }
1947 }
1948
1949 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
1950
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001951 /* Assign a new ID for the location list */
1952 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01001953 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001954
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001955 /* When no valid entries are present in the list, qf_ptr points to
1956 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02001957 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001958 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001959 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02001960 to_qfl->qf_index = 1;
1961 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001962 }
1963
1964 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
1965}
1966
1967/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01001968 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001969 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 */
1971 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001972qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
Bram Moolenaar82404332016-07-10 17:00:38 +02001974 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001975 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02001976 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001977
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (fname == NULL || *fname == NUL) /* no file name */
1979 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
Bram Moolenaare60acc12011-05-10 16:41:25 +02001981#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001982 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001983#endif
1984#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001985 if (directory != NULL)
1986 slash_adjust(directory);
1987 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001988#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001989 if (directory != NULL && !vim_isAbsName(fname)
1990 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
1991 {
1992 /*
1993 * Here we check if the file really exists.
1994 * This should normally be true, but if make works without
1995 * "leaving directory"-messages we might have missed a
1996 * directory change.
1997 */
1998 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002001 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002002 if (directory)
2003 ptr = concat_fnames(directory, fname, TRUE);
2004 else
2005 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002007 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02002008 bufname = ptr;
2009 }
2010 else
2011 bufname = fname;
2012
2013 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002014 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002015 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002016 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002017 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002019 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002020 {
2021 vim_free(qf_last_bufname);
2022 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2023 if (bufname == ptr)
2024 qf_last_bufname = bufname;
2025 else
2026 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002027 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002028 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002029 if (buf == NULL)
2030 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002031
Bram Moolenaarc1542742016-07-20 21:44:37 +02002032 buf->b_has_qf_entry =
2033 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002034 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035}
2036
2037/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002038 * Push dirbuf onto the directory stack and return pointer to actual dir or
2039 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 */
2041 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002042qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043{
2044 struct dir_stack_T *ds_new;
2045 struct dir_stack_T *ds_ptr;
2046
2047 /* allocate new stack element and hook it in */
2048 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2049 if (ds_new == NULL)
2050 return NULL;
2051
2052 ds_new->next = *stackptr;
2053 *stackptr = ds_new;
2054
2055 /* store directory on the stack */
2056 if (vim_isAbsName(dirbuf)
2057 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002058 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 (*stackptr)->dirname = vim_strsave(dirbuf);
2060 else
2061 {
2062 /* Okay we don't have an absolute path.
2063 * dirbuf must be a subdir of one of the directories on the stack.
2064 * Let's search...
2065 */
2066 ds_new = (*stackptr)->next;
2067 (*stackptr)->dirname = NULL;
2068 while (ds_new)
2069 {
2070 vim_free((*stackptr)->dirname);
2071 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2072 TRUE);
2073 if (mch_isdir((*stackptr)->dirname) == TRUE)
2074 break;
2075
2076 ds_new = ds_new->next;
2077 }
2078
2079 /* clean up all dirs we already left */
2080 while ((*stackptr)->next != ds_new)
2081 {
2082 ds_ptr = (*stackptr)->next;
2083 (*stackptr)->next = (*stackptr)->next->next;
2084 vim_free(ds_ptr->dirname);
2085 vim_free(ds_ptr);
2086 }
2087
2088 /* Nothing found -> it must be on top level */
2089 if (ds_new == NULL)
2090 {
2091 vim_free((*stackptr)->dirname);
2092 (*stackptr)->dirname = vim_strsave(dirbuf);
2093 }
2094 }
2095
2096 if ((*stackptr)->dirname != NULL)
2097 return (*stackptr)->dirname;
2098 else
2099 {
2100 ds_ptr = *stackptr;
2101 *stackptr = (*stackptr)->next;
2102 vim_free(ds_ptr);
2103 return NULL;
2104 }
2105}
2106
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107/*
2108 * pop dirbuf from the directory stack and return previous directory or NULL if
2109 * stack is empty
2110 */
2111 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002112qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 struct dir_stack_T *ds_ptr;
2115
2116 /* TODO: Should we check if dirbuf is the directory on top of the stack?
2117 * What to do if it isn't? */
2118
2119 /* pop top element and free it */
2120 if (*stackptr != NULL)
2121 {
2122 ds_ptr = *stackptr;
2123 *stackptr = (*stackptr)->next;
2124 vim_free(ds_ptr->dirname);
2125 vim_free(ds_ptr);
2126 }
2127
2128 /* return NEW top element as current dir or NULL if stack is empty*/
2129 return *stackptr ? (*stackptr)->dirname : NULL;
2130}
2131
2132/*
2133 * clean up directory stack
2134 */
2135 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002136qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
2138 struct dir_stack_T *ds_ptr;
2139
2140 while ((ds_ptr = *stackptr) != NULL)
2141 {
2142 *stackptr = (*stackptr)->next;
2143 vim_free(ds_ptr->dirname);
2144 vim_free(ds_ptr);
2145 }
2146}
2147
2148/*
2149 * Check in which directory of the directory stack the given file can be
2150 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002151 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 * Cleans up intermediate directory entries.
2153 *
2154 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002155 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 * ./
2157 * ./aa
2158 * ./aa/bb
2159 * ./bb
2160 * ./bb/x.c
2161 * and make says:
2162 * making all in aa
2163 * making all in bb
2164 * x.c:9: Error
2165 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2166 * qf_guess_filepath will return NULL.
2167 */
2168 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002169qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170{
2171 struct dir_stack_T *ds_ptr;
2172 struct dir_stack_T *ds_tmp;
2173 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002174 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
2176 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002177 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 return NULL;
2179
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002180 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 fullname = NULL;
2182 while (ds_ptr)
2183 {
2184 vim_free(fullname);
2185 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2186
2187 /* If concat_fnames failed, just go on. The worst thing that can happen
2188 * is that we delete the entire stack.
2189 */
2190 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2191 break;
2192
2193 ds_ptr = ds_ptr->next;
2194 }
2195
2196 vim_free(fullname);
2197
2198 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002199 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002201 ds_tmp = qfl->qf_dir_stack->next;
2202 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 vim_free(ds_tmp->dirname);
2204 vim_free(ds_tmp);
2205 }
2206
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002207 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208}
2209
2210/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002211 * Returns TRUE if a quickfix/location list with the given identifier exists.
2212 */
2213 static int
2214qflist_valid (win_T *wp, int_u qf_id)
2215{
2216 qf_info_T *qi = &ql_info;
2217 int i;
2218
2219 if (wp != NULL)
2220 {
2221 qi = GET_LOC_LIST(wp); /* Location list */
2222 if (qi == NULL)
2223 return FALSE;
2224 }
2225
2226 for (i = 0; i < qi->qf_listcount; ++i)
2227 if (qi->qf_lists[i].qf_id == qf_id)
2228 return TRUE;
2229
2230 return FALSE;
2231}
2232
2233/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002234 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002235 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002236 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002237 * Similar to location list.
2238 */
2239 static int
2240is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
2241{
2242 qf_list_T *qfl;
2243 qfline_T *qfp;
2244 int i;
2245
2246 qfl = &qi->qf_lists[qi->qf_curlist];
2247
2248 /* Search for the entry in the current list */
2249 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2250 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002251 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002252 break;
2253
2254 if (i == qfl->qf_count) /* Entry is not found */
2255 return FALSE;
2256
2257 return TRUE;
2258}
2259
2260/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002261 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002262 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002263 */
2264 static qfline_T *
2265get_next_valid_entry(
2266 qf_info_T *qi,
2267 qfline_T *qf_ptr,
2268 int *qf_index,
2269 int dir)
2270{
2271 int idx;
2272 int old_qf_fnum;
2273
2274 idx = *qf_index;
2275 old_qf_fnum = qf_ptr->qf_fnum;
2276
2277 do
2278 {
2279 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2280 || qf_ptr->qf_next == NULL)
2281 return NULL;
2282 ++idx;
2283 qf_ptr = qf_ptr->qf_next;
2284 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2285 && !qf_ptr->qf_valid)
2286 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2287
2288 *qf_index = idx;
2289 return qf_ptr;
2290}
2291
2292/*
2293 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002294 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002295 */
2296 static qfline_T *
2297get_prev_valid_entry(
2298 qf_info_T *qi,
2299 qfline_T *qf_ptr,
2300 int *qf_index,
2301 int dir)
2302{
2303 int idx;
2304 int old_qf_fnum;
2305
2306 idx = *qf_index;
2307 old_qf_fnum = qf_ptr->qf_fnum;
2308
2309 do
2310 {
2311 if (idx == 1 || qf_ptr->qf_prev == NULL)
2312 return NULL;
2313 --idx;
2314 qf_ptr = qf_ptr->qf_prev;
2315 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2316 && !qf_ptr->qf_valid)
2317 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2318
2319 *qf_index = idx;
2320 return qf_ptr;
2321}
2322
2323/*
2324 * Get the n'th (errornr) previous/next valid entry from the current entry in
2325 * the quickfix list.
2326 * dir == FORWARD or FORWARD_FILE: next valid entry
2327 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2328 */
2329 static qfline_T *
2330get_nth_valid_entry(
2331 qf_info_T *qi,
2332 int errornr,
2333 qfline_T *qf_ptr,
2334 int *qf_index,
2335 int dir)
2336{
2337 qfline_T *prev_qf_ptr;
2338 int prev_index;
2339 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2340 char_u *err = e_no_more_items;
2341
2342 while (errornr--)
2343 {
2344 prev_qf_ptr = qf_ptr;
2345 prev_index = *qf_index;
2346
2347 if (dir == FORWARD || dir == FORWARD_FILE)
2348 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2349 else
2350 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2351 if (qf_ptr == NULL)
2352 {
2353 qf_ptr = prev_qf_ptr;
2354 *qf_index = prev_index;
2355 if (err != NULL)
2356 {
2357 EMSG(_(err));
2358 return NULL;
2359 }
2360 break;
2361 }
2362
2363 err = NULL;
2364 }
2365
2366 return qf_ptr;
2367}
2368
2369/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002370 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002371 */
2372 static qfline_T *
2373get_nth_entry(
2374 qf_info_T *qi,
2375 int errornr,
2376 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002377 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002378{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002379 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002380
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002381 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002382 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2383 {
2384 --qf_idx;
2385 qf_ptr = qf_ptr->qf_prev;
2386 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002387 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002388 while (errornr > qf_idx &&
2389 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2390 qf_ptr->qf_next != NULL)
2391 {
2392 ++qf_idx;
2393 qf_ptr = qf_ptr->qf_next;
2394 }
2395
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002396 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002397 return qf_ptr;
2398}
2399
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002400/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002401 * Find a window displaying a Vim help file.
2402 */
2403 static win_T *
2404qf_find_help_win(void)
2405{
2406 win_T *wp;
2407
2408 FOR_ALL_WINDOWS(wp)
2409 if (bt_help(wp->w_buffer))
2410 return wp;
2411
2412 return NULL;
2413}
2414
2415/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002416 * Find a help window or open one.
2417 */
2418 static int
2419jump_to_help_window(qf_info_T *qi, int *opened_window)
2420{
2421 win_T *wp;
2422 int flags;
2423
2424 if (cmdmod.tab != 0)
2425 wp = NULL;
2426 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002427 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002428 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2429 win_enter(wp, TRUE);
2430 else
2431 {
2432 /*
2433 * Split off help window; put it at far top if no position
2434 * specified, the current window is vertically split and narrow.
2435 */
2436 flags = WSP_HELP;
2437 if (cmdmod.split == 0 && curwin->w_width != Columns
2438 && curwin->w_width < 80)
2439 flags |= WSP_TOP;
2440 if (qi != &ql_info)
2441 flags |= WSP_NEWLOC; /* don't copy the location list */
2442
2443 if (win_split(0, flags) == FAIL)
2444 return FAIL;
2445
2446 *opened_window = TRUE;
2447
2448 if (curwin->w_height < p_hh)
2449 win_setheight((int)p_hh);
2450
2451 if (qi != &ql_info) /* not a quickfix list */
2452 {
2453 /* The new window should use the supplied location list */
2454 curwin->w_llist = qi;
2455 qi->qf_refcount++;
2456 }
2457 }
2458
2459 if (!p_im)
2460 restart_edit = 0; /* don't want insert mode in help file */
2461
2462 return OK;
2463}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002464
2465/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002466 * Find a non-quickfix window using the given location list.
2467 * Returns NULL if a matching window is not found.
2468 */
2469 static win_T *
2470qf_find_win_with_loclist(qf_info_T *ll)
2471{
2472 win_T *wp;
2473
2474 FOR_ALL_WINDOWS(wp)
2475 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2476 return wp;
2477
2478 return NULL;
2479}
2480
2481/*
2482 * Find a window containing a normal buffer
2483 */
2484 static win_T *
2485qf_find_win_with_normal_buf(void)
2486{
2487 win_T *wp;
2488
2489 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002490 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002491 return wp;
2492
2493 return NULL;
2494}
2495
2496/*
2497 * Go to a window in any tabpage containing the specified file. Returns TRUE
2498 * if successfully jumped to the window. Otherwise returns FALSE.
2499 */
2500 static int
2501qf_goto_tabwin_with_file(int fnum)
2502{
2503 tabpage_T *tp;
2504 win_T *wp;
2505
2506 FOR_ALL_TAB_WINDOWS(tp, wp)
2507 if (wp->w_buffer->b_fnum == fnum)
2508 {
2509 goto_tabpage_win(tp, wp);
2510 return TRUE;
2511 }
2512
2513 return FALSE;
2514}
2515
2516/*
2517 * Create a new window to show a file above the quickfix window. Called when
2518 * only the quickfix window is present.
2519 */
2520 static int
2521qf_open_new_file_win(qf_info_T *ll_ref)
2522{
2523 int flags;
2524
2525 flags = WSP_ABOVE;
2526 if (ll_ref != NULL)
2527 flags |= WSP_NEWLOC;
2528 if (win_split(0, flags) == FAIL)
2529 return FAIL; /* not enough room for window */
2530 p_swb = empty_option; /* don't split again */
2531 swb_flags = 0;
2532 RESET_BINDING(curwin);
2533 if (ll_ref != NULL)
2534 {
2535 /* The new window should use the location list from the
2536 * location list window */
2537 curwin->w_llist = ll_ref;
2538 ll_ref->qf_refcount++;
2539 }
2540 return OK;
2541}
2542
2543/*
2544 * Go to a window that shows the right buffer. If the window is not found, go
2545 * to the window just above the location list window. This is used for opening
2546 * a file from a location window and not from a quickfix window. If some usable
2547 * window is previously found, then it is supplied in 'use_win'.
2548 */
2549 static void
2550qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2551{
2552 win_T *win = use_win;
2553
2554 if (win == NULL)
2555 {
2556 /* Find the window showing the selected file */
2557 FOR_ALL_WINDOWS(win)
2558 if (win->w_buffer->b_fnum == qf_fnum)
2559 break;
2560 if (win == NULL)
2561 {
2562 /* Find a previous usable window */
2563 win = curwin;
2564 do
2565 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002566 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002567 break;
2568 if (win->w_prev == NULL)
2569 win = lastwin; /* wrap around the top */
2570 else
2571 win = win->w_prev; /* go to previous window */
2572 } while (win != curwin);
2573 }
2574 }
2575 win_goto(win);
2576
2577 /* If the location list for the window is not set, then set it
2578 * to the location list from the location window */
2579 if (win->w_llist == NULL)
2580 {
2581 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002582 if (ll_ref != NULL)
2583 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002584 }
2585}
2586
2587/*
2588 * Go to a window that shows the specified file. If a window is not found, go
2589 * to the window just above the quickfix window. This is used for opening a
2590 * file from a quickfix window and not from a location window.
2591 */
2592 static void
2593qf_goto_win_with_qfl_file(int qf_fnum)
2594{
2595 win_T *win;
2596 win_T *altwin;
2597
2598 win = curwin;
2599 altwin = NULL;
2600 for (;;)
2601 {
2602 if (win->w_buffer->b_fnum == qf_fnum)
2603 break;
2604 if (win->w_prev == NULL)
2605 win = lastwin; /* wrap around the top */
2606 else
2607 win = win->w_prev; /* go to previous window */
2608
2609 if (IS_QF_WINDOW(win))
2610 {
2611 /* Didn't find it, go to the window before the quickfix
2612 * window. */
2613 if (altwin != NULL)
2614 win = altwin;
2615 else if (curwin->w_prev != NULL)
2616 win = curwin->w_prev;
2617 else
2618 win = curwin->w_next;
2619 break;
2620 }
2621
2622 /* Remember a usable window. */
Bram Moolenaar91335e52018-08-01 17:53:12 +02002623 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002624 altwin = win;
2625 }
2626
2627 win_goto(win);
2628}
2629
2630/*
2631 * Find a suitable window for opening a file (qf_fnum) from the
2632 * quickfix/location list and jump to it. If the file is already opened in a
2633 * window, jump to it. Otherwise open a new window to display the file. This is
2634 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002635 */
2636 static int
2637qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2638{
2639 win_T *usable_win_ptr = NULL;
2640 int usable_win;
2641 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002642 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002643
2644 usable_win = 0;
2645
2646 ll_ref = curwin->w_llist_ref;
2647 if (ll_ref != NULL)
2648 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002649 /* Find a non-quickfix window with this location list */
2650 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2651 if (usable_win_ptr != NULL)
2652 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002653 }
2654
2655 if (!usable_win)
2656 {
2657 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002658 win = qf_find_win_with_normal_buf();
2659 if (win != NULL)
2660 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002661 }
2662
2663 /*
2664 * If no usable window is found and 'switchbuf' contains "usetab"
2665 * then search in other tabs.
2666 */
2667 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002668 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002669
2670 /*
2671 * If there is only one window and it is the quickfix window, create a
2672 * new one above the quickfix window.
2673 */
2674 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2675 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002676 if (qf_open_new_file_win(ll_ref) != OK)
2677 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002678 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002679 }
2680 else
2681 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002682 if (curwin->w_llist_ref != NULL) /* In a location window */
2683 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2684 else /* In a quickfix window */
2685 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002686 }
2687
2688 return OK;
2689}
2690
2691/*
2692 * Edit the selected file or help file.
2693 */
2694 static int
2695qf_jump_edit_buffer(
2696 qf_info_T *qi,
2697 qfline_T *qf_ptr,
2698 int forceit,
2699 win_T *oldwin,
2700 int *opened_window,
2701 int *abort)
2702{
2703 int retval = OK;
2704
2705 if (qf_ptr->qf_type == 1)
2706 {
2707 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2708 * set b_p_ro flag). */
2709 if (!can_abandon(curbuf, forceit))
2710 {
2711 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002712 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002713 }
2714 else
2715 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2716 ECMD_HIDE + ECMD_SET_HELP,
2717 oldwin == curwin ? curwin : NULL);
2718 }
2719 else
2720 {
2721 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002722 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002723
2724 retval = buflist_getfile(qf_ptr->qf_fnum,
2725 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002726
2727 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002728 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002729 /*
2730 * Location list. Check whether the associated window is still
2731 * present and the list is still valid.
2732 */
2733 if (!win_valid_any_tab(oldwin))
2734 {
2735 EMSG(_("E924: Current window was closed"));
2736 *abort = TRUE;
2737 *opened_window = FALSE;
2738 }
2739 else if (!qflist_valid(oldwin, save_qfid))
2740 {
2741 EMSG(_(e_loc_list_changed));
2742 *abort = TRUE;
2743 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002744 }
2745 else if (old_qf_curlist != qi->qf_curlist
2746 || !is_qf_entry_present(qi, qf_ptr))
2747 {
2748 if (qi == &ql_info)
2749 EMSG(_("E925: Current quickfix was changed"));
2750 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002751 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002752 *abort = TRUE;
2753 }
2754
2755 if (*abort)
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002756 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002757 }
2758
2759 return retval;
2760}
2761
2762/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002763 * Go to the error line in the current file using either line/column number or
2764 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002765 */
2766 static void
2767qf_jump_goto_line(
2768 linenr_T qf_lnum,
2769 int qf_col,
2770 char_u qf_viscol,
2771 char_u *qf_pattern)
2772{
2773 linenr_T i;
2774 char_u *line;
2775 colnr_T screen_col;
2776 colnr_T char_col;
2777
2778 if (qf_pattern == NULL)
2779 {
2780 /*
2781 * Go to line with error, unless qf_lnum is 0.
2782 */
2783 i = qf_lnum;
2784 if (i > 0)
2785 {
2786 if (i > curbuf->b_ml.ml_line_count)
2787 i = curbuf->b_ml.ml_line_count;
2788 curwin->w_cursor.lnum = i;
2789 }
2790 if (qf_col > 0)
2791 {
2792 curwin->w_cursor.col = qf_col - 1;
2793#ifdef FEAT_VIRTUALEDIT
2794 curwin->w_cursor.coladd = 0;
2795#endif
2796 if (qf_viscol == TRUE)
2797 {
2798 /*
2799 * Check each character from the beginning of the error
2800 * line up to the error column. For each tab character
2801 * found, reduce the error column value by the length of
2802 * a tab character.
2803 */
2804 line = ml_get_curline();
2805 screen_col = 0;
2806 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2807 {
2808 if (*line == NUL)
2809 break;
2810 if (*line++ == '\t')
2811 {
2812 curwin->w_cursor.col -= 7 - (screen_col % 8);
2813 screen_col += 8 - (screen_col % 8);
2814 }
2815 else
2816 ++screen_col;
2817 }
2818 }
2819 check_cursor();
2820 }
2821 else
2822 beginline(BL_WHITE | BL_FIX);
2823 }
2824 else
2825 {
2826 pos_T save_cursor;
2827
2828 /* Move the cursor to the first line in the buffer */
2829 save_cursor = curwin->w_cursor;
2830 curwin->w_cursor.lnum = 0;
2831 if (!do_search(NULL, '/', qf_pattern, (long)1,
2832 SEARCH_KEEP, NULL, NULL))
2833 curwin->w_cursor = save_cursor;
2834 }
2835}
2836
2837/*
2838 * Display quickfix list index and size message
2839 */
2840 static void
2841qf_jump_print_msg(
2842 qf_info_T *qi,
2843 int qf_index,
2844 qfline_T *qf_ptr,
2845 buf_T *old_curbuf,
2846 linenr_T old_lnum)
2847{
2848 linenr_T i;
2849 int len;
2850
2851 /* Update the screen before showing the message, unless the screen
2852 * scrolled up. */
2853 if (!msg_scrolled)
2854 update_topline_redraw();
2855 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
2856 qi->qf_lists[qi->qf_curlist].qf_count,
2857 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
2858 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
2859 /* Add the message, skipping leading whitespace and newlines. */
2860 len = (int)STRLEN(IObuff);
2861 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
2862
2863 /* Output the message. Overwrite to avoid scrolling when the 'O'
2864 * flag is present in 'shortmess'; But when not jumping, print the
2865 * whole message. */
2866 i = msg_scroll;
2867 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
2868 msg_scroll = TRUE;
2869 else if (!msg_scrolled && shortmess(SHM_OVERALL))
2870 msg_scroll = FALSE;
2871 msg_attr_keep(IObuff, 0, TRUE);
2872 msg_scroll = i;
2873}
2874
2875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 * jump to a quickfix line
2877 * if dir == FORWARD go "errornr" valid entries forward
2878 * if dir == BACKWARD go "errornr" valid entries backward
2879 * if dir == FORWARD_FILE go "errornr" valid entries files backward
2880 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
2881 * else if "errornr" is zero, redisplay the same line
2882 * else go to entry "errornr"
2883 */
2884 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002885qf_jump(qf_info_T *qi,
2886 int dir,
2887 int errornr,
2888 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002890 qfline_T *qf_ptr;
2891 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 buf_T *old_curbuf;
2895 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002896 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00002897 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002899 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901#ifdef FEAT_FOLDING
2902 int old_KeyTyped = KeyTyped; /* getting file may reset it */
2903#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002904 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002906 if (qi == NULL)
2907 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002908
2909 if (qi->qf_curlist >= qi->qf_listcount
2910 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
2912 EMSG(_(e_quickfix));
2913 return;
2914 }
2915
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002916 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002918 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02002920 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002922 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
2923 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002925 qf_ptr = old_qf_ptr;
2926 qf_index = old_qf_index;
2927 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 }
2929 }
2930 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002931 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002933 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
2934 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 /* No need to print the error message if it's visible in the error
2936 * window */
2937 print_message = FALSE;
2938
2939 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002940 * For ":helpgrep" find a help window or open one.
2941 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002942 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002943 if (jump_to_help_window(qi, &opened_window) == FAIL)
2944 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002945
2946 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 * If currently in the quickfix window, find another window to show the
2948 * file in.
2949 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00002950 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 {
2952 /*
2953 * If there is no file specified, we don't know where to go.
2954 * But do advance, otherwise ":cn" gets stuck.
2955 */
2956 if (qf_ptr->qf_fnum == 0)
2957 goto theend;
2958
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002959 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
2960 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
2963 /*
2964 * If there is a file name,
2965 * read the wanted file if needed, and check autowrite etc.
2966 */
2967 old_curbuf = curbuf;
2968 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002969
2970 if (qf_ptr->qf_fnum != 0)
2971 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002972 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002973
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002974 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
2975 &opened_window, &abort);
2976 if (abort)
2977 {
2978 qi = NULL;
2979 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01002980 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002981 }
2982
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002983 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 {
2985 /* When not switched to another buffer, still need to set pc mark */
2986 if (curbuf == old_curbuf)
2987 setpcmark();
2988
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002989 if (qf_ptr != NULL)
2990 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col,
2991 qf_ptr->qf_viscol, qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992
2993#ifdef FEAT_FOLDING
2994 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
2995 foldOpenCursor();
2996#endif
2997 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002998 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 }
3000 else
3001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 if (opened_window)
3003 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01003004 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 {
3006 /*
3007 * Couldn't open file, so put index back where it was. This could
3008 * happen if the file was readonly and we changed something.
3009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 qf_ptr = old_qf_ptr;
3012 qf_index = old_qf_index;
3013 }
3014 }
3015theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003016 if (qi != NULL)
3017 {
3018 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
3019 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
3020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 if (p_swb != old_swb && opened_window)
3022 {
3023 /* Restore old 'switchbuf' value, but not when an autocommand or
3024 * modeline has changed the value. */
3025 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003028 swb_flags = old_swb_flags;
3029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 else
3031 free_string_option(old_swb);
3032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033}
3034
3035/*
3036 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003037 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 */
3039 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003040qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003042 buf_T *buf;
3043 char_u *fname;
3044 qfline_T *qfp;
3045 int i;
3046 int idx1 = 1;
3047 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003048 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003049 int plus = FALSE;
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003050 int qfFileAttr;
3051 int qfSepAttr;
3052 int qfLineAttr;
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02003053 int filter_entry;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003054 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003056 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003058 if (eap->cmdidx == CMD_llist)
3059 {
3060 qi = GET_LOC_LIST(curwin);
3061 if (qi == NULL)
3062 {
3063 EMSG(_(e_loclist));
3064 return;
3065 }
3066 }
3067
3068 if (qi->qf_curlist >= qi->qf_listcount
3069 || qi->qf_lists[qi->qf_curlist].qf_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 {
3071 EMSG(_(e_quickfix));
3072 return;
3073 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003074 if (*arg == '+')
3075 {
3076 ++arg;
3077 plus = TRUE;
3078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3080 {
3081 EMSG(_(e_trailing));
3082 return;
3083 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003084 if (plus)
3085 {
3086 i = qi->qf_lists[qi->qf_curlist].qf_index;
3087 idx2 = i + idx1;
3088 idx1 = i;
3089 }
3090 else
3091 {
3092 i = qi->qf_lists[qi->qf_curlist].qf_count;
3093 if (idx1 < 0)
3094 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3095 if (idx2 < 0)
3096 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
Bram Moolenaara796d462018-05-01 14:30:36 +02003099 /* Shorten all the file names, so that it is easy to read */
3100 shorten_fnames(FALSE);
3101
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003102 /*
3103 * Get the attributes for the different quickfix highlight items. Note
3104 * that this depends on syntax items defined in the qf.vim syntax file
3105 */
3106 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3107 if (qfFileAttr == 0)
3108 qfFileAttr = HL_ATTR(HLF_D);
3109 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3110 if (qfSepAttr == 0)
3111 qfSepAttr = HL_ATTR(HLF_D);
3112 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3113 if (qfLineAttr == 0)
3114 qfLineAttr = HL_ATTR(HLF_N);
3115
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003116 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003118 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3119 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
3121 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3122 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003123 if (got_int)
3124 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003125
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003126 fname = NULL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02003127 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3128 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", i, (char *)qfp->qf_module);
3129 else {
3130 if (qfp->qf_fnum != 0
3131 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3132 {
3133 fname = buf->b_fname;
3134 if (qfp->qf_type == 1) /* :helpgrep */
3135 fname = gettail(fname);
3136 }
3137 if (fname == NULL)
3138 sprintf((char *)IObuff, "%2d", i);
3139 else
3140 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
Bram Moolenaara9defad2018-07-08 18:20:24 +02003141 i, (char *)fname);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003142 }
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02003143
3144 // Support for filtering entries using :filter /pat/ clist
Bram Moolenaara9defad2018-07-08 18:20:24 +02003145 // Match against the module name, file name, search pattern and
3146 // text of the entry.
3147 filter_entry = TRUE;
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02003148 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3149 filter_entry &= message_filtered(qfp->qf_module);
Bram Moolenaara9defad2018-07-08 18:20:24 +02003150 if (filter_entry && fname != NULL)
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02003151 filter_entry &= message_filtered(fname);
Bram Moolenaara9defad2018-07-08 18:20:24 +02003152 if (filter_entry && qfp->qf_pattern != NULL)
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02003153 filter_entry &= message_filtered(qfp->qf_pattern);
Bram Moolenaara9defad2018-07-08 18:20:24 +02003154 if (filter_entry)
3155 filter_entry &= message_filtered(qfp->qf_text);
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02003156 if (filter_entry)
3157 goto next_entry;
3158
3159 msg_putchar('\n');
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003160 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003161 ? HL_ATTR(HLF_QFL) : qfFileAttr);
3162
3163 if (qfp->qf_lnum != 0)
3164 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003165 if (qfp->qf_lnum == 0)
3166 IObuff[0] = NUL;
3167 else if (qfp->qf_col == 0)
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003168 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003169 else
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003170 sprintf((char *)IObuff, "%ld col %d",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003171 qfp->qf_lnum, qfp->qf_col);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003172 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003173 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003174 msg_puts_attr(IObuff, qfLineAttr);
3175 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003176 if (qfp->qf_pattern != NULL)
3177 {
3178 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003179 msg_puts(IObuff);
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003180 msg_puts_attr((char_u *)":", qfSepAttr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003181 }
3182 msg_puts((char_u *)" ");
3183
3184 /* Remove newlines and leading whitespace from the text. For an
3185 * unrecognized line keep the indent, the compiler may mark a word
3186 * with ^^^^. */
3187 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3189 IObuff, IOSIZE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003190 msg_prt_line(IObuff, FALSE);
3191 out_flush(); /* show one line at a time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003193
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02003194next_entry:
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003195 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003196 if (qfp == NULL)
3197 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003198 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 ui_breakcheck();
3200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201}
3202
3203/*
3204 * Remove newlines and leading whitespace from an error message.
3205 * Put the result in "buf[bufsize]".
3206 */
3207 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003208qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
3210 int i;
3211 char_u *p = text;
3212
3213 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3214 {
3215 if (*p == '\n')
3216 {
3217 buf[i] = ' ';
3218 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003219 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 break;
3221 }
3222 else
3223 buf[i] = *p++;
3224 }
3225 buf[i] = NUL;
3226}
3227
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003228/*
3229 * Display information (list number, list size and the title) about a
3230 * quickfix/location list.
3231 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003232 static void
3233qf_msg(qf_info_T *qi, int which, char *lead)
3234{
3235 char *title = (char *)qi->qf_lists[which].qf_title;
3236 int count = qi->qf_lists[which].qf_count;
3237 char_u buf[IOSIZE];
3238
3239 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3240 lead,
3241 which + 1,
3242 qi->qf_listcount,
3243 count);
3244
3245 if (title != NULL)
3246 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003247 size_t len = STRLEN(buf);
3248
3249 if (len < 34)
3250 {
3251 vim_memset(buf + len, ' ', 34 - len);
3252 buf[34] = NUL;
3253 }
3254 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003255 }
3256 trunc_string(buf, buf, Columns - 1, IOSIZE);
3257 msg(buf);
3258}
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260/*
3261 * ":colder [count]": Up in the quickfix stack.
3262 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003263 * ":lolder [count]": Up in the location list stack.
3264 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 */
3266 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003267qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003269 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 int count;
3271
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003272 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
3273 {
3274 qi = GET_LOC_LIST(curwin);
3275 if (qi == NULL)
3276 {
3277 EMSG(_(e_loclist));
3278 return;
3279 }
3280 }
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (eap->addr_count != 0)
3283 count = eap->line2;
3284 else
3285 count = 1;
3286 while (count--)
3287 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003288 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003290 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 {
3292 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003293 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003295 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
3297 else
3298 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003299 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 {
3301 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003302 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003304 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
3306 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003307 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003308 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309}
3310
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003311/*
3312 * Display the information about all the quickfix/location lists in the stack
3313 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003314 void
3315qf_history(exarg_T *eap)
3316{
3317 qf_info_T *qi = &ql_info;
3318 int i;
3319
3320 if (eap->cmdidx == CMD_lhistory)
3321 qi = GET_LOC_LIST(curwin);
3322 if (qi == NULL || (qi->qf_listcount == 0
3323 && qi->qf_lists[qi->qf_curlist].qf_count == 0))
3324 MSG(_("No entries"));
3325 else
3326 for (i = 0; i < qi->qf_listcount; ++i)
3327 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3328}
3329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003331 * Free all the entries in the error list "idx". Note that other information
3332 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 */
3334 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003335qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003337 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003338 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003339 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003340 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003342 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003344 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003345 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003346 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003347 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003348 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003349 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003350 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003351 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003352 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003353 if (stop)
3354 /* Somehow qf_count may have an incorrect value, set it to 1
3355 * to avoid crashing when it's wrong.
3356 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003357 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003358 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003359 qfl->qf_start = qfpnext;
3360 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003362
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003363 qfl->qf_index = 0;
3364 qfl->qf_start = NULL;
3365 qfl->qf_last = NULL;
3366 qfl->qf_ptr = NULL;
3367 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003368
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003369 qf_clean_dir_stack(&qfl->qf_dir_stack);
3370 qfl->qf_directory = NULL;
3371 qf_clean_dir_stack(&qfl->qf_file_stack);
3372 qfl->qf_currfile = NULL;
3373 qfl->qf_multiline = FALSE;
3374 qfl->qf_multiignore = FALSE;
3375 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376}
3377
3378/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003379 * Free error list "idx". Frees all the entries in the quickfix list,
3380 * associated context information and the title.
3381 */
3382 static void
3383qf_free(qf_info_T *qi, int idx)
3384{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003385 qf_list_T *qfl = &qi->qf_lists[idx];
3386
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003387 qf_free_items(qi, idx);
3388
Bram Moolenaard23a8232018-02-10 18:45:26 +01003389 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003390 free_tv(qfl->qf_ctx);
3391 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003392 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003393 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003394}
3395
3396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 * qf_mark_adjust: adjust marks
3398 */
3399 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003400qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003401 win_T *wp,
3402 linenr_T line1,
3403 linenr_T line2,
3404 long amount,
3405 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003407 int i;
3408 qfline_T *qfp;
3409 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003410 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003411 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003412 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
Bram Moolenaarc1542742016-07-20 21:44:37 +02003414 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003415 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003416 if (wp != NULL)
3417 {
3418 if (wp->w_llist == NULL)
3419 return;
3420 qi = wp->w_llist;
3421 }
3422
3423 for (idx = 0; idx < qi->qf_listcount; ++idx)
3424 if (qi->qf_lists[idx].qf_count)
3425 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003426 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3427 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 if (qfp->qf_fnum == curbuf->b_fnum)
3429 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003430 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3432 {
3433 if (amount == MAXLNUM)
3434 qfp->qf_cleared = TRUE;
3435 else
3436 qfp->qf_lnum += amount;
3437 }
3438 else if (amount_after && qfp->qf_lnum > line2)
3439 qfp->qf_lnum += amount_after;
3440 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003441
3442 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003443 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444}
3445
3446/*
3447 * Make a nice message out of the error character and the error number:
3448 * char number message
3449 * e or E 0 " error"
3450 * w or W 0 " warning"
3451 * i or I 0 " info"
3452 * 0 0 ""
3453 * other 0 " c"
3454 * e or E n " error n"
3455 * w or W n " warning n"
3456 * i or I n " info n"
3457 * 0 n " error n"
3458 * other n " c n"
3459 * 1 x "" :helpgrep
3460 */
3461 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003462qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
3464 static char_u buf[20];
3465 static char_u cc[3];
3466 char_u *p;
3467
3468 if (c == 'W' || c == 'w')
3469 p = (char_u *)" warning";
3470 else if (c == 'I' || c == 'i')
3471 p = (char_u *)" info";
3472 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3473 p = (char_u *)" error";
3474 else if (c == 0 || c == 1)
3475 p = (char_u *)"";
3476 else
3477 {
3478 cc[0] = ' ';
3479 cc[1] = c;
3480 cc[2] = NUL;
3481 p = cc;
3482 }
3483
3484 if (nr <= 0)
3485 return p;
3486
3487 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3488 return buf;
3489}
3490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003492 * When "split" is FALSE: Open the entry/result under the cursor.
3493 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3494 */
3495 void
3496qf_view_result(int split)
3497{
3498 qf_info_T *qi = &ql_info;
3499
3500 if (!bt_quickfix(curbuf))
3501 return;
3502
3503 if (IS_LL_WINDOW(curwin))
3504 qi = GET_LOC_LIST(curwin);
3505
3506 if (qi == NULL || qi->qf_lists[qi->qf_curlist].qf_count == 0)
3507 {
3508 EMSG(_(e_quickfix));
3509 return;
3510 }
3511
3512 if (split)
3513 {
3514 char_u cmd[32];
3515
3516 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3517 (long)curwin->w_cursor.lnum,
3518 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3519 if (do_cmdline_cmd(cmd) == OK)
3520 do_cmdline_cmd((char_u *) "clearjumps");
3521 return;
3522 }
3523
3524 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3525}
3526
3527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 * ":cwindow": open the quickfix window if we have errors to display,
3529 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003530 * ":lwindow": open the location list window if we have locations to display,
3531 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 */
3533 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003534ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003536 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 win_T *win;
3538
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003539 if (eap->cmdidx == CMD_lwindow)
3540 {
3541 qi = GET_LOC_LIST(curwin);
3542 if (qi == NULL)
3543 return;
3544 }
3545
3546 /* Look for an existing quickfix window. */
3547 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
3549 /*
3550 * If a quickfix window is open but we have no errors to display,
3551 * close the window. If a quickfix window is not open, then open
3552 * it if we have errors; otherwise, leave it closed.
3553 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003554 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaard236ac02011-05-05 17:14:14 +02003555 || qi->qf_lists[qi->qf_curlist].qf_count == 0
Bram Moolenaard68071d2006-05-02 22:08:30 +00003556 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
3558 if (win != NULL)
3559 ex_cclose(eap);
3560 }
3561 else if (win == NULL)
3562 ex_copen(eap);
3563}
3564
3565/*
3566 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003567 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003570ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003572 win_T *win = NULL;
3573 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003575 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3576 {
3577 qi = GET_LOC_LIST(curwin);
3578 if (qi == NULL)
3579 return;
3580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003582 /* Find existing quickfix window and close it. */
3583 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 if (win != NULL)
3585 win_close(win, FALSE);
3586}
3587
3588/*
3589 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003590 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 */
3592 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003593ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003595 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003598 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003599 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003600 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003602 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3603 {
3604 qi = GET_LOC_LIST(curwin);
3605 if (qi == NULL)
3606 {
3607 EMSG(_(e_loclist));
3608 return;
3609 }
3610 }
3611
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 if (eap->addr_count != 0)
3613 height = eap->line2;
3614 else
3615 height = QF_WINHEIGHT;
3616
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618#ifdef FEAT_GUI
3619 need_mouse_correct = TRUE;
3620#endif
3621
3622 /*
3623 * Find existing quickfix window, or open a new one.
3624 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003625 win = qf_find_win(qi);
3626
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003627 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003630 if (eap->addr_count != 0)
3631 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003632 if (cmdmod.split & WSP_VERT)
3633 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003634 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003635 win_setwidth(height);
3636 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003637 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003638 win_setheight(height);
3639 }
3640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 else
3642 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003643 int flags = 0;
3644
Bram Moolenaar9c102382006-05-03 21:26:49 +00003645 qf_buf = qf_find_buf(qi);
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 /* The current window becomes the previous window afterwards. */
3648 win = curwin;
3649
Bram Moolenaar77642c02012-11-20 17:55:10 +01003650 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3651 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003652 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003653 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003654 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003655 /* Default is to open the window below the current window */
3656 if (cmdmod.split == 0)
3657 flags = WSP_BELOW;
3658 flags |= WSP_NEWLOC;
3659 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003661 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003663 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003665 /*
3666 * For the location list window, create a reference to the
3667 * location list from the window 'win'.
3668 */
3669 curwin->w_llist_ref = win->w_llist;
3670 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003672
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003673 if (oldwin != curwin)
3674 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003675 if (qf_buf != NULL)
3676 /* Use the existing quickfix buffer */
3677 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003678 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003679 else
3680 {
3681 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003682 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003683 /* switch off 'swapfile' */
3684 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3685 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003686 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003687 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003688 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003689#ifdef FEAT_DIFF
3690 curwin->w_p_diff = FALSE;
3691#endif
3692#ifdef FEAT_FOLDING
3693 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3694 OPT_LOCAL);
3695#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003698 /* Only set the height when still in the same tab page and there is no
3699 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003700 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 win_setheight(height);
3702 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3703 if (win_valid(win))
3704 prevwin = win;
3705 }
3706
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003707 qf_set_title_var(qi);
3708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 /*
3710 * Fill the buffer with the quickfix list.
3711 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003712 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003714 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 curwin->w_cursor.col = 0;
3716 check_cursor();
3717 update_topline(); /* scroll to show the line */
3718}
3719
3720/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003721 * Move the cursor in the quickfix window to "lnum".
3722 */
3723 static void
3724qf_win_goto(win_T *win, linenr_T lnum)
3725{
3726 win_T *old_curwin = curwin;
3727
3728 curwin = win;
3729 curbuf = win->w_buffer;
3730 curwin->w_cursor.lnum = lnum;
3731 curwin->w_cursor.col = 0;
3732#ifdef FEAT_VIRTUALEDIT
3733 curwin->w_cursor.coladd = 0;
3734#endif
3735 curwin->w_curswant = 0;
3736 update_topline(); /* scroll to show the line */
3737 redraw_later(VALID);
3738 curwin->w_redr_status = TRUE; /* update ruler */
3739 curwin = old_curwin;
3740 curbuf = curwin->w_buffer;
3741}
3742
3743/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003744 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003745 */
3746 void
3747ex_cbottom(exarg_T *eap UNUSED)
3748{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003749 qf_info_T *qi = &ql_info;
3750 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003751
Bram Moolenaar537ef082016-07-09 17:56:19 +02003752 if (eap->cmdidx == CMD_lbottom)
3753 {
3754 qi = GET_LOC_LIST(curwin);
3755 if (qi == NULL)
3756 {
3757 EMSG(_(e_loclist));
3758 return;
3759 }
3760 }
3761
3762 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003763 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3764 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3765}
3766
3767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 * Return the number of the current entry (line number in the quickfix
3769 * window).
3770 */
3771 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003772qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003774 qf_info_T *qi = &ql_info;
3775
3776 if (IS_LL_WINDOW(wp))
3777 /* In the location list window, use the referenced location list */
3778 qi = wp->w_llist_ref;
3779
3780 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781}
3782
3783/*
3784 * Update the cursor position in the quickfix window to the current error.
3785 * Return TRUE if there is a quickfix window.
3786 */
3787 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003788qf_win_pos_update(
3789 qf_info_T *qi,
3790 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791{
3792 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003793 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794
3795 /*
3796 * Put the cursor on the current error in the quickfix window, so that
3797 * it's viewable.
3798 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003799 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 if (win != NULL
3801 && qf_index <= win->w_buffer->b_ml.ml_line_count
3802 && old_qf_index != qf_index)
3803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (qf_index > old_qf_index)
3805 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003806 win->w_redraw_top = old_qf_index;
3807 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809 else
3810 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02003811 win->w_redraw_top = qf_index;
3812 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02003814 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 return win != NULL;
3817}
3818
3819/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003820 * Check whether the given window is displaying the specified quickfix/location
3821 * list buffer
3822 */
3823 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003824is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003825{
3826 /*
3827 * A window displaying the quickfix buffer will have the w_llist_ref field
3828 * set to NULL.
3829 * A window displaying a location list buffer will have the w_llist_ref
3830 * pointing to the location list.
3831 */
3832 if (bt_quickfix(win->w_buffer))
3833 if ((qi == &ql_info && win->w_llist_ref == NULL)
3834 || (qi != &ql_info && win->w_llist_ref == qi))
3835 return TRUE;
3836
3837 return FALSE;
3838}
3839
3840/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003841 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003842 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003843 */
3844 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003845qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003846{
3847 win_T *win;
3848
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003849 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00003850 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003851 return win;
3852 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003853}
3854
3855/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00003856 * Find a quickfix buffer.
3857 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 */
3859 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003860qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
Bram Moolenaar9c102382006-05-03 21:26:49 +00003862 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003863 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
Bram Moolenaar9c102382006-05-03 21:26:49 +00003865 FOR_ALL_TAB_WINDOWS(tp, win)
3866 if (is_qf_win(win, qi))
3867 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003868
Bram Moolenaar9c102382006-05-03 21:26:49 +00003869 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870}
3871
3872/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02003873 * Update the w:quickfix_title variable in the quickfix/location list window
3874 */
3875 static void
3876qf_update_win_titlevar(qf_info_T *qi)
3877{
3878 win_T *win;
3879 win_T *curwin_save;
3880
3881 if ((win = qf_find_win(qi)) != NULL)
3882 {
3883 curwin_save = curwin;
3884 curwin = win;
3885 qf_set_title_var(qi);
3886 curwin = curwin_save;
3887 }
3888}
3889
3890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 * Find the quickfix buffer. If it exists, update the contents.
3892 */
3893 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02003894qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895{
3896 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003897 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899
3900 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003901 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 if (buf != NULL)
3903 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02003904 linenr_T old_line_count = buf->b_ml.ml_line_count;
3905
3906 if (old_last == NULL)
3907 /* set curwin/curbuf to buf and save a few things */
3908 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909
Bram Moolenaard823fa92016-08-12 16:29:27 +02003910 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003911
Bram Moolenaar864293a2016-06-02 13:40:04 +02003912 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02003913 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01003914
Bram Moolenaar864293a2016-06-02 13:40:04 +02003915 if (old_last == NULL)
3916 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02003917 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02003918
3919 /* restore curwin/curbuf and a few other things */
3920 aucmd_restbuf(&aco);
3921 }
3922
3923 /* Only redraw when added lines are visible. This avoids flickering
3924 * when the added lines are not visible. */
3925 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
3926 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
3928}
3929
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003930/*
3931 * Set "w:quickfix_title" if "qi" has a title.
3932 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003934qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003935{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003936 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
3937 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02003938 qi->qf_lists[qi->qf_curlist].qf_title);
3939}
3940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02003942 * Add an error line to the quickfix buffer.
3943 */
3944 static int
3945qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
3946{
3947 int len;
3948 buf_T *errbuf;
3949
3950 if (qfp->qf_module != NULL)
3951 {
3952 STRCPY(IObuff, qfp->qf_module);
3953 len = (int)STRLEN(IObuff);
3954 }
3955 else if (qfp->qf_fnum != 0
3956 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
3957 && errbuf->b_fname != NULL)
3958 {
3959 if (qfp->qf_type == 1) /* :helpgrep */
3960 STRCPY(IObuff, gettail(errbuf->b_fname));
3961 else
3962 {
3963 /* shorten the file name if not done already */
3964 if (errbuf->b_sfname == NULL
3965 || mch_isFullName(errbuf->b_sfname))
3966 {
3967 if (*dirname == NUL)
3968 mch_dirname(dirname, MAXPATHL);
3969 shorten_buf_fname(errbuf, dirname, FALSE);
3970 }
3971 STRCPY(IObuff, errbuf->b_fname);
3972 }
3973 len = (int)STRLEN(IObuff);
3974 }
3975 else
3976 len = 0;
3977 IObuff[len++] = '|';
3978
3979 if (qfp->qf_lnum > 0)
3980 {
3981 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
3982 len += (int)STRLEN(IObuff + len);
3983
3984 if (qfp->qf_col > 0)
3985 {
3986 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
3987 len += (int)STRLEN(IObuff + len);
3988 }
3989
3990 sprintf((char *)IObuff + len, "%s",
3991 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3992 len += (int)STRLEN(IObuff + len);
3993 }
3994 else if (qfp->qf_pattern != NULL)
3995 {
3996 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
3997 len += (int)STRLEN(IObuff + len);
3998 }
3999 IObuff[len++] = '|';
4000 IObuff[len++] = ' ';
4001
4002 /* Remove newlines and leading whitespace from the text.
4003 * For an unrecognized line keep the indent, the compiler may
4004 * mark a word with ^^^^. */
4005 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4006 IObuff + len, IOSIZE - len);
4007
4008 if (ml_append_buf(buf, lnum, IObuff,
4009 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4010 return FAIL;
4011
4012 return OK;
4013}
4014
4015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 * Fill current buffer with quickfix errors, replacing any previous contents.
4017 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004018 * If "old_last" is not NULL append the items after this one.
4019 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4020 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 */
4022 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004023qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004025 linenr_T lnum;
4026 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004027 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
Bram Moolenaar864293a2016-06-02 13:40:04 +02004029 if (old_last == NULL)
4030 {
4031 if (buf != curbuf)
4032 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004033 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004034 return;
4035 }
4036
4037 /* delete all existing lines */
4038 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4039 (void)ml_delete((linenr_T)1, FALSE);
4040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041
4042 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004043 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaara796d462018-05-01 14:30:36 +02004045 char_u dirname[MAXPATHL];
4046
4047 *dirname = NUL;
4048
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004050 if (old_last == NULL)
4051 {
4052 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4053 lnum = 0;
4054 }
4055 else
4056 {
4057 qfp = old_last->qf_next;
4058 lnum = buf->b_ml.ml_line_count;
4059 }
4060 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004062 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004064
Bram Moolenaar864293a2016-06-02 13:40:04 +02004065 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004067 if (qfp == NULL)
4068 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004070
4071 if (old_last == NULL)
4072 /* Delete the empty line which is now at the end */
4073 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075
4076 /* correct cursor position */
4077 check_lnums(TRUE);
4078
Bram Moolenaar864293a2016-06-02 13:40:04 +02004079 if (old_last == NULL)
4080 {
4081 /* Set the 'filetype' to "qf" each time after filling the buffer.
4082 * This resembles reading a file into a buffer, it's more logical when
4083 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02004084 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004085 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4086 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
Bram Moolenaar864293a2016-06-02 13:40:04 +02004088 keep_filetype = TRUE; /* don't detect 'filetype' */
4089 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004091 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004093 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004094 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004095
Bram Moolenaar864293a2016-06-02 13:40:04 +02004096 /* make sure it will be redrawn */
4097 redraw_curbuf_later(NOT_VALID);
4098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
4100 /* Restore KeyTyped, setting 'filetype' may reset it. */
4101 KeyTyped = old_KeyTyped;
4102}
4103
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004104/*
4105 * For every change made to the quickfix list, update the changed tick.
4106 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004107 static void
4108qf_list_changed(qf_info_T *qi, int qf_idx)
4109{
4110 qi->qf_lists[qf_idx].qf_changedtick++;
4111}
4112
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004114 * Return the quickfix/location list number with the given identifier.
4115 * Returns -1 if list is not found.
4116 */
4117 static int
4118qf_id2nr(qf_info_T *qi, int_u qfid)
4119{
4120 int qf_idx;
4121
4122 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4123 if (qi->qf_lists[qf_idx].qf_id == qfid)
4124 return qf_idx;
4125 return INVALID_QFIDX;
4126}
4127
4128/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004129 * Jump to the first entry if there is one.
4130 */
4131 static void
4132qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4133{
4134 // If autocommands changed the current list, then restore it
4135 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4136 qi->qf_curlist = qf_id2nr(qi, save_qfid);
4137
4138 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4139 qf_jump(qi, 0, 0, forceit);
4140}
4141
4142/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004143 * Return TRUE when using ":vimgrep" for ":grep".
4144 */
4145 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004146grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004147{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004148 return ((cmdidx == CMD_grep
4149 || cmdidx == CMD_lgrep
4150 || cmdidx == CMD_grepadd
4151 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004152 && STRCMP("internal",
4153 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4154}
4155
4156/*
Bram Moolenaara6557602006-02-04 22:43:20 +00004157 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 */
4159 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004160ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161{
Bram Moolenaar7c626922005-02-07 22:01:03 +00004162 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004164 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00004166 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004167 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004168 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004169 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004170 int_u save_qfid;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004171
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004172 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
4173 if (grep_internal(eap->cmdidx))
4174 {
4175 ex_vimgrep(eap);
4176 return;
4177 }
4178
Bram Moolenaar7c626922005-02-07 22:01:03 +00004179 switch (eap->cmdidx)
4180 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00004181 case CMD_make: au_name = (char_u *)"make"; break;
4182 case CMD_lmake: au_name = (char_u *)"lmake"; break;
4183 case CMD_grep: au_name = (char_u *)"grep"; break;
4184 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4185 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4186 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004187 default: break;
4188 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004189 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4190 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004191 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004192#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004193 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004194 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004195#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004196 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004197#ifdef FEAT_MBYTE
4198 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
Bram Moolenaara6557602006-02-04 22:43:20 +00004201 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
4202 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004203 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004204
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00004206 fname = get_mef_name();
4207 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004209 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
4211 /*
4212 * If 'shellpipe' empty: don't redirect to 'errorfile'.
4213 */
4214 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
4215 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00004216 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 cmd = alloc(len);
4218 if (cmd == NULL)
4219 return;
4220 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
4221 (char *)p_shq);
4222 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004223 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 /*
4225 * Output a newline if there's something else than the :make command that
4226 * was typed (in which case the cursor is in column 0).
4227 */
4228 if (msg_col == 0)
4229 msg_didout = FALSE;
4230 msg_start();
4231 MSG_PUTS(":!");
4232 msg_outtrans(cmd); /* show what we are doing */
4233
4234 /* let the shell know if we are redirecting output or not */
4235 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4236
4237#ifdef AMIGA
4238 out_flush();
4239 /* read window status report and redraw before message */
4240 (void)char_avail();
4241#endif
4242
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004243 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00004244 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4245 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004246 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004247 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004248 if (wp != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004249 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004250 qi = GET_LOC_LIST(wp);
4251 if (qi == NULL)
4252 goto cleanup;
4253 }
4254 if (res >= 0)
4255 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004256
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004257 // Remember the current quickfix list identifier, so that we can
4258 // check for autocommands changing the current quickfix list.
4259 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4260 if (au_name != NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004261 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4262 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004263 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004264 // display the first error
4265 qf_jump_first(qi, save_qfid, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004267cleanup:
Bram Moolenaar7c626922005-02-07 22:01:03 +00004268 mch_remove(fname);
4269 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 vim_free(cmd);
4271}
4272
4273/*
4274 * Return the name for the errorfile, in allocated memory.
4275 * Find a new unique name when 'makeef' contains "##".
4276 * Returns NULL for error.
4277 */
4278 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004279get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280{
4281 char_u *p;
4282 char_u *name;
4283 static int start = -1;
4284 static int off = 0;
4285#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004286 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287#endif
4288
4289 if (*p_mef == NUL)
4290 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004291 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 if (name == NULL)
4293 EMSG(_(e_notmp));
4294 return name;
4295 }
4296
4297 for (p = p_mef; *p; ++p)
4298 if (p[0] == '#' && p[1] == '#')
4299 break;
4300
4301 if (*p == NUL)
4302 return vim_strsave(p_mef);
4303
4304 /* Keep trying until the name doesn't exist yet. */
4305 for (;;)
4306 {
4307 if (start == -1)
4308 start = mch_get_pid();
4309 else
4310 off += 19;
4311
4312 name = alloc((unsigned)STRLEN(p_mef) + 30);
4313 if (name == NULL)
4314 break;
4315 STRCPY(name, p_mef);
4316 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4317 STRCAT(name, p + 2);
4318 if (mch_getperm(name) < 0
4319#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02004320 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 && mch_lstat((char *)name, &sb) < 0
4322#endif
4323 )
4324 break;
4325 vim_free(name);
4326 }
4327 return name;
4328}
4329
4330/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004331 * Returns the number of valid entries in the current quickfix/location list.
4332 */
4333 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004334qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004335{
4336 qf_info_T *qi = &ql_info;
4337 qfline_T *qfp;
4338 int i, sz = 0;
4339 int prev_fnum = 0;
4340
4341 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4342 {
4343 /* Location list */
4344 qi = GET_LOC_LIST(curwin);
4345 if (qi == NULL)
4346 return 0;
4347 }
4348
4349 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004350 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004351 ++i, qfp = qfp->qf_next)
4352 {
4353 if (qfp->qf_valid)
4354 {
4355 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4356 sz++; /* Count all valid entries */
4357 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4358 {
4359 /* Count the number of files */
4360 sz++;
4361 prev_fnum = qfp->qf_fnum;
4362 }
4363 }
4364 }
4365
4366 return sz;
4367}
4368
4369/*
4370 * Returns the current index of the quickfix/location list.
4371 * Returns 0 if there is an error.
4372 */
4373 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004374qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004375{
4376 qf_info_T *qi = &ql_info;
4377
4378 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4379 {
4380 /* Location list */
4381 qi = GET_LOC_LIST(curwin);
4382 if (qi == NULL)
4383 return 0;
4384 }
4385
4386 return qi->qf_lists[qi->qf_curlist].qf_index;
4387}
4388
4389/*
4390 * Returns the current index in the quickfix/location list (counting only valid
4391 * entries). If no valid entries are in the list, then returns 1.
4392 */
4393 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004394qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004395{
4396 qf_info_T *qi = &ql_info;
4397 qf_list_T *qfl;
4398 qfline_T *qfp;
4399 int i, eidx = 0;
4400 int prev_fnum = 0;
4401
4402 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4403 {
4404 /* Location list */
4405 qi = GET_LOC_LIST(curwin);
4406 if (qi == NULL)
4407 return 1;
4408 }
4409
4410 qfl = &qi->qf_lists[qi->qf_curlist];
4411 qfp = qfl->qf_start;
4412
4413 /* check if the list has valid errors */
4414 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4415 return 1;
4416
4417 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4418 {
4419 if (qfp->qf_valid)
4420 {
4421 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4422 {
4423 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4424 {
4425 /* Count the number of files */
4426 eidx++;
4427 prev_fnum = qfp->qf_fnum;
4428 }
4429 }
4430 else
4431 eidx++;
4432 }
4433 }
4434
4435 return eidx ? eidx : 1;
4436}
4437
4438/*
4439 * Get the 'n'th valid error entry in the quickfix or location list.
4440 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4441 * For :cdo and :ldo returns the 'n'th valid error entry.
4442 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4443 */
4444 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004445qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004446{
4447 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4448 qfline_T *qfp = qfl->qf_start;
4449 int i, eidx;
4450 int prev_fnum = 0;
4451
4452 /* check if the list has valid errors */
4453 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4454 return 1;
4455
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004456 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004457 i++, qfp = qfp->qf_next)
4458 {
4459 if (qfp->qf_valid)
4460 {
4461 if (fdo)
4462 {
4463 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4464 {
4465 /* Count the number of files */
4466 eidx++;
4467 prev_fnum = qfp->qf_fnum;
4468 }
4469 }
4470 else
4471 eidx++;
4472 }
4473
4474 if (eidx == n)
4475 break;
4476 }
4477
4478 if (i <= qfl->qf_count)
4479 return i;
4480 else
4481 return 1;
4482}
4483
4484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004486 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004487 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 */
4489 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004490ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004492 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004493 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004494
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004495 if (eap->cmdidx == CMD_ll
4496 || eap->cmdidx == CMD_lrewind
4497 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004498 || eap->cmdidx == CMD_llast
4499 || eap->cmdidx == CMD_ldo
4500 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004501 {
4502 qi = GET_LOC_LIST(curwin);
4503 if (qi == NULL)
4504 {
4505 EMSG(_(e_loclist));
4506 return;
4507 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004508 }
4509
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004510 if (eap->addr_count > 0)
4511 errornr = (int)eap->line2;
4512 else
4513 {
4514 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4515 errornr = 0;
4516 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4517 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4518 errornr = 1;
4519 else
4520 errornr = 32767;
4521 }
4522
4523 /* For cdo and ldo commands, jump to the nth valid error.
4524 * For cfdo and lfdo commands, jump to the nth valid file entry.
4525 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004526 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4527 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004528 errornr = qf_get_nth_valid_entry(qi,
4529 eap->addr_count > 0 ? (int)eap->line1 : 1,
4530 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4531
4532 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533}
4534
4535/*
4536 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004537 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004538 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 */
4540 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004541ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004543 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004544 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004545
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004546 if (eap->cmdidx == CMD_lnext
4547 || eap->cmdidx == CMD_lNext
4548 || eap->cmdidx == CMD_lprevious
4549 || eap->cmdidx == CMD_lnfile
4550 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004551 || eap->cmdidx == CMD_lpfile
4552 || eap->cmdidx == CMD_ldo
4553 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004554 {
4555 qi = GET_LOC_LIST(curwin);
4556 if (qi == NULL)
4557 {
4558 EMSG(_(e_loclist));
4559 return;
4560 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004561 }
4562
Bram Moolenaar55b69262017-08-13 13:42:01 +02004563 if (eap->addr_count > 0
4564 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4565 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004566 errornr = (int)eap->line2;
4567 else
4568 errornr = 1;
4569
4570 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4571 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004573 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4574 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004576 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4577 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 ? BACKWARD_FILE
4579 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004580 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581}
4582
4583/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004584 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004585 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 */
4587 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004588ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004590 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004591 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004592 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004593 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004594 int_u save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004595 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004596
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004597 switch (eap->cmdidx)
4598 {
4599 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4600 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4601 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4602 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4603 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4604 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4605 default: break;
4606 }
4607 if (au_name != NULL)
4608 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004609#ifdef FEAT_MBYTE
4610 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4611#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004612#ifdef FEAT_BROWSE
4613 if (cmdmod.browse)
4614 {
4615 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004616 NULL, NULL,
4617 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004618 if (browse_file == NULL)
4619 return;
4620 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4621 vim_free(browse_file);
4622 }
4623 else
4624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004626 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004627
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004628 if (eap->cmdidx == CMD_lfile
4629 || eap->cmdidx == CMD_lgetfile
4630 || eap->cmdidx == CMD_laddfile)
4631 wp = curwin;
4632
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004633 /*
4634 * This function is used by the :cfile, :cgetfile and :caddfile
4635 * commands.
4636 * :cfile always creates a new quickfix list and jumps to the
4637 * first error.
4638 * :cgetfile creates a new quickfix list but doesn't jump to the
4639 * first error.
4640 * :caddfile adds to an existing quickfix list. If there is no
4641 * quickfix list then a new list is created.
4642 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004643 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004644 && eap->cmdidx != CMD_laddfile),
4645 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004646 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004647 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01004648 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004649 if (qi == NULL)
4650 return;
4651 }
4652 if (res >= 0)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004653 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004654 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004655 if (au_name != NULL)
4656 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004657
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004658 // Jump to the first error for a new list and if autocmds didn't
4659 // free the list.
4660 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
4661 && qflist_valid(wp, save_qfid))
4662 {
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004663 // display the first error
4664 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004665 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004666}
4667
4668/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004669 * Return the vimgrep autocmd name.
4670 */
4671 static char_u *
4672vgr_get_auname(cmdidx_T cmdidx)
4673{
4674 switch (cmdidx)
4675 {
4676 case CMD_vimgrep: return (char_u *)"vimgrep";
4677 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4678 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4679 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4680 case CMD_grep: return (char_u *)"grep";
4681 case CMD_lgrep: return (char_u *)"lgrep";
4682 case CMD_grepadd: return (char_u *)"grepadd";
4683 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4684 default: return NULL;
4685 }
4686}
4687
4688/*
4689 * Initialize the regmatch used by vimgrep for pattern "s".
4690 */
4691 static void
4692vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4693{
4694 /* Get the search pattern: either white-separated or enclosed in // */
4695 regmatch->regprog = NULL;
4696
4697 if (s == NULL || *s == NUL)
4698 {
4699 /* Pattern is empty, use last search pattern. */
4700 if (last_search_pat() == NULL)
4701 {
4702 EMSG(_(e_noprevre));
4703 return;
4704 }
4705 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4706 }
4707 else
4708 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4709
4710 regmatch->rmm_ic = p_ic;
4711 regmatch->rmm_maxcol = 0;
4712}
4713
4714/*
4715 * Display a file name when vimgrep is running.
4716 */
4717 static void
4718vgr_display_fname(char_u *fname)
4719{
4720 char_u *p;
4721
4722 msg_start();
4723 p = msg_strtrunc(fname, TRUE);
4724 if (p == NULL)
4725 msg_outtrans(fname);
4726 else
4727 {
4728 msg_outtrans(p);
4729 vim_free(p);
4730 }
4731 msg_clr_eos();
4732 msg_didout = FALSE; /* overwrite this message */
4733 msg_nowait = TRUE; /* don't wait for this message */
4734 msg_col = 0;
4735 out_flush();
4736}
4737
4738/*
4739 * Load a dummy buffer to search for a pattern using vimgrep.
4740 */
4741 static buf_T *
4742vgr_load_dummy_buf(
4743 char_u *fname,
4744 char_u *dirname_start,
4745 char_u *dirname_now)
4746{
4747 int save_mls;
4748#if defined(FEAT_SYN_HL)
4749 char_u *save_ei = NULL;
4750#endif
4751 buf_T *buf;
4752
4753#if defined(FEAT_SYN_HL)
4754 /* Don't do Filetype autocommands to avoid loading syntax and
4755 * indent scripts, a great speed improvement. */
4756 save_ei = au_event_disable(",Filetype");
4757#endif
4758 /* Don't use modelines here, it's useless. */
4759 save_mls = p_mls;
4760 p_mls = 0;
4761
4762 /* Load file into a buffer, so that 'fileencoding' is detected,
4763 * autocommands applied, etc. */
4764 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4765
4766 p_mls = save_mls;
4767#if defined(FEAT_SYN_HL)
4768 au_event_restore(save_ei);
4769#endif
4770
4771 return buf;
4772}
4773
4774/*
4775 * Check whether a quickfix/location list valid. Autocmds may remove or change
4776 * a quickfix list when vimgrep is running. If the list is not found, create a
4777 * new list.
4778 */
4779 static int
4780vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004781 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004782 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004783 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004784 char_u *title)
4785{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004786 /* Verify that the quickfix/location list was not freed by an autocmd */
4787 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004788 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004789 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004790 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004791 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004792 EMSG(_(e_loc_list_changed));
4793 return FALSE;
4794 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004795 else
4796 {
4797 /* Quickfix list is not found, create a new one. */
4798 qf_new_list(qi, title);
4799 return TRUE;
4800 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004801 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004802
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004803 if (qi->qf_lists[qi->qf_curlist].qf_id != qfid)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004804 /* Autocommands changed the quickfix list. Find the one we were
4805 * using and restore it. */
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004806 qi->qf_curlist = qf_id2nr(qi, qfid);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004807
4808 return TRUE;
4809}
4810
4811/*
4812 * Search for a pattern in all the lines in a buffer and add the matching lines
4813 * to a quickfix list.
4814 */
4815 static int
4816vgr_match_buflines(
4817 qf_info_T *qi,
4818 char_u *fname,
4819 buf_T *buf,
4820 regmmatch_T *regmatch,
4821 long tomatch,
4822 int duplicate_name,
4823 int flags)
4824{
4825 int found_match = FALSE;
4826 long lnum;
4827 colnr_T col;
4828
4829 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
4830 {
4831 col = 0;
4832 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
4833 col, NULL, NULL) > 0)
4834 {
4835 /* Pass the buffer number so that it gets used even for a
4836 * dummy buffer, unless duplicate_name is set, then the
4837 * buffer will be wiped out below. */
4838 if (qf_add_entry(qi,
4839 qi->qf_curlist,
4840 NULL, /* dir */
4841 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02004842 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004843 duplicate_name ? 0 : buf->b_fnum,
4844 ml_get_buf(buf,
4845 regmatch->startpos[0].lnum + lnum, FALSE),
4846 regmatch->startpos[0].lnum + lnum,
4847 regmatch->startpos[0].col + 1,
4848 FALSE, /* vis_col */
4849 NULL, /* search pattern */
4850 0, /* nr */
4851 0, /* type */
4852 TRUE /* valid */
4853 ) == FAIL)
4854 {
4855 got_int = TRUE;
4856 break;
4857 }
4858 found_match = TRUE;
4859 if (--tomatch == 0)
4860 break;
4861 if ((flags & VGR_GLOBAL) == 0
4862 || regmatch->endpos[0].lnum > 0)
4863 break;
4864 col = regmatch->endpos[0].col
4865 + (col == regmatch->endpos[0].col);
4866 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
4867 break;
4868 }
4869 line_breakcheck();
4870 if (got_int)
4871 break;
4872 }
4873
4874 return found_match;
4875}
4876
4877/*
4878 * Jump to the first match and update the directory.
4879 */
4880 static void
4881vgr_jump_to_match(
4882 qf_info_T *qi,
4883 int forceit,
4884 int *redraw_for_dummy,
4885 buf_T *first_match_buf,
4886 char_u *target_dir)
4887{
4888 buf_T *buf;
4889
4890 buf = curbuf;
4891 qf_jump(qi, 0, 0, forceit);
4892 if (buf != curbuf)
4893 /* If we jumped to another buffer redrawing will already be
4894 * taken care of. */
4895 *redraw_for_dummy = FALSE;
4896
4897 /* Jump to the directory used after loading the buffer. */
4898 if (curbuf == first_match_buf && target_dir != NULL)
4899 {
4900 exarg_T ea;
4901
4902 ea.arg = target_dir;
4903 ea.cmdidx = CMD_lcd;
4904 ex_cd(&ea);
4905 }
4906}
4907
4908/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004909 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00004910 * ":vimgrepadd {pattern} file(s)"
4911 * ":lvimgrep {pattern} file(s)"
4912 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00004913 */
4914 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004915ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004916{
Bram Moolenaar81695252004-12-29 20:58:21 +00004917 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004918 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004919 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004920 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01004921 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004922 char_u *s;
4923 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004924 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00004925 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01004926 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004927 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00004928 buf_T *buf;
4929 int duplicate_name = FALSE;
4930 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00004931 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00004932 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00004933 buf_T *first_match_buf = NULL;
4934 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00004935 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004936 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004937 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004938 char_u *dirname_start = NULL;
4939 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004940 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00004941 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004942
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004943 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01004944 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4945 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004946 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004947#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004948 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004949 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004950#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004951 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004952
Bram Moolenaar754b5602006-02-09 23:53:20 +00004953 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004954 || eap->cmdidx == CMD_lvimgrep
4955 || eap->cmdidx == CMD_lgrepadd
4956 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004957 {
4958 qi = ll_get_or_alloc_list(curwin);
4959 if (qi == NULL)
4960 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004961 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004962 }
4963
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004964 if (eap->addr_count > 0)
4965 tomatch = eap->line2;
4966 else
4967 tomatch = MAXLNUM;
4968
Bram Moolenaar81695252004-12-29 20:58:21 +00004969 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00004970 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004971 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00004972 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004973 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004974 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00004975 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00004976 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00004977 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01004978
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004979 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00004980 if (regmatch.regprog == NULL)
4981 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004982
4983 p = skipwhite(p);
4984 if (*p == NUL)
4985 {
4986 EMSG(_("E683: File name missing or invalid pattern"));
4987 goto theend;
4988 }
4989
Bram Moolenaar55b69262017-08-13 13:42:01 +02004990 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004991 && eap->cmdidx != CMD_vimgrepadd
4992 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004993 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004994 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004995 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00004996
4997 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02004998 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004999 goto theend;
5000 if (fcount == 0)
5001 {
5002 EMSG(_(e_nomatch));
5003 goto theend;
5004 }
5005
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005006 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5007 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005008 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005009 {
5010 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005011 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005012 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005013
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005014 /* Remember the current directory, because a BufRead autocommand that does
5015 * ":lcd %:p:h" changes the meaning of short path names. */
5016 mch_dirname(dirname_start, MAXPATHL);
5017
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005018 /* Remember the current quickfix list identifier, so that we can check for
5019 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005020 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005021
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005022 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005023 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005024 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005025 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005026 if (time(NULL) > seconds)
5027 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005028 /* Display the file name every second or so, show the user we are
5029 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005030 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005031 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005032 }
5033
Bram Moolenaar81695252004-12-29 20:58:21 +00005034 buf = buflist_findname_exp(fnames[fi]);
5035 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5036 {
5037 /* Remember that a buffer with this name already exists. */
5038 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005039 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005040 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005041
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005042 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005043 }
5044 else
5045 /* Use existing, loaded buffer. */
5046 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005047
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005048 /* Check whether the quickfix list is still valid. When loading a
5049 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005050 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005051 {
5052 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005053 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005054 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005055 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005056
Bram Moolenaar81695252004-12-29 20:58:21 +00005057 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005058 {
5059 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005060 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005061 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005062 else
5063 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005064 /* Try for a match in all lines of the buffer.
5065 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005066 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5067 tomatch, duplicate_name, flags);
5068
Bram Moolenaar81695252004-12-29 20:58:21 +00005069 if (using_dummy)
5070 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005071 if (found_match && first_match_buf == NULL)
5072 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005073 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005074 {
Bram Moolenaar81695252004-12-29 20:58:21 +00005075 /* Never keep a dummy buffer if there is another buffer
5076 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005077 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005078 buf = NULL;
5079 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005080 else if (!cmdmod.hide
5081 || buf->b_p_bh[0] == 'u' /* "unload" */
5082 || buf->b_p_bh[0] == 'w' /* "wipe" */
5083 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00005084 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005085 /* When no match was found we don't need to remember the
5086 * buffer, wipe it out. If there was a match and it
5087 * wasn't the first one or we won't jump there: only
5088 * unload the buffer.
5089 * Ignore 'hidden' here, because it may lead to having too
5090 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00005091 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005092 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005093 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005094 buf = NULL;
5095 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005096 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005097 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005098 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02005099 /* Keeping the buffer, remove the dummy flag. */
5100 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005101 buf = NULL;
5102 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005103 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005104
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005105 if (buf != NULL)
5106 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02005107 /* Keeping the buffer, remove the dummy flag. */
5108 buf->b_flags &= ~BF_DUMMY;
5109
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005110 /* If the buffer is still loaded we need to use the
5111 * directory we jumped to below. */
5112 if (buf == first_match_buf
5113 && target_dir == NULL
5114 && STRCMP(dirname_start, dirname_now) != 0)
5115 target_dir = vim_strsave(dirname_now);
5116
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005117 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005118 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00005119 * need to be done (again). But not the window-local
5120 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005121 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005122#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005123 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5124 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005125#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005126 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005127 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005128 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005129 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005130 }
5131 }
5132
5133 FreeWild(fcount, fnames);
5134
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005135 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5136 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5137 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005138 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005139
Bram Moolenaar864293a2016-06-02 13:40:04 +02005140 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005141
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005142 if (au_name != NULL)
5143 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5144 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005145 /*
5146 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5147 * is still valid.
5148 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005149 if (!qflist_valid(wp, save_qfid))
5150 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005151
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005152 // If autocommands changed the current list, then restore it
5153 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
5154 qi->qf_curlist = qf_id2nr(qi, save_qfid);
5155
Bram Moolenaar86b68352004-12-27 21:59:20 +00005156 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005157 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005158 {
5159 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005160 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5161 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005162 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005163 else
5164 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005165
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005166 /* If we loaded a dummy buffer into the current window, the autocommands
5167 * may have messed up things, need to redraw and recompute folds. */
5168 if (redraw_for_dummy)
5169 {
5170#ifdef FEAT_FOLDING
5171 foldUpdateAll(curwin);
5172#else
5173 redraw_later(NOT_VALID);
5174#endif
5175 }
5176
Bram Moolenaar86b68352004-12-27 21:59:20 +00005177theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005178 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005179 vim_free(dirname_now);
5180 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005181 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005182 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005183}
5184
5185/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005186 * Restore current working directory to "dirname_start" if they differ, taking
5187 * into account whether it is set locally or globally.
5188 */
5189 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005190restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005191{
5192 char_u *dirname_now = alloc(MAXPATHL);
5193
5194 if (NULL != dirname_now)
5195 {
5196 mch_dirname(dirname_now, MAXPATHL);
5197 if (STRCMP(dirname_start, dirname_now) != 0)
5198 {
5199 /* If the directory has changed, change it back by building up an
5200 * appropriate ex command and executing it. */
5201 exarg_T ea;
5202
5203 ea.arg = dirname_start;
5204 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5205 ex_cd(&ea);
5206 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005207 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005208 }
5209}
5210
5211/*
5212 * Load file "fname" into a dummy buffer and return the buffer pointer,
5213 * placing the directory resulting from the buffer load into the
5214 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5215 * prior to calling this function. Restores directory to "dirname_start" prior
5216 * to returning, if autocmds or the 'autochdir' option have changed it.
5217 *
5218 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5219 * or wipe_dummy_buffer() later!
5220 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005221 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005222 */
5223 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005224load_dummy_buffer(
5225 char_u *fname,
5226 char_u *dirname_start, /* in: old directory */
5227 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005228{
5229 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005230 bufref_T newbufref;
5231 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005232 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005233 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005234 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005235
5236 /* Allocate a buffer without putting it in the buffer list. */
5237 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5238 if (newbuf == NULL)
5239 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005240 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005241
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005242 /* Init the options. */
5243 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5244
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005245 /* need to open the memfile before putting the buffer in a window */
5246 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005247 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005248 /* Make sure this buffer isn't wiped out by autocommands. */
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005249 ++newbuf->b_locked;
5250
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005251 /* set curwin/curbuf to buf and save a few things */
5252 aucmd_prepbuf(&aco, newbuf);
5253
5254 /* Need to set the filename for autocommands. */
5255 (void)setfname(curbuf, fname, NULL, FALSE);
5256
Bram Moolenaar81695252004-12-29 20:58:21 +00005257 /* Create swap file now to avoid the ATTENTION message. */
5258 check_need_swap(TRUE);
5259
5260 /* Remove the "dummy" flag, otherwise autocommands may not
5261 * work. */
5262 curbuf->b_flags &= ~BF_DUMMY;
5263
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005264 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005265 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005266 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005267 NULL, READ_NEW | READ_DUMMY);
5268 --newbuf->b_locked;
5269 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005270 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005271 && !(curbuf->b_flags & BF_NEW))
5272 {
5273 failed = FALSE;
5274 if (curbuf != newbuf)
5275 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005276 /* Bloody autocommands changed the buffer! Can happen when
5277 * using netrw and editing a remote file. Use the current
5278 * buffer instead, delete the dummy one after restoring the
5279 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005280 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005281 newbuf = curbuf;
5282 }
5283 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005284
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005285 /* restore curwin/curbuf and a few other things */
5286 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005287 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5288 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005289
5290 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5291 * skip it. */
5292 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005293 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005294
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005295 /*
5296 * When autocommands/'autochdir' option changed directory: go back.
5297 * Let the caller know what the resulting dir was first, in case it is
5298 * important.
5299 */
5300 mch_dirname(resulting_dir, MAXPATHL);
5301 restore_start_dir(dirname_start);
5302
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005303 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005304 return NULL;
5305 if (failed)
5306 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005307 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005308 return NULL;
5309 }
5310 return newbuf;
5311}
5312
5313/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005314 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5315 * directory to "dirname_start" prior to returning, if autocmds or the
5316 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005317 */
5318 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005319wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005320{
5321 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005322 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005323#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005324 cleanup_T cs;
5325
5326 /* Reset the error/interrupt/exception state here so that aborting()
5327 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5328 * work when got_int is set. */
5329 enter_cleanup(&cs);
5330#endif
5331
Bram Moolenaar81695252004-12-29 20:58:21 +00005332 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005333
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005334#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005335 /* Restore the error/interrupt/exception state if not discarded by a
5336 * new aborting error, interrupt, or uncaught exception. */
5337 leave_cleanup(&cs);
5338#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005339 /* When autocommands/'autochdir' option changed directory: go back. */
5340 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005341 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005342}
5343
5344/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005345 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5346 * directory to "dirname_start" prior to returning, if autocmds or the
5347 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005348 */
5349 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005350unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005351{
5352 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005353 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005354 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005355
5356 /* When autocommands/'autochdir' option changed directory: go back. */
5357 restore_start_dir(dirname_start);
5358 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005359}
5360
Bram Moolenaar05159a02005-02-26 23:04:13 +00005361#if defined(FEAT_EVAL) || defined(PROTO)
5362/*
5363 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005364 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005365 */
5366 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005367get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005368{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005369 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005370 dict_T *dict;
5371 char_u buf[2];
5372 qfline_T *qfp;
5373 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005374 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005375
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005376 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005377 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005378 qi = &ql_info;
5379 if (wp != NULL)
5380 {
5381 qi = GET_LOC_LIST(wp);
5382 if (qi == NULL)
5383 return FAIL;
5384 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005385 }
5386
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005387 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005388 qf_idx = qi->qf_curlist;
5389
5390 if (qf_idx >= qi->qf_listcount
5391 || qi->qf_lists[qf_idx].qf_count == 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005392 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005393
Bram Moolenaard823fa92016-08-12 16:29:27 +02005394 qfp = qi->qf_lists[qf_idx].qf_start;
5395 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005396 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005397 /* Handle entries with a non-existing buffer number. */
5398 bufnum = qfp->qf_fnum;
5399 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5400 bufnum = 0;
5401
Bram Moolenaar05159a02005-02-26 23:04:13 +00005402 if ((dict = dict_alloc()) == NULL)
5403 return FAIL;
5404 if (list_append_dict(list, dict) == FAIL)
5405 return FAIL;
5406
5407 buf[0] = qfp->qf_type;
5408 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005409 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5410 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5411 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5412 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5413 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5414 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5415 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5416 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5417 || dict_add_string(dict, "type", buf) == FAIL
5418 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005419 return FAIL;
5420
5421 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005422 if (qfp == NULL)
5423 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005424 }
5425 return OK;
5426}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005427
5428/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005429 * Flags used by getqflist()/getloclist() to determine which fields to return.
5430 */
5431enum {
5432 QF_GETLIST_NONE = 0x0,
5433 QF_GETLIST_TITLE = 0x1,
5434 QF_GETLIST_ITEMS = 0x2,
5435 QF_GETLIST_NR = 0x4,
5436 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005437 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005438 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005439 QF_GETLIST_IDX = 0x40,
5440 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005441 QF_GETLIST_TICK = 0x100,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005442 QF_GETLIST_ALL = 0x1FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005443};
5444
5445/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005446 * Parse text from 'di' and return the quickfix list items.
5447 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005448 */
5449 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005450qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005451{
5452 int status = FAIL;
5453 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005454 char_u *errorformat = p_efm;
5455 dictitem_T *efm_di;
5456 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005457
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005458 /* Only a List value is supported */
5459 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005460 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005461 /* If errorformat is supplied then use it, otherwise use the 'efm'
5462 * option setting
5463 */
5464 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5465 {
5466 if (efm_di->di_tv.v_type != VAR_STRING ||
5467 efm_di->di_tv.vval.v_string == NULL)
5468 return FAIL;
5469 errorformat = efm_di->di_tv.vval.v_string;
5470 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005471
Bram Moolenaar36538222017-09-02 19:51:44 +02005472 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005473 if (l == NULL)
5474 return FAIL;
5475
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005476 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
5477 if (qi != NULL)
5478 {
5479 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
5480 qi->qf_refcount++;
5481
Bram Moolenaar36538222017-09-02 19:51:44 +02005482 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005483 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5484 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005485 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005486 qf_free(qi, 0);
5487 }
5488 free(qi);
5489 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005490 dict_add_list(retdict, "items", l);
5491 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005492 }
5493
5494 return status;
5495}
5496
5497/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005498 * Return the quickfix/location list window identifier in the current tabpage.
5499 */
5500 static int
5501qf_winid(qf_info_T *qi)
5502{
5503 win_T *win;
5504
5505 /* The quickfix window can be opened even if the quickfix list is not set
5506 * using ":copen". This is not true for location lists. */
5507 if (qi == NULL)
5508 return 0;
5509 win = qf_find_win(qi);
5510 if (win != NULL)
5511 return win->w_id;
5512 return 0;
5513}
5514
5515/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005516 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005517 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005518 static int
5519qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005520{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005521 int flags = QF_GETLIST_NONE;
5522
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005523 if (dict_find(what, (char_u *)"all", -1) != NULL)
5524 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005525
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005526 if (dict_find(what, (char_u *)"title", -1) != NULL)
5527 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005528
Bram Moolenaara6d48492017-12-12 22:45:31 +01005529 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5530 flags |= QF_GETLIST_NR;
5531
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005532 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5533 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005534
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005535 if (dict_find(what, (char_u *)"context", -1) != NULL)
5536 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005537
Bram Moolenaara6d48492017-12-12 22:45:31 +01005538 if (dict_find(what, (char_u *)"id", -1) != NULL)
5539 flags |= QF_GETLIST_ID;
5540
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005541 if (dict_find(what, (char_u *)"items", -1) != NULL)
5542 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005543
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005544 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5545 flags |= QF_GETLIST_IDX;
5546
5547 if (dict_find(what, (char_u *)"size", -1) != NULL)
5548 flags |= QF_GETLIST_SIZE;
5549
Bram Moolenaarb254af32017-12-18 19:48:58 +01005550 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5551 flags |= QF_GETLIST_TICK;
5552
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005553 return flags;
5554}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005555
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005556/*
5557 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5558 * If 'nr' and 'id' are not present in 'what' then return the current
5559 * quickfix list index.
5560 * If 'nr' is zero then return the current quickfix list index.
5561 * If 'nr' is '$' then return the last quickfix list index.
5562 * If 'id' is present then return the index of the quickfix list with that id.
5563 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5564 * Return -1, if quickfix list is not present or if the stack is empty.
5565 */
5566 static int
5567qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5568{
5569 int qf_idx;
5570 dictitem_T *di;
5571
5572 qf_idx = qi->qf_curlist; /* default is the current list */
5573 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5574 {
5575 /* Use the specified quickfix/location list */
5576 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005577 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005578 /* for zero use the current list */
5579 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005580 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005581 qf_idx = di->di_tv.vval.v_number - 1;
5582 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005583 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005584 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005585 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005586 else if (di->di_tv.v_type == VAR_STRING
5587 && di->di_tv.vval.v_string != NULL
5588 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5589 /* Get the last quickfix list number */
5590 qf_idx = qi->qf_listcount - 1;
5591 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005592 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005593 }
5594
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005595 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5596 {
5597 /* Look for a list with the specified id */
5598 if (di->di_tv.v_type == VAR_NUMBER)
5599 {
5600 /*
5601 * For zero, use the current list or the list specified by 'nr'
5602 */
5603 if (di->di_tv.vval.v_number != 0)
5604 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5605 }
5606 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005607 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005608 }
5609
5610 return qf_idx;
5611}
5612
5613/*
5614 * Return default values for quickfix list properties in retdict.
5615 */
5616 static int
5617qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5618{
5619 int status = OK;
5620
5621 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005622 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005623 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5624 {
5625 list_T *l = list_alloc();
5626 if (l != NULL)
5627 status = dict_add_list(retdict, "items", l);
5628 else
5629 status = FAIL;
5630 }
5631 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005632 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005633 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005634 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005635 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005636 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005637 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005638 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005639 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005640 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005641 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005642 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005643 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005644 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005645
5646 return status;
5647}
5648
5649/*
5650 * Return the quickfix list title as 'title' in retdict
5651 */
5652 static int
5653qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5654{
Bram Moolenaare0be1672018-07-08 16:50:37 +02005655 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005656}
5657
5658/*
5659 * Return the quickfix list items/entries as 'items' in retdict
5660 */
5661 static int
5662qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5663{
5664 int status = OK;
5665 list_T *l = list_alloc();
5666 if (l != NULL)
5667 {
5668 (void)get_errorlist(qi, NULL, qf_idx, l);
5669 dict_add_list(retdict, "items", l);
5670 }
5671 else
5672 status = FAIL;
5673
5674 return status;
5675}
5676
5677/*
5678 * Return the quickfix list context (if any) as 'context' in retdict.
5679 */
5680 static int
5681qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5682{
5683 int status;
5684 dictitem_T *di;
5685
5686 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5687 {
5688 di = dictitem_alloc((char_u *)"context");
5689 if (di != NULL)
5690 {
5691 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5692 status = dict_add(retdict, di);
5693 if (status == FAIL)
5694 dictitem_free(di);
5695 }
5696 else
5697 status = FAIL;
5698 }
5699 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02005700 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005701
5702 return status;
5703}
5704
5705/*
5706 * Return the quickfix list index as 'idx' in retdict
5707 */
5708 static int
5709qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5710{
5711 int idx = qi->qf_lists[qf_idx].qf_index;
5712 if (qi->qf_lists[qf_idx].qf_count == 0)
5713 /* For empty lists, qf_index is set to 1 */
5714 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005715 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005716}
5717
5718/*
5719 * Return quickfix/location list details (title) as a
5720 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5721 * then current list is used. Otherwise the specified list is used.
5722 */
5723 int
5724qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5725{
5726 qf_info_T *qi = &ql_info;
5727 int status = OK;
5728 int qf_idx;
5729 dictitem_T *di;
5730 int flags = QF_GETLIST_NONE;
5731
5732 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5733 return qf_get_list_from_lines(what, di, retdict);
5734
5735 if (wp != NULL)
5736 qi = GET_LOC_LIST(wp);
5737
5738 flags = qf_getprop_keys2flags(what);
5739
5740 if (qi != NULL && qi->qf_listcount != 0)
5741 qf_idx = qf_getprop_qfidx(qi, what);
5742
Bram Moolenaara6d48492017-12-12 22:45:31 +01005743 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005744 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005745 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005746
Bram Moolenaard823fa92016-08-12 16:29:27 +02005747 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005748 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005749 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005750 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005751 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005752 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005753 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005754 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005755 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005756 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005757 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005758 status = dict_add_number(retdict, "id", qi->qf_lists[qf_idx].qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005759 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005760 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005761 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005762 status = dict_add_number(retdict, "size",
5763 qi->qf_lists[qf_idx].qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005764 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005765 status = dict_add_number(retdict, "changedtick",
5766 qi->qf_lists[qf_idx].qf_changedtick);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005767
Bram Moolenaard823fa92016-08-12 16:29:27 +02005768 return status;
5769}
5770
5771/*
5772 * Add list of entries to quickfix/location list. Each list entry is
5773 * a dictionary with item information.
5774 */
5775 static int
5776qf_add_entries(
5777 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005778 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005779 list_T *list,
5780 char_u *title,
5781 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005782{
5783 listitem_T *li;
5784 dict_T *d;
Bram Moolenaard76ce852018-05-01 15:02:04 +02005785 char_u *filename, *module, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005786 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005787 long lnum;
5788 int col, nr;
5789 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005790 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005791 int valid, status;
5792 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005793 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005794
Bram Moolenaara3921f42017-06-04 15:30:34 +02005795 if (action == ' ' || qf_idx == qi->qf_listcount)
5796 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005797 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01005798 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005799 qf_idx = qi->qf_curlist;
5800 }
Bram Moolenaara3921f42017-06-04 15:30:34 +02005801 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005802 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005803 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005804 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02005805 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005806 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02005807 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02005808 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005809
5810 for (li = list->lv_first; li != NULL; li = li->li_next)
5811 {
5812 if (li->li_tv.v_type != VAR_DICT)
5813 continue; /* Skip non-dict items */
5814
5815 d = li->li_tv.vval.v_dict;
5816 if (d == NULL)
5817 continue;
5818
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005819 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaard76ce852018-05-01 15:02:04 +02005820 module = get_dict_string(d, (char_u *)"module", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005821 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
5822 lnum = (int)get_dict_number(d, (char_u *)"lnum");
5823 col = (int)get_dict_number(d, (char_u *)"col");
5824 vcol = (int)get_dict_number(d, (char_u *)"vcol");
5825 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005826 type = get_dict_string(d, (char_u *)"type", TRUE);
5827 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
5828 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005829 if (text == NULL)
5830 text = vim_strsave((char_u *)"");
5831
5832 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005833 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005834 valid = FALSE;
5835
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005836 /* Mark entries with non-existing buffer number as not valid. Give the
5837 * error message only once. */
5838 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5839 {
5840 if (!did_bufnr_emsg)
5841 {
5842 did_bufnr_emsg = TRUE;
5843 EMSGN(_("E92: Buffer %ld not found"), bufnum);
5844 }
5845 valid = FALSE;
5846 bufnum = 0;
5847 }
5848
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02005849 /* If the 'valid' field is present it overrules the detected value. */
5850 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
5851 valid = (int)get_dict_number(d, (char_u *)"valid");
5852
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005853 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005854 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005855 NULL, /* dir */
5856 filename,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005857 module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005858 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005859 text,
5860 lnum,
5861 col,
5862 vcol, /* vis_col */
5863 pattern, /* search pattern */
5864 nr,
5865 type == NULL ? NUL : *type,
5866 valid);
5867
5868 vim_free(filename);
Bram Moolenaard76ce852018-05-01 15:02:04 +02005869 vim_free(module);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005870 vim_free(pattern);
5871 vim_free(text);
5872 vim_free(type);
5873
5874 if (status == FAIL)
5875 {
5876 retval = FAIL;
5877 break;
5878 }
5879 }
5880
Bram Moolenaara3921f42017-06-04 15:30:34 +02005881 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02005882 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02005883 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02005884 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02005885 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02005886 if (action != 'a')
5887 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02005888 qi->qf_lists[qf_idx].qf_ptr =
5889 qi->qf_lists[qf_idx].qf_start;
5890 if (qi->qf_lists[qf_idx].qf_count > 0)
5891 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005892 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005893
Bram Moolenaarc1808d52016-04-18 20:04:00 +02005894 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02005895 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005896
5897 return retval;
5898}
Bram Moolenaard823fa92016-08-12 16:29:27 +02005899
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005900/*
5901 * Get the quickfix list index from 'nr' or 'id'
5902 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02005903 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005904qf_setprop_get_qfidx(
5905 qf_info_T *qi,
5906 dict_T *what,
5907 int action,
5908 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005909{
5910 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005911 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005912
Bram Moolenaard823fa92016-08-12 16:29:27 +02005913 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5914 {
5915 /* Use the specified quickfix/location list */
5916 if (di->di_tv.v_type == VAR_NUMBER)
5917 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02005918 /* for zero use the current list */
5919 if (di->di_tv.vval.v_number != 0)
5920 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005921
Bram Moolenaar55b69262017-08-13 13:42:01 +02005922 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
5923 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005924 /*
5925 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02005926 * non-available list and add the new list at the end of the
5927 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005928 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005929 *newlist = TRUE;
5930 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005931 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005932 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005933 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02005934 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005935 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02005936 }
5937 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005938 && di->di_tv.vval.v_string != NULL
5939 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005940 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02005941 if (qi->qf_listcount > 0)
5942 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005943 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02005944 qf_idx = 0;
5945 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005946 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005947 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005948 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005949 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02005950 }
5951
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005952 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005953 {
5954 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005955 if (di->di_tv.v_type != VAR_NUMBER)
5956 return INVALID_QFIDX;
5957
5958 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005959 }
5960
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005961 return qf_idx;
5962}
5963
5964/*
5965 * Set the quickfix list title.
5966 */
5967 static int
5968qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
5969{
5970 if (di->di_tv.v_type != VAR_STRING)
5971 return FAIL;
5972
5973 vim_free(qi->qf_lists[qf_idx].qf_title);
5974 qi->qf_lists[qf_idx].qf_title =
5975 get_dict_string(what, (char_u *)"title", TRUE);
5976 if (qf_idx == qi->qf_curlist)
5977 qf_update_win_titlevar(qi);
5978
5979 return OK;
5980}
5981
5982/*
5983 * Set quickfix list items/entries.
5984 */
5985 static int
5986qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
5987{
5988 int retval = FAIL;
5989 char_u *title_save;
5990
5991 if (di->di_tv.v_type != VAR_LIST)
5992 return FAIL;
5993
5994 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
5995 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
5996 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005997 vim_free(title_save);
5998
5999 return retval;
6000}
6001
6002/*
6003 * Set quickfix list items/entries from a list of lines.
6004 */
6005 static int
6006qf_setprop_items_from_lines(
6007 qf_info_T *qi,
6008 int qf_idx,
6009 dict_T *what,
6010 dictitem_T *di,
6011 int action)
6012{
6013 char_u *errorformat = p_efm;
6014 dictitem_T *efm_di;
6015 int retval = FAIL;
6016
6017 /* Use the user supplied errorformat settings (if present) */
6018 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6019 {
6020 if (efm_di->di_tv.v_type != VAR_STRING ||
6021 efm_di->di_tv.vval.v_string == NULL)
6022 return FAIL;
6023 errorformat = efm_di->di_tv.vval.v_string;
6024 }
6025
6026 /* Only a List value is supported */
6027 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6028 return FAIL;
6029
6030 if (action == 'r')
6031 qf_free_items(qi, qf_idx);
6032 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6033 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6034 retval = OK;
6035
6036 return retval;
6037}
6038
6039/*
6040 * Set quickfix list context.
6041 */
6042 static int
6043qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di)
6044{
6045 typval_T *ctx;
6046
6047 free_tv(qi->qf_lists[qf_idx].qf_ctx);
6048 ctx = alloc_tv();
6049 if (ctx != NULL)
6050 copy_tv(&di->di_tv, ctx);
6051 qi->qf_lists[qf_idx].qf_ctx = ctx;
6052
6053 return OK;
6054}
6055
6056/*
6057 * Set quickfix/location list properties (title, items, context).
6058 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006059 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006060 */
6061 static int
6062qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6063{
6064 dictitem_T *di;
6065 int retval = FAIL;
6066 int qf_idx;
6067 int newlist = FALSE;
6068
6069 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
6070 newlist = TRUE;
6071
6072 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6073 if (qf_idx == INVALID_QFIDX) /* List not found */
6074 return FAIL;
6075
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006076 if (newlist)
6077 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006078 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006079 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006080 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006081 }
6082
6083 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006084 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006085 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006086 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006087 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006088 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006089 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006090 retval = qf_setprop_context(qi, qf_idx, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006091
Bram Moolenaarb254af32017-12-18 19:48:58 +01006092 if (retval == OK)
6093 qf_list_changed(qi, qf_idx);
6094
Bram Moolenaard823fa92016-08-12 16:29:27 +02006095 return retval;
6096}
6097
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006098/*
6099 * Find the non-location list window with the specified location list.
6100 */
6101 static win_T *
6102find_win_with_ll(qf_info_T *qi)
6103{
6104 win_T *wp = NULL;
6105
6106 FOR_ALL_WINDOWS(wp)
6107 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6108 return wp;
6109
6110 return NULL;
6111}
6112
6113/*
6114 * Free the entire quickfix/location list stack.
6115 * If the quickfix/location list window is open, then clear it.
6116 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006117 static void
6118qf_free_stack(win_T *wp, qf_info_T *qi)
6119{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006120 win_T *qfwin = qf_find_win(qi);
6121 win_T *llwin = NULL;
6122 win_T *orig_wp = wp;
6123
6124 if (qfwin != NULL)
6125 {
6126 /* If the quickfix/location list window is open, then clear it */
6127 if (qi->qf_curlist < qi->qf_listcount)
6128 qf_free(qi, qi->qf_curlist);
6129 qf_update_buffer(qi, NULL);
6130 }
6131
6132 if (wp != NULL && IS_LL_WINDOW(wp))
6133 {
6134 /* If in the location list window, then use the non-location list
6135 * window with this location list (if present)
6136 */
6137 llwin = find_win_with_ll(qi);
6138 if (llwin != NULL)
6139 wp = llwin;
6140 }
6141
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006142 qf_free_all(wp);
6143 if (wp == NULL)
6144 {
6145 /* quickfix list */
6146 qi->qf_curlist = 0;
6147 qi->qf_listcount = 0;
6148 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006149 else if (IS_LL_WINDOW(orig_wp))
6150 {
6151 /* If the location list window is open, then create a new empty
6152 * location list */
6153 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006154
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006155 /* first free the list reference in the location list window */
6156 ll_free_all(&orig_wp->w_llist_ref);
6157
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006158 orig_wp->w_llist_ref = new_ll;
6159 if (llwin != NULL)
6160 {
6161 llwin->w_llist = new_ll;
6162 new_ll->qf_refcount++;
6163 }
6164 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006165}
6166
Bram Moolenaard823fa92016-08-12 16:29:27 +02006167/*
6168 * Populate the quickfix list with the items supplied in the list
6169 * of dictionaries. "title" will be copied to w:quickfix_title.
6170 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6171 */
6172 int
6173set_errorlist(
6174 win_T *wp,
6175 list_T *list,
6176 int action,
6177 char_u *title,
6178 dict_T *what)
6179{
6180 qf_info_T *qi = &ql_info;
6181 int retval = OK;
6182
6183 if (wp != NULL)
6184 {
6185 qi = ll_get_or_alloc_list(wp);
6186 if (qi == NULL)
6187 return FAIL;
6188 }
6189
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006190 if (action == 'f')
6191 {
6192 /* Free the entire quickfix or location list stack */
6193 qf_free_stack(wp, qi);
6194 }
6195 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006196 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006197 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006198 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006199 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006200 if (retval == OK)
6201 qf_list_changed(qi, qi->qf_curlist);
6202 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006203
6204 return retval;
6205}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006206
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006207/*
6208 * Mark the context as in use for all the lists in a quickfix stack.
6209 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006210 static int
6211mark_quickfix_ctx(qf_info_T *qi, int copyID)
6212{
6213 int i;
6214 int abort = FALSE;
6215 typval_T *ctx;
6216
6217 for (i = 0; i < LISTCOUNT && !abort; ++i)
6218 {
6219 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006220 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6221 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006222 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6223 }
6224
6225 return abort;
6226}
6227
6228/*
6229 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006230 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006231 */
6232 int
6233set_ref_in_quickfix(int copyID)
6234{
6235 int abort = FALSE;
6236 tabpage_T *tp;
6237 win_T *win;
6238
6239 abort = mark_quickfix_ctx(&ql_info, copyID);
6240 if (abort)
6241 return abort;
6242
6243 FOR_ALL_TAB_WINDOWS(tp, win)
6244 {
6245 if (win->w_llist != NULL)
6246 {
6247 abort = mark_quickfix_ctx(win->w_llist, copyID);
6248 if (abort)
6249 return abort;
6250 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006251 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6252 {
6253 /* In a location list window and none of the other windows is
6254 * referring to this location list. Mark the location list
6255 * context as still in use.
6256 */
6257 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6258 if (abort)
6259 return abort;
6260 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006261 }
6262
6263 return abort;
6264}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006265#endif
6266
Bram Moolenaar81695252004-12-29 20:58:21 +00006267/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006268 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006269 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006270 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006271 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006272 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006273 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006274 */
6275 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006276ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006277{
6278 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006279 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006280 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006281 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006282 int_u save_qfid;
6283 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006284
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006285 switch (eap->cmdidx)
6286 {
6287 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6288 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6289 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6290 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6291 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6292 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6293 default: break;
6294 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006295 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6296 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006297 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006298#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006299 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006300 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006301#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006302 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006303
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006304 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01006305 if (eap->cmdidx == CMD_lbuffer
6306 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006307 || eap->cmdidx == CMD_laddbuffer)
6308 {
6309 qi = ll_get_or_alloc_list(curwin);
6310 if (qi == NULL)
6311 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006312 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006313 }
6314
Bram Moolenaar86b68352004-12-27 21:59:20 +00006315 if (*eap->arg == NUL)
6316 buf = curbuf;
6317 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6318 buf = buflist_findnr(atoi((char *)eap->arg));
6319 if (buf == NULL)
6320 EMSG(_(e_invarg));
6321 else if (buf->b_ml.ml_mfp == NULL)
6322 EMSG(_("E681: Buffer is not loaded"));
6323 else
6324 {
6325 if (eap->addr_count == 0)
6326 {
6327 eap->line1 = 1;
6328 eap->line2 = buf->b_ml.ml_line_count;
6329 }
6330 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6331 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6332 EMSG(_(e_invrange));
6333 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006334 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006335 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006336
6337 if (buf->b_sfname)
6338 {
6339 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6340 (char *)qf_title, (char *)buf->b_sfname);
6341 qf_title = IObuff;
6342 }
6343
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006344 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006345 (eap->cmdidx != CMD_caddbuffer
6346 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006347 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006348 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006349 if (res >= 0)
6350 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006351
6352 // Remember the current quickfix list identifier, so that we can
6353 // check for autocommands changing the current quickfix list.
6354 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006355 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006356 {
6357 buf_T *curbuf_old = curbuf;
6358
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006359 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6360 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006361 if (curbuf != curbuf_old)
6362 // Autocommands changed buffer, don't jump now, "qi" may
6363 // be invalid.
6364 res = 0;
6365 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006366 // Jump to the first error for a new list and if autocmds didn't
6367 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006368 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006369 eap->cmdidx == CMD_lbuffer)
6370 && qflist_valid(wp, save_qfid))
6371 {
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006372 // display the first error
6373 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006374 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00006375 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006376 }
6377}
6378
Bram Moolenaar1e015462005-09-25 22:16:38 +00006379#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006380/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006381 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6382 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006383 */
6384 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006385ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006386{
6387 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006388 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006389 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006390 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006391 int_u save_qfid;
6392 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006393
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006394 switch (eap->cmdidx)
6395 {
6396 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6397 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6398 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6399 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6400 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6401 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6402 default: break;
6403 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006404 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6405 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006406 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006407#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006408 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006409 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006410#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006411 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006412
Bram Moolenaar3c097222017-12-21 20:54:49 +01006413 if (eap->cmdidx == CMD_lexpr
6414 || eap->cmdidx == CMD_lgetexpr
6415 || eap->cmdidx == CMD_laddexpr)
6416 {
6417 qi = ll_get_or_alloc_list(curwin);
6418 if (qi == NULL)
6419 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006420 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006421 }
6422
Bram Moolenaar4770d092006-01-12 23:22:24 +00006423 /* Evaluate the expression. When the result is a string or a list we can
6424 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006425 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006426 if (tv != NULL)
6427 {
6428 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6429 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6430 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006431 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006432 (eap->cmdidx != CMD_caddexpr
6433 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006434 (linenr_T)0, (linenr_T)0,
6435 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006436 if (res >= 0)
6437 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006438
6439 // Remember the current quickfix list identifier, so that we can
6440 // check for autocommands changing the current quickfix list.
6441 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006442 if (au_name != NULL)
6443 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6444 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006445
6446 // Jump to the first error for a new list and if autocmds didn't
6447 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006448 if (res > 0 && (eap->cmdidx == CMD_cexpr
6449 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006450 && qflist_valid(wp, save_qfid))
6451 {
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006452 // display the first error
6453 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006454 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006455 }
6456 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006457 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006458 free_tv(tv);
6459 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006460}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006461#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006462
6463/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006464 * Get the location list for ":lhelpgrep"
6465 */
6466 static qf_info_T *
6467hgr_get_ll(int *new_ll)
6468{
6469 win_T *wp;
6470 qf_info_T *qi;
6471
6472 /* If the current window is a help window, then use it */
6473 if (bt_help(curwin->w_buffer))
6474 wp = curwin;
6475 else
6476 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006477 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006478
6479 if (wp == NULL) /* Help window not found */
6480 qi = NULL;
6481 else
6482 qi = wp->w_llist;
6483
6484 if (qi == NULL)
6485 {
6486 /* Allocate a new location list for help text matches */
6487 if ((qi = ll_new_list()) == NULL)
6488 return NULL;
6489 *new_ll = TRUE;
6490 }
6491
6492 return qi;
6493}
6494
6495/*
6496 * Search for a pattern in a help file.
6497 */
6498 static void
6499hgr_search_file(
6500 qf_info_T *qi,
6501 char_u *fname,
6502#ifdef FEAT_MBYTE
6503 vimconv_T *p_vc,
6504#endif
6505 regmatch_T *p_regmatch)
6506{
6507 FILE *fd;
6508 long lnum;
6509
6510 fd = mch_fopen((char *)fname, "r");
6511 if (fd == NULL)
6512 return;
6513
6514 lnum = 1;
6515 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6516 {
6517 char_u *line = IObuff;
6518#ifdef FEAT_MBYTE
6519 /* Convert a line if 'encoding' is not utf-8 and
6520 * the line contains a non-ASCII character. */
6521 if (p_vc->vc_type != CONV_NONE
6522 && has_non_ascii(IObuff))
6523 {
6524 line = string_convert(p_vc, IObuff, NULL);
6525 if (line == NULL)
6526 line = IObuff;
6527 }
6528#endif
6529
6530 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6531 {
6532 int l = (int)STRLEN(line);
6533
6534 /* remove trailing CR, LF, spaces, etc. */
6535 while (l > 0 && line[l - 1] <= ' ')
6536 line[--l] = NUL;
6537
6538 if (qf_add_entry(qi,
6539 qi->qf_curlist,
6540 NULL, /* dir */
6541 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006542 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006543 0,
6544 line,
6545 lnum,
6546 (int)(p_regmatch->startp[0] - line)
6547 + 1, /* col */
6548 FALSE, /* vis_col */
6549 NULL, /* search pattern */
6550 0, /* nr */
6551 1, /* type */
6552 TRUE /* valid */
6553 ) == FAIL)
6554 {
6555 got_int = TRUE;
6556#ifdef FEAT_MBYTE
6557 if (line != IObuff)
6558 vim_free(line);
6559#endif
6560 break;
6561 }
6562 }
6563#ifdef FEAT_MBYTE
6564 if (line != IObuff)
6565 vim_free(line);
6566#endif
6567 ++lnum;
6568 line_breakcheck();
6569 }
6570 fclose(fd);
6571}
6572
6573/*
6574 * Search for a pattern in all the help files in the doc directory under
6575 * the given directory.
6576 */
6577 static void
6578hgr_search_files_in_dir(
6579 qf_info_T *qi,
6580 char_u *dirname,
6581 regmatch_T *p_regmatch
6582#ifdef FEAT_MBYTE
6583 , vimconv_T *p_vc
6584#endif
6585#ifdef FEAT_MULTI_LANG
6586 , char_u *lang
6587#endif
6588 )
6589{
6590 int fcount;
6591 char_u **fnames;
6592 int fi;
6593
6594 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6595 add_pathsep(dirname);
6596 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6597 if (gen_expand_wildcards(1, &dirname, &fcount,
6598 &fnames, EW_FILE|EW_SILENT) == OK
6599 && fcount > 0)
6600 {
6601 for (fi = 0; fi < fcount && !got_int; ++fi)
6602 {
6603#ifdef FEAT_MULTI_LANG
6604 /* Skip files for a different language. */
6605 if (lang != NULL
6606 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006607 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006608 && !(STRNICMP(lang, "en", 2) == 0
6609 && STRNICMP("txt", fnames[fi]
6610 + STRLEN(fnames[fi]) - 3, 3) == 0))
6611 continue;
6612#endif
6613
6614 hgr_search_file(qi, fnames[fi],
6615#ifdef FEAT_MBYTE
6616 p_vc,
6617#endif
6618 p_regmatch);
6619 }
6620 FreeWild(fcount, fnames);
6621 }
6622}
6623
6624/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006625 * Search for a pattern in all the help files in the 'runtimepath'
6626 * and add the matches to a quickfix list.
6627 * 'arg' is the language specifier. If supplied, then only matches in the
6628 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006629 */
6630 static void
6631hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *arg)
6632{
6633 char_u *p;
6634#ifdef FEAT_MULTI_LANG
6635 char_u *lang;
6636#endif
6637
6638#ifdef FEAT_MBYTE
6639 vimconv_T vc;
6640
6641 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6642 * differs. */
6643 vc.vc_type = CONV_NONE;
6644 if (!enc_utf8)
6645 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6646#endif
6647
6648#ifdef FEAT_MULTI_LANG
6649 /* Check for a specified language */
6650 lang = check_help_lang(arg);
6651#endif
6652
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006653 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006654 p = p_rtp;
6655 while (*p != NUL && !got_int)
6656 {
6657 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6658
6659 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6660#ifdef FEAT_MBYTE
6661 , &vc
6662#endif
6663#ifdef FEAT_MULTI_LANG
6664 , lang
6665#endif
6666 );
6667 }
6668
6669#ifdef FEAT_MBYTE
6670 if (vc.vc_type != CONV_NONE)
6671 convert_setup(&vc, NULL, NULL);
6672#endif
6673}
6674
6675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 * ":helpgrep {pattern}"
6677 */
6678 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006679ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
6681 regmatch_T regmatch;
6682 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006683 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006684 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006685 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686
Bram Moolenaar73633f82012-01-20 13:39:07 +01006687 switch (eap->cmdidx)
6688 {
6689 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6690 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6691 default: break;
6692 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006693 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6694 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006695 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006696#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006697 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006698 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006699#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006700 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006701
6702 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6703 save_cpo = p_cpo;
6704 p_cpo = empty_option;
6705
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006706 if (eap->cmdidx == CMD_lhelpgrep)
6707 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006708 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006709 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006710 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006711 }
6712
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6714 regmatch.rm_ic = FALSE;
6715 if (regmatch.regprog != NULL)
6716 {
6717 /* create a new quickfix list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006718 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006720 hgr_search_in_rtp(qi, &regmatch, eap->arg);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006721
Bram Moolenaar473de612013-06-08 18:19:48 +02006722 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006724 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6725 qi->qf_lists[qi->qf_curlist].qf_ptr =
6726 qi->qf_lists[qi->qf_curlist].qf_start;
6727 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 }
6729
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006730 if (p_cpo == empty_option)
6731 p_cpo = save_cpo;
6732 else
6733 /* Darn, some plugin changed the value. */
6734 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
Bram Moolenaarb254af32017-12-18 19:48:58 +01006736 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006737 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738
Bram Moolenaar73633f82012-01-20 13:39:07 +01006739 if (au_name != NULL)
6740 {
6741 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6742 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02006743 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006744 /* autocommands made "qi" invalid */
6745 return;
6746 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 /* Jump to first match. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006749 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006750 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006751 else
6752 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006753
6754 if (eap->cmdidx == CMD_lhelpgrep)
6755 {
6756 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006757 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006758 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006759 {
6760 if (new_qi)
6761 ll_free_all(&qi);
6762 }
6763 else if (curwin->w_llist == NULL)
6764 curwin->w_llist = qi;
6765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766}
6767
6768#endif /* FEAT_QUICKFIX */