blob: 30f9bdeb2d93b7285119e655825880d77df3090a [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 Moolenaarfe15b7d2018-09-18 22:50:06 +0200139static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static 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 Moolenaarfe15b7d2018-09-18 22:50:06 +0200144static char_u *qf_guess_filepath(qf_list_T *qfl, 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 Moolenaar864293a2016-06-02 13:40:04 +0200150static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
152static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
153static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
154static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000156/* Quickfix window check helper macro */
157#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
158/* Location list window check helper macro */
159#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200160
161// Quickfix and location list stack check helper macros
162#define IS_QF_STACK(qi) (qi == &ql_info)
163#define IS_LL_STACK(qi) (qi != &ql_info)
164
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000165/*
166 * Return location list for window 'wp'
167 * For location list window, return the referenced location list
168 */
169#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
170
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200172 * Looking up a buffer can be slow if there are many. Remember the last one
173 * to make this a lot faster if there are multiple matches in the same file.
174 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200175static char_u *qf_last_bufname = NULL;
176static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200177
Bram Moolenaar3c097222017-12-21 20:54:49 +0100178static char *e_loc_list_changed =
179 N_("E926: Current location list was changed");
180
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200181/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200182 * Maximum number of bytes allowed per line while reading a errorfile.
183 */
184#define LINE_MAXLEN 4096
185
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200186static struct fmtpattern
187{
188 char_u convchar;
189 char *pattern;
190} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200191 {
192 {'f', ".\\+"}, /* only used when at end */
193 {'n', "\\d\\+"},
194 {'l', "\\d\\+"},
195 {'c', "\\d\\+"},
196 {'t', "."},
197 {'m', ".\\+"},
198 {'r', ".*"},
199 {'p', "[- .]*"},
200 {'v', "\\d\\+"},
201 {'s', ".\\+"},
202 {'o', ".\\+"}
203 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200204
205/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200206 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200207 * See fmt_pat definition above for the list of supported patterns. The
208 * pattern specifier is supplied in "efmpat". The converted pattern is stored
209 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200210 */
211 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200212efmpat_to_regpat(
213 char_u *efmpat,
214 char_u *regpat,
215 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200216 int idx,
217 int round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200218 char_u *errmsg)
219{
220 char_u *srcptr;
221
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200222 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200223 {
224 /* Each errorformat pattern can occur only once */
225 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200226 _("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200227 EMSG(errmsg);
228 return NULL;
229 }
230 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200231 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200232 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200233 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200234 {
235 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200236 _("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200237 EMSG(errmsg);
238 return NULL;
239 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200240 efminfo->addr[idx] = (char_u)++round;
241 *regpat++ = '\\';
242 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200243#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200244 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200245 {
246 /* Also match "c:" in the file name, even when
247 * checking for a colon next: "%f:".
248 * "\%(\a:\)\=" */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200249 STRCPY(regpat, "\\%(\\a:\\)\\=");
250 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200251 }
252#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200253 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200255 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200256 {
257 /* A file name may contain spaces, but this isn't
258 * in "\f". For "%f:%l:%m" there may be a ":" in
259 * the file name. Use ".\{-1,}x" instead (x is
260 * the next character), the requirement that :999:
261 * follows should work. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200262 STRCPY(regpat, ".\\{-1,}");
263 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200264 }
265 else
266 {
267 /* File name followed by '\\' or '%': include as
268 * many file name chars as possible. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200269 STRCPY(regpat, "\\f\\+");
270 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200271 }
272 }
273 else
274 {
275 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200276 while ((*regpat = *srcptr++) != NUL)
277 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200278 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200279 *regpat++ = '\\';
280 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200282 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200283}
284
285/*
286 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200287 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200288 */
289 static char_u *
290scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200291 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200292 char_u *efm,
293 int len,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200294 char_u *regpat,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200295 char_u *errmsg)
296{
297 char_u *efmp = *pefmp;
298
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200299 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200300 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200301 if ((*regpat++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200302 {
303 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200304 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200305 if (efmp < efm + len)
306 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200307 *regpat++ = *++efmp; /* could be ']' */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200308 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200309 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200310 /* skip */;
311 if (efmp == efm + len)
312 {
313 EMSG(_("E374: Missing ] in format string"));
314 return NULL;
315 }
316 }
317 }
318 else if (efmp < efm + len) /* %*\D, %*\s etc. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200319 *regpat++ = *++efmp;
320 *regpat++ = '\\';
321 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200322 }
323 else
324 {
325 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
326 sprintf((char *)errmsg,
327 _("E375: Unsupported %%%c in format string"), *efmp);
328 EMSG(errmsg);
329 return NULL;
330 }
331
332 *pefmp = efmp;
333
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200334 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200335}
336
337/*
338 * Analyze/parse an errorformat prefix.
339 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200340 static char_u *
341efm_analyze_prefix(char_u *efmp, efm_T *efminfo, char_u *errmsg)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200342{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200343 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200344 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200345 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200346 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200347 else
348 {
349 sprintf((char *)errmsg,
350 _("E376: Invalid %%%c in format string prefix"), *efmp);
351 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200352 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200353 }
354
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200355 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200356}
357
358/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200359 * Converts a 'errorformat' string part in 'efm' to a regular expression
360 * pattern. The resulting regex pattern is returned in "regpat". Additional
361 * information about the 'erroformat' pattern is returned in "fmt_ptr".
362 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200363 */
364 static int
365efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200366 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200367 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200368 efm_T *fmt_ptr,
369 char_u *regpat,
370 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200371{
372 char_u *ptr;
373 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200374 int round;
375 int idx = 0;
376
377 /*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200378 * Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200379 */
380 ptr = regpat;
381 *ptr++ = '^';
382 round = 0;
383 for (efmp = efm; efmp < efm + len; ++efmp)
384 {
385 if (*efmp == '%')
386 {
387 ++efmp;
388 for (idx = 0; idx < FMT_PATTERNS; ++idx)
389 if (fmt_pat[idx].convchar == *efmp)
390 break;
391 if (idx < FMT_PATTERNS)
392 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200393 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200394 errmsg);
395 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200396 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200397 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200398 }
399 else if (*efmp == '*')
400 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200401 ++efmp;
402 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr, errmsg);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200403 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200404 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200405 }
406 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
407 *ptr++ = *efmp; /* regexp magic characters */
408 else if (*efmp == '#')
409 *ptr++ = '*';
410 else if (*efmp == '>')
411 fmt_ptr->conthere = TRUE;
412 else if (efmp == efm + 1) /* analyse prefix */
413 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200414 /*
415 * prefix is allowed only at the beginning of the errorformat
416 * option part
417 */
418 efmp = efm_analyze_prefix(efmp, fmt_ptr, errmsg);
419 if (efmp == NULL)
420 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200421 }
422 else
423 {
424 sprintf((char *)errmsg,
425 _("E377: Invalid %%%c in format string"), *efmp);
426 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200427 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200428 }
429 }
430 else /* copy normal character */
431 {
432 if (*efmp == '\\' && efmp + 1 < efm + len)
433 ++efmp;
434 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
435 *ptr++ = '\\'; /* escape regexp atoms */
436 if (*efmp)
437 *ptr++ = *efmp;
438 }
439 }
440 *ptr++ = '$';
441 *ptr = NUL;
442
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200443 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200444}
445
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200446/*
447 * Free the 'errorformat' information list
448 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200449 static void
450free_efm_list(efm_T **efm_first)
451{
452 efm_T *efm_ptr;
453
454 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
455 {
456 *efm_first = efm_ptr->next;
457 vim_regfree(efm_ptr->prog);
458 vim_free(efm_ptr);
459 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100460 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200461}
462
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200463/*
464 * Compute the size of the buffer used to convert a 'errorformat' pattern into
465 * a regular expression pattern.
466 */
467 static int
468efm_regpat_bufsz(char_u *efm)
469{
470 int sz;
471 int i;
472
473 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
474 for (i = FMT_PATTERNS; i > 0; )
475 sz += (int)STRLEN(fmt_pat[--i].pattern);
476#ifdef BACKSLASH_IN_FILENAME
477 sz += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
478#else
479 sz += 2; /* "%f" can become two chars longer */
480#endif
481
482 return sz;
483}
484
485/*
486 * Return the length of a 'errorformat' option part (separated by ",").
487 */
488 static int
489efm_option_part_len(char_u *efm)
490{
491 int len;
492
493 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
494 if (efm[len] == '\\' && efm[len + 1] != NUL)
495 ++len;
496
497 return len;
498}
499
500/*
501 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
502 * are parsed and converted to regular expressions. Returns information about
503 * the parsed 'errorformat' option.
504 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200505 static efm_T *
506parse_efm_option(char_u *efm)
507{
508 char_u *errmsg = NULL;
509 int errmsglen;
510 efm_T *fmt_ptr = NULL;
511 efm_T *fmt_first = NULL;
512 efm_T *fmt_last = NULL;
513 char_u *fmtstr = NULL;
514 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200515 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200516
517 errmsglen = CMDBUFFSIZE + 1;
518 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
519 if (errmsg == NULL)
520 goto parse_efm_end;
521
522 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200523 * Each part of the format string is copied and modified from errorformat
524 * to regex prog. Only a few % characters are allowed.
525 */
526
527 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200528 * Get some space to modify the format string into.
529 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200530 sz = efm_regpat_bufsz(efm);
531 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200532 goto parse_efm_error;
533
534 while (efm[0] != NUL)
535 {
536 /*
537 * Allocate a new eformat structure and put it at the end of the list
538 */
539 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
540 if (fmt_ptr == NULL)
541 goto parse_efm_error;
542 if (fmt_first == NULL) /* first one */
543 fmt_first = fmt_ptr;
544 else
545 fmt_last->next = fmt_ptr;
546 fmt_last = fmt_ptr;
547
548 /*
549 * Isolate one part in the 'errorformat' option
550 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200551 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200552
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200553 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200554 goto parse_efm_error;
555 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
556 goto parse_efm_error;
557 /*
558 * Advance to next part
559 */
560 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
561 }
562
563 if (fmt_first == NULL) /* nothing found */
564 EMSG(_("E378: 'errorformat' contains no pattern"));
565
566 goto parse_efm_end;
567
568parse_efm_error:
569 free_efm_list(&fmt_first);
570
571parse_efm_end:
572 vim_free(fmtstr);
573 vim_free(errmsg);
574
575 return fmt_first;
576}
577
Bram Moolenaare0d37972016-07-15 22:36:01 +0200578enum {
579 QF_FAIL = 0,
580 QF_OK = 1,
581 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200582 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200583 QF_IGNORE_LINE = 4,
584 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200585};
586
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200587/*
588 * State information used to parse lines and add entries to a quickfix/location
589 * list.
590 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200591typedef struct {
592 char_u *linebuf;
593 int linelen;
594 char_u *growbuf;
595 int growbufsiz;
596 FILE *fd;
597 typval_T *tv;
598 char_u *p_str;
599 listitem_T *p_li;
600 buf_T *buf;
601 linenr_T buflnum;
602 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100603 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200604} qfstate_T;
605
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200606/*
607 * Allocate more memory for the line buffer used for parsing lines.
608 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200609 static char_u *
610qf_grow_linebuf(qfstate_T *state, int newsz)
611{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200612 char_u *p;
613
Bram Moolenaare0d37972016-07-15 22:36:01 +0200614 /*
615 * If the line exceeds LINE_MAXLEN exclude the last
616 * byte since it's not a NL character.
617 */
618 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
619 if (state->growbuf == NULL)
620 {
621 state->growbuf = alloc(state->linelen + 1);
622 if (state->growbuf == NULL)
623 return NULL;
624 state->growbufsiz = state->linelen;
625 }
626 else if (state->linelen > state->growbufsiz)
627 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200628 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200629 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200630 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200631 state->growbufsiz = state->linelen;
632 }
633 return state->growbuf;
634}
635
636/*
637 * Get the next string (separated by newline) from state->p_str.
638 */
639 static int
640qf_get_next_str_line(qfstate_T *state)
641{
642 /* Get the next line from the supplied string */
643 char_u *p_str = state->p_str;
644 char_u *p;
645 int len;
646
647 if (*p_str == NUL) /* Reached the end of the string */
648 return QF_END_OF_INPUT;
649
650 p = vim_strchr(p_str, '\n');
651 if (p != NULL)
652 len = (int)(p - p_str) + 1;
653 else
654 len = (int)STRLEN(p_str);
655
656 if (len > IOSIZE - 2)
657 {
658 state->linebuf = qf_grow_linebuf(state, len);
659 if (state->linebuf == NULL)
660 return QF_NOMEM;
661 }
662 else
663 {
664 state->linebuf = IObuff;
665 state->linelen = len;
666 }
667 vim_strncpy(state->linebuf, p_str, state->linelen);
668
669 /*
670 * Increment using len in order to discard the rest of the
671 * line if it exceeds LINE_MAXLEN.
672 */
673 p_str += len;
674 state->p_str = p_str;
675
676 return QF_OK;
677}
678
679/*
680 * Get the next string from state->p_Li.
681 */
682 static int
683qf_get_next_list_line(qfstate_T *state)
684{
685 listitem_T *p_li = state->p_li;
686 int len;
687
688 while (p_li != NULL
689 && (p_li->li_tv.v_type != VAR_STRING
690 || p_li->li_tv.vval.v_string == NULL))
691 p_li = p_li->li_next; /* Skip non-string items */
692
693 if (p_li == NULL) /* End of the list */
694 {
695 state->p_li = NULL;
696 return QF_END_OF_INPUT;
697 }
698
699 len = (int)STRLEN(p_li->li_tv.vval.v_string);
700 if (len > IOSIZE - 2)
701 {
702 state->linebuf = qf_grow_linebuf(state, len);
703 if (state->linebuf == NULL)
704 return QF_NOMEM;
705 }
706 else
707 {
708 state->linebuf = IObuff;
709 state->linelen = len;
710 }
711
712 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
713
714 state->p_li = p_li->li_next; /* next item */
715 return QF_OK;
716}
717
718/*
719 * Get the next string from state->buf.
720 */
721 static int
722qf_get_next_buf_line(qfstate_T *state)
723{
724 char_u *p_buf = NULL;
725 int len;
726
727 /* Get the next line from the supplied buffer */
728 if (state->buflnum > state->lnumlast)
729 return QF_END_OF_INPUT;
730
731 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
732 state->buflnum += 1;
733
734 len = (int)STRLEN(p_buf);
735 if (len > IOSIZE - 2)
736 {
737 state->linebuf = qf_grow_linebuf(state, len);
738 if (state->linebuf == NULL)
739 return QF_NOMEM;
740 }
741 else
742 {
743 state->linebuf = IObuff;
744 state->linelen = len;
745 }
746 vim_strncpy(state->linebuf, p_buf, state->linelen);
747
748 return QF_OK;
749}
750
751/*
752 * Get the next string from file state->fd.
753 */
754 static int
755qf_get_next_file_line(qfstate_T *state)
756{
757 int discard;
758 int growbuflen;
759
760 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
761 return QF_END_OF_INPUT;
762
763 discard = FALSE;
764 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200765 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200766 {
767 /*
768 * The current line exceeds IObuff, continue reading using
769 * growbuf until EOL or LINE_MAXLEN bytes is read.
770 */
771 if (state->growbuf == NULL)
772 {
773 state->growbufsiz = 2 * (IOSIZE - 1);
774 state->growbuf = alloc(state->growbufsiz);
775 if (state->growbuf == NULL)
776 return QF_NOMEM;
777 }
778
779 /* Copy the read part of the line, excluding null-terminator */
780 memcpy(state->growbuf, IObuff, IOSIZE - 1);
781 growbuflen = state->linelen;
782
783 for (;;)
784 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200785 char_u *p;
786
Bram Moolenaare0d37972016-07-15 22:36:01 +0200787 if (fgets((char *)state->growbuf + growbuflen,
788 state->growbufsiz - growbuflen, state->fd) == NULL)
789 break;
790 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
791 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200792 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200793 break;
794 if (state->growbufsiz == LINE_MAXLEN)
795 {
796 discard = TRUE;
797 break;
798 }
799
800 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
801 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200802 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200803 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200804 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200805 }
806
807 while (discard)
808 {
809 /*
810 * The current line is longer than LINE_MAXLEN, continue
811 * reading but discard everything until EOL or EOF is
812 * reached.
813 */
814 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
815 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200816 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200817 break;
818 }
819
820 state->linebuf = state->growbuf;
821 state->linelen = growbuflen;
822 }
823 else
824 state->linebuf = IObuff;
825
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100826#ifdef FEAT_MBYTE
827 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200828 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
829 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100830 char_u *line;
831
832 line = string_convert(&state->vc, state->linebuf, &state->linelen);
833 if (line != NULL)
834 {
835 if (state->linelen < IOSIZE)
836 {
837 STRCPY(state->linebuf, line);
838 vim_free(line);
839 }
840 else
841 {
842 vim_free(state->growbuf);
843 state->linebuf = state->growbuf = line;
844 state->growbufsiz = state->linelen < LINE_MAXLEN
845 ? state->linelen : LINE_MAXLEN;
846 }
847 }
848 }
849#endif
850
Bram Moolenaare0d37972016-07-15 22:36:01 +0200851 return QF_OK;
852}
853
854/*
855 * Get the next string from a file/buffer/list/string.
856 */
857 static int
858qf_get_nextline(qfstate_T *state)
859{
860 int status = QF_FAIL;
861
862 if (state->fd == NULL)
863 {
864 if (state->tv != NULL)
865 {
866 if (state->tv->v_type == VAR_STRING)
867 /* Get the next line from the supplied string */
868 status = qf_get_next_str_line(state);
869 else if (state->tv->v_type == VAR_LIST)
870 /* Get the next line from the supplied list */
871 status = qf_get_next_list_line(state);
872 }
873 else
874 /* Get the next line from the supplied buffer */
875 status = qf_get_next_buf_line(state);
876 }
877 else
878 /* Get the next line from the supplied file */
879 status = qf_get_next_file_line(state);
880
881 if (status != QF_OK)
882 return status;
883
884 /* remove newline/CR from the line */
885 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200886 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200887 state->linebuf[state->linelen - 1] = NUL;
888#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200889 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
890 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200891#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200892 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200893
894#ifdef FEAT_MBYTE
895 remove_bom(state->linebuf);
896#endif
897
898 return QF_OK;
899}
900
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200901typedef struct {
902 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200903 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200904 char_u *errmsg;
905 int errmsglen;
906 long lnum;
907 int col;
908 char_u use_viscol;
909 char_u *pattern;
910 int enr;
911 int type;
912 int valid;
913} qffields_T;
914
915/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200916 * Parse the match for filename ('%f') pattern in regmatch.
917 * Return the matched value in "fields->namebuf".
918 */
919 static int
920qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
921{
922 int c;
923
924 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
925 return QF_FAIL;
926
927 /* Expand ~/file and $HOME/file to full path. */
928 c = *rmp->endp[midx];
929 *rmp->endp[midx] = NUL;
930 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
931 *rmp->endp[midx] = c;
932
933 /*
934 * For separate filename patterns (%O, %P and %Q), the specified file
935 * should exist.
936 */
937 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
938 && mch_getperm(fields->namebuf) == -1)
939 return QF_FAIL;
940
941 return QF_OK;
942}
943
944/*
945 * Parse the match for error number ('%n') pattern in regmatch.
946 * Return the matched value in "fields->enr".
947 */
948 static int
949qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
950{
951 if (rmp->startp[midx] == NULL)
952 return QF_FAIL;
953 fields->enr = (int)atol((char *)rmp->startp[midx]);
954 return QF_OK;
955}
956
957/*
958 * Parse the match for line number (%l') pattern in regmatch.
959 * Return the matched value in "fields->lnum".
960 */
961 static int
962qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
963{
964 if (rmp->startp[midx] == NULL)
965 return QF_FAIL;
966 fields->lnum = atol((char *)rmp->startp[midx]);
967 return QF_OK;
968}
969
970/*
971 * Parse the match for column number ('%c') pattern in regmatch.
972 * Return the matched value in "fields->col".
973 */
974 static int
975qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
976{
977 if (rmp->startp[midx] == NULL)
978 return QF_FAIL;
979 fields->col = (int)atol((char *)rmp->startp[midx]);
980 return QF_OK;
981}
982
983/*
984 * Parse the match for error type ('%t') pattern in regmatch.
985 * Return the matched value in "fields->type".
986 */
987 static int
988qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
989{
990 if (rmp->startp[midx] == NULL)
991 return QF_FAIL;
992 fields->type = *rmp->startp[midx];
993 return QF_OK;
994}
995
996/*
997 * Parse the match for '%+' format pattern. The whole matching line is included
998 * in the error string. Return the matched line in "fields->errmsg".
999 */
1000 static int
1001qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
1002{
1003 char_u *p;
1004
1005 if (linelen >= fields->errmsglen)
1006 {
1007 /* linelen + null terminator */
1008 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1009 return QF_NOMEM;
1010 fields->errmsg = p;
1011 fields->errmsglen = linelen + 1;
1012 }
1013 vim_strncpy(fields->errmsg, linebuf, linelen);
1014 return QF_OK;
1015}
1016
1017/*
1018 * Parse the match for error message ('%m') pattern in regmatch.
1019 * Return the matched value in "fields->errmsg".
1020 */
1021 static int
1022qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1023{
1024 char_u *p;
1025 int len;
1026
1027 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1028 return QF_FAIL;
1029 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1030 if (len >= fields->errmsglen)
1031 {
1032 /* len + null terminator */
1033 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1034 return QF_NOMEM;
1035 fields->errmsg = p;
1036 fields->errmsglen = len + 1;
1037 }
1038 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1039 return QF_OK;
1040}
1041
1042/*
1043 * Parse the match for rest of a single-line file message ('%r') pattern.
1044 * Return the matched value in "tail".
1045 */
1046 static int
1047qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1048{
1049 if (rmp->startp[midx] == NULL)
1050 return QF_FAIL;
1051 *tail = rmp->startp[midx];
1052 return QF_OK;
1053}
1054
1055/*
1056 * Parse the match for the pointer line ('%p') pattern in regmatch.
1057 * Return the matched value in "fields->col".
1058 */
1059 static int
1060qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1061{
1062 char_u *match_ptr;
1063
1064 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1065 return QF_FAIL;
1066 fields->col = 0;
1067 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1068 ++match_ptr)
1069 {
1070 ++fields->col;
1071 if (*match_ptr == TAB)
1072 {
1073 fields->col += 7;
1074 fields->col -= fields->col % 8;
1075 }
1076 }
1077 ++fields->col;
1078 fields->use_viscol = TRUE;
1079 return QF_OK;
1080}
1081
1082/*
1083 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1084 * Return the matched value in "fields->col".
1085 */
1086 static int
1087qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1088{
1089 if (rmp->startp[midx] == NULL)
1090 return QF_FAIL;
1091 fields->col = (int)atol((char *)rmp->startp[midx]);
1092 fields->use_viscol = TRUE;
1093 return QF_OK;
1094}
1095
1096/*
1097 * Parse the match for the search text ('%s') pattern in regmatch.
1098 * Return the matched value in "fields->pattern".
1099 */
1100 static int
1101qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1102{
1103 int len;
1104
1105 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1106 return QF_FAIL;
1107 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1108 if (len > CMDBUFFSIZE - 5)
1109 len = CMDBUFFSIZE - 5;
1110 STRCPY(fields->pattern, "^\\V");
1111 STRNCAT(fields->pattern, rmp->startp[midx], len);
1112 fields->pattern[len + 3] = '\\';
1113 fields->pattern[len + 4] = '$';
1114 fields->pattern[len + 5] = NUL;
1115 return QF_OK;
1116}
1117
1118/*
1119 * Parse the match for the module ('%o') pattern in regmatch.
1120 * Return the matched value in "fields->module".
1121 */
1122 static int
1123qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1124{
1125 int len;
1126
1127 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1128 return QF_FAIL;
1129 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1130 if (len > CMDBUFFSIZE)
1131 len = CMDBUFFSIZE;
1132 STRNCAT(fields->module, rmp->startp[midx], len);
1133 return QF_OK;
1134}
1135
1136/*
1137 * 'errorformat' format pattern parser functions.
1138 * The '%f' and '%r' formats are parsed differently from other formats.
1139 * See qf_parse_match() for details.
1140 */
1141static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1142{
1143 NULL,
1144 qf_parse_fmt_n,
1145 qf_parse_fmt_l,
1146 qf_parse_fmt_c,
1147 qf_parse_fmt_t,
1148 qf_parse_fmt_m,
1149 NULL,
1150 qf_parse_fmt_p,
1151 qf_parse_fmt_v,
1152 qf_parse_fmt_s,
1153 qf_parse_fmt_o
1154};
1155
1156/*
1157 * Parse the error format pattern matches in "regmatch" and set the values in
1158 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1159 * match. Returns QF_OK if all the matches are successfully parsed. On
1160 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001161 */
1162 static int
1163qf_parse_match(
1164 char_u *linebuf,
1165 int linelen,
1166 efm_T *fmt_ptr,
1167 regmatch_T *regmatch,
1168 qffields_T *fields,
1169 int qf_multiline,
1170 int qf_multiscan,
1171 char_u **tail)
1172{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001173 int idx = fmt_ptr->prefix;
1174 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001175 int midx;
1176 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001177
1178 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1179 return QF_FAIL;
1180 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1181 fields->type = idx;
1182 else
1183 fields->type = 0;
1184 /*
1185 * Extract error message data from matched line.
1186 * We check for an actual submatch, because "\[" and "\]" in
1187 * the 'errorformat' may cause the wrong submatch to be used.
1188 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001189 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001190 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001191 status = QF_OK;
1192 midx = (int)fmt_ptr->addr[i];
1193 if (i == 0 && midx > 0) /* %f */
1194 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1195 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001196 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001197 if (fmt_ptr->flags == '+' && !qf_multiscan) /* %+ */
1198 status = qf_parse_fmt_plus(linebuf, linelen, fields);
1199 else if (midx > 0) /* %m */
1200 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001201 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001202 else if (i == 6 && midx > 0) /* %r */
1203 status = qf_parse_fmt_r(regmatch, midx, tail);
1204 else if (midx > 0) /* others */
1205 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001206
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001207 if (status != QF_OK)
1208 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001209 }
1210
1211 return QF_OK;
1212}
1213
1214/*
1215 * Parse an error line in 'linebuf' using a single error format string in
1216 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1217 * Returns QF_OK if the efm format matches completely and the fields are
1218 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1219 */
1220 static int
1221qf_parse_get_fields(
1222 char_u *linebuf,
1223 int linelen,
1224 efm_T *fmt_ptr,
1225 qffields_T *fields,
1226 int qf_multiline,
1227 int qf_multiscan,
1228 char_u **tail)
1229{
1230 regmatch_T regmatch;
1231 int status = QF_FAIL;
1232 int r;
1233
1234 if (qf_multiscan &&
1235 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1236 return QF_FAIL;
1237
1238 fields->namebuf[0] = NUL;
1239 fields->module[0] = NUL;
1240 fields->pattern[0] = NUL;
1241 if (!qf_multiscan)
1242 fields->errmsg[0] = NUL;
1243 fields->lnum = 0;
1244 fields->col = 0;
1245 fields->use_viscol = FALSE;
1246 fields->enr = -1;
1247 fields->type = 0;
1248 *tail = NULL;
1249
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001250 /* Always ignore case when looking for a matching error. */
1251 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001252 regmatch.regprog = fmt_ptr->prog;
1253 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1254 fmt_ptr->prog = regmatch.regprog;
1255 if (r)
1256 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1257 fields, qf_multiline, qf_multiscan, tail);
1258
1259 return status;
1260}
1261
1262/*
1263 * Parse directory error format prefixes (%D and %X).
1264 * Push and pop directories from the directory stack when scanning directory
1265 * names.
1266 */
1267 static int
1268qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1269{
1270 if (idx == 'D') /* enter directory */
1271 {
1272 if (*fields->namebuf == NUL)
1273 {
1274 EMSG(_("E379: Missing or empty directory name"));
1275 return QF_FAIL;
1276 }
1277 qfl->qf_directory =
1278 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1279 if (qfl->qf_directory == NULL)
1280 return QF_FAIL;
1281 }
1282 else if (idx == 'X') /* leave directory */
1283 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1284
1285 return QF_OK;
1286}
1287
1288/*
1289 * Parse global file name error format prefixes (%O, %P and %Q).
1290 */
1291 static int
1292qf_parse_file_pfx(
1293 int idx,
1294 qffields_T *fields,
1295 qf_list_T *qfl,
1296 char_u *tail)
1297{
1298 fields->valid = FALSE;
1299 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1300 {
1301 if (*fields->namebuf && idx == 'P')
1302 qfl->qf_currfile =
1303 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1304 else if (idx == 'Q')
1305 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1306 *fields->namebuf = NUL;
1307 if (tail && *tail)
1308 {
1309 STRMOVE(IObuff, skipwhite(tail));
1310 qfl->qf_multiscan = TRUE;
1311 return QF_MULTISCAN;
1312 }
1313 }
1314
1315 return QF_OK;
1316}
1317
1318/*
1319 * Parse a non-error line (a line which doesn't match any of the error
1320 * format in 'efm').
1321 */
1322 static int
1323qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1324{
1325 char_u *p;
1326
1327 fields->namebuf[0] = NUL; /* no match found, remove file name */
1328 fields->lnum = 0; /* don't jump to this line */
1329 fields->valid = FALSE;
1330 if (linelen >= fields->errmsglen)
1331 {
1332 /* linelen + null terminator */
1333 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1334 return QF_NOMEM;
1335 fields->errmsg = p;
1336 fields->errmsglen = linelen + 1;
1337 }
1338 /* copy whole line to error message */
1339 vim_strncpy(fields->errmsg, linebuf, linelen);
1340
1341 return QF_OK;
1342}
1343
1344/*
1345 * Parse multi-line error format prefixes (%C and %Z)
1346 */
1347 static int
1348qf_parse_multiline_pfx(
1349 qf_info_T *qi,
1350 int qf_idx,
1351 int idx,
1352 qf_list_T *qfl,
1353 qffields_T *fields)
1354{
1355 char_u *ptr;
1356 int len;
1357
1358 if (!qfl->qf_multiignore)
1359 {
1360 qfline_T *qfprev = qfl->qf_last;
1361
1362 if (qfprev == NULL)
1363 return QF_FAIL;
1364 if (*fields->errmsg && !qfl->qf_multiignore)
1365 {
1366 len = (int)STRLEN(qfprev->qf_text);
1367 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1368 == NULL)
1369 return QF_FAIL;
1370 STRCPY(ptr, qfprev->qf_text);
1371 vim_free(qfprev->qf_text);
1372 qfprev->qf_text = ptr;
1373 *(ptr += len) = '\n';
1374 STRCPY(++ptr, fields->errmsg);
1375 }
1376 if (qfprev->qf_nr == -1)
1377 qfprev->qf_nr = fields->enr;
1378 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1379 /* only printable chars allowed */
1380 qfprev->qf_type = fields->type;
1381
1382 if (!qfprev->qf_lnum)
1383 qfprev->qf_lnum = fields->lnum;
1384 if (!qfprev->qf_col)
1385 qfprev->qf_col = fields->col;
1386 qfprev->qf_viscol = fields->use_viscol;
1387 if (!qfprev->qf_fnum)
1388 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1389 qfl->qf_directory,
1390 *fields->namebuf || qfl->qf_directory != NULL
1391 ? fields->namebuf
1392 : qfl->qf_currfile != NULL && fields->valid
1393 ? qfl->qf_currfile : 0);
1394 }
1395 if (idx == 'Z')
1396 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1397 line_breakcheck();
1398
1399 return QF_IGNORE_LINE;
1400}
1401
1402/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001403 * Parse a line and get the quickfix fields.
1404 * Return the QF_ status.
1405 */
1406 static int
1407qf_parse_line(
1408 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001409 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001410 char_u *linebuf,
1411 int linelen,
1412 efm_T *fmt_first,
1413 qffields_T *fields)
1414{
1415 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001416 int idx = 0;
1417 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001418 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001419 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001420
Bram Moolenaare333e792018-04-08 13:27:39 +02001421restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001422 /* If there was no %> item start at the first pattern */
1423 if (fmt_start == NULL)
1424 fmt_ptr = fmt_first;
1425 else
1426 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001427 /* Otherwise start from the last used pattern */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001428 fmt_ptr = fmt_start;
1429 fmt_start = NULL;
1430 }
1431
1432 /*
1433 * Try to match each part of 'errorformat' until we find a complete
1434 * match or no match.
1435 */
1436 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001437 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1438 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001439 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001440 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1441 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1442 if (status == QF_NOMEM)
1443 return status;
1444 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001445 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001446 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001447 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001448
1449 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1450 {
1451 if (fmt_ptr != NULL)
1452 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001453 /* 'D' and 'X' directory specifiers */
1454 status = qf_parse_dir_pfx(idx, fields, qfl);
1455 if (status != QF_OK)
1456 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001457 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001458
1459 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1460 if (status != QF_OK)
1461 return status;
1462
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001463 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001464 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001465 }
1466 else if (fmt_ptr != NULL)
1467 {
1468 /* honor %> item */
1469 if (fmt_ptr->conthere)
1470 fmt_start = fmt_ptr;
1471
1472 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1473 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001474 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1475 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001476 }
1477 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1478 { /* continuation of multi-line msg */
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001479 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1480 if (status != QF_OK)
1481 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001482 }
1483 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001484 { /* global file names */
1485 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1486 if (status == QF_MULTISCAN)
1487 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001488 }
1489 if (fmt_ptr->flags == '-') /* generally exclude this line */
1490 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001491 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001492 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001493 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001494 return QF_IGNORE_LINE;
1495 }
1496 }
1497
1498 return QF_OK;
1499}
1500
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001501/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001502 * Returns TRUE if the specified quickfix/location stack is empty
1503 */
1504 static int
1505qf_stack_empty(qf_info_T *qi)
1506{
1507 return qi == NULL || qi->qf_listcount <= 0;
1508}
1509
1510/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001511 * Returns TRUE if the specified quickfix/location list is empty.
1512 */
1513 static int
1514qf_list_empty(qf_info_T *qi, int qf_idx)
1515{
1516 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1517 return TRUE;
1518 return qi->qf_lists[qf_idx].qf_count <= 0;
1519}
1520
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001521/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001522 * Allocate the fields used for parsing lines and populating a quickfix list.
1523 */
1524 static int
1525qf_alloc_fields(qffields_T *pfields)
1526{
1527 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1528 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1529 pfields->errmsglen = CMDBUFFSIZE + 1;
1530 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1531 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1532 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1533 || pfields->pattern == NULL || pfields->module == NULL)
1534 return FAIL;
1535
1536 return OK;
1537}
1538
1539/*
1540 * Free the fields used for parsing lines and populating a quickfix list.
1541 */
1542 static void
1543qf_free_fields(qffields_T *pfields)
1544{
1545 vim_free(pfields->namebuf);
1546 vim_free(pfields->module);
1547 vim_free(pfields->errmsg);
1548 vim_free(pfields->pattern);
1549}
1550
1551/*
1552 * Setup the state information used for parsing lines and populating a
1553 * quickfix list.
1554 */
1555 static int
1556qf_setup_state(
1557 qfstate_T *pstate,
1558 char_u *enc,
1559 char_u *efile,
1560 typval_T *tv,
1561 buf_T *buf,
1562 linenr_T lnumfirst,
1563 linenr_T lnumlast)
1564{
1565#ifdef FEAT_MBYTE
1566 pstate->vc.vc_type = CONV_NONE;
1567 if (enc != NULL && *enc != NUL)
1568 convert_setup(&pstate->vc, enc, p_enc);
1569#endif
1570
1571 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1572 {
1573 EMSG2(_(e_openerrf), efile);
1574 return FAIL;
1575 }
1576
1577 if (tv != NULL)
1578 {
1579 if (tv->v_type == VAR_STRING)
1580 pstate->p_str = tv->vval.v_string;
1581 else if (tv->v_type == VAR_LIST)
1582 pstate->p_li = tv->vval.v_list->lv_first;
1583 pstate->tv = tv;
1584 }
1585 pstate->buf = buf;
1586 pstate->buflnum = lnumfirst;
1587 pstate->lnumlast = lnumlast;
1588
1589 return OK;
1590}
1591
1592/*
1593 * Cleanup the state information used for parsing lines and populating a
1594 * quickfix list.
1595 */
1596 static void
1597qf_cleanup_state(qfstate_T *pstate)
1598{
1599 if (pstate->fd != NULL)
1600 fclose(pstate->fd);
1601
1602 vim_free(pstate->growbuf);
1603#ifdef FEAT_MBYTE
1604 if (pstate->vc.vc_type != CONV_NONE)
1605 convert_setup(&pstate->vc, NULL, NULL);
1606#endif
1607}
1608
1609/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001610 * Read the errorfile "efile" into memory, line by line, building the error
1611 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001612 * Alternative: when "efile" is NULL read errors from buffer "buf".
1613 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001614 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001615 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1616 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001617 * Return -1 for error, number of errors for success.
1618 */
1619 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001620qf_init_ext(
1621 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001622 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001623 char_u *efile,
1624 buf_T *buf,
1625 typval_T *tv,
1626 char_u *errorformat,
1627 int newlist, /* TRUE: start a new error list */
1628 linenr_T lnumfirst, /* first line number to use */
1629 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001630 char_u *qf_title,
1631 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001632{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001633 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001634 qfstate_T state;
1635 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001636 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001637 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001638 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001640 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001642 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001644 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001645 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001646
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001647 vim_memset(&state, 0, sizeof(state));
1648 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001649 if ((qf_alloc_fields(&fields) == FAIL) ||
1650 (qf_setup_state(&state, enc, efile, tv, buf,
1651 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 goto qf_init_end;
1653
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001654 if (newlist || qf_idx == qi->qf_listcount)
1655 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001657 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001658 qf_idx = qi->qf_curlist;
1659 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001660 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001661 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001662 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001663 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001664 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001665 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001668 qfl = &qi->qf_lists[qf_idx];
1669
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001671 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001672 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 else
1674 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001676 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001677 * If the errorformat didn't change between calls, then reuse the
1678 * previously parsed values.
1679 */
1680 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1681 {
1682 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001683 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001684 free_efm_list(&fmt_first);
1685
1686 /* parse the current 'efm' */
1687 fmt_first = parse_efm_option(efm);
1688 if (fmt_first != NULL)
1689 last_efm = vim_strsave(efm);
1690 }
1691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
1695 /*
1696 * got_int is reset here, because it was probably set when killing the
1697 * ":make" command, but we still want to read the errorfile then.
1698 */
1699 got_int = FALSE;
1700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 /*
1702 * Read the lines in the error file one by one.
1703 * Try to recognize one of the error formats in each line.
1704 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001705 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001707 /* Get the next line from a file/buffer/list/string */
1708 status = qf_get_nextline(&state);
1709 if (status == QF_NOMEM) /* memory alloc failure */
1710 goto qf_init_end;
1711 if (status == QF_END_OF_INPUT) /* end of input */
1712 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001714 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1715 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001716 if (status == QF_FAIL)
1717 goto error2;
1718 if (status == QF_NOMEM)
1719 goto qf_init_end;
1720 if (status == QF_IGNORE_LINE)
1721 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001723 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001724 qf_idx,
1725 qfl->qf_directory,
1726 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001727 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001728 : ((qfl->qf_currfile != NULL && fields.valid)
1729 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001730 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001731 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001732 fields.errmsg,
1733 fields.lnum,
1734 fields.col,
1735 fields.use_viscol,
1736 fields.pattern,
1737 fields.enr,
1738 fields.type,
1739 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 goto error2;
1741 line_breakcheck();
1742 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001743 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001745 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001747 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001748 qfl->qf_ptr = qfl->qf_start;
1749 qfl->qf_index = 1;
1750 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752 else
1753 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001754 qfl->qf_nonevalid = FALSE;
1755 if (qfl->qf_ptr == NULL)
1756 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001758 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001759 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001760 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
1762 EMSG(_(e_readerrf));
1763error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001764 if (!adding)
1765 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001766 /* Error when creating a new list. Free the new list */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001767 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001768 qi->qf_listcount--;
1769 if (qi->qf_curlist > 0)
1770 --qi->qf_curlist;
1771 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001772qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001773 if (qf_idx == qi->qf_curlist)
1774 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001775 qf_cleanup_state(&state);
1776 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 return retval;
1779}
1780
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001781/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001782 * Read the errorfile "efile" into memory, line by line, building the error
1783 * list. Set the error list's title to qf_title.
1784 * Return -1 for error, number of errors for success.
1785 */
1786 int
1787qf_init(win_T *wp,
1788 char_u *efile,
1789 char_u *errorformat,
1790 int newlist, /* TRUE: start a new error list */
1791 char_u *qf_title,
1792 char_u *enc)
1793{
1794 qf_info_T *qi = &ql_info;
1795
1796 if (wp != NULL)
1797 {
1798 qi = ll_get_or_alloc_list(wp);
1799 if (qi == NULL)
1800 return FAIL;
1801 }
1802
1803 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1804 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1805}
1806
1807/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001808 * Set the title of the specified quickfix list. Frees the previous title.
1809 * Prepends ':' to the title.
1810 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001811 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001812qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001813{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001814 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001815
Bram Moolenaarfb604092014-07-23 15:55:00 +02001816 if (title != NULL)
1817 {
1818 char_u *p = alloc((int)STRLEN(title) + 2);
1819
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001820 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001821 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001822 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001823 }
1824}
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001827 * The title of a quickfix/location list is set, by default, to the command
1828 * that created the quickfix list with the ":" prefix.
1829 * Create a quickfix list title string by prepending ":" to a user command.
1830 * Returns a pointer to a static buffer with the title.
1831 */
1832 static char_u *
1833qf_cmdtitle(char_u *cmd)
1834{
1835 static char_u qftitle_str[IOSIZE];
1836
1837 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1838 return qftitle_str;
1839}
1840
1841/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001842 * Prepare for adding a new quickfix list. If the current list is in the
1843 * middle of the stack, then all the following lists are freed and then
1844 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 */
1846 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001847qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
1849 int i;
1850
1851 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001852 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 * the current entry. This makes it possible to browse in a tree-like
1854 * way with ":grep'.
1855 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001856 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001857 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
1859 /*
1860 * When the stack is full, remove to oldest entry
1861 * Otherwise, add a new entry.
1862 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001863 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001865 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001867 qi->qf_lists[i - 1] = qi->qf_lists[i];
1868 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 }
1870 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001871 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001872 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001873 qf_store_title(&qi->qf_lists[qi->qf_curlist], qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001874 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875}
1876
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001877/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001878 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001879 */
1880 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001881ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001882{
1883 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001884 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001885
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001886 qi = *pqi;
1887 if (qi == NULL)
1888 return;
1889 *pqi = NULL; /* Remove reference to this list */
1890
1891 qi->qf_refcount--;
1892 if (qi->qf_refcount < 1)
1893 {
1894 /* No references to this location list */
1895 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001896 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001897 vim_free(qi);
1898 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001899}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001900
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001901/*
1902 * Free all the quickfix/location lists in the stack.
1903 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001904 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001905qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001906{
1907 int i;
1908 qf_info_T *qi = &ql_info;
1909
1910 if (wp != NULL)
1911 {
1912 /* location list */
1913 ll_free_all(&wp->w_llist);
1914 ll_free_all(&wp->w_llist_ref);
1915 }
1916 else
1917 /* quickfix list */
1918 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001919 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001920}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922/*
1923 * Add an entry to the end of the list of errors.
1924 * Returns OK or FAIL.
1925 */
1926 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001927qf_add_entry(
1928 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001929 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001930 char_u *dir, /* optional directory name */
1931 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001932 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001933 int bufnum, /* buffer number or zero */
1934 char_u *mesg, /* message */
1935 long lnum, /* line number */
1936 int col, /* column */
1937 int vis_col, /* using visual column */
1938 char_u *pattern, /* search pattern */
1939 int nr, /* error number */
1940 int type, /* type character */
1941 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001943 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001944 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001945 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001947 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001949 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001950 {
1951 buf_T *buf = buflist_findnr(bufnum);
1952
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001953 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001954 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001955 buf->b_has_qf_entry |=
Bram Moolenaar4d77c652018-08-18 19:59:54 +02001956 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001957 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001958 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001959 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1961 {
1962 vim_free(qfp);
1963 return FAIL;
1964 }
1965 qfp->qf_lnum = lnum;
1966 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001967 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001968 if (pattern == NULL || *pattern == NUL)
1969 qfp->qf_pattern = NULL;
1970 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1971 {
1972 vim_free(qfp->qf_text);
1973 vim_free(qfp);
1974 return FAIL;
1975 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001976 if (module == NULL || *module == NUL)
1977 qfp->qf_module = NULL;
1978 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1979 {
1980 vim_free(qfp->qf_text);
1981 vim_free(qfp->qf_pattern);
1982 vim_free(qfp);
1983 return FAIL;
1984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 qfp->qf_nr = nr;
1986 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1987 type = 0;
1988 qfp->qf_type = type;
1989 qfp->qf_valid = valid;
1990
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001991 lastp = &qfl->qf_last;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001992 if (qf_list_empty(qi, qf_idx)) /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001994 qfl->qf_start = qfp;
1995 qfl->qf_ptr = qfp;
1996 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001997 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 else
2000 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002001 qfp->qf_prev = *lastp;
2002 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002004 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002006 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002007 ++qfl->qf_count;
2008 if (qfl->qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002009 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002011 qfl->qf_index = qfl->qf_count;
2012 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 }
2014
2015 return OK;
2016}
2017
2018/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002019 * Allocate a new location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002020 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002021 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002022ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002023{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002024 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002025
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002026 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002027 if (qi != NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002028 qi->qf_refcount++;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002029 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002030}
2031
2032/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002033 * Return the location list stack for window 'wp'.
2034 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002035 */
2036 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002037ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002038{
2039 if (IS_LL_WINDOW(wp))
2040 /* For a location list window, use the referenced location list */
2041 return wp->w_llist_ref;
2042
2043 /*
2044 * For a non-location list window, w_llist_ref should not point to a
2045 * location list.
2046 */
2047 ll_free_all(&wp->w_llist_ref);
2048
2049 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002050 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002051 return wp->w_llist;
2052}
2053
2054/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002055 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2056 */
2057 static int
2058copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2059{
2060 int i;
2061 qfline_T *from_qfp;
2062 qfline_T *prevp;
2063
2064 // copy all the location entries in this list
2065 for (i = 0, from_qfp = from_qfl->qf_start;
2066 i < from_qfl->qf_count && from_qfp != NULL;
2067 ++i, from_qfp = from_qfp->qf_next)
2068 {
2069 if (qf_add_entry(to_qi,
2070 to_qi->qf_curlist,
2071 NULL,
2072 NULL,
2073 from_qfp->qf_module,
2074 0,
2075 from_qfp->qf_text,
2076 from_qfp->qf_lnum,
2077 from_qfp->qf_col,
2078 from_qfp->qf_viscol,
2079 from_qfp->qf_pattern,
2080 from_qfp->qf_nr,
2081 0,
2082 from_qfp->qf_valid) == FAIL)
2083 return FAIL;
2084
2085 // qf_add_entry() will not set the qf_num field, as the
2086 // directory and file names are not supplied. So the qf_fnum
2087 // field is copied here.
2088 prevp = to_qfl->qf_last;
2089 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2090 prevp->qf_type = from_qfp->qf_type; // error type
2091 if (from_qfl->qf_ptr == from_qfp)
2092 to_qfl->qf_ptr = prevp; // current location
2093 }
2094
2095 return OK;
2096}
2097
2098/*
2099 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2100 */
2101 static int
2102copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2103{
2104 // Some of the fields are populated by qf_add_entry()
2105 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2106 to_qfl->qf_count = 0;
2107 to_qfl->qf_index = 0;
2108 to_qfl->qf_start = NULL;
2109 to_qfl->qf_last = NULL;
2110 to_qfl->qf_ptr = NULL;
2111 if (from_qfl->qf_title != NULL)
2112 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2113 else
2114 to_qfl->qf_title = NULL;
2115 if (from_qfl->qf_ctx != NULL)
2116 {
2117 to_qfl->qf_ctx = alloc_tv();
2118 if (to_qfl->qf_ctx != NULL)
2119 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2120 }
2121 else
2122 to_qfl->qf_ctx = NULL;
2123
2124 if (from_qfl->qf_count)
2125 if (copy_loclist_entries(from_qfl, to_qfl, to_qi) == FAIL)
2126 return FAIL;
2127
2128 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2129
2130 // Assign a new ID for the location list
2131 to_qfl->qf_id = ++last_qf_id;
2132 to_qfl->qf_changedtick = 0L;
2133
2134 // When no valid entries are present in the list, qf_ptr points to
2135 // the first item in the list
2136 if (to_qfl->qf_nonevalid)
2137 {
2138 to_qfl->qf_ptr = to_qfl->qf_start;
2139 to_qfl->qf_index = 1;
2140 }
2141
2142 return OK;
2143}
2144
2145/*
2146 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002147 */
2148 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002149copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002150{
2151 qf_info_T *qi;
2152 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002153
Bram Moolenaar09037502018-09-25 22:08:14 +02002154 // When copying from a location list window, copy the referenced
2155 // location list. For other windows, copy the location list for
2156 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002157 if (IS_LL_WINDOW(from))
2158 qi = from->w_llist_ref;
2159 else
2160 qi = from->w_llist;
2161
Bram Moolenaar09037502018-09-25 22:08:14 +02002162 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002163 return;
2164
Bram Moolenaar09037502018-09-25 22:08:14 +02002165 // allocate a new location list
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002166 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002167 return;
2168
2169 to->w_llist->qf_listcount = qi->qf_listcount;
2170
Bram Moolenaar09037502018-09-25 22:08:14 +02002171 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002172 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002173 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002174 to->w_llist->qf_curlist = idx;
2175
Bram Moolenaar09037502018-09-25 22:08:14 +02002176 if (copy_loclist(&qi->qf_lists[idx],
2177 &to->w_llist->qf_lists[idx], to->w_llist) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002178 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002179 qf_free_all(to);
2180 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002181 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002182 }
2183
Bram Moolenaar09037502018-09-25 22:08:14 +02002184 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002185}
2186
2187/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002188 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002189 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 */
2191 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002192qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193{
Bram Moolenaar82404332016-07-10 17:00:38 +02002194 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002195 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002196 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002197
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 if (fname == NULL || *fname == NUL) /* no file name */
2199 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
Bram Moolenaare60acc12011-05-10 16:41:25 +02002201#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002202 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002203#endif
2204#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002205 if (directory != NULL)
2206 slash_adjust(directory);
2207 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002208#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002209 if (directory != NULL && !vim_isAbsName(fname)
2210 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2211 {
2212 /*
2213 * Here we check if the file really exists.
2214 * This should normally be true, but if make works without
2215 * "leaving directory"-messages we might have missed a
2216 * directory change.
2217 */
2218 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 vim_free(ptr);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002221 directory = qf_guess_filepath(&qi->qf_lists[qf_idx], fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002222 if (directory)
2223 ptr = concat_fnames(directory, fname, TRUE);
2224 else
2225 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002227 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02002228 bufname = ptr;
2229 }
2230 else
2231 bufname = fname;
2232
2233 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002234 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002235 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002236 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002237 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002239 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002240 {
2241 vim_free(qf_last_bufname);
2242 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2243 if (bufname == ptr)
2244 qf_last_bufname = bufname;
2245 else
2246 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002247 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002248 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002249 if (buf == NULL)
2250 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002251
Bram Moolenaarc1542742016-07-20 21:44:37 +02002252 buf->b_has_qf_entry =
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002253 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002254 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255}
2256
2257/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002258 * Push dirbuf onto the directory stack and return pointer to actual dir or
2259 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 */
2261 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002262qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263{
2264 struct dir_stack_T *ds_new;
2265 struct dir_stack_T *ds_ptr;
2266
2267 /* allocate new stack element and hook it in */
2268 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2269 if (ds_new == NULL)
2270 return NULL;
2271
2272 ds_new->next = *stackptr;
2273 *stackptr = ds_new;
2274
2275 /* store directory on the stack */
2276 if (vim_isAbsName(dirbuf)
2277 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002278 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 (*stackptr)->dirname = vim_strsave(dirbuf);
2280 else
2281 {
2282 /* Okay we don't have an absolute path.
2283 * dirbuf must be a subdir of one of the directories on the stack.
2284 * Let's search...
2285 */
2286 ds_new = (*stackptr)->next;
2287 (*stackptr)->dirname = NULL;
2288 while (ds_new)
2289 {
2290 vim_free((*stackptr)->dirname);
2291 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2292 TRUE);
2293 if (mch_isdir((*stackptr)->dirname) == TRUE)
2294 break;
2295
2296 ds_new = ds_new->next;
2297 }
2298
2299 /* clean up all dirs we already left */
2300 while ((*stackptr)->next != ds_new)
2301 {
2302 ds_ptr = (*stackptr)->next;
2303 (*stackptr)->next = (*stackptr)->next->next;
2304 vim_free(ds_ptr->dirname);
2305 vim_free(ds_ptr);
2306 }
2307
2308 /* Nothing found -> it must be on top level */
2309 if (ds_new == NULL)
2310 {
2311 vim_free((*stackptr)->dirname);
2312 (*stackptr)->dirname = vim_strsave(dirbuf);
2313 }
2314 }
2315
2316 if ((*stackptr)->dirname != NULL)
2317 return (*stackptr)->dirname;
2318 else
2319 {
2320 ds_ptr = *stackptr;
2321 *stackptr = (*stackptr)->next;
2322 vim_free(ds_ptr);
2323 return NULL;
2324 }
2325}
2326
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327/*
2328 * pop dirbuf from the directory stack and return previous directory or NULL if
2329 * stack is empty
2330 */
2331 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002332qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 struct dir_stack_T *ds_ptr;
2335
2336 /* TODO: Should we check if dirbuf is the directory on top of the stack?
2337 * What to do if it isn't? */
2338
2339 /* pop top element and free it */
2340 if (*stackptr != NULL)
2341 {
2342 ds_ptr = *stackptr;
2343 *stackptr = (*stackptr)->next;
2344 vim_free(ds_ptr->dirname);
2345 vim_free(ds_ptr);
2346 }
2347
2348 /* return NEW top element as current dir or NULL if stack is empty*/
2349 return *stackptr ? (*stackptr)->dirname : NULL;
2350}
2351
2352/*
2353 * clean up directory stack
2354 */
2355 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002356qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357{
2358 struct dir_stack_T *ds_ptr;
2359
2360 while ((ds_ptr = *stackptr) != NULL)
2361 {
2362 *stackptr = (*stackptr)->next;
2363 vim_free(ds_ptr->dirname);
2364 vim_free(ds_ptr);
2365 }
2366}
2367
2368/*
2369 * Check in which directory of the directory stack the given file can be
2370 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002371 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 * Cleans up intermediate directory entries.
2373 *
2374 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002375 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 * ./
2377 * ./aa
2378 * ./aa/bb
2379 * ./bb
2380 * ./bb/x.c
2381 * and make says:
2382 * making all in aa
2383 * making all in bb
2384 * x.c:9: Error
2385 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2386 * qf_guess_filepath will return NULL.
2387 */
2388 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002389qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391 struct dir_stack_T *ds_ptr;
2392 struct dir_stack_T *ds_tmp;
2393 char_u *fullname;
2394
2395 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002396 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 return NULL;
2398
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002399 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 fullname = NULL;
2401 while (ds_ptr)
2402 {
2403 vim_free(fullname);
2404 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2405
2406 /* If concat_fnames failed, just go on. The worst thing that can happen
2407 * is that we delete the entire stack.
2408 */
2409 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2410 break;
2411
2412 ds_ptr = ds_ptr->next;
2413 }
2414
2415 vim_free(fullname);
2416
2417 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002418 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002420 ds_tmp = qfl->qf_dir_stack->next;
2421 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 vim_free(ds_tmp->dirname);
2423 vim_free(ds_tmp);
2424 }
2425
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002426 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427}
2428
2429/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002430 * Returns TRUE if a quickfix/location list with the given identifier exists.
2431 */
2432 static int
2433qflist_valid (win_T *wp, int_u qf_id)
2434{
2435 qf_info_T *qi = &ql_info;
2436 int i;
2437
2438 if (wp != NULL)
2439 {
2440 qi = GET_LOC_LIST(wp); /* Location list */
2441 if (qi == NULL)
2442 return FALSE;
2443 }
2444
2445 for (i = 0; i < qi->qf_listcount; ++i)
2446 if (qi->qf_lists[i].qf_id == qf_id)
2447 return TRUE;
2448
2449 return FALSE;
2450}
2451
2452/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002453 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002454 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002455 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002456 * Similar to location list.
2457 */
2458 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002459is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002460{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002461 qfline_T *qfp;
2462 int i;
2463
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002464 /* Search for the entry in the current list */
2465 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2466 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002467 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002468 break;
2469
2470 if (i == qfl->qf_count) /* Entry is not found */
2471 return FALSE;
2472
2473 return TRUE;
2474}
2475
2476/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002477 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002478 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002479 */
2480 static qfline_T *
2481get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002482 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002483 qfline_T *qf_ptr,
2484 int *qf_index,
2485 int dir)
2486{
2487 int idx;
2488 int old_qf_fnum;
2489
2490 idx = *qf_index;
2491 old_qf_fnum = qf_ptr->qf_fnum;
2492
2493 do
2494 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002495 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002496 return NULL;
2497 ++idx;
2498 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002499 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002500 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2501
2502 *qf_index = idx;
2503 return qf_ptr;
2504}
2505
2506/*
2507 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002508 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002509 */
2510 static qfline_T *
2511get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002512 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002513 qfline_T *qf_ptr,
2514 int *qf_index,
2515 int dir)
2516{
2517 int idx;
2518 int old_qf_fnum;
2519
2520 idx = *qf_index;
2521 old_qf_fnum = qf_ptr->qf_fnum;
2522
2523 do
2524 {
2525 if (idx == 1 || qf_ptr->qf_prev == NULL)
2526 return NULL;
2527 --idx;
2528 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002529 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002530 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2531
2532 *qf_index = idx;
2533 return qf_ptr;
2534}
2535
2536/*
2537 * Get the n'th (errornr) previous/next valid entry from the current entry in
2538 * the quickfix list.
2539 * dir == FORWARD or FORWARD_FILE: next valid entry
2540 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2541 */
2542 static qfline_T *
2543get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002544 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002545 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002546 int dir,
2547 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002548{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002549 qfline_T *qf_ptr = qfl->qf_ptr;
2550 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002551 qfline_T *prev_qf_ptr;
2552 int prev_index;
2553 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2554 char_u *err = e_no_more_items;
2555
2556 while (errornr--)
2557 {
2558 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002559 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002560
2561 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002562 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002563 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002564 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002565 if (qf_ptr == NULL)
2566 {
2567 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002568 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002569 if (err != NULL)
2570 {
2571 EMSG(_(err));
2572 return NULL;
2573 }
2574 break;
2575 }
2576
2577 err = NULL;
2578 }
2579
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002580 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002581 return qf_ptr;
2582}
2583
2584/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002585 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2586 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002587 */
2588 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002589get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002590{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002591 qfline_T *qf_ptr = qfl->qf_ptr;
2592 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002593
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002594 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002595 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2596 {
2597 --qf_idx;
2598 qf_ptr = qf_ptr->qf_prev;
2599 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002600 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002601 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2602 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002603 {
2604 ++qf_idx;
2605 qf_ptr = qf_ptr->qf_next;
2606 }
2607
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002608 *new_qfidx = qf_idx;
2609 return qf_ptr;
2610}
2611
2612/*
2613 * Get a entry specied by 'errornr' and 'dir' from the current
2614 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2615 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2616 * Returns a pointer to the entry and the index of the new entry is stored in
2617 * 'new_qfidx'.
2618 */
2619 static qfline_T *
2620qf_get_entry(
2621 qf_list_T *qfl,
2622 int errornr,
2623 int dir,
2624 int *new_qfidx)
2625{
2626 qfline_T *qf_ptr = qfl->qf_ptr;
2627 int qfidx = qfl->qf_index;
2628
2629 if (dir != 0) // next/prev valid entry
2630 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2631 else if (errornr != 0) // go to specified number
2632 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2633
2634 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002635 return qf_ptr;
2636}
2637
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002638/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002639 * Find a window displaying a Vim help file.
2640 */
2641 static win_T *
2642qf_find_help_win(void)
2643{
2644 win_T *wp;
2645
2646 FOR_ALL_WINDOWS(wp)
2647 if (bt_help(wp->w_buffer))
2648 return wp;
2649
2650 return NULL;
2651}
2652
2653/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002654 * Find a help window or open one.
2655 */
2656 static int
2657jump_to_help_window(qf_info_T *qi, int *opened_window)
2658{
2659 win_T *wp;
2660 int flags;
2661
2662 if (cmdmod.tab != 0)
2663 wp = NULL;
2664 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002665 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002666 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2667 win_enter(wp, TRUE);
2668 else
2669 {
2670 /*
2671 * Split off help window; put it at far top if no position
2672 * specified, the current window is vertically split and narrow.
2673 */
2674 flags = WSP_HELP;
2675 if (cmdmod.split == 0 && curwin->w_width != Columns
2676 && curwin->w_width < 80)
2677 flags |= WSP_TOP;
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002678 if (IS_LL_STACK(qi))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002679 flags |= WSP_NEWLOC; /* don't copy the location list */
2680
2681 if (win_split(0, flags) == FAIL)
2682 return FAIL;
2683
2684 *opened_window = TRUE;
2685
2686 if (curwin->w_height < p_hh)
2687 win_setheight((int)p_hh);
2688
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002689 if (IS_LL_STACK(qi)) // not a quickfix list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002690 {
2691 /* The new window should use the supplied location list */
2692 curwin->w_llist = qi;
2693 qi->qf_refcount++;
2694 }
2695 }
2696
2697 if (!p_im)
2698 restart_edit = 0; /* don't want insert mode in help file */
2699
2700 return OK;
2701}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002702
2703/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002704 * Find a non-quickfix window using the given location list.
2705 * Returns NULL if a matching window is not found.
2706 */
2707 static win_T *
2708qf_find_win_with_loclist(qf_info_T *ll)
2709{
2710 win_T *wp;
2711
2712 FOR_ALL_WINDOWS(wp)
2713 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2714 return wp;
2715
2716 return NULL;
2717}
2718
2719/*
2720 * Find a window containing a normal buffer
2721 */
2722 static win_T *
2723qf_find_win_with_normal_buf(void)
2724{
2725 win_T *wp;
2726
2727 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002728 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002729 return wp;
2730
2731 return NULL;
2732}
2733
2734/*
2735 * Go to a window in any tabpage containing the specified file. Returns TRUE
2736 * if successfully jumped to the window. Otherwise returns FALSE.
2737 */
2738 static int
2739qf_goto_tabwin_with_file(int fnum)
2740{
2741 tabpage_T *tp;
2742 win_T *wp;
2743
2744 FOR_ALL_TAB_WINDOWS(tp, wp)
2745 if (wp->w_buffer->b_fnum == fnum)
2746 {
2747 goto_tabpage_win(tp, wp);
2748 return TRUE;
2749 }
2750
2751 return FALSE;
2752}
2753
2754/*
2755 * Create a new window to show a file above the quickfix window. Called when
2756 * only the quickfix window is present.
2757 */
2758 static int
2759qf_open_new_file_win(qf_info_T *ll_ref)
2760{
2761 int flags;
2762
2763 flags = WSP_ABOVE;
2764 if (ll_ref != NULL)
2765 flags |= WSP_NEWLOC;
2766 if (win_split(0, flags) == FAIL)
2767 return FAIL; /* not enough room for window */
2768 p_swb = empty_option; /* don't split again */
2769 swb_flags = 0;
2770 RESET_BINDING(curwin);
2771 if (ll_ref != NULL)
2772 {
2773 /* The new window should use the location list from the
2774 * location list window */
2775 curwin->w_llist = ll_ref;
2776 ll_ref->qf_refcount++;
2777 }
2778 return OK;
2779}
2780
2781/*
2782 * Go to a window that shows the right buffer. If the window is not found, go
2783 * to the window just above the location list window. This is used for opening
2784 * a file from a location window and not from a quickfix window. If some usable
2785 * window is previously found, then it is supplied in 'use_win'.
2786 */
2787 static void
2788qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2789{
2790 win_T *win = use_win;
2791
2792 if (win == NULL)
2793 {
2794 /* Find the window showing the selected file */
2795 FOR_ALL_WINDOWS(win)
2796 if (win->w_buffer->b_fnum == qf_fnum)
2797 break;
2798 if (win == NULL)
2799 {
2800 /* Find a previous usable window */
2801 win = curwin;
2802 do
2803 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002804 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002805 break;
2806 if (win->w_prev == NULL)
2807 win = lastwin; /* wrap around the top */
2808 else
2809 win = win->w_prev; /* go to previous window */
2810 } while (win != curwin);
2811 }
2812 }
2813 win_goto(win);
2814
2815 /* If the location list for the window is not set, then set it
2816 * to the location list from the location window */
2817 if (win->w_llist == NULL)
2818 {
2819 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002820 if (ll_ref != NULL)
2821 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002822 }
2823}
2824
2825/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002826 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2827 * not found, then go to the window just above the quickfix window. This is
2828 * used for opening a file from a quickfix window and not from a location
2829 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002830 */
2831 static void
2832qf_goto_win_with_qfl_file(int qf_fnum)
2833{
2834 win_T *win;
2835 win_T *altwin;
2836
2837 win = curwin;
2838 altwin = NULL;
2839 for (;;)
2840 {
2841 if (win->w_buffer->b_fnum == qf_fnum)
2842 break;
2843 if (win->w_prev == NULL)
2844 win = lastwin; /* wrap around the top */
2845 else
2846 win = win->w_prev; /* go to previous window */
2847
2848 if (IS_QF_WINDOW(win))
2849 {
2850 /* Didn't find it, go to the window before the quickfix
2851 * window. */
2852 if (altwin != NULL)
2853 win = altwin;
2854 else if (curwin->w_prev != NULL)
2855 win = curwin->w_prev;
2856 else
2857 win = curwin->w_next;
2858 break;
2859 }
2860
2861 /* Remember a usable window. */
Bram Moolenaar91335e52018-08-01 17:53:12 +02002862 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002863 altwin = win;
2864 }
2865
2866 win_goto(win);
2867}
2868
2869/*
2870 * Find a suitable window for opening a file (qf_fnum) from the
2871 * quickfix/location list and jump to it. If the file is already opened in a
2872 * window, jump to it. Otherwise open a new window to display the file. This is
2873 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002874 */
2875 static int
2876qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2877{
2878 win_T *usable_win_ptr = NULL;
2879 int usable_win;
2880 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002881 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002882
2883 usable_win = 0;
2884
2885 ll_ref = curwin->w_llist_ref;
2886 if (ll_ref != NULL)
2887 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002888 /* Find a non-quickfix window with this location list */
2889 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2890 if (usable_win_ptr != NULL)
2891 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002892 }
2893
2894 if (!usable_win)
2895 {
2896 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002897 win = qf_find_win_with_normal_buf();
2898 if (win != NULL)
2899 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002900 }
2901
2902 /*
2903 * If no usable window is found and 'switchbuf' contains "usetab"
2904 * then search in other tabs.
2905 */
2906 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002907 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002908
2909 /*
2910 * If there is only one window and it is the quickfix window, create a
2911 * new one above the quickfix window.
2912 */
2913 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2914 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002915 if (qf_open_new_file_win(ll_ref) != OK)
2916 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002917 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002918 }
2919 else
2920 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002921 if (curwin->w_llist_ref != NULL) /* In a location window */
2922 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2923 else /* In a quickfix window */
2924 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002925 }
2926
2927 return OK;
2928}
2929
2930/*
2931 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002932 * Returns OK if successfully edited the file, FAIL on failing to open the
2933 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
2934 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002935 */
2936 static int
2937qf_jump_edit_buffer(
2938 qf_info_T *qi,
2939 qfline_T *qf_ptr,
2940 int forceit,
2941 win_T *oldwin,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002942 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002943{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002944 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002945 int retval = OK;
2946
2947 if (qf_ptr->qf_type == 1)
2948 {
2949 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2950 * set b_p_ro flag). */
2951 if (!can_abandon(curbuf, forceit))
2952 {
2953 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002954 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002955 }
2956 else
2957 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2958 ECMD_HIDE + ECMD_SET_HELP,
2959 oldwin == curwin ? curwin : NULL);
2960 }
2961 else
2962 {
2963 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002964 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002965
2966 retval = buflist_getfile(qf_ptr->qf_fnum,
2967 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002968
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002969 if (IS_LL_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002970 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002971 /*
2972 * Location list. Check whether the associated window is still
2973 * present and the list is still valid.
2974 */
2975 if (!win_valid_any_tab(oldwin))
2976 {
2977 EMSG(_("E924: Current window was closed"));
Bram Moolenaar3c097222017-12-21 20:54:49 +01002978 *opened_window = FALSE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002979 return NOTDONE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002980 }
2981 else if (!qflist_valid(oldwin, save_qfid))
2982 {
2983 EMSG(_(e_loc_list_changed));
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002984 return NOTDONE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002985 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002986 }
2987 else if (old_qf_curlist != qi->qf_curlist
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002988 || !is_qf_entry_present(qfl, qf_ptr))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002989 {
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002990 if (IS_QF_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002991 EMSG(_("E925: Current quickfix was changed"));
2992 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002993 EMSG(_(e_loc_list_changed));
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002994 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002995 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002996 }
2997
2998 return retval;
2999}
3000
3001/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003002 * Go to the error line in the current file using either line/column number or
3003 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003004 */
3005 static void
3006qf_jump_goto_line(
3007 linenr_T qf_lnum,
3008 int qf_col,
3009 char_u qf_viscol,
3010 char_u *qf_pattern)
3011{
3012 linenr_T i;
3013 char_u *line;
3014 colnr_T screen_col;
3015 colnr_T char_col;
3016
3017 if (qf_pattern == NULL)
3018 {
3019 /*
3020 * Go to line with error, unless qf_lnum is 0.
3021 */
3022 i = qf_lnum;
3023 if (i > 0)
3024 {
3025 if (i > curbuf->b_ml.ml_line_count)
3026 i = curbuf->b_ml.ml_line_count;
3027 curwin->w_cursor.lnum = i;
3028 }
3029 if (qf_col > 0)
3030 {
3031 curwin->w_cursor.col = qf_col - 1;
3032#ifdef FEAT_VIRTUALEDIT
3033 curwin->w_cursor.coladd = 0;
3034#endif
3035 if (qf_viscol == TRUE)
3036 {
3037 /*
3038 * Check each character from the beginning of the error
3039 * line up to the error column. For each tab character
3040 * found, reduce the error column value by the length of
3041 * a tab character.
3042 */
3043 line = ml_get_curline();
3044 screen_col = 0;
3045 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3046 {
3047 if (*line == NUL)
3048 break;
3049 if (*line++ == '\t')
3050 {
3051 curwin->w_cursor.col -= 7 - (screen_col % 8);
3052 screen_col += 8 - (screen_col % 8);
3053 }
3054 else
3055 ++screen_col;
3056 }
3057 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003058 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003059 check_cursor();
3060 }
3061 else
3062 beginline(BL_WHITE | BL_FIX);
3063 }
3064 else
3065 {
3066 pos_T save_cursor;
3067
3068 /* Move the cursor to the first line in the buffer */
3069 save_cursor = curwin->w_cursor;
3070 curwin->w_cursor.lnum = 0;
3071 if (!do_search(NULL, '/', qf_pattern, (long)1,
3072 SEARCH_KEEP, NULL, NULL))
3073 curwin->w_cursor = save_cursor;
3074 }
3075}
3076
3077/*
3078 * Display quickfix list index and size message
3079 */
3080 static void
3081qf_jump_print_msg(
3082 qf_info_T *qi,
3083 int qf_index,
3084 qfline_T *qf_ptr,
3085 buf_T *old_curbuf,
3086 linenr_T old_lnum)
3087{
3088 linenr_T i;
3089 int len;
3090
3091 /* Update the screen before showing the message, unless the screen
3092 * scrolled up. */
3093 if (!msg_scrolled)
3094 update_topline_redraw();
3095 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3096 qi->qf_lists[qi->qf_curlist].qf_count,
3097 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3098 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
3099 /* Add the message, skipping leading whitespace and newlines. */
3100 len = (int)STRLEN(IObuff);
3101 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3102
3103 /* Output the message. Overwrite to avoid scrolling when the 'O'
3104 * flag is present in 'shortmess'; But when not jumping, print the
3105 * whole message. */
3106 i = msg_scroll;
3107 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3108 msg_scroll = TRUE;
3109 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3110 msg_scroll = FALSE;
3111 msg_attr_keep(IObuff, 0, TRUE);
3112 msg_scroll = i;
3113}
3114
3115/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003116 * Find a usable window for opening a file from the quickfix/location list. If
3117 * a window is not found then open a new window.
3118 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3119 * able to jump/open a window. Returns NOTDONE if a file is not associated
3120 * with the entry.
3121 */
3122 static int
3123qf_jump_open_window(qf_info_T *qi, qfline_T *qf_ptr, int *opened_window)
3124{
3125 // For ":helpgrep" find a help window or open one.
3126 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
3127 if (jump_to_help_window(qi, opened_window) == FAIL)
3128 return FAIL;
3129
3130 // If currently in the quickfix window, find another window to show the
3131 // file in.
3132 if (bt_quickfix(curbuf) && !*opened_window)
3133 {
3134 // If there is no file specified, we don't know where to go.
3135 // But do advance, otherwise ":cn" gets stuck.
3136 if (qf_ptr->qf_fnum == 0)
3137 return NOTDONE;
3138
3139 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, opened_window) == FAIL)
3140 return FAIL;
3141 }
3142
3143 return OK;
3144}
3145
3146/*
3147 * Edit a selected file from the quickfix/location list and jump to a
3148 * particular line/column, adjust the folds and display a message about the
3149 * jump.
3150 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3151 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3152 * the file.
3153 */
3154 static int
3155qf_jump_to_buffer(
3156 qf_info_T *qi,
3157 int qf_index,
3158 qfline_T *qf_ptr,
3159 int forceit,
3160 win_T *oldwin,
3161 int *opened_window,
3162 int openfold,
3163 int print_message)
3164{
3165 buf_T *old_curbuf;
3166 linenr_T old_lnum;
3167 int retval = OK;
3168
3169 // If there is a file name, read the wanted file if needed, and check
3170 // autowrite etc.
3171 old_curbuf = curbuf;
3172 old_lnum = curwin->w_cursor.lnum;
3173
3174 if (qf_ptr->qf_fnum != 0)
3175 {
3176 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3177 opened_window);
3178 if (retval != OK)
3179 return retval;
3180 }
3181
3182 // When not switched to another buffer, still need to set pc mark
3183 if (curbuf == old_curbuf)
3184 setpcmark();
3185
3186 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3187 qf_ptr->qf_pattern);
3188
3189#ifdef FEAT_FOLDING
3190 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3191 foldOpenCursor();
3192#endif
3193 if (print_message)
3194 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3195
3196 return retval;
3197}
3198
3199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 * jump to a quickfix line
3201 * if dir == FORWARD go "errornr" valid entries forward
3202 * if dir == BACKWARD go "errornr" valid entries backward
3203 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3204 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3205 * else if "errornr" is zero, redisplay the same line
3206 * else go to entry "errornr"
3207 */
3208 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003209qf_jump(qf_info_T *qi,
3210 int dir,
3211 int errornr,
3212 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003214 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003215 qfline_T *qf_ptr;
3216 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003219 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003220 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003222 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003225 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003227 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003229 if (qi == NULL)
3230 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003231
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003232 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 {
3234 EMSG(_(e_quickfix));
3235 return;
3236 }
3237
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003238 qfl = &qi->qf_lists[qi->qf_curlist];
3239
3240 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003242 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003244
3245 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3246 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003248 qf_ptr = old_qf_ptr;
3249 qf_index = old_qf_index;
3250 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003253 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003254 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003255 // No need to print the error message if it's visible in the error
3256 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 print_message = FALSE;
3258
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003259 retval = qf_jump_open_window(qi, qf_ptr, &opened_window);
3260 if (retval == FAIL)
3261 goto failed;
3262 if (retval == NOTDONE)
3263 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003264
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003265 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3266 &opened_window, old_KeyTyped, print_message);
3267 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003269 // Quickfix/location list is freed by an autocmd
3270 qi = NULL;
3271 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003274 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003277 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003278 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003280 // Couldn't open file, so put index back where it was. This could
3281 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 qf_ptr = old_qf_ptr;
3284 qf_index = old_qf_index;
3285 }
3286 }
3287theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003288 if (qi != NULL)
3289 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003290 qfl->qf_ptr = qf_ptr;
3291 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (p_swb != old_swb && opened_window)
3294 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003295 // Restore old 'switchbuf' value, but not when an autocommand or
3296 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003300 swb_flags = old_swb_flags;
3301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 else
3303 free_string_option(old_swb);
3304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305}
3306
3307/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003308 * Highlight attributes used for displaying entries from the quickfix list.
3309 */
3310static int qfFileAttr;
3311static int qfSepAttr;
3312static int qfLineAttr;
3313
3314/*
3315 * Display information about a single entry from the quickfix/location list.
3316 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003317 * 'cursel' will be set to TRUE for the currently selected entry in the
3318 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003319 */
3320 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003321qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003322{
3323 char_u *fname;
3324 buf_T *buf;
3325 int filter_entry;
3326
3327 fname = NULL;
3328 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3329 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3330 (char *)qfp->qf_module);
3331 else {
3332 if (qfp->qf_fnum != 0
3333 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3334 {
3335 fname = buf->b_fname;
3336 if (qfp->qf_type == 1) /* :helpgrep */
3337 fname = gettail(fname);
3338 }
3339 if (fname == NULL)
3340 sprintf((char *)IObuff, "%2d", qf_idx);
3341 else
3342 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3343 qf_idx, (char *)fname);
3344 }
3345
3346 // Support for filtering entries using :filter /pat/ clist
3347 // Match against the module name, file name, search pattern and
3348 // text of the entry.
3349 filter_entry = TRUE;
3350 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3351 filter_entry &= message_filtered(qfp->qf_module);
3352 if (filter_entry && fname != NULL)
3353 filter_entry &= message_filtered(fname);
3354 if (filter_entry && qfp->qf_pattern != NULL)
3355 filter_entry &= message_filtered(qfp->qf_pattern);
3356 if (filter_entry)
3357 filter_entry &= message_filtered(qfp->qf_text);
3358 if (filter_entry)
3359 return;
3360
3361 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003362 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003363
3364 if (qfp->qf_lnum != 0)
3365 msg_puts_attr((char_u *)":", qfSepAttr);
3366 if (qfp->qf_lnum == 0)
3367 IObuff[0] = NUL;
3368 else if (qfp->qf_col == 0)
3369 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3370 else
3371 sprintf((char *)IObuff, "%ld col %d",
3372 qfp->qf_lnum, qfp->qf_col);
3373 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3374 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3375 msg_puts_attr(IObuff, qfLineAttr);
3376 msg_puts_attr((char_u *)":", qfSepAttr);
3377 if (qfp->qf_pattern != NULL)
3378 {
3379 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3380 msg_puts(IObuff);
3381 msg_puts_attr((char_u *)":", qfSepAttr);
3382 }
3383 msg_puts((char_u *)" ");
3384
3385 /* Remove newlines and leading whitespace from the text. For an
3386 * unrecognized line keep the indent, the compiler may mark a word
3387 * with ^^^^. */
3388 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3389 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3390 IObuff, IOSIZE);
3391 msg_prt_line(IObuff, FALSE);
3392 out_flush(); /* show one line at a time */
3393}
3394
3395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003397 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 */
3399 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003400qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003402 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003403 qfline_T *qfp;
3404 int i;
3405 int idx1 = 1;
3406 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003407 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003408 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003409 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003411 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
Bram Moolenaar39665952018-08-15 20:59:48 +02003413 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003414 {
3415 qi = GET_LOC_LIST(curwin);
3416 if (qi == NULL)
3417 {
3418 EMSG(_(e_loclist));
3419 return;
3420 }
3421 }
3422
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003423 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
3425 EMSG(_(e_quickfix));
3426 return;
3427 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003428 if (*arg == '+')
3429 {
3430 ++arg;
3431 plus = TRUE;
3432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3434 {
3435 EMSG(_(e_trailing));
3436 return;
3437 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003438 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003439 if (plus)
3440 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003441 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003442 idx2 = i + idx1;
3443 idx1 = i;
3444 }
3445 else
3446 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003447 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003448 if (idx1 < 0)
3449 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3450 if (idx2 < 0)
3451 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
Bram Moolenaara796d462018-05-01 14:30:36 +02003454 /* Shorten all the file names, so that it is easy to read */
3455 shorten_fnames(FALSE);
3456
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003457 /*
3458 * Get the attributes for the different quickfix highlight items. Note
3459 * that this depends on syntax items defined in the qf.vim syntax file
3460 */
3461 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3462 if (qfFileAttr == 0)
3463 qfFileAttr = HL_ATTR(HLF_D);
3464 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3465 if (qfSepAttr == 0)
3466 qfSepAttr = HL_ATTR(HLF_D);
3467 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3468 if (qfLineAttr == 0)
3469 qfLineAttr = HL_ATTR(HLF_N);
3470
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003471 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003473 qfp = qfl->qf_start;
3474 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3477 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003478 if (got_int)
3479 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003480
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003481 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003483
3484 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003485 if (qfp == NULL)
3486 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003487 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 ui_breakcheck();
3489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490}
3491
3492/*
3493 * Remove newlines and leading whitespace from an error message.
3494 * Put the result in "buf[bufsize]".
3495 */
3496 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003497qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498{
3499 int i;
3500 char_u *p = text;
3501
3502 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3503 {
3504 if (*p == '\n')
3505 {
3506 buf[i] = ' ';
3507 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003508 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 break;
3510 }
3511 else
3512 buf[i] = *p++;
3513 }
3514 buf[i] = NUL;
3515}
3516
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003517/*
3518 * Display information (list number, list size and the title) about a
3519 * quickfix/location list.
3520 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003521 static void
3522qf_msg(qf_info_T *qi, int which, char *lead)
3523{
3524 char *title = (char *)qi->qf_lists[which].qf_title;
3525 int count = qi->qf_lists[which].qf_count;
3526 char_u buf[IOSIZE];
3527
3528 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3529 lead,
3530 which + 1,
3531 qi->qf_listcount,
3532 count);
3533
3534 if (title != NULL)
3535 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003536 size_t len = STRLEN(buf);
3537
3538 if (len < 34)
3539 {
3540 vim_memset(buf + len, ' ', 34 - len);
3541 buf[34] = NUL;
3542 }
3543 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003544 }
3545 trunc_string(buf, buf, Columns - 1, IOSIZE);
3546 msg(buf);
3547}
3548
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549/*
3550 * ":colder [count]": Up in the quickfix stack.
3551 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003552 * ":lolder [count]": Up in the location list stack.
3553 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 */
3555 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003556qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003558 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 int count;
3560
Bram Moolenaar39665952018-08-15 20:59:48 +02003561 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003562 {
3563 qi = GET_LOC_LIST(curwin);
3564 if (qi == NULL)
3565 {
3566 EMSG(_(e_loclist));
3567 return;
3568 }
3569 }
3570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 if (eap->addr_count != 0)
3572 count = eap->line2;
3573 else
3574 count = 1;
3575 while (count--)
3576 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003577 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003579 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003582 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003584 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
3586 else
3587 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003588 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
3590 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003591 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003593 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003596 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003597 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598}
3599
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003600/*
3601 * Display the information about all the quickfix/location lists in the stack
3602 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003603 void
3604qf_history(exarg_T *eap)
3605{
3606 qf_info_T *qi = &ql_info;
3607 int i;
3608
Bram Moolenaar39665952018-08-15 20:59:48 +02003609 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003610 qi = GET_LOC_LIST(curwin);
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003611 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003612 MSG(_("No entries"));
3613 else
3614 for (i = 0; i < qi->qf_listcount; ++i)
3615 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3616}
3617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003619 * Free all the entries in the error list "idx". Note that other information
3620 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 */
3622 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003623qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003625 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003626 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003627 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003629 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003631 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003632 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003633 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003634 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003635 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003636 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003637 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003638 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003639 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003640 if (stop)
3641 /* Somehow qf_count may have an incorrect value, set it to 1
3642 * to avoid crashing when it's wrong.
3643 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003644 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003645 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003646 qfl->qf_start = qfpnext;
3647 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003649
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003650 qfl->qf_index = 0;
3651 qfl->qf_start = NULL;
3652 qfl->qf_last = NULL;
3653 qfl->qf_ptr = NULL;
3654 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003655
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003656 qf_clean_dir_stack(&qfl->qf_dir_stack);
3657 qfl->qf_directory = NULL;
3658 qf_clean_dir_stack(&qfl->qf_file_stack);
3659 qfl->qf_currfile = NULL;
3660 qfl->qf_multiline = FALSE;
3661 qfl->qf_multiignore = FALSE;
3662 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663}
3664
3665/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003666 * Free error list "idx". Frees all the entries in the quickfix list,
3667 * associated context information and the title.
3668 */
3669 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003670qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003671{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003672 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003673
Bram Moolenaard23a8232018-02-10 18:45:26 +01003674 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003675 free_tv(qfl->qf_ctx);
3676 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003677 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003678 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003679}
3680
3681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 * qf_mark_adjust: adjust marks
3683 */
3684 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003685qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003686 win_T *wp,
3687 linenr_T line1,
3688 linenr_T line2,
3689 long amount,
3690 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003692 int i;
3693 qfline_T *qfp;
3694 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003695 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003696 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003697 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698
Bram Moolenaarc1542742016-07-20 21:44:37 +02003699 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003700 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003701 if (wp != NULL)
3702 {
3703 if (wp->w_llist == NULL)
3704 return;
3705 qi = wp->w_llist;
3706 }
3707
3708 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003709 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003710 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003711 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3712 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 if (qfp->qf_fnum == curbuf->b_fnum)
3714 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003715 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3717 {
3718 if (amount == MAXLNUM)
3719 qfp->qf_cleared = TRUE;
3720 else
3721 qfp->qf_lnum += amount;
3722 }
3723 else if (amount_after && qfp->qf_lnum > line2)
3724 qfp->qf_lnum += amount_after;
3725 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003726
3727 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003728 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729}
3730
3731/*
3732 * Make a nice message out of the error character and the error number:
3733 * char number message
3734 * e or E 0 " error"
3735 * w or W 0 " warning"
3736 * i or I 0 " info"
3737 * 0 0 ""
3738 * other 0 " c"
3739 * e or E n " error n"
3740 * w or W n " warning n"
3741 * i or I n " info n"
3742 * 0 n " error n"
3743 * other n " c n"
3744 * 1 x "" :helpgrep
3745 */
3746 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003747qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
3749 static char_u buf[20];
3750 static char_u cc[3];
3751 char_u *p;
3752
3753 if (c == 'W' || c == 'w')
3754 p = (char_u *)" warning";
3755 else if (c == 'I' || c == 'i')
3756 p = (char_u *)" info";
3757 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3758 p = (char_u *)" error";
3759 else if (c == 0 || c == 1)
3760 p = (char_u *)"";
3761 else
3762 {
3763 cc[0] = ' ';
3764 cc[1] = c;
3765 cc[2] = NUL;
3766 p = cc;
3767 }
3768
3769 if (nr <= 0)
3770 return p;
3771
3772 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3773 return buf;
3774}
3775
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003777 * When "split" is FALSE: Open the entry/result under the cursor.
3778 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3779 */
3780 void
3781qf_view_result(int split)
3782{
3783 qf_info_T *qi = &ql_info;
3784
3785 if (!bt_quickfix(curbuf))
3786 return;
3787
3788 if (IS_LL_WINDOW(curwin))
3789 qi = GET_LOC_LIST(curwin);
3790
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003791 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003792 {
3793 EMSG(_(e_quickfix));
3794 return;
3795 }
3796
3797 if (split)
3798 {
3799 char_u cmd[32];
3800
3801 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3802 (long)curwin->w_cursor.lnum,
3803 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3804 if (do_cmdline_cmd(cmd) == OK)
3805 do_cmdline_cmd((char_u *) "clearjumps");
3806 return;
3807 }
3808
3809 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3810}
3811
3812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 * ":cwindow": open the quickfix window if we have errors to display,
3814 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003815 * ":lwindow": open the location list window if we have locations to display,
3816 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 */
3818 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003819ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003821 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 win_T *win;
3823
Bram Moolenaar39665952018-08-15 20:59:48 +02003824 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003825 {
3826 qi = GET_LOC_LIST(curwin);
3827 if (qi == NULL)
3828 return;
3829 }
3830
3831 /* Look for an existing quickfix window. */
3832 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834 /*
3835 * If a quickfix window is open but we have no errors to display,
3836 * close the window. If a quickfix window is not open, then open
3837 * it if we have errors; otherwise, leave it closed.
3838 */
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003839 if (qf_stack_empty(qi)
3840 || qi->qf_lists[qi->qf_curlist].qf_nonevalid
3841 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 {
3843 if (win != NULL)
3844 ex_cclose(eap);
3845 }
3846 else if (win == NULL)
3847 ex_copen(eap);
3848}
3849
3850/*
3851 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003852 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003855ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003857 win_T *win = NULL;
3858 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859
Bram Moolenaar39665952018-08-15 20:59:48 +02003860 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003861 {
3862 qi = GET_LOC_LIST(curwin);
3863 if (qi == NULL)
3864 return;
3865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003867 /* Find existing quickfix window and close it. */
3868 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (win != NULL)
3870 win_close(win, FALSE);
3871}
3872
3873/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003874 * Set "w:quickfix_title" if "qi" has a title.
3875 */
3876 static void
3877qf_set_title_var(qf_list_T *qfl)
3878{
3879 if (qfl->qf_title != NULL)
3880 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3881}
3882
3883/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003884 * Goto a quickfix or location list window (if present).
3885 * Returns OK if the window is found, FAIL otherwise.
3886 */
3887 static int
3888qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3889{
3890 win_T *win;
3891
3892 win = qf_find_win(qi);
3893 if (win == NULL)
3894 return FAIL;
3895
3896 win_goto(win);
3897 if (resize)
3898 {
3899 if (vertsplit)
3900 {
3901 if (sz != win->w_width)
3902 win_setwidth(sz);
3903 }
3904 else if (sz != win->w_height)
3905 win_setheight(sz);
3906 }
3907
3908 return OK;
3909}
3910
3911/*
3912 * Open a new quickfix or location list window, load the quickfix buffer and
3913 * set the appropriate options for the window.
3914 * Returns FAIL if the window could not be opened.
3915 */
3916 static int
3917qf_open_new_cwindow(qf_info_T *qi, int height)
3918{
3919 buf_T *qf_buf;
3920 win_T *oldwin = curwin;
3921 tabpage_T *prevtab = curtab;
3922 int flags = 0;
3923 win_T *win;
3924
3925 qf_buf = qf_find_buf(qi);
3926
3927 // The current window becomes the previous window afterwards.
3928 win = curwin;
3929
3930 if (IS_QF_STACK(qi) && cmdmod.split == 0)
3931 // Create the new quickfix window at the very bottom, except when
3932 // :belowright or :aboveleft is used.
3933 win_goto(lastwin);
3934 // Default is to open the window below the current window
3935 if (cmdmod.split == 0)
3936 flags = WSP_BELOW;
3937 flags |= WSP_NEWLOC;
3938 if (win_split(height, flags) == FAIL)
3939 return FAIL; // not enough room for window
3940 RESET_BINDING(curwin);
3941
3942 if (IS_LL_STACK(qi))
3943 {
3944 // For the location list window, create a reference to the
3945 // location list from the window 'win'.
3946 curwin->w_llist_ref = win->w_llist;
3947 win->w_llist->qf_refcount++;
3948 }
3949
3950 if (oldwin != curwin)
3951 oldwin = NULL; // don't store info when in another window
3952 if (qf_buf != NULL)
3953 {
3954 // Use the existing quickfix buffer
3955 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
3956 ECMD_HIDE + ECMD_OLDBUF, oldwin);
3957 }
3958 else
3959 {
3960 // Create a new quickfix buffer
3961 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
3962
3963 // switch off 'swapfile'
3964 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3965 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
3966 OPT_LOCAL);
3967 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
3968 RESET_BINDING(curwin);
3969#ifdef FEAT_DIFF
3970 curwin->w_p_diff = FALSE;
3971#endif
3972#ifdef FEAT_FOLDING
3973 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3974 OPT_LOCAL);
3975#endif
3976 }
3977
3978 // Only set the height when still in the same tab page and there is no
3979 // window to the side.
3980 if (curtab == prevtab && curwin->w_width == Columns)
3981 win_setheight(height);
3982 curwin->w_p_wfh = TRUE; // set 'winfixheight'
3983 if (win_valid(win))
3984 prevwin = win;
3985
3986 return OK;
3987}
3988
3989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003991 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 */
3993 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003994ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003996 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003998 int status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
Bram Moolenaar39665952018-08-15 20:59:48 +02004000 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004001 {
4002 qi = GET_LOC_LIST(curwin);
4003 if (qi == NULL)
4004 {
4005 EMSG(_(e_loclist));
4006 return;
4007 }
4008 }
4009
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (eap->addr_count != 0)
4011 height = eap->line2;
4012 else
4013 height = QF_WINHEIGHT;
4014
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004015 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#ifdef FEAT_GUI
4017 need_mouse_correct = TRUE;
4018#endif
4019
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004020 // Find an existing quickfix window, or open a new one.
4021 if (cmdmod.tab == 0)
4022 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4023 cmdmod.split & WSP_VERT);
4024 if (status == FAIL)
4025 if (qf_open_new_cwindow(qi, height) == FAIL)
4026 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004028 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004029
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004030 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004031 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004033 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 curwin->w_cursor.col = 0;
4035 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004036 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037}
4038
4039/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004040 * Move the cursor in the quickfix window to "lnum".
4041 */
4042 static void
4043qf_win_goto(win_T *win, linenr_T lnum)
4044{
4045 win_T *old_curwin = curwin;
4046
4047 curwin = win;
4048 curbuf = win->w_buffer;
4049 curwin->w_cursor.lnum = lnum;
4050 curwin->w_cursor.col = 0;
4051#ifdef FEAT_VIRTUALEDIT
4052 curwin->w_cursor.coladd = 0;
4053#endif
4054 curwin->w_curswant = 0;
4055 update_topline(); /* scroll to show the line */
4056 redraw_later(VALID);
4057 curwin->w_redr_status = TRUE; /* update ruler */
4058 curwin = old_curwin;
4059 curbuf = curwin->w_buffer;
4060}
4061
4062/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004063 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004064 */
4065 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004066ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004067{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004068 qf_info_T *qi = &ql_info;
4069 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004070
Bram Moolenaar39665952018-08-15 20:59:48 +02004071 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004072 {
4073 qi = GET_LOC_LIST(curwin);
4074 if (qi == NULL)
4075 {
4076 EMSG(_(e_loclist));
4077 return;
4078 }
4079 }
4080
4081 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004082 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4083 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4084}
4085
4086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 * Return the number of the current entry (line number in the quickfix
4088 * window).
4089 */
4090 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004091qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004093 qf_info_T *qi = &ql_info;
4094
4095 if (IS_LL_WINDOW(wp))
4096 /* In the location list window, use the referenced location list */
4097 qi = wp->w_llist_ref;
4098
4099 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100}
4101
4102/*
4103 * Update the cursor position in the quickfix window to the current error.
4104 * Return TRUE if there is a quickfix window.
4105 */
4106 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004107qf_win_pos_update(
4108 qf_info_T *qi,
4109 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110{
4111 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004112 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
4114 /*
4115 * Put the cursor on the current error in the quickfix window, so that
4116 * it's viewable.
4117 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004118 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 if (win != NULL
4120 && qf_index <= win->w_buffer->b_ml.ml_line_count
4121 && old_qf_index != qf_index)
4122 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (qf_index > old_qf_index)
4124 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004125 win->w_redraw_top = old_qf_index;
4126 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
4128 else
4129 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004130 win->w_redraw_top = qf_index;
4131 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004133 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135 return win != NULL;
4136}
4137
4138/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004139 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004140 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004141 */
4142 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004143is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004144{
4145 /*
4146 * A window displaying the quickfix buffer will have the w_llist_ref field
4147 * set to NULL.
4148 * A window displaying a location list buffer will have the w_llist_ref
4149 * pointing to the location list.
4150 */
4151 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004152 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4153 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004154 return TRUE;
4155
4156 return FALSE;
4157}
4158
4159/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004160 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004161 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004162 */
4163 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004164qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004165{
4166 win_T *win;
4167
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004168 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004169 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004170 return win;
4171 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004172}
4173
4174/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004175 * Find a quickfix buffer.
4176 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 */
4178 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004179qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004181 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004182 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
Bram Moolenaar9c102382006-05-03 21:26:49 +00004184 FOR_ALL_TAB_WINDOWS(tp, win)
4185 if (is_qf_win(win, qi))
4186 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004187
Bram Moolenaar9c102382006-05-03 21:26:49 +00004188 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189}
4190
4191/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004192 * Update the w:quickfix_title variable in the quickfix/location list window
4193 */
4194 static void
4195qf_update_win_titlevar(qf_info_T *qi)
4196{
4197 win_T *win;
4198 win_T *curwin_save;
4199
4200 if ((win = qf_find_win(qi)) != NULL)
4201 {
4202 curwin_save = curwin;
4203 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004204 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004205 curwin = curwin_save;
4206 }
4207}
4208
4209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 * Find the quickfix buffer. If it exists, update the contents.
4211 */
4212 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004213qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214{
4215 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004216 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
4219 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004220 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 if (buf != NULL)
4222 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004223 linenr_T old_line_count = buf->b_ml.ml_line_count;
4224
4225 if (old_last == NULL)
4226 /* set curwin/curbuf to buf and save a few things */
4227 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
Bram Moolenaard823fa92016-08-12 16:29:27 +02004229 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004230
Bram Moolenaar864293a2016-06-02 13:40:04 +02004231 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004232 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004233
Bram Moolenaar864293a2016-06-02 13:40:04 +02004234 if (old_last == NULL)
4235 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004236 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004237
4238 /* restore curwin/curbuf and a few other things */
4239 aucmd_restbuf(&aco);
4240 }
4241
4242 /* Only redraw when added lines are visible. This avoids flickering
4243 * when the added lines are not visible. */
4244 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4245 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247}
4248
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004249/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004250 * Add an error line to the quickfix buffer.
4251 */
4252 static int
4253qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4254{
4255 int len;
4256 buf_T *errbuf;
4257
4258 if (qfp->qf_module != NULL)
4259 {
4260 STRCPY(IObuff, qfp->qf_module);
4261 len = (int)STRLEN(IObuff);
4262 }
4263 else if (qfp->qf_fnum != 0
4264 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4265 && errbuf->b_fname != NULL)
4266 {
4267 if (qfp->qf_type == 1) /* :helpgrep */
4268 STRCPY(IObuff, gettail(errbuf->b_fname));
4269 else
4270 {
4271 /* shorten the file name if not done already */
4272 if (errbuf->b_sfname == NULL
4273 || mch_isFullName(errbuf->b_sfname))
4274 {
4275 if (*dirname == NUL)
4276 mch_dirname(dirname, MAXPATHL);
4277 shorten_buf_fname(errbuf, dirname, FALSE);
4278 }
4279 STRCPY(IObuff, errbuf->b_fname);
4280 }
4281 len = (int)STRLEN(IObuff);
4282 }
4283 else
4284 len = 0;
4285 IObuff[len++] = '|';
4286
4287 if (qfp->qf_lnum > 0)
4288 {
4289 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4290 len += (int)STRLEN(IObuff + len);
4291
4292 if (qfp->qf_col > 0)
4293 {
4294 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4295 len += (int)STRLEN(IObuff + len);
4296 }
4297
4298 sprintf((char *)IObuff + len, "%s",
4299 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4300 len += (int)STRLEN(IObuff + len);
4301 }
4302 else if (qfp->qf_pattern != NULL)
4303 {
4304 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4305 len += (int)STRLEN(IObuff + len);
4306 }
4307 IObuff[len++] = '|';
4308 IObuff[len++] = ' ';
4309
4310 /* Remove newlines and leading whitespace from the text.
4311 * For an unrecognized line keep the indent, the compiler may
4312 * mark a word with ^^^^. */
4313 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4314 IObuff + len, IOSIZE - len);
4315
4316 if (ml_append_buf(buf, lnum, IObuff,
4317 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4318 return FAIL;
4319
4320 return OK;
4321}
4322
4323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 * Fill current buffer with quickfix errors, replacing any previous contents.
4325 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004326 * If "old_last" is not NULL append the items after this one.
4327 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4328 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 */
4330 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004331qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004333 linenr_T lnum;
4334 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004335 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
Bram Moolenaar864293a2016-06-02 13:40:04 +02004337 if (old_last == NULL)
4338 {
4339 if (buf != curbuf)
4340 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004341 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004342 return;
4343 }
4344
4345 /* delete all existing lines */
4346 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4347 (void)ml_delete((linenr_T)1, FALSE);
4348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
4350 /* Check if there is anything to display */
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004351 if (!qf_stack_empty(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
Bram Moolenaara796d462018-05-01 14:30:36 +02004353 char_u dirname[MAXPATHL];
4354
4355 *dirname = NUL;
4356
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004358 if (old_last == NULL)
4359 {
4360 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4361 lnum = 0;
4362 }
4363 else
4364 {
4365 qfp = old_last->qf_next;
4366 lnum = buf->b_ml.ml_line_count;
4367 }
4368 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004370 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004372
Bram Moolenaar864293a2016-06-02 13:40:04 +02004373 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004375 if (qfp == NULL)
4376 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004378
4379 if (old_last == NULL)
4380 /* Delete the empty line which is now at the end */
4381 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383
4384 /* correct cursor position */
4385 check_lnums(TRUE);
4386
Bram Moolenaar864293a2016-06-02 13:40:04 +02004387 if (old_last == NULL)
4388 {
4389 /* Set the 'filetype' to "qf" each time after filling the buffer.
4390 * This resembles reading a file into a buffer, it's more logical when
4391 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02004392 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004393 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4394 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
Bram Moolenaar864293a2016-06-02 13:40:04 +02004396 keep_filetype = TRUE; /* don't detect 'filetype' */
4397 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004399 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004401 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004402 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004403
Bram Moolenaar864293a2016-06-02 13:40:04 +02004404 /* make sure it will be redrawn */
4405 redraw_curbuf_later(NOT_VALID);
4406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
4408 /* Restore KeyTyped, setting 'filetype' may reset it. */
4409 KeyTyped = old_KeyTyped;
4410}
4411
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004412/*
4413 * For every change made to the quickfix list, update the changed tick.
4414 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004415 static void
4416qf_list_changed(qf_info_T *qi, int qf_idx)
4417{
4418 qi->qf_lists[qf_idx].qf_changedtick++;
4419}
4420
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004422 * Return the quickfix/location list number with the given identifier.
4423 * Returns -1 if list is not found.
4424 */
4425 static int
4426qf_id2nr(qf_info_T *qi, int_u qfid)
4427{
4428 int qf_idx;
4429
4430 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4431 if (qi->qf_lists[qf_idx].qf_id == qfid)
4432 return qf_idx;
4433 return INVALID_QFIDX;
4434}
4435
4436/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004437 * If the current list is not "save_qfid" and we can find the list with that ID
4438 * then make it the current list.
4439 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004440 * Returns OK if successfully restored the list. Returns FAIL if the list with
4441 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004442 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004443 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004444qf_restore_list(qf_info_T *qi, int_u save_qfid)
4445{
4446 int curlist;
4447
4448 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4449 {
4450 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004451 if (curlist < 0)
4452 // list is not present
4453 return FAIL;
4454 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004455 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004456 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004457}
4458
4459/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004460 * Jump to the first entry if there is one.
4461 */
4462 static void
4463qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4464{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004465 if (qf_restore_list(qi, save_qfid) == FAIL)
4466 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004467
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004468 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004469 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004470 qf_jump(qi, 0, 0, forceit);
4471}
4472
4473/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004474 * Return TRUE when using ":vimgrep" for ":grep".
4475 */
4476 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004477grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004478{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004479 return ((cmdidx == CMD_grep
4480 || cmdidx == CMD_lgrep
4481 || cmdidx == CMD_grepadd
4482 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004483 && STRCMP("internal",
4484 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4485}
4486
4487/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004488 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004490 static char_u *
4491make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004493 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004494 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004495 case CMD_make: return (char_u *)"make";
4496 case CMD_lmake: return (char_u *)"lmake";
4497 case CMD_grep: return (char_u *)"grep";
4498 case CMD_lgrep: return (char_u *)"lgrep";
4499 case CMD_grepadd: return (char_u *)"grepadd";
4500 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4501 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503}
4504
4505/*
4506 * Return the name for the errorfile, in allocated memory.
4507 * Find a new unique name when 'makeef' contains "##".
4508 * Returns NULL for error.
4509 */
4510 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004511get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512{
4513 char_u *p;
4514 char_u *name;
4515 static int start = -1;
4516 static int off = 0;
4517#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004518 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519#endif
4520
4521 if (*p_mef == NUL)
4522 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004523 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 if (name == NULL)
4525 EMSG(_(e_notmp));
4526 return name;
4527 }
4528
4529 for (p = p_mef; *p; ++p)
4530 if (p[0] == '#' && p[1] == '#')
4531 break;
4532
4533 if (*p == NUL)
4534 return vim_strsave(p_mef);
4535
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004536 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 for (;;)
4538 {
4539 if (start == -1)
4540 start = mch_get_pid();
4541 else
4542 off += 19;
4543
4544 name = alloc((unsigned)STRLEN(p_mef) + 30);
4545 if (name == NULL)
4546 break;
4547 STRCPY(name, p_mef);
4548 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4549 STRCAT(name, p + 2);
4550 if (mch_getperm(name) < 0
4551#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004552 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 && mch_lstat((char *)name, &sb) < 0
4554#endif
4555 )
4556 break;
4557 vim_free(name);
4558 }
4559 return name;
4560}
4561
4562/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004563 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4564 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4565 */
4566 static char_u *
4567make_get_fullcmd(char_u *makecmd, char_u *fname)
4568{
4569 char_u *cmd;
4570 unsigned len;
4571
4572 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4573 if (*p_sp != NUL)
4574 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4575 cmd = alloc(len);
4576 if (cmd == NULL)
4577 return NULL;
4578 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4579 (char *)p_shq);
4580
4581 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4582 if (*p_sp != NUL)
4583 append_redir(cmd, len, p_sp, fname);
4584
4585 // Display the fully formed command. Output a newline if there's something
4586 // else than the :make command that was typed (in which case the cursor is
4587 // in column 0).
4588 if (msg_col == 0)
4589 msg_didout = FALSE;
4590 msg_start();
4591 MSG_PUTS(":!");
4592 msg_outtrans(cmd); // show what we are doing
4593
4594 return cmd;
4595}
4596
4597/*
4598 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4599 */
4600 void
4601ex_make(exarg_T *eap)
4602{
4603 char_u *fname;
4604 char_u *cmd;
4605 char_u *enc = NULL;
4606 win_T *wp = NULL;
4607 qf_info_T *qi = &ql_info;
4608 int res;
4609 char_u *au_name = NULL;
4610 int_u save_qfid;
4611
4612 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4613 if (grep_internal(eap->cmdidx))
4614 {
4615 ex_vimgrep(eap);
4616 return;
4617 }
4618
4619 au_name = make_get_auname(eap->cmdidx);
4620 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4621 curbuf->b_fname, TRUE, curbuf))
4622 {
4623#ifdef FEAT_EVAL
4624 if (aborting())
4625 return;
4626#endif
4627 }
4628#ifdef FEAT_MBYTE
4629 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4630#endif
4631
4632 if (is_loclist_cmd(eap->cmdidx))
4633 wp = curwin;
4634
4635 autowrite_all();
4636 fname = get_mef_name();
4637 if (fname == NULL)
4638 return;
4639 mch_remove(fname); // in case it's not unique
4640
4641 cmd = make_get_fullcmd(eap->arg, fname);
4642 if (cmd == NULL)
4643 return;
4644
4645 // let the shell know if we are redirecting output or not
4646 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4647
4648#ifdef AMIGA
4649 out_flush();
4650 // read window status report and redraw before message
4651 (void)char_avail();
4652#endif
4653
4654 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4655 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4656 (eap->cmdidx != CMD_grepadd
4657 && eap->cmdidx != CMD_lgrepadd),
4658 qf_cmdtitle(*eap->cmdlinep), enc);
4659 if (wp != NULL)
4660 {
4661 qi = GET_LOC_LIST(wp);
4662 if (qi == NULL)
4663 goto cleanup;
4664 }
4665 if (res >= 0)
4666 qf_list_changed(qi, qi->qf_curlist);
4667
4668 // Remember the current quickfix list identifier, so that we can
4669 // check for autocommands changing the current quickfix list.
4670 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4671 if (au_name != NULL)
4672 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4673 curbuf->b_fname, TRUE, curbuf);
4674 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4675 // display the first error
4676 qf_jump_first(qi, save_qfid, FALSE);
4677
4678cleanup:
4679 mch_remove(fname);
4680 vim_free(fname);
4681 vim_free(cmd);
4682}
4683
4684/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004685 * Returns the number of valid entries in the current quickfix/location list.
4686 */
4687 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004688qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004689{
4690 qf_info_T *qi = &ql_info;
4691 qfline_T *qfp;
4692 int i, sz = 0;
4693 int prev_fnum = 0;
4694
Bram Moolenaar39665952018-08-15 20:59:48 +02004695 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004696 {
4697 /* Location list */
4698 qi = GET_LOC_LIST(curwin);
4699 if (qi == NULL)
4700 return 0;
4701 }
4702
4703 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004704 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004705 ++i, qfp = qfp->qf_next)
4706 {
4707 if (qfp->qf_valid)
4708 {
4709 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4710 sz++; /* Count all valid entries */
4711 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4712 {
4713 /* Count the number of files */
4714 sz++;
4715 prev_fnum = qfp->qf_fnum;
4716 }
4717 }
4718 }
4719
4720 return sz;
4721}
4722
4723/*
4724 * Returns the current index of the quickfix/location list.
4725 * Returns 0 if there is an error.
4726 */
4727 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004728qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004729{
4730 qf_info_T *qi = &ql_info;
4731
Bram Moolenaar39665952018-08-15 20:59:48 +02004732 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004733 {
4734 /* Location list */
4735 qi = GET_LOC_LIST(curwin);
4736 if (qi == NULL)
4737 return 0;
4738 }
4739
4740 return qi->qf_lists[qi->qf_curlist].qf_index;
4741}
4742
4743/*
4744 * Returns the current index in the quickfix/location list (counting only valid
4745 * entries). If no valid entries are in the list, then returns 1.
4746 */
4747 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004748qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004749{
4750 qf_info_T *qi = &ql_info;
4751 qf_list_T *qfl;
4752 qfline_T *qfp;
4753 int i, eidx = 0;
4754 int prev_fnum = 0;
4755
Bram Moolenaar39665952018-08-15 20:59:48 +02004756 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004757 {
4758 /* Location list */
4759 qi = GET_LOC_LIST(curwin);
4760 if (qi == NULL)
4761 return 1;
4762 }
4763
4764 qfl = &qi->qf_lists[qi->qf_curlist];
4765 qfp = qfl->qf_start;
4766
4767 /* check if the list has valid errors */
4768 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4769 return 1;
4770
4771 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4772 {
4773 if (qfp->qf_valid)
4774 {
4775 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4776 {
4777 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4778 {
4779 /* Count the number of files */
4780 eidx++;
4781 prev_fnum = qfp->qf_fnum;
4782 }
4783 }
4784 else
4785 eidx++;
4786 }
4787 }
4788
4789 return eidx ? eidx : 1;
4790}
4791
4792/*
4793 * Get the 'n'th valid error entry in the quickfix or location list.
4794 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4795 * For :cdo and :ldo returns the 'n'th valid error entry.
4796 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4797 */
4798 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004799qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004800{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004801 qfline_T *qfp = qfl->qf_start;
4802 int i, eidx;
4803 int prev_fnum = 0;
4804
4805 /* check if the list has valid errors */
4806 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4807 return 1;
4808
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004809 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004810 i++, qfp = qfp->qf_next)
4811 {
4812 if (qfp->qf_valid)
4813 {
4814 if (fdo)
4815 {
4816 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4817 {
4818 /* Count the number of files */
4819 eidx++;
4820 prev_fnum = qfp->qf_fnum;
4821 }
4822 }
4823 else
4824 eidx++;
4825 }
4826
4827 if (eidx == n)
4828 break;
4829 }
4830
4831 if (i <= qfl->qf_count)
4832 return i;
4833 else
4834 return 1;
4835}
4836
4837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004839 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004840 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 */
4842 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004843ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004845 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004846 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004847
Bram Moolenaar39665952018-08-15 20:59:48 +02004848 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004849 {
4850 qi = GET_LOC_LIST(curwin);
4851 if (qi == NULL)
4852 {
4853 EMSG(_(e_loclist));
4854 return;
4855 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004856 }
4857
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004858 if (eap->addr_count > 0)
4859 errornr = (int)eap->line2;
4860 else
4861 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004862 switch (eap->cmdidx)
4863 {
4864 case CMD_cc: case CMD_ll:
4865 errornr = 0;
4866 break;
4867 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4868 case CMD_lfirst:
4869 errornr = 1;
4870 break;
4871 default:
4872 errornr = 32767;
4873 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004874 }
4875
4876 /* For cdo and ldo commands, jump to the nth valid error.
4877 * For cfdo and lfdo commands, jump to the nth valid file entry.
4878 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004879 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4880 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004881 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004882 eap->addr_count > 0 ? (int)eap->line1 : 1,
4883 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4884
4885 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886}
4887
4888/*
4889 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004890 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004891 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 */
4893 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004894ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004896 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004897 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004898 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004899
Bram Moolenaar39665952018-08-15 20:59:48 +02004900 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004901 {
4902 qi = GET_LOC_LIST(curwin);
4903 if (qi == NULL)
4904 {
4905 EMSG(_(e_loclist));
4906 return;
4907 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004908 }
4909
Bram Moolenaar55b69262017-08-13 13:42:01 +02004910 if (eap->addr_count > 0
4911 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4912 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004913 errornr = (int)eap->line2;
4914 else
4915 errornr = 1;
4916
Bram Moolenaar39665952018-08-15 20:59:48 +02004917 // Depending on the command jump to either next or previous entry/file.
4918 switch (eap->cmdidx)
4919 {
4920 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4921 dir = FORWARD;
4922 break;
4923 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4924 case CMD_lNext:
4925 dir = BACKWARD;
4926 break;
4927 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4928 dir = FORWARD_FILE;
4929 break;
4930 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4931 dir = BACKWARD_FILE;
4932 break;
4933 default:
4934 dir = FORWARD;
4935 break;
4936 }
4937
4938 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939}
4940
4941/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004942 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004943 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 */
4945 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004946ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004948 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004949 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004950 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004951 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004952 int_u save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004953 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004954
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004955 switch (eap->cmdidx)
4956 {
4957 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4958 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4959 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4960 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4961 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4962 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4963 default: break;
4964 }
4965 if (au_name != NULL)
4966 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004967#ifdef FEAT_MBYTE
4968 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4969#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004970#ifdef FEAT_BROWSE
4971 if (cmdmod.browse)
4972 {
4973 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004974 NULL, NULL,
4975 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004976 if (browse_file == NULL)
4977 return;
4978 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4979 vim_free(browse_file);
4980 }
4981 else
4982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004984 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004985
Bram Moolenaar39665952018-08-15 20:59:48 +02004986 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004987 wp = curwin;
4988
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004989 /*
4990 * This function is used by the :cfile, :cgetfile and :caddfile
4991 * commands.
4992 * :cfile always creates a new quickfix list and jumps to the
4993 * first error.
4994 * :cgetfile creates a new quickfix list but doesn't jump to the
4995 * first error.
4996 * :caddfile adds to an existing quickfix list. If there is no
4997 * quickfix list then a new list is created.
4998 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004999 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005000 && eap->cmdidx != CMD_laddfile),
5001 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005002 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005003 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005004 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005005 if (qi == NULL)
5006 return;
5007 }
5008 if (res >= 0)
Bram Moolenaarb254af32017-12-18 19:48:58 +01005009 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005010 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005011 if (au_name != NULL)
5012 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005013
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005014 // Jump to the first error for a new list and if autocmds didn't
5015 // free the list.
5016 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5017 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005018 // display the first error
5019 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005020}
5021
5022/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005023 * Return the vimgrep autocmd name.
5024 */
5025 static char_u *
5026vgr_get_auname(cmdidx_T cmdidx)
5027{
5028 switch (cmdidx)
5029 {
5030 case CMD_vimgrep: return (char_u *)"vimgrep";
5031 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5032 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5033 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5034 case CMD_grep: return (char_u *)"grep";
5035 case CMD_lgrep: return (char_u *)"lgrep";
5036 case CMD_grepadd: return (char_u *)"grepadd";
5037 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5038 default: return NULL;
5039 }
5040}
5041
5042/*
5043 * Initialize the regmatch used by vimgrep for pattern "s".
5044 */
5045 static void
5046vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5047{
5048 /* Get the search pattern: either white-separated or enclosed in // */
5049 regmatch->regprog = NULL;
5050
5051 if (s == NULL || *s == NUL)
5052 {
5053 /* Pattern is empty, use last search pattern. */
5054 if (last_search_pat() == NULL)
5055 {
5056 EMSG(_(e_noprevre));
5057 return;
5058 }
5059 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5060 }
5061 else
5062 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5063
5064 regmatch->rmm_ic = p_ic;
5065 regmatch->rmm_maxcol = 0;
5066}
5067
5068/*
5069 * Display a file name when vimgrep is running.
5070 */
5071 static void
5072vgr_display_fname(char_u *fname)
5073{
5074 char_u *p;
5075
5076 msg_start();
5077 p = msg_strtrunc(fname, TRUE);
5078 if (p == NULL)
5079 msg_outtrans(fname);
5080 else
5081 {
5082 msg_outtrans(p);
5083 vim_free(p);
5084 }
5085 msg_clr_eos();
5086 msg_didout = FALSE; /* overwrite this message */
5087 msg_nowait = TRUE; /* don't wait for this message */
5088 msg_col = 0;
5089 out_flush();
5090}
5091
5092/*
5093 * Load a dummy buffer to search for a pattern using vimgrep.
5094 */
5095 static buf_T *
5096vgr_load_dummy_buf(
5097 char_u *fname,
5098 char_u *dirname_start,
5099 char_u *dirname_now)
5100{
5101 int save_mls;
5102#if defined(FEAT_SYN_HL)
5103 char_u *save_ei = NULL;
5104#endif
5105 buf_T *buf;
5106
5107#if defined(FEAT_SYN_HL)
5108 /* Don't do Filetype autocommands to avoid loading syntax and
5109 * indent scripts, a great speed improvement. */
5110 save_ei = au_event_disable(",Filetype");
5111#endif
5112 /* Don't use modelines here, it's useless. */
5113 save_mls = p_mls;
5114 p_mls = 0;
5115
5116 /* Load file into a buffer, so that 'fileencoding' is detected,
5117 * autocommands applied, etc. */
5118 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5119
5120 p_mls = save_mls;
5121#if defined(FEAT_SYN_HL)
5122 au_event_restore(save_ei);
5123#endif
5124
5125 return buf;
5126}
5127
5128/*
5129 * Check whether a quickfix/location list valid. Autocmds may remove or change
5130 * a quickfix list when vimgrep is running. If the list is not found, create a
5131 * new list.
5132 */
5133 static int
5134vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005135 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005136 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005137 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005138 char_u *title)
5139{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005140 /* Verify that the quickfix/location list was not freed by an autocmd */
5141 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005142 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005143 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005144 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005145 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005146 EMSG(_(e_loc_list_changed));
5147 return FALSE;
5148 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005149 else
5150 {
5151 /* Quickfix list is not found, create a new one. */
5152 qf_new_list(qi, title);
5153 return TRUE;
5154 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005155 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005156
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005157 if (qf_restore_list(qi, qfid) == FAIL)
5158 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005159
5160 return TRUE;
5161}
5162
5163/*
5164 * Search for a pattern in all the lines in a buffer and add the matching lines
5165 * to a quickfix list.
5166 */
5167 static int
5168vgr_match_buflines(
5169 qf_info_T *qi,
5170 char_u *fname,
5171 buf_T *buf,
5172 regmmatch_T *regmatch,
5173 long tomatch,
5174 int duplicate_name,
5175 int flags)
5176{
5177 int found_match = FALSE;
5178 long lnum;
5179 colnr_T col;
5180
5181 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5182 {
5183 col = 0;
5184 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5185 col, NULL, NULL) > 0)
5186 {
5187 /* Pass the buffer number so that it gets used even for a
5188 * dummy buffer, unless duplicate_name is set, then the
5189 * buffer will be wiped out below. */
5190 if (qf_add_entry(qi,
5191 qi->qf_curlist,
5192 NULL, /* dir */
5193 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005194 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005195 duplicate_name ? 0 : buf->b_fnum,
5196 ml_get_buf(buf,
5197 regmatch->startpos[0].lnum + lnum, FALSE),
5198 regmatch->startpos[0].lnum + lnum,
5199 regmatch->startpos[0].col + 1,
5200 FALSE, /* vis_col */
5201 NULL, /* search pattern */
5202 0, /* nr */
5203 0, /* type */
5204 TRUE /* valid */
5205 ) == FAIL)
5206 {
5207 got_int = TRUE;
5208 break;
5209 }
5210 found_match = TRUE;
5211 if (--tomatch == 0)
5212 break;
5213 if ((flags & VGR_GLOBAL) == 0
5214 || regmatch->endpos[0].lnum > 0)
5215 break;
5216 col = regmatch->endpos[0].col
5217 + (col == regmatch->endpos[0].col);
5218 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5219 break;
5220 }
5221 line_breakcheck();
5222 if (got_int)
5223 break;
5224 }
5225
5226 return found_match;
5227}
5228
5229/*
5230 * Jump to the first match and update the directory.
5231 */
5232 static void
5233vgr_jump_to_match(
5234 qf_info_T *qi,
5235 int forceit,
5236 int *redraw_for_dummy,
5237 buf_T *first_match_buf,
5238 char_u *target_dir)
5239{
5240 buf_T *buf;
5241
5242 buf = curbuf;
5243 qf_jump(qi, 0, 0, forceit);
5244 if (buf != curbuf)
5245 /* If we jumped to another buffer redrawing will already be
5246 * taken care of. */
5247 *redraw_for_dummy = FALSE;
5248
5249 /* Jump to the directory used after loading the buffer. */
5250 if (curbuf == first_match_buf && target_dir != NULL)
5251 {
5252 exarg_T ea;
5253
5254 ea.arg = target_dir;
5255 ea.cmdidx = CMD_lcd;
5256 ex_cd(&ea);
5257 }
5258}
5259
5260/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005261 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005262 * ":vimgrepadd {pattern} file(s)"
5263 * ":lvimgrep {pattern} file(s)"
5264 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005265 */
5266 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005267ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005268{
Bram Moolenaar81695252004-12-29 20:58:21 +00005269 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005270 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005271 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005272 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005273 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005274 char_u *s;
5275 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005276 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005277 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005278 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005279 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005280 buf_T *buf;
5281 int duplicate_name = FALSE;
5282 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005283 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005284 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005285 buf_T *first_match_buf = NULL;
5286 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005287 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005288 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005289 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005290 char_u *dirname_start = NULL;
5291 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005292 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005293 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005294
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005295 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005296 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5297 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005298 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005299#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005300 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005301 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005302#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005303 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005304
Bram Moolenaar39665952018-08-15 20:59:48 +02005305 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005306 {
5307 qi = ll_get_or_alloc_list(curwin);
5308 if (qi == NULL)
5309 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005310 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005311 }
5312
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005313 if (eap->addr_count > 0)
5314 tomatch = eap->line2;
5315 else
5316 tomatch = MAXLNUM;
5317
Bram Moolenaar81695252004-12-29 20:58:21 +00005318 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005319 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005320 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005321 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005322 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005323 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005324 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005325 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005326 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005327
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005328 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005329 if (regmatch.regprog == NULL)
5330 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005331
5332 p = skipwhite(p);
5333 if (*p == NUL)
5334 {
5335 EMSG(_("E683: File name missing or invalid pattern"));
5336 goto theend;
5337 }
5338
Bram Moolenaar55b69262017-08-13 13:42:01 +02005339 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005340 && eap->cmdidx != CMD_vimgrepadd
5341 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005342 || qf_stack_empty(qi))
Bram Moolenaar86b68352004-12-27 21:59:20 +00005343 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005344 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005345
5346 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005347 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005348 goto theend;
5349 if (fcount == 0)
5350 {
5351 EMSG(_(e_nomatch));
5352 goto theend;
5353 }
5354
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005355 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5356 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005357 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005358 {
5359 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005360 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005361 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005362
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005363 /* Remember the current directory, because a BufRead autocommand that does
5364 * ":lcd %:p:h" changes the meaning of short path names. */
5365 mch_dirname(dirname_start, MAXPATHL);
5366
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005367 /* Remember the current quickfix list identifier, so that we can check for
5368 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005369 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005370
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005371 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005372 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005373 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005374 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005375 if (time(NULL) > seconds)
5376 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005377 /* Display the file name every second or so, show the user we are
5378 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005379 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005380 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005381 }
5382
Bram Moolenaar81695252004-12-29 20:58:21 +00005383 buf = buflist_findname_exp(fnames[fi]);
5384 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5385 {
5386 /* Remember that a buffer with this name already exists. */
5387 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005388 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005389 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005390
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005391 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005392 }
5393 else
5394 /* Use existing, loaded buffer. */
5395 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005396
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005397 /* Check whether the quickfix list is still valid. When loading a
5398 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005399 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005400 {
5401 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005402 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005403 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005404 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005405
Bram Moolenaar81695252004-12-29 20:58:21 +00005406 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005407 {
5408 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005409 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005410 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005411 else
5412 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005413 /* Try for a match in all lines of the buffer.
5414 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005415 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5416 tomatch, duplicate_name, flags);
5417
Bram Moolenaar81695252004-12-29 20:58:21 +00005418 if (using_dummy)
5419 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005420 if (found_match && first_match_buf == NULL)
5421 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005422 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005423 {
Bram Moolenaar81695252004-12-29 20:58:21 +00005424 /* Never keep a dummy buffer if there is another buffer
5425 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005426 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005427 buf = NULL;
5428 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005429 else if (!cmdmod.hide
5430 || buf->b_p_bh[0] == 'u' /* "unload" */
5431 || buf->b_p_bh[0] == 'w' /* "wipe" */
5432 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00005433 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005434 /* When no match was found we don't need to remember the
5435 * buffer, wipe it out. If there was a match and it
5436 * wasn't the first one or we won't jump there: only
5437 * unload the buffer.
5438 * Ignore 'hidden' here, because it may lead to having too
5439 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00005440 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005441 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005442 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005443 buf = NULL;
5444 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005445 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005446 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005447 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02005448 /* Keeping the buffer, remove the dummy flag. */
5449 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005450 buf = NULL;
5451 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005452 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005453
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005454 if (buf != NULL)
5455 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02005456 /* Keeping the buffer, remove the dummy flag. */
5457 buf->b_flags &= ~BF_DUMMY;
5458
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005459 /* If the buffer is still loaded we need to use the
5460 * directory we jumped to below. */
5461 if (buf == first_match_buf
5462 && target_dir == NULL
5463 && STRCMP(dirname_start, dirname_now) != 0)
5464 target_dir = vim_strsave(dirname_now);
5465
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005466 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005467 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00005468 * need to be done (again). But not the window-local
5469 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005470 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005471#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005472 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5473 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005474#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005475 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005476 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005477 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005478 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005479 }
5480 }
5481
5482 FreeWild(fcount, fnames);
5483
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005484 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5485 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5486 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005487 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005488
Bram Moolenaar864293a2016-06-02 13:40:04 +02005489 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005490
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005491 if (au_name != NULL)
5492 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5493 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005494 /*
5495 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5496 * is still valid.
5497 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005498 if (!qflist_valid(wp, save_qfid))
5499 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005500
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005501 if (qf_restore_list(qi, save_qfid) == FAIL)
5502 goto theend;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005503
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005504 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005505 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005506 {
5507 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005508 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5509 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005510 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005511 else
5512 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005513
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005514 /* If we loaded a dummy buffer into the current window, the autocommands
5515 * may have messed up things, need to redraw and recompute folds. */
5516 if (redraw_for_dummy)
5517 {
5518#ifdef FEAT_FOLDING
5519 foldUpdateAll(curwin);
5520#else
5521 redraw_later(NOT_VALID);
5522#endif
5523 }
5524
Bram Moolenaar86b68352004-12-27 21:59:20 +00005525theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005526 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005527 vim_free(dirname_now);
5528 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005529 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005530 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005531}
5532
5533/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005534 * Restore current working directory to "dirname_start" if they differ, taking
5535 * into account whether it is set locally or globally.
5536 */
5537 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005538restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005539{
5540 char_u *dirname_now = alloc(MAXPATHL);
5541
5542 if (NULL != dirname_now)
5543 {
5544 mch_dirname(dirname_now, MAXPATHL);
5545 if (STRCMP(dirname_start, dirname_now) != 0)
5546 {
5547 /* If the directory has changed, change it back by building up an
5548 * appropriate ex command and executing it. */
5549 exarg_T ea;
5550
5551 ea.arg = dirname_start;
5552 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5553 ex_cd(&ea);
5554 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005555 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005556 }
5557}
5558
5559/*
5560 * Load file "fname" into a dummy buffer and return the buffer pointer,
5561 * placing the directory resulting from the buffer load into the
5562 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5563 * prior to calling this function. Restores directory to "dirname_start" prior
5564 * to returning, if autocmds or the 'autochdir' option have changed it.
5565 *
5566 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5567 * or wipe_dummy_buffer() later!
5568 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005569 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005570 */
5571 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005572load_dummy_buffer(
5573 char_u *fname,
5574 char_u *dirname_start, /* in: old directory */
5575 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005576{
5577 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005578 bufref_T newbufref;
5579 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005580 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005581 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005582 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005583
5584 /* Allocate a buffer without putting it in the buffer list. */
5585 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5586 if (newbuf == NULL)
5587 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005588 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005589
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005590 /* Init the options. */
5591 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5592
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005593 /* need to open the memfile before putting the buffer in a window */
5594 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005595 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005596 /* Make sure this buffer isn't wiped out by autocommands. */
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005597 ++newbuf->b_locked;
5598
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005599 /* set curwin/curbuf to buf and save a few things */
5600 aucmd_prepbuf(&aco, newbuf);
5601
5602 /* Need to set the filename for autocommands. */
5603 (void)setfname(curbuf, fname, NULL, FALSE);
5604
Bram Moolenaar81695252004-12-29 20:58:21 +00005605 /* Create swap file now to avoid the ATTENTION message. */
5606 check_need_swap(TRUE);
5607
5608 /* Remove the "dummy" flag, otherwise autocommands may not
5609 * work. */
5610 curbuf->b_flags &= ~BF_DUMMY;
5611
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005612 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005613 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005614 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005615 NULL, READ_NEW | READ_DUMMY);
5616 --newbuf->b_locked;
5617 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005618 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005619 && !(curbuf->b_flags & BF_NEW))
5620 {
5621 failed = FALSE;
5622 if (curbuf != newbuf)
5623 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005624 /* Bloody autocommands changed the buffer! Can happen when
5625 * using netrw and editing a remote file. Use the current
5626 * buffer instead, delete the dummy one after restoring the
5627 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005628 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005629 newbuf = curbuf;
5630 }
5631 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005632
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005633 /* restore curwin/curbuf and a few other things */
5634 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005635 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5636 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005637
5638 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5639 * skip it. */
5640 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005641 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005642
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005643 /*
5644 * When autocommands/'autochdir' option changed directory: go back.
5645 * Let the caller know what the resulting dir was first, in case it is
5646 * important.
5647 */
5648 mch_dirname(resulting_dir, MAXPATHL);
5649 restore_start_dir(dirname_start);
5650
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005651 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005652 return NULL;
5653 if (failed)
5654 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005655 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005656 return NULL;
5657 }
5658 return newbuf;
5659}
5660
5661/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005662 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5663 * directory to "dirname_start" prior to returning, if autocmds or the
5664 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005665 */
5666 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005667wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005668{
5669 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005670 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005671#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005672 cleanup_T cs;
5673
5674 /* Reset the error/interrupt/exception state here so that aborting()
5675 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5676 * work when got_int is set. */
5677 enter_cleanup(&cs);
5678#endif
5679
Bram Moolenaar81695252004-12-29 20:58:21 +00005680 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005681
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005682#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005683 /* Restore the error/interrupt/exception state if not discarded by a
5684 * new aborting error, interrupt, or uncaught exception. */
5685 leave_cleanup(&cs);
5686#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005687 /* When autocommands/'autochdir' option changed directory: go back. */
5688 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005689 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005690}
5691
5692/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005693 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5694 * directory to "dirname_start" prior to returning, if autocmds or the
5695 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005696 */
5697 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005698unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005699{
5700 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005701 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005702 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005703
5704 /* When autocommands/'autochdir' option changed directory: go back. */
5705 restore_start_dir(dirname_start);
5706 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005707}
5708
Bram Moolenaar05159a02005-02-26 23:04:13 +00005709#if defined(FEAT_EVAL) || defined(PROTO)
5710/*
5711 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005712 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005713 */
5714 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005715get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005716{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005717 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005718 dict_T *dict;
5719 char_u buf[2];
5720 qfline_T *qfp;
5721 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005722 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005723
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005724 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005725 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005726 qi = &ql_info;
5727 if (wp != NULL)
5728 {
5729 qi = GET_LOC_LIST(wp);
5730 if (qi == NULL)
5731 return FAIL;
5732 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005733 }
5734
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005735 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005736 qf_idx = qi->qf_curlist;
5737
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005738 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005739 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005740
Bram Moolenaard823fa92016-08-12 16:29:27 +02005741 qfp = qi->qf_lists[qf_idx].qf_start;
5742 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005743 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005744 /* Handle entries with a non-existing buffer number. */
5745 bufnum = qfp->qf_fnum;
5746 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5747 bufnum = 0;
5748
Bram Moolenaar05159a02005-02-26 23:04:13 +00005749 if ((dict = dict_alloc()) == NULL)
5750 return FAIL;
5751 if (list_append_dict(list, dict) == FAIL)
5752 return FAIL;
5753
5754 buf[0] = qfp->qf_type;
5755 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005756 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5757 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5758 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5759 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5760 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5761 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5762 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5763 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5764 || dict_add_string(dict, "type", buf) == FAIL
5765 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005766 return FAIL;
5767
5768 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005769 if (qfp == NULL)
5770 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005771 }
5772 return OK;
5773}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005774
5775/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005776 * Flags used by getqflist()/getloclist() to determine which fields to return.
5777 */
5778enum {
5779 QF_GETLIST_NONE = 0x0,
5780 QF_GETLIST_TITLE = 0x1,
5781 QF_GETLIST_ITEMS = 0x2,
5782 QF_GETLIST_NR = 0x4,
5783 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005784 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005785 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005786 QF_GETLIST_IDX = 0x40,
5787 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005788 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005789 QF_GETLIST_FILEWINID = 0x200,
5790 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005791};
5792
5793/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005794 * Parse text from 'di' and return the quickfix list items.
5795 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005796 */
5797 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005798qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005799{
5800 int status = FAIL;
5801 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005802 char_u *errorformat = p_efm;
5803 dictitem_T *efm_di;
5804 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005805
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005806 /* Only a List value is supported */
5807 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005808 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005809 /* If errorformat is supplied then use it, otherwise use the 'efm'
5810 * option setting
5811 */
5812 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5813 {
5814 if (efm_di->di_tv.v_type != VAR_STRING ||
5815 efm_di->di_tv.vval.v_string == NULL)
5816 return FAIL;
5817 errorformat = efm_di->di_tv.vval.v_string;
5818 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005819
Bram Moolenaar36538222017-09-02 19:51:44 +02005820 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005821 if (l == NULL)
5822 return FAIL;
5823
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005824 qi = ll_new_list();
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005825 if (qi != NULL)
5826 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005827 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005828 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5829 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005830 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005831 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005832 }
5833 free(qi);
5834 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005835 dict_add_list(retdict, "items", l);
5836 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005837 }
5838
5839 return status;
5840}
5841
5842/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005843 * Return the quickfix/location list window identifier in the current tabpage.
5844 */
5845 static int
5846qf_winid(qf_info_T *qi)
5847{
5848 win_T *win;
5849
5850 /* The quickfix window can be opened even if the quickfix list is not set
5851 * using ":copen". This is not true for location lists. */
5852 if (qi == NULL)
5853 return 0;
5854 win = qf_find_win(qi);
5855 if (win != NULL)
5856 return win->w_id;
5857 return 0;
5858}
5859
5860/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005861 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005862 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005863 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005864qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005865{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005866 int flags = QF_GETLIST_NONE;
5867
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005868 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005869 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005870 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005871 if (!loclist)
5872 // File window ID is applicable only to location list windows
5873 flags &= ~ QF_GETLIST_FILEWINID;
5874 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005875
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005876 if (dict_find(what, (char_u *)"title", -1) != NULL)
5877 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005878
Bram Moolenaara6d48492017-12-12 22:45:31 +01005879 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5880 flags |= QF_GETLIST_NR;
5881
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005882 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5883 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005884
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005885 if (dict_find(what, (char_u *)"context", -1) != NULL)
5886 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005887
Bram Moolenaara6d48492017-12-12 22:45:31 +01005888 if (dict_find(what, (char_u *)"id", -1) != NULL)
5889 flags |= QF_GETLIST_ID;
5890
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005891 if (dict_find(what, (char_u *)"items", -1) != NULL)
5892 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005893
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005894 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5895 flags |= QF_GETLIST_IDX;
5896
5897 if (dict_find(what, (char_u *)"size", -1) != NULL)
5898 flags |= QF_GETLIST_SIZE;
5899
Bram Moolenaarb254af32017-12-18 19:48:58 +01005900 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5901 flags |= QF_GETLIST_TICK;
5902
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005903 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5904 flags |= QF_GETLIST_FILEWINID;
5905
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005906 return flags;
5907}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005908
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005909/*
5910 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5911 * If 'nr' and 'id' are not present in 'what' then return the current
5912 * quickfix list index.
5913 * If 'nr' is zero then return the current quickfix list index.
5914 * If 'nr' is '$' then return the last quickfix list index.
5915 * If 'id' is present then return the index of the quickfix list with that id.
5916 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5917 * Return -1, if quickfix list is not present or if the stack is empty.
5918 */
5919 static int
5920qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5921{
5922 int qf_idx;
5923 dictitem_T *di;
5924
5925 qf_idx = qi->qf_curlist; /* default is the current list */
5926 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5927 {
5928 /* Use the specified quickfix/location list */
5929 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005930 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005931 /* for zero use the current list */
5932 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005933 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005934 qf_idx = di->di_tv.vval.v_number - 1;
5935 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005936 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005937 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005938 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005939 else if (di->di_tv.v_type == VAR_STRING
5940 && di->di_tv.vval.v_string != NULL
5941 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5942 /* Get the last quickfix list number */
5943 qf_idx = qi->qf_listcount - 1;
5944 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005945 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005946 }
5947
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005948 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5949 {
5950 /* Look for a list with the specified id */
5951 if (di->di_tv.v_type == VAR_NUMBER)
5952 {
5953 /*
5954 * For zero, use the current list or the list specified by 'nr'
5955 */
5956 if (di->di_tv.vval.v_number != 0)
5957 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5958 }
5959 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005960 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005961 }
5962
5963 return qf_idx;
5964}
5965
5966/*
5967 * Return default values for quickfix list properties in retdict.
5968 */
5969 static int
5970qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5971{
5972 int status = OK;
5973
5974 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005975 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005976 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5977 {
5978 list_T *l = list_alloc();
5979 if (l != NULL)
5980 status = dict_add_list(retdict, "items", l);
5981 else
5982 status = FAIL;
5983 }
5984 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005985 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005986 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005987 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005988 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005989 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005990 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005991 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005992 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005993 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005994 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005995 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005996 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005997 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005998 if ((status == OK) && (qi != &ql_info) && (flags & QF_GETLIST_FILEWINID))
5999 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006000
6001 return status;
6002}
6003
6004/*
6005 * Return the quickfix list title as 'title' in retdict
6006 */
6007 static int
6008qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
6009{
Bram Moolenaare0be1672018-07-08 16:50:37 +02006010 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006011}
6012
6013/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006014 * Returns the identifier of the window used to display files from a location
6015 * list. If there is no associated window, then returns 0. Useful only when
6016 * called from a location list window.
6017 */
6018 static int
6019qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6020{
6021 int winid = 0;
6022
6023 if (wp != NULL && IS_LL_WINDOW(wp))
6024 {
6025 win_T *ll_wp = qf_find_win_with_loclist(qi);
6026 if (ll_wp != NULL)
6027 winid = ll_wp->w_id;
6028 }
6029
6030 return dict_add_number(retdict, "filewinid", winid);
6031}
6032
6033/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006034 * Return the quickfix list items/entries as 'items' in retdict
6035 */
6036 static int
6037qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6038{
6039 int status = OK;
6040 list_T *l = list_alloc();
6041 if (l != NULL)
6042 {
6043 (void)get_errorlist(qi, NULL, qf_idx, l);
6044 dict_add_list(retdict, "items", l);
6045 }
6046 else
6047 status = FAIL;
6048
6049 return status;
6050}
6051
6052/*
6053 * Return the quickfix list context (if any) as 'context' in retdict.
6054 */
6055 static int
6056qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6057{
6058 int status;
6059 dictitem_T *di;
6060
6061 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
6062 {
6063 di = dictitem_alloc((char_u *)"context");
6064 if (di != NULL)
6065 {
6066 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
6067 status = dict_add(retdict, di);
6068 if (status == FAIL)
6069 dictitem_free(di);
6070 }
6071 else
6072 status = FAIL;
6073 }
6074 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006075 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006076
6077 return status;
6078}
6079
6080/*
6081 * Return the quickfix list index as 'idx' in retdict
6082 */
6083 static int
6084qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6085{
6086 int idx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006087 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006088 /* For empty lists, qf_index is set to 1 */
6089 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02006090 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006091}
6092
6093/*
6094 * Return quickfix/location list details (title) as a
6095 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6096 * then current list is used. Otherwise the specified list is used.
6097 */
6098 int
6099qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6100{
6101 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006102 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006103 int status = OK;
6104 int qf_idx;
6105 dictitem_T *di;
6106 int flags = QF_GETLIST_NONE;
6107
6108 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6109 return qf_get_list_from_lines(what, di, retdict);
6110
6111 if (wp != NULL)
6112 qi = GET_LOC_LIST(wp);
6113
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006114 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006115
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006116 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006117 qf_idx = qf_getprop_qfidx(qi, what);
6118
Bram Moolenaara6d48492017-12-12 22:45:31 +01006119 /* List is not present or is empty */
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006120 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006121 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006122
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006123 qfl = &qi->qf_lists[qf_idx];
6124
Bram Moolenaard823fa92016-08-12 16:29:27 +02006125 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006126 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006127 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006128 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006129 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006130 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006131 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006132 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006133 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006134 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006135 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006136 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006137 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006138 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006139 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006140 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006141 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006142 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006143 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6144 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006145
Bram Moolenaard823fa92016-08-12 16:29:27 +02006146 return status;
6147}
6148
6149/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006150 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
6151 * items in the dict 'd'.
6152 */
6153 static int
6154qf_add_entry_from_dict(
6155 qf_info_T *qi,
6156 int qf_idx,
6157 dict_T *d,
6158 int first_entry)
6159{
6160 static int did_bufnr_emsg;
6161 char_u *filename, *module, *pattern, *text, *type;
6162 int bufnum, valid, status, col, vcol, nr;
6163 long lnum;
6164
6165 if (first_entry)
6166 did_bufnr_emsg = FALSE;
6167
6168 filename = get_dict_string(d, (char_u *)"filename", TRUE);
6169 module = get_dict_string(d, (char_u *)"module", TRUE);
6170 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6171 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6172 col = (int)get_dict_number(d, (char_u *)"col");
6173 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6174 nr = (int)get_dict_number(d, (char_u *)"nr");
6175 type = get_dict_string(d, (char_u *)"type", TRUE);
6176 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6177 text = get_dict_string(d, (char_u *)"text", TRUE);
6178 if (text == NULL)
6179 text = vim_strsave((char_u *)"");
6180
6181 valid = TRUE;
6182 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6183 valid = FALSE;
6184
6185 // Mark entries with non-existing buffer number as not valid. Give the
6186 // error message only once.
6187 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6188 {
6189 if (!did_bufnr_emsg)
6190 {
6191 did_bufnr_emsg = TRUE;
6192 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6193 }
6194 valid = FALSE;
6195 bufnum = 0;
6196 }
6197
6198 // If the 'valid' field is present it overrules the detected value.
6199 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6200 valid = (int)get_dict_number(d, (char_u *)"valid");
6201
6202 status = qf_add_entry(qi,
6203 qf_idx,
6204 NULL, // dir
6205 filename,
6206 module,
6207 bufnum,
6208 text,
6209 lnum,
6210 col,
6211 vcol, // vis_col
6212 pattern, // search pattern
6213 nr,
6214 type == NULL ? NUL : *type,
6215 valid);
6216
6217 vim_free(filename);
6218 vim_free(module);
6219 vim_free(pattern);
6220 vim_free(text);
6221 vim_free(type);
6222
6223 return status;
6224}
6225
6226/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006227 * Add list of entries to quickfix/location list. Each list entry is
6228 * a dictionary with item information.
6229 */
6230 static int
6231qf_add_entries(
6232 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006233 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006234 list_T *list,
6235 char_u *title,
6236 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006237{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006238 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006239 listitem_T *li;
6240 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006241 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006242 int retval = OK;
6243
Bram Moolenaara3921f42017-06-04 15:30:34 +02006244 if (action == ' ' || qf_idx == qi->qf_listcount)
6245 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006246 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006247 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006248 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006249 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006250 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006251 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006252 /* Adding to existing list, use last entry. */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006253 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006254 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006255 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006256 qf_free_items(qfl);
6257 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006258 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006259
6260 for (li = list->lv_first; li != NULL; li = li->li_next)
6261 {
6262 if (li->li_tv.v_type != VAR_DICT)
6263 continue; /* Skip non-dict items */
6264
6265 d = li->li_tv.vval.v_dict;
6266 if (d == NULL)
6267 continue;
6268
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006269 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first);
6270 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006271 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006272 }
6273
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006274 if (qfl->qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02006275 /* no valid entry */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006276 qfl->qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006277 else
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006278 qfl->qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006279 if (action != 'a')
6280 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006281 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006282 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006283 qfl->qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006284 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006285
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006286 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02006287 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006288
6289 return retval;
6290}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006291
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006292/*
6293 * Get the quickfix list index from 'nr' or 'id'
6294 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006295 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006296qf_setprop_get_qfidx(
6297 qf_info_T *qi,
6298 dict_T *what,
6299 int action,
6300 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006301{
6302 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006303 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006304
Bram Moolenaard823fa92016-08-12 16:29:27 +02006305 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6306 {
6307 /* Use the specified quickfix/location list */
6308 if (di->di_tv.v_type == VAR_NUMBER)
6309 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006310 /* for zero use the current list */
6311 if (di->di_tv.vval.v_number != 0)
6312 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006313
Bram Moolenaar55b69262017-08-13 13:42:01 +02006314 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6315 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006316 /*
6317 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02006318 * non-available list and add the new list at the end of the
6319 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006320 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006321 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006322 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006323 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006324 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006325 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006326 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006327 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02006328 }
6329 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006330 && di->di_tv.vval.v_string != NULL
6331 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006332 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006333 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006334 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006335 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006336 qf_idx = 0;
6337 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006338 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006339 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006340 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006341 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006342 }
6343
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006344 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006345 {
6346 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006347 if (di->di_tv.v_type != VAR_NUMBER)
6348 return INVALID_QFIDX;
6349
6350 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006351 }
6352
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006353 return qf_idx;
6354}
6355
6356/*
6357 * Set the quickfix list title.
6358 */
6359 static int
6360qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6361{
6362 if (di->di_tv.v_type != VAR_STRING)
6363 return FAIL;
6364
6365 vim_free(qi->qf_lists[qf_idx].qf_title);
6366 qi->qf_lists[qf_idx].qf_title =
6367 get_dict_string(what, (char_u *)"title", TRUE);
6368 if (qf_idx == qi->qf_curlist)
6369 qf_update_win_titlevar(qi);
6370
6371 return OK;
6372}
6373
6374/*
6375 * Set quickfix list items/entries.
6376 */
6377 static int
6378qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6379{
6380 int retval = FAIL;
6381 char_u *title_save;
6382
6383 if (di->di_tv.v_type != VAR_LIST)
6384 return FAIL;
6385
6386 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6387 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6388 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006389 vim_free(title_save);
6390
6391 return retval;
6392}
6393
6394/*
6395 * Set quickfix list items/entries from a list of lines.
6396 */
6397 static int
6398qf_setprop_items_from_lines(
6399 qf_info_T *qi,
6400 int qf_idx,
6401 dict_T *what,
6402 dictitem_T *di,
6403 int action)
6404{
6405 char_u *errorformat = p_efm;
6406 dictitem_T *efm_di;
6407 int retval = FAIL;
6408
6409 /* Use the user supplied errorformat settings (if present) */
6410 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6411 {
6412 if (efm_di->di_tv.v_type != VAR_STRING ||
6413 efm_di->di_tv.vval.v_string == NULL)
6414 return FAIL;
6415 errorformat = efm_di->di_tv.vval.v_string;
6416 }
6417
6418 /* Only a List value is supported */
6419 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6420 return FAIL;
6421
6422 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006423 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006424 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6425 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6426 retval = OK;
6427
6428 return retval;
6429}
6430
6431/*
6432 * Set quickfix list context.
6433 */
6434 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006435qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006436{
6437 typval_T *ctx;
6438
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006439 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006440 ctx = alloc_tv();
6441 if (ctx != NULL)
6442 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006443 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006444
6445 return OK;
6446}
6447
6448/*
6449 * Set quickfix/location list properties (title, items, context).
6450 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006451 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006452 */
6453 static int
6454qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6455{
6456 dictitem_T *di;
6457 int retval = FAIL;
6458 int qf_idx;
6459 int newlist = FALSE;
6460
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006461 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006462 newlist = TRUE;
6463
6464 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6465 if (qf_idx == INVALID_QFIDX) /* List not found */
6466 return FAIL;
6467
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006468 if (newlist)
6469 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006470 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006471 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006472 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006473 }
6474
6475 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006476 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006477 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006478 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006479 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006480 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006481 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006482 retval = qf_setprop_context(&qi->qf_lists[qf_idx], di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006483
Bram Moolenaarb254af32017-12-18 19:48:58 +01006484 if (retval == OK)
6485 qf_list_changed(qi, qf_idx);
6486
Bram Moolenaard823fa92016-08-12 16:29:27 +02006487 return retval;
6488}
6489
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006490/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006491 * Find the non-location list window with the specified location list in the
6492 * current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006493 */
6494 static win_T *
6495find_win_with_ll(qf_info_T *qi)
6496{
6497 win_T *wp = NULL;
6498
6499 FOR_ALL_WINDOWS(wp)
6500 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6501 return wp;
6502
6503 return NULL;
6504}
6505
6506/*
6507 * Free the entire quickfix/location list stack.
6508 * If the quickfix/location list window is open, then clear it.
6509 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006510 static void
6511qf_free_stack(win_T *wp, qf_info_T *qi)
6512{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006513 win_T *qfwin = qf_find_win(qi);
6514 win_T *llwin = NULL;
6515 win_T *orig_wp = wp;
6516
6517 if (qfwin != NULL)
6518 {
6519 /* If the quickfix/location list window is open, then clear it */
6520 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006521 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006522 qf_update_buffer(qi, NULL);
6523 }
6524
6525 if (wp != NULL && IS_LL_WINDOW(wp))
6526 {
6527 /* If in the location list window, then use the non-location list
6528 * window with this location list (if present)
6529 */
6530 llwin = find_win_with_ll(qi);
6531 if (llwin != NULL)
6532 wp = llwin;
6533 }
6534
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006535 qf_free_all(wp);
6536 if (wp == NULL)
6537 {
6538 /* quickfix list */
6539 qi->qf_curlist = 0;
6540 qi->qf_listcount = 0;
6541 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006542 else if (IS_LL_WINDOW(orig_wp))
6543 {
6544 /* If the location list window is open, then create a new empty
6545 * location list */
6546 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006547
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006548 /* first free the list reference in the location list window */
6549 ll_free_all(&orig_wp->w_llist_ref);
6550
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006551 orig_wp->w_llist_ref = new_ll;
6552 if (llwin != NULL)
6553 {
6554 llwin->w_llist = new_ll;
6555 new_ll->qf_refcount++;
6556 }
6557 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006558}
6559
Bram Moolenaard823fa92016-08-12 16:29:27 +02006560/*
6561 * Populate the quickfix list with the items supplied in the list
6562 * of dictionaries. "title" will be copied to w:quickfix_title.
6563 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6564 */
6565 int
6566set_errorlist(
6567 win_T *wp,
6568 list_T *list,
6569 int action,
6570 char_u *title,
6571 dict_T *what)
6572{
6573 qf_info_T *qi = &ql_info;
6574 int retval = OK;
6575
6576 if (wp != NULL)
6577 {
6578 qi = ll_get_or_alloc_list(wp);
6579 if (qi == NULL)
6580 return FAIL;
6581 }
6582
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006583 if (action == 'f')
6584 {
6585 /* Free the entire quickfix or location list stack */
6586 qf_free_stack(wp, qi);
6587 }
6588 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006589 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006590 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006591 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006592 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006593 if (retval == OK)
6594 qf_list_changed(qi, qi->qf_curlist);
6595 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006596
6597 return retval;
6598}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006599
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006600/*
6601 * Mark the context as in use for all the lists in a quickfix stack.
6602 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006603 static int
6604mark_quickfix_ctx(qf_info_T *qi, int copyID)
6605{
6606 int i;
6607 int abort = FALSE;
6608 typval_T *ctx;
6609
6610 for (i = 0; i < LISTCOUNT && !abort; ++i)
6611 {
6612 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006613 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6614 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006615 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6616 }
6617
6618 return abort;
6619}
6620
6621/*
6622 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006623 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006624 */
6625 int
6626set_ref_in_quickfix(int copyID)
6627{
6628 int abort = FALSE;
6629 tabpage_T *tp;
6630 win_T *win;
6631
6632 abort = mark_quickfix_ctx(&ql_info, copyID);
6633 if (abort)
6634 return abort;
6635
6636 FOR_ALL_TAB_WINDOWS(tp, win)
6637 {
6638 if (win->w_llist != NULL)
6639 {
6640 abort = mark_quickfix_ctx(win->w_llist, copyID);
6641 if (abort)
6642 return abort;
6643 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006644 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6645 {
6646 /* In a location list window and none of the other windows is
6647 * referring to this location list. Mark the location list
6648 * context as still in use.
6649 */
6650 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6651 if (abort)
6652 return abort;
6653 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006654 }
6655
6656 return abort;
6657}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006658#endif
6659
Bram Moolenaar81695252004-12-29 20:58:21 +00006660/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006661 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006662 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006663 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006664 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006665 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006666 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006667 */
6668 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006669ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006670{
6671 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006672 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006673 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006674 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006675 int_u save_qfid;
6676 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006677
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006678 switch (eap->cmdidx)
6679 {
6680 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6681 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6682 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6683 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6684 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6685 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6686 default: break;
6687 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006688 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6689 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006690 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006691#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006692 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006693 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006694#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006695 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006696
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006697 /* Must come after autocommands. */
Bram Moolenaar39665952018-08-15 20:59:48 +02006698 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006699 {
6700 qi = ll_get_or_alloc_list(curwin);
6701 if (qi == NULL)
6702 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006703 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006704 }
6705
Bram Moolenaar86b68352004-12-27 21:59:20 +00006706 if (*eap->arg == NUL)
6707 buf = curbuf;
6708 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6709 buf = buflist_findnr(atoi((char *)eap->arg));
6710 if (buf == NULL)
6711 EMSG(_(e_invarg));
6712 else if (buf->b_ml.ml_mfp == NULL)
6713 EMSG(_("E681: Buffer is not loaded"));
6714 else
6715 {
6716 if (eap->addr_count == 0)
6717 {
6718 eap->line1 = 1;
6719 eap->line2 = buf->b_ml.ml_line_count;
6720 }
6721 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6722 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6723 EMSG(_(e_invrange));
6724 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006725 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006726 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006727
6728 if (buf->b_sfname)
6729 {
6730 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6731 (char *)qf_title, (char *)buf->b_sfname);
6732 qf_title = IObuff;
6733 }
6734
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006735 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006736 (eap->cmdidx != CMD_caddbuffer
6737 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006738 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006739 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006740 if (res >= 0)
6741 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006742
6743 // Remember the current quickfix list identifier, so that we can
6744 // check for autocommands changing the current quickfix list.
6745 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006746 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006747 {
6748 buf_T *curbuf_old = curbuf;
6749
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006750 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6751 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006752 if (curbuf != curbuf_old)
6753 // Autocommands changed buffer, don't jump now, "qi" may
6754 // be invalid.
6755 res = 0;
6756 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006757 // Jump to the first error for a new list and if autocmds didn't
6758 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006759 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006760 eap->cmdidx == CMD_lbuffer)
6761 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006762 // display the first error
6763 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar754b5602006-02-09 23:53:20 +00006764 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006765 }
6766}
6767
Bram Moolenaar1e015462005-09-25 22:16:38 +00006768#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006769/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006770 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6771 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006772 */
6773 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006774ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006775{
6776 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006777 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006778 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006779 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006780 int_u save_qfid;
6781 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006782
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006783 switch (eap->cmdidx)
6784 {
6785 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6786 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6787 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6788 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6789 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6790 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6791 default: break;
6792 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006793 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6794 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006795 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006796#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006797 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006798 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006799#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006800 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006801
Bram Moolenaar39665952018-08-15 20:59:48 +02006802 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006803 {
6804 qi = ll_get_or_alloc_list(curwin);
6805 if (qi == NULL)
6806 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006807 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006808 }
6809
Bram Moolenaar4770d092006-01-12 23:22:24 +00006810 /* Evaluate the expression. When the result is a string or a list we can
6811 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006812 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006813 if (tv != NULL)
6814 {
6815 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6816 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6817 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006818 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006819 (eap->cmdidx != CMD_caddexpr
6820 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006821 (linenr_T)0, (linenr_T)0,
6822 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006823 if (res >= 0)
6824 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006825
6826 // Remember the current quickfix list identifier, so that we can
6827 // check for autocommands changing the current quickfix list.
6828 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006829 if (au_name != NULL)
6830 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6831 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006832
6833 // Jump to the first error for a new list and if autocmds didn't
6834 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006835 if (res > 0 && (eap->cmdidx == CMD_cexpr
6836 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006837 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006838 // display the first error
6839 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006840 }
6841 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006842 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006843 free_tv(tv);
6844 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006845}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006846#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006847
6848/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006849 * Get the location list for ":lhelpgrep"
6850 */
6851 static qf_info_T *
6852hgr_get_ll(int *new_ll)
6853{
6854 win_T *wp;
6855 qf_info_T *qi;
6856
6857 /* If the current window is a help window, then use it */
6858 if (bt_help(curwin->w_buffer))
6859 wp = curwin;
6860 else
6861 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006862 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006863
6864 if (wp == NULL) /* Help window not found */
6865 qi = NULL;
6866 else
6867 qi = wp->w_llist;
6868
6869 if (qi == NULL)
6870 {
6871 /* Allocate a new location list for help text matches */
6872 if ((qi = ll_new_list()) == NULL)
6873 return NULL;
6874 *new_ll = TRUE;
6875 }
6876
6877 return qi;
6878}
6879
6880/*
6881 * Search for a pattern in a help file.
6882 */
6883 static void
6884hgr_search_file(
6885 qf_info_T *qi,
6886 char_u *fname,
6887#ifdef FEAT_MBYTE
6888 vimconv_T *p_vc,
6889#endif
6890 regmatch_T *p_regmatch)
6891{
6892 FILE *fd;
6893 long lnum;
6894
6895 fd = mch_fopen((char *)fname, "r");
6896 if (fd == NULL)
6897 return;
6898
6899 lnum = 1;
6900 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6901 {
6902 char_u *line = IObuff;
6903#ifdef FEAT_MBYTE
6904 /* Convert a line if 'encoding' is not utf-8 and
6905 * the line contains a non-ASCII character. */
6906 if (p_vc->vc_type != CONV_NONE
6907 && has_non_ascii(IObuff))
6908 {
6909 line = string_convert(p_vc, IObuff, NULL);
6910 if (line == NULL)
6911 line = IObuff;
6912 }
6913#endif
6914
6915 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6916 {
6917 int l = (int)STRLEN(line);
6918
6919 /* remove trailing CR, LF, spaces, etc. */
6920 while (l > 0 && line[l - 1] <= ' ')
6921 line[--l] = NUL;
6922
6923 if (qf_add_entry(qi,
6924 qi->qf_curlist,
6925 NULL, /* dir */
6926 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006927 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006928 0,
6929 line,
6930 lnum,
6931 (int)(p_regmatch->startp[0] - line)
6932 + 1, /* col */
6933 FALSE, /* vis_col */
6934 NULL, /* search pattern */
6935 0, /* nr */
6936 1, /* type */
6937 TRUE /* valid */
6938 ) == FAIL)
6939 {
6940 got_int = TRUE;
6941#ifdef FEAT_MBYTE
6942 if (line != IObuff)
6943 vim_free(line);
6944#endif
6945 break;
6946 }
6947 }
6948#ifdef FEAT_MBYTE
6949 if (line != IObuff)
6950 vim_free(line);
6951#endif
6952 ++lnum;
6953 line_breakcheck();
6954 }
6955 fclose(fd);
6956}
6957
6958/*
6959 * Search for a pattern in all the help files in the doc directory under
6960 * the given directory.
6961 */
6962 static void
6963hgr_search_files_in_dir(
6964 qf_info_T *qi,
6965 char_u *dirname,
6966 regmatch_T *p_regmatch
6967#ifdef FEAT_MBYTE
6968 , vimconv_T *p_vc
6969#endif
6970#ifdef FEAT_MULTI_LANG
6971 , char_u *lang
6972#endif
6973 )
6974{
6975 int fcount;
6976 char_u **fnames;
6977 int fi;
6978
6979 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6980 add_pathsep(dirname);
6981 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6982 if (gen_expand_wildcards(1, &dirname, &fcount,
6983 &fnames, EW_FILE|EW_SILENT) == OK
6984 && fcount > 0)
6985 {
6986 for (fi = 0; fi < fcount && !got_int; ++fi)
6987 {
6988#ifdef FEAT_MULTI_LANG
6989 /* Skip files for a different language. */
6990 if (lang != NULL
6991 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006992 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006993 && !(STRNICMP(lang, "en", 2) == 0
6994 && STRNICMP("txt", fnames[fi]
6995 + STRLEN(fnames[fi]) - 3, 3) == 0))
6996 continue;
6997#endif
6998
6999 hgr_search_file(qi, fnames[fi],
7000#ifdef FEAT_MBYTE
7001 p_vc,
7002#endif
7003 p_regmatch);
7004 }
7005 FreeWild(fcount, fnames);
7006 }
7007}
7008
7009/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007010 * Search for a pattern in all the help files in the 'runtimepath'
7011 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007012 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007013 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007014 */
7015 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007016hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007017{
7018 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007019
7020#ifdef FEAT_MBYTE
7021 vimconv_T vc;
7022
7023 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
7024 * differs. */
7025 vc.vc_type = CONV_NONE;
7026 if (!enc_utf8)
7027 convert_setup(&vc, (char_u *)"utf-8", p_enc);
7028#endif
7029
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007030
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007031 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007032 p = p_rtp;
7033 while (*p != NUL && !got_int)
7034 {
7035 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7036
7037 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
7038#ifdef FEAT_MBYTE
7039 , &vc
7040#endif
7041#ifdef FEAT_MULTI_LANG
7042 , lang
7043#endif
7044 );
7045 }
7046
7047#ifdef FEAT_MBYTE
7048 if (vc.vc_type != CONV_NONE)
7049 convert_setup(&vc, NULL, NULL);
7050#endif
7051}
7052
7053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 * ":helpgrep {pattern}"
7055 */
7056 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007057ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058{
7059 regmatch_T regmatch;
7060 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007061 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007062 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007063 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007064 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
Bram Moolenaar73633f82012-01-20 13:39:07 +01007066 switch (eap->cmdidx)
7067 {
7068 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7069 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7070 default: break;
7071 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007072 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7073 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007074 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007075#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007076 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007077 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007078#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007079 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007080
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007081 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007082 save_cpo = p_cpo;
7083 p_cpo = empty_option;
7084
Bram Moolenaar39665952018-08-15 20:59:48 +02007085 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007086 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007087 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007088 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007089 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007090 }
7091
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007092#ifdef FEAT_MULTI_LANG
7093 // Check for a specified language
7094 lang = check_help_lang(eap->arg);
7095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7097 regmatch.rm_ic = FALSE;
7098 if (regmatch.regprog != NULL)
7099 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007100 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007101 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007103 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007104
Bram Moolenaar473de612013-06-08 18:19:48 +02007105 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007107 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
7108 qi->qf_lists[qi->qf_curlist].qf_ptr =
7109 qi->qf_lists[qi->qf_curlist].qf_start;
7110 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 }
7112
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007113 if (p_cpo == empty_option)
7114 p_cpo = save_cpo;
7115 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007116 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007117 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
Bram Moolenaarb254af32017-12-18 19:48:58 +01007119 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02007120 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121
Bram Moolenaar73633f82012-01-20 13:39:07 +01007122 if (au_name != NULL)
7123 {
7124 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7125 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007126 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01007127 /* autocommands made "qi" invalid */
7128 return;
7129 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007130
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007132 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007133 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007134 else
7135 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007136
7137 if (eap->cmdidx == CMD_lhelpgrep)
7138 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007139 // If the help window is not opened or if it already points to the
7140 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007141 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007142 {
7143 if (new_qi)
7144 ll_free_all(&qi);
7145 }
7146 else if (curwin->w_llist == NULL)
7147 curwin->w_llist = qi;
7148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149}
7150
7151#endif /* FEAT_QUICKFIX */