blob: 19363d3582906ab0e1ca2d7fb27a84ad6119f539 [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 Moolenaarde3b3672018-08-07 21:54:41 +02001502 * Returns TRUE if the specified quickfix/location list is empty.
1503 */
1504 static int
1505qf_list_empty(qf_info_T *qi, int qf_idx)
1506{
1507 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1508 return TRUE;
1509 return qi->qf_lists[qf_idx].qf_count <= 0;
1510}
1511
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001512/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001513 * Allocate the fields used for parsing lines and populating a quickfix list.
1514 */
1515 static int
1516qf_alloc_fields(qffields_T *pfields)
1517{
1518 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1519 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1520 pfields->errmsglen = CMDBUFFSIZE + 1;
1521 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1522 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1523 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1524 || pfields->pattern == NULL || pfields->module == NULL)
1525 return FAIL;
1526
1527 return OK;
1528}
1529
1530/*
1531 * Free the fields used for parsing lines and populating a quickfix list.
1532 */
1533 static void
1534qf_free_fields(qffields_T *pfields)
1535{
1536 vim_free(pfields->namebuf);
1537 vim_free(pfields->module);
1538 vim_free(pfields->errmsg);
1539 vim_free(pfields->pattern);
1540}
1541
1542/*
1543 * Setup the state information used for parsing lines and populating a
1544 * quickfix list.
1545 */
1546 static int
1547qf_setup_state(
1548 qfstate_T *pstate,
1549 char_u *enc,
1550 char_u *efile,
1551 typval_T *tv,
1552 buf_T *buf,
1553 linenr_T lnumfirst,
1554 linenr_T lnumlast)
1555{
1556#ifdef FEAT_MBYTE
1557 pstate->vc.vc_type = CONV_NONE;
1558 if (enc != NULL && *enc != NUL)
1559 convert_setup(&pstate->vc, enc, p_enc);
1560#endif
1561
1562 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1563 {
1564 EMSG2(_(e_openerrf), efile);
1565 return FAIL;
1566 }
1567
1568 if (tv != NULL)
1569 {
1570 if (tv->v_type == VAR_STRING)
1571 pstate->p_str = tv->vval.v_string;
1572 else if (tv->v_type == VAR_LIST)
1573 pstate->p_li = tv->vval.v_list->lv_first;
1574 pstate->tv = tv;
1575 }
1576 pstate->buf = buf;
1577 pstate->buflnum = lnumfirst;
1578 pstate->lnumlast = lnumlast;
1579
1580 return OK;
1581}
1582
1583/*
1584 * Cleanup the state information used for parsing lines and populating a
1585 * quickfix list.
1586 */
1587 static void
1588qf_cleanup_state(qfstate_T *pstate)
1589{
1590 if (pstate->fd != NULL)
1591 fclose(pstate->fd);
1592
1593 vim_free(pstate->growbuf);
1594#ifdef FEAT_MBYTE
1595 if (pstate->vc.vc_type != CONV_NONE)
1596 convert_setup(&pstate->vc, NULL, NULL);
1597#endif
1598}
1599
1600/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001601 * Read the errorfile "efile" into memory, line by line, building the error
1602 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001603 * Alternative: when "efile" is NULL read errors from buffer "buf".
1604 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001605 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001606 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1607 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001608 * Return -1 for error, number of errors for success.
1609 */
1610 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001611qf_init_ext(
1612 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001613 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001614 char_u *efile,
1615 buf_T *buf,
1616 typval_T *tv,
1617 char_u *errorformat,
1618 int newlist, /* TRUE: start a new error list */
1619 linenr_T lnumfirst, /* first line number to use */
1620 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001621 char_u *qf_title,
1622 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001623{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001624 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001625 qfstate_T state;
1626 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001627 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001628 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001629 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001631 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001633 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001635 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001636 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001637
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001638 vim_memset(&state, 0, sizeof(state));
1639 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001640 if ((qf_alloc_fields(&fields) == FAIL) ||
1641 (qf_setup_state(&state, enc, efile, tv, buf,
1642 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 goto qf_init_end;
1644
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001645 if (newlist || qf_idx == qi->qf_listcount)
1646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001648 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001649 qf_idx = qi->qf_curlist;
1650 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001651 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001652 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001653 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001654 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001655 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001656 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001659 qfl = &qi->qf_lists[qf_idx];
1660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001662 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001663 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 else
1665 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001667 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001668 * If the errorformat didn't change between calls, then reuse the
1669 * previously parsed values.
1670 */
1671 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1672 {
1673 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001674 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001675 free_efm_list(&fmt_first);
1676
1677 /* parse the current 'efm' */
1678 fmt_first = parse_efm_option(efm);
1679 if (fmt_first != NULL)
1680 last_efm = vim_strsave(efm);
1681 }
1682
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
1686 /*
1687 * got_int is reset here, because it was probably set when killing the
1688 * ":make" command, but we still want to read the errorfile then.
1689 */
1690 got_int = FALSE;
1691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 /*
1693 * Read the lines in the error file one by one.
1694 * Try to recognize one of the error formats in each line.
1695 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001696 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001698 /* Get the next line from a file/buffer/list/string */
1699 status = qf_get_nextline(&state);
1700 if (status == QF_NOMEM) /* memory alloc failure */
1701 goto qf_init_end;
1702 if (status == QF_END_OF_INPUT) /* end of input */
1703 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001705 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1706 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001707 if (status == QF_FAIL)
1708 goto error2;
1709 if (status == QF_NOMEM)
1710 goto qf_init_end;
1711 if (status == QF_IGNORE_LINE)
1712 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001714 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001715 qf_idx,
1716 qfl->qf_directory,
1717 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001718 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001719 : ((qfl->qf_currfile != NULL && fields.valid)
1720 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001721 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001722 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001723 fields.errmsg,
1724 fields.lnum,
1725 fields.col,
1726 fields.use_viscol,
1727 fields.pattern,
1728 fields.enr,
1729 fields.type,
1730 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 goto error2;
1732 line_breakcheck();
1733 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001734 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001736 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001738 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001739 qfl->qf_ptr = qfl->qf_start;
1740 qfl->qf_index = 1;
1741 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 }
1743 else
1744 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001745 qfl->qf_nonevalid = FALSE;
1746 if (qfl->qf_ptr == NULL)
1747 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001749 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001750 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001751 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 }
1753 EMSG(_(e_readerrf));
1754error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001755 if (!adding)
1756 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001757 /* Error when creating a new list. Free the new list */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001758 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001759 qi->qf_listcount--;
1760 if (qi->qf_curlist > 0)
1761 --qi->qf_curlist;
1762 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001763qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001764 if (qf_idx == qi->qf_curlist)
1765 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001766 qf_cleanup_state(&state);
1767 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769 return retval;
1770}
1771
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001772/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001773 * Read the errorfile "efile" into memory, line by line, building the error
1774 * list. Set the error list's title to qf_title.
1775 * Return -1 for error, number of errors for success.
1776 */
1777 int
1778qf_init(win_T *wp,
1779 char_u *efile,
1780 char_u *errorformat,
1781 int newlist, /* TRUE: start a new error list */
1782 char_u *qf_title,
1783 char_u *enc)
1784{
1785 qf_info_T *qi = &ql_info;
1786
1787 if (wp != NULL)
1788 {
1789 qi = ll_get_or_alloc_list(wp);
1790 if (qi == NULL)
1791 return FAIL;
1792 }
1793
1794 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1795 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1796}
1797
1798/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001799 * Set the title of the specified quickfix list. Frees the previous title.
1800 * Prepends ':' to the title.
1801 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001802 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001803qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001804{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001805 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001806
Bram Moolenaarfb604092014-07-23 15:55:00 +02001807 if (title != NULL)
1808 {
1809 char_u *p = alloc((int)STRLEN(title) + 2);
1810
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001811 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001812 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001813 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001814 }
1815}
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001818 * The title of a quickfix/location list is set, by default, to the command
1819 * that created the quickfix list with the ":" prefix.
1820 * Create a quickfix list title string by prepending ":" to a user command.
1821 * Returns a pointer to a static buffer with the title.
1822 */
1823 static char_u *
1824qf_cmdtitle(char_u *cmd)
1825{
1826 static char_u qftitle_str[IOSIZE];
1827
1828 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1829 return qftitle_str;
1830}
1831
1832/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001833 * Prepare for adding a new quickfix list. If the current list is in the
1834 * middle of the stack, then all the following lists are freed and then
1835 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
1837 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001838qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
1840 int i;
1841
1842 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001843 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 * the current entry. This makes it possible to browse in a tree-like
1845 * way with ":grep'.
1846 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001847 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001848 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
1850 /*
1851 * When the stack is full, remove to oldest entry
1852 * Otherwise, add a new entry.
1853 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001854 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001856 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001858 qi->qf_lists[i - 1] = qi->qf_lists[i];
1859 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 }
1861 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001862 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001863 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001864 qf_store_title(&qi->qf_lists[qi->qf_curlist], qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001865 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866}
1867
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001868/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001869 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001870 */
1871 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001872ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001873{
1874 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001875 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001876
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001877 qi = *pqi;
1878 if (qi == NULL)
1879 return;
1880 *pqi = NULL; /* Remove reference to this list */
1881
1882 qi->qf_refcount--;
1883 if (qi->qf_refcount < 1)
1884 {
1885 /* No references to this location list */
1886 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001887 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001888 vim_free(qi);
1889 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001890}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001891
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001892/*
1893 * Free all the quickfix/location lists in the stack.
1894 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001895 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001896qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001897{
1898 int i;
1899 qf_info_T *qi = &ql_info;
1900
1901 if (wp != NULL)
1902 {
1903 /* location list */
1904 ll_free_all(&wp->w_llist);
1905 ll_free_all(&wp->w_llist_ref);
1906 }
1907 else
1908 /* quickfix list */
1909 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001910 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001911}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001912
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913/*
1914 * Add an entry to the end of the list of errors.
1915 * Returns OK or FAIL.
1916 */
1917 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001918qf_add_entry(
1919 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001920 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001921 char_u *dir, /* optional directory name */
1922 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001923 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001924 int bufnum, /* buffer number or zero */
1925 char_u *mesg, /* message */
1926 long lnum, /* line number */
1927 int col, /* column */
1928 int vis_col, /* using visual column */
1929 char_u *pattern, /* search pattern */
1930 int nr, /* error number */
1931 int type, /* type character */
1932 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001934 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001935 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001936 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001938 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001940 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001941 {
1942 buf_T *buf = buflist_findnr(bufnum);
1943
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001944 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001945 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001946 buf->b_has_qf_entry |=
Bram Moolenaar4d77c652018-08-18 19:59:54 +02001947 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001948 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001949 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001950 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1952 {
1953 vim_free(qfp);
1954 return FAIL;
1955 }
1956 qfp->qf_lnum = lnum;
1957 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001958 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001959 if (pattern == NULL || *pattern == NUL)
1960 qfp->qf_pattern = NULL;
1961 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1962 {
1963 vim_free(qfp->qf_text);
1964 vim_free(qfp);
1965 return FAIL;
1966 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001967 if (module == NULL || *module == NUL)
1968 qfp->qf_module = NULL;
1969 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1970 {
1971 vim_free(qfp->qf_text);
1972 vim_free(qfp->qf_pattern);
1973 vim_free(qfp);
1974 return FAIL;
1975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 qfp->qf_nr = nr;
1977 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1978 type = 0;
1979 qfp->qf_type = type;
1980 qfp->qf_valid = valid;
1981
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001982 lastp = &qfl->qf_last;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001983 if (qf_list_empty(qi, qf_idx)) /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001985 qfl->qf_start = qfp;
1986 qfl->qf_ptr = qfp;
1987 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001988 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
1990 else
1991 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001992 qfp->qf_prev = *lastp;
1993 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001995 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001997 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001998 ++qfl->qf_count;
1999 if (qfl->qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002000 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002002 qfl->qf_index = qfl->qf_count;
2003 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 }
2005
2006 return OK;
2007}
2008
2009/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002010 * Allocate a new location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002011 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002012 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002013ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002014{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002015 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002016
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002017 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002018 if (qi != NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002019 qi->qf_refcount++;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002020 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002021}
2022
2023/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002024 * Return the location list stack for window 'wp'.
2025 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002026 */
2027 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002028ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002029{
2030 if (IS_LL_WINDOW(wp))
2031 /* For a location list window, use the referenced location list */
2032 return wp->w_llist_ref;
2033
2034 /*
2035 * For a non-location list window, w_llist_ref should not point to a
2036 * location list.
2037 */
2038 ll_free_all(&wp->w_llist_ref);
2039
2040 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002041 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002042 return wp->w_llist;
2043}
2044
2045/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002046 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2047 */
2048 static int
2049copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2050{
2051 int i;
2052 qfline_T *from_qfp;
2053 qfline_T *prevp;
2054
2055 // copy all the location entries in this list
2056 for (i = 0, from_qfp = from_qfl->qf_start;
2057 i < from_qfl->qf_count && from_qfp != NULL;
2058 ++i, from_qfp = from_qfp->qf_next)
2059 {
2060 if (qf_add_entry(to_qi,
2061 to_qi->qf_curlist,
2062 NULL,
2063 NULL,
2064 from_qfp->qf_module,
2065 0,
2066 from_qfp->qf_text,
2067 from_qfp->qf_lnum,
2068 from_qfp->qf_col,
2069 from_qfp->qf_viscol,
2070 from_qfp->qf_pattern,
2071 from_qfp->qf_nr,
2072 0,
2073 from_qfp->qf_valid) == FAIL)
2074 return FAIL;
2075
2076 // qf_add_entry() will not set the qf_num field, as the
2077 // directory and file names are not supplied. So the qf_fnum
2078 // field is copied here.
2079 prevp = to_qfl->qf_last;
2080 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2081 prevp->qf_type = from_qfp->qf_type; // error type
2082 if (from_qfl->qf_ptr == from_qfp)
2083 to_qfl->qf_ptr = prevp; // current location
2084 }
2085
2086 return OK;
2087}
2088
2089/*
2090 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2091 */
2092 static int
2093copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2094{
2095 // Some of the fields are populated by qf_add_entry()
2096 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2097 to_qfl->qf_count = 0;
2098 to_qfl->qf_index = 0;
2099 to_qfl->qf_start = NULL;
2100 to_qfl->qf_last = NULL;
2101 to_qfl->qf_ptr = NULL;
2102 if (from_qfl->qf_title != NULL)
2103 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2104 else
2105 to_qfl->qf_title = NULL;
2106 if (from_qfl->qf_ctx != NULL)
2107 {
2108 to_qfl->qf_ctx = alloc_tv();
2109 if (to_qfl->qf_ctx != NULL)
2110 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2111 }
2112 else
2113 to_qfl->qf_ctx = NULL;
2114
2115 if (from_qfl->qf_count)
2116 if (copy_loclist_entries(from_qfl, to_qfl, to_qi) == FAIL)
2117 return FAIL;
2118
2119 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2120
2121 // Assign a new ID for the location list
2122 to_qfl->qf_id = ++last_qf_id;
2123 to_qfl->qf_changedtick = 0L;
2124
2125 // When no valid entries are present in the list, qf_ptr points to
2126 // the first item in the list
2127 if (to_qfl->qf_nonevalid)
2128 {
2129 to_qfl->qf_ptr = to_qfl->qf_start;
2130 to_qfl->qf_index = 1;
2131 }
2132
2133 return OK;
2134}
2135
2136/*
2137 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002138 */
2139 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002140copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002141{
2142 qf_info_T *qi;
2143 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002144
Bram Moolenaar09037502018-09-25 22:08:14 +02002145 // When copying from a location list window, copy the referenced
2146 // location list. For other windows, copy the location list for
2147 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002148 if (IS_LL_WINDOW(from))
2149 qi = from->w_llist_ref;
2150 else
2151 qi = from->w_llist;
2152
Bram Moolenaar09037502018-09-25 22:08:14 +02002153 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002154 return;
2155
Bram Moolenaar09037502018-09-25 22:08:14 +02002156 // allocate a new location list
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002157 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002158 return;
2159
2160 to->w_llist->qf_listcount = qi->qf_listcount;
2161
Bram Moolenaar09037502018-09-25 22:08:14 +02002162 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002163 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002164 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002165 to->w_llist->qf_curlist = idx;
2166
Bram Moolenaar09037502018-09-25 22:08:14 +02002167 if (copy_loclist(&qi->qf_lists[idx],
2168 &to->w_llist->qf_lists[idx], to->w_llist) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002169 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002170 qf_free_all(to);
2171 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002172 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002173 }
2174
Bram Moolenaar09037502018-09-25 22:08:14 +02002175 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002176}
2177
2178/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002179 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002180 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 */
2182 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002183qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
Bram Moolenaar82404332016-07-10 17:00:38 +02002185 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002186 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002187 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002188
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 if (fname == NULL || *fname == NUL) /* no file name */
2190 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
Bram Moolenaare60acc12011-05-10 16:41:25 +02002192#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002193 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002194#endif
2195#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002196 if (directory != NULL)
2197 slash_adjust(directory);
2198 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002199#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002200 if (directory != NULL && !vim_isAbsName(fname)
2201 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2202 {
2203 /*
2204 * Here we check if the file really exists.
2205 * This should normally be true, but if make works without
2206 * "leaving directory"-messages we might have missed a
2207 * directory change.
2208 */
2209 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 vim_free(ptr);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002212 directory = qf_guess_filepath(&qi->qf_lists[qf_idx], fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002213 if (directory)
2214 ptr = concat_fnames(directory, fname, TRUE);
2215 else
2216 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002218 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02002219 bufname = ptr;
2220 }
2221 else
2222 bufname = fname;
2223
2224 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002225 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002226 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002227 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002228 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002230 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002231 {
2232 vim_free(qf_last_bufname);
2233 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2234 if (bufname == ptr)
2235 qf_last_bufname = bufname;
2236 else
2237 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002238 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002239 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002240 if (buf == NULL)
2241 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002242
Bram Moolenaarc1542742016-07-20 21:44:37 +02002243 buf->b_has_qf_entry =
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002244 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002245 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246}
2247
2248/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002249 * Push dirbuf onto the directory stack and return pointer to actual dir or
2250 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 */
2252 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002253qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254{
2255 struct dir_stack_T *ds_new;
2256 struct dir_stack_T *ds_ptr;
2257
2258 /* allocate new stack element and hook it in */
2259 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2260 if (ds_new == NULL)
2261 return NULL;
2262
2263 ds_new->next = *stackptr;
2264 *stackptr = ds_new;
2265
2266 /* store directory on the stack */
2267 if (vim_isAbsName(dirbuf)
2268 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002269 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 (*stackptr)->dirname = vim_strsave(dirbuf);
2271 else
2272 {
2273 /* Okay we don't have an absolute path.
2274 * dirbuf must be a subdir of one of the directories on the stack.
2275 * Let's search...
2276 */
2277 ds_new = (*stackptr)->next;
2278 (*stackptr)->dirname = NULL;
2279 while (ds_new)
2280 {
2281 vim_free((*stackptr)->dirname);
2282 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2283 TRUE);
2284 if (mch_isdir((*stackptr)->dirname) == TRUE)
2285 break;
2286
2287 ds_new = ds_new->next;
2288 }
2289
2290 /* clean up all dirs we already left */
2291 while ((*stackptr)->next != ds_new)
2292 {
2293 ds_ptr = (*stackptr)->next;
2294 (*stackptr)->next = (*stackptr)->next->next;
2295 vim_free(ds_ptr->dirname);
2296 vim_free(ds_ptr);
2297 }
2298
2299 /* Nothing found -> it must be on top level */
2300 if (ds_new == NULL)
2301 {
2302 vim_free((*stackptr)->dirname);
2303 (*stackptr)->dirname = vim_strsave(dirbuf);
2304 }
2305 }
2306
2307 if ((*stackptr)->dirname != NULL)
2308 return (*stackptr)->dirname;
2309 else
2310 {
2311 ds_ptr = *stackptr;
2312 *stackptr = (*stackptr)->next;
2313 vim_free(ds_ptr);
2314 return NULL;
2315 }
2316}
2317
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318/*
2319 * pop dirbuf from the directory stack and return previous directory or NULL if
2320 * stack is empty
2321 */
2322 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002323qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324{
2325 struct dir_stack_T *ds_ptr;
2326
2327 /* TODO: Should we check if dirbuf is the directory on top of the stack?
2328 * What to do if it isn't? */
2329
2330 /* pop top element and free it */
2331 if (*stackptr != NULL)
2332 {
2333 ds_ptr = *stackptr;
2334 *stackptr = (*stackptr)->next;
2335 vim_free(ds_ptr->dirname);
2336 vim_free(ds_ptr);
2337 }
2338
2339 /* return NEW top element as current dir or NULL if stack is empty*/
2340 return *stackptr ? (*stackptr)->dirname : NULL;
2341}
2342
2343/*
2344 * clean up directory stack
2345 */
2346 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002347qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349 struct dir_stack_T *ds_ptr;
2350
2351 while ((ds_ptr = *stackptr) != NULL)
2352 {
2353 *stackptr = (*stackptr)->next;
2354 vim_free(ds_ptr->dirname);
2355 vim_free(ds_ptr);
2356 }
2357}
2358
2359/*
2360 * Check in which directory of the directory stack the given file can be
2361 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002362 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 * Cleans up intermediate directory entries.
2364 *
2365 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002366 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 * ./
2368 * ./aa
2369 * ./aa/bb
2370 * ./bb
2371 * ./bb/x.c
2372 * and make says:
2373 * making all in aa
2374 * making all in bb
2375 * x.c:9: Error
2376 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2377 * qf_guess_filepath will return NULL.
2378 */
2379 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002380qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 struct dir_stack_T *ds_ptr;
2383 struct dir_stack_T *ds_tmp;
2384 char_u *fullname;
2385
2386 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002387 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 return NULL;
2389
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002390 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 fullname = NULL;
2392 while (ds_ptr)
2393 {
2394 vim_free(fullname);
2395 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2396
2397 /* If concat_fnames failed, just go on. The worst thing that can happen
2398 * is that we delete the entire stack.
2399 */
2400 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2401 break;
2402
2403 ds_ptr = ds_ptr->next;
2404 }
2405
2406 vim_free(fullname);
2407
2408 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002409 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002411 ds_tmp = qfl->qf_dir_stack->next;
2412 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 vim_free(ds_tmp->dirname);
2414 vim_free(ds_tmp);
2415 }
2416
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002417 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418}
2419
2420/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002421 * Returns TRUE if a quickfix/location list with the given identifier exists.
2422 */
2423 static int
2424qflist_valid (win_T *wp, int_u qf_id)
2425{
2426 qf_info_T *qi = &ql_info;
2427 int i;
2428
2429 if (wp != NULL)
2430 {
2431 qi = GET_LOC_LIST(wp); /* Location list */
2432 if (qi == NULL)
2433 return FALSE;
2434 }
2435
2436 for (i = 0; i < qi->qf_listcount; ++i)
2437 if (qi->qf_lists[i].qf_id == qf_id)
2438 return TRUE;
2439
2440 return FALSE;
2441}
2442
2443/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002444 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002445 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002446 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002447 * Similar to location list.
2448 */
2449 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002450is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002451{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002452 qfline_T *qfp;
2453 int i;
2454
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002455 /* Search for the entry in the current list */
2456 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2457 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002458 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002459 break;
2460
2461 if (i == qfl->qf_count) /* Entry is not found */
2462 return FALSE;
2463
2464 return TRUE;
2465}
2466
2467/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002468 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002469 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002470 */
2471 static qfline_T *
2472get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002473 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002474 qfline_T *qf_ptr,
2475 int *qf_index,
2476 int dir)
2477{
2478 int idx;
2479 int old_qf_fnum;
2480
2481 idx = *qf_index;
2482 old_qf_fnum = qf_ptr->qf_fnum;
2483
2484 do
2485 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002486 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002487 return NULL;
2488 ++idx;
2489 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002490 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002491 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2492
2493 *qf_index = idx;
2494 return qf_ptr;
2495}
2496
2497/*
2498 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002499 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002500 */
2501 static qfline_T *
2502get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002503 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002504 qfline_T *qf_ptr,
2505 int *qf_index,
2506 int dir)
2507{
2508 int idx;
2509 int old_qf_fnum;
2510
2511 idx = *qf_index;
2512 old_qf_fnum = qf_ptr->qf_fnum;
2513
2514 do
2515 {
2516 if (idx == 1 || qf_ptr->qf_prev == NULL)
2517 return NULL;
2518 --idx;
2519 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002520 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002521 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2522
2523 *qf_index = idx;
2524 return qf_ptr;
2525}
2526
2527/*
2528 * Get the n'th (errornr) previous/next valid entry from the current entry in
2529 * the quickfix list.
2530 * dir == FORWARD or FORWARD_FILE: next valid entry
2531 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2532 */
2533 static qfline_T *
2534get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002535 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002536 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002537 int dir,
2538 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002539{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002540 qfline_T *qf_ptr = qfl->qf_ptr;
2541 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002542 qfline_T *prev_qf_ptr;
2543 int prev_index;
2544 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2545 char_u *err = e_no_more_items;
2546
2547 while (errornr--)
2548 {
2549 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002550 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002551
2552 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002553 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002554 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002555 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002556 if (qf_ptr == NULL)
2557 {
2558 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002559 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002560 if (err != NULL)
2561 {
2562 EMSG(_(err));
2563 return NULL;
2564 }
2565 break;
2566 }
2567
2568 err = NULL;
2569 }
2570
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002571 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002572 return qf_ptr;
2573}
2574
2575/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002576 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2577 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002578 */
2579 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002580get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002581{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002582 qfline_T *qf_ptr = qfl->qf_ptr;
2583 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002584
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002585 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002586 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2587 {
2588 --qf_idx;
2589 qf_ptr = qf_ptr->qf_prev;
2590 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002591 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002592 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2593 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002594 {
2595 ++qf_idx;
2596 qf_ptr = qf_ptr->qf_next;
2597 }
2598
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002599 *new_qfidx = qf_idx;
2600 return qf_ptr;
2601}
2602
2603/*
2604 * Get a entry specied by 'errornr' and 'dir' from the current
2605 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2606 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2607 * Returns a pointer to the entry and the index of the new entry is stored in
2608 * 'new_qfidx'.
2609 */
2610 static qfline_T *
2611qf_get_entry(
2612 qf_list_T *qfl,
2613 int errornr,
2614 int dir,
2615 int *new_qfidx)
2616{
2617 qfline_T *qf_ptr = qfl->qf_ptr;
2618 int qfidx = qfl->qf_index;
2619
2620 if (dir != 0) // next/prev valid entry
2621 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2622 else if (errornr != 0) // go to specified number
2623 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2624
2625 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002626 return qf_ptr;
2627}
2628
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002629/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002630 * Find a window displaying a Vim help file.
2631 */
2632 static win_T *
2633qf_find_help_win(void)
2634{
2635 win_T *wp;
2636
2637 FOR_ALL_WINDOWS(wp)
2638 if (bt_help(wp->w_buffer))
2639 return wp;
2640
2641 return NULL;
2642}
2643
2644/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002645 * Find a help window or open one.
2646 */
2647 static int
2648jump_to_help_window(qf_info_T *qi, int *opened_window)
2649{
2650 win_T *wp;
2651 int flags;
2652
2653 if (cmdmod.tab != 0)
2654 wp = NULL;
2655 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002656 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002657 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2658 win_enter(wp, TRUE);
2659 else
2660 {
2661 /*
2662 * Split off help window; put it at far top if no position
2663 * specified, the current window is vertically split and narrow.
2664 */
2665 flags = WSP_HELP;
2666 if (cmdmod.split == 0 && curwin->w_width != Columns
2667 && curwin->w_width < 80)
2668 flags |= WSP_TOP;
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002669 if (IS_LL_STACK(qi))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002670 flags |= WSP_NEWLOC; /* don't copy the location list */
2671
2672 if (win_split(0, flags) == FAIL)
2673 return FAIL;
2674
2675 *opened_window = TRUE;
2676
2677 if (curwin->w_height < p_hh)
2678 win_setheight((int)p_hh);
2679
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002680 if (IS_LL_STACK(qi)) // not a quickfix list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002681 {
2682 /* The new window should use the supplied location list */
2683 curwin->w_llist = qi;
2684 qi->qf_refcount++;
2685 }
2686 }
2687
2688 if (!p_im)
2689 restart_edit = 0; /* don't want insert mode in help file */
2690
2691 return OK;
2692}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002693
2694/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002695 * Find a non-quickfix window using the given location list.
2696 * Returns NULL if a matching window is not found.
2697 */
2698 static win_T *
2699qf_find_win_with_loclist(qf_info_T *ll)
2700{
2701 win_T *wp;
2702
2703 FOR_ALL_WINDOWS(wp)
2704 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2705 return wp;
2706
2707 return NULL;
2708}
2709
2710/*
2711 * Find a window containing a normal buffer
2712 */
2713 static win_T *
2714qf_find_win_with_normal_buf(void)
2715{
2716 win_T *wp;
2717
2718 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002719 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002720 return wp;
2721
2722 return NULL;
2723}
2724
2725/*
2726 * Go to a window in any tabpage containing the specified file. Returns TRUE
2727 * if successfully jumped to the window. Otherwise returns FALSE.
2728 */
2729 static int
2730qf_goto_tabwin_with_file(int fnum)
2731{
2732 tabpage_T *tp;
2733 win_T *wp;
2734
2735 FOR_ALL_TAB_WINDOWS(tp, wp)
2736 if (wp->w_buffer->b_fnum == fnum)
2737 {
2738 goto_tabpage_win(tp, wp);
2739 return TRUE;
2740 }
2741
2742 return FALSE;
2743}
2744
2745/*
2746 * Create a new window to show a file above the quickfix window. Called when
2747 * only the quickfix window is present.
2748 */
2749 static int
2750qf_open_new_file_win(qf_info_T *ll_ref)
2751{
2752 int flags;
2753
2754 flags = WSP_ABOVE;
2755 if (ll_ref != NULL)
2756 flags |= WSP_NEWLOC;
2757 if (win_split(0, flags) == FAIL)
2758 return FAIL; /* not enough room for window */
2759 p_swb = empty_option; /* don't split again */
2760 swb_flags = 0;
2761 RESET_BINDING(curwin);
2762 if (ll_ref != NULL)
2763 {
2764 /* The new window should use the location list from the
2765 * location list window */
2766 curwin->w_llist = ll_ref;
2767 ll_ref->qf_refcount++;
2768 }
2769 return OK;
2770}
2771
2772/*
2773 * Go to a window that shows the right buffer. If the window is not found, go
2774 * to the window just above the location list window. This is used for opening
2775 * a file from a location window and not from a quickfix window. If some usable
2776 * window is previously found, then it is supplied in 'use_win'.
2777 */
2778 static void
2779qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2780{
2781 win_T *win = use_win;
2782
2783 if (win == NULL)
2784 {
2785 /* Find the window showing the selected file */
2786 FOR_ALL_WINDOWS(win)
2787 if (win->w_buffer->b_fnum == qf_fnum)
2788 break;
2789 if (win == NULL)
2790 {
2791 /* Find a previous usable window */
2792 win = curwin;
2793 do
2794 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002795 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002796 break;
2797 if (win->w_prev == NULL)
2798 win = lastwin; /* wrap around the top */
2799 else
2800 win = win->w_prev; /* go to previous window */
2801 } while (win != curwin);
2802 }
2803 }
2804 win_goto(win);
2805
2806 /* If the location list for the window is not set, then set it
2807 * to the location list from the location window */
2808 if (win->w_llist == NULL)
2809 {
2810 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002811 if (ll_ref != NULL)
2812 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002813 }
2814}
2815
2816/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002817 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2818 * not found, then go to the window just above the quickfix window. This is
2819 * used for opening a file from a quickfix window and not from a location
2820 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002821 */
2822 static void
2823qf_goto_win_with_qfl_file(int qf_fnum)
2824{
2825 win_T *win;
2826 win_T *altwin;
2827
2828 win = curwin;
2829 altwin = NULL;
2830 for (;;)
2831 {
2832 if (win->w_buffer->b_fnum == qf_fnum)
2833 break;
2834 if (win->w_prev == NULL)
2835 win = lastwin; /* wrap around the top */
2836 else
2837 win = win->w_prev; /* go to previous window */
2838
2839 if (IS_QF_WINDOW(win))
2840 {
2841 /* Didn't find it, go to the window before the quickfix
2842 * window. */
2843 if (altwin != NULL)
2844 win = altwin;
2845 else if (curwin->w_prev != NULL)
2846 win = curwin->w_prev;
2847 else
2848 win = curwin->w_next;
2849 break;
2850 }
2851
2852 /* Remember a usable window. */
Bram Moolenaar91335e52018-08-01 17:53:12 +02002853 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002854 altwin = win;
2855 }
2856
2857 win_goto(win);
2858}
2859
2860/*
2861 * Find a suitable window for opening a file (qf_fnum) from the
2862 * quickfix/location list and jump to it. If the file is already opened in a
2863 * window, jump to it. Otherwise open a new window to display the file. This is
2864 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002865 */
2866 static int
2867qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2868{
2869 win_T *usable_win_ptr = NULL;
2870 int usable_win;
2871 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002872 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002873
2874 usable_win = 0;
2875
2876 ll_ref = curwin->w_llist_ref;
2877 if (ll_ref != NULL)
2878 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002879 /* Find a non-quickfix window with this location list */
2880 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2881 if (usable_win_ptr != NULL)
2882 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002883 }
2884
2885 if (!usable_win)
2886 {
2887 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002888 win = qf_find_win_with_normal_buf();
2889 if (win != NULL)
2890 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002891 }
2892
2893 /*
2894 * If no usable window is found and 'switchbuf' contains "usetab"
2895 * then search in other tabs.
2896 */
2897 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002898 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002899
2900 /*
2901 * If there is only one window and it is the quickfix window, create a
2902 * new one above the quickfix window.
2903 */
2904 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2905 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002906 if (qf_open_new_file_win(ll_ref) != OK)
2907 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002908 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002909 }
2910 else
2911 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002912 if (curwin->w_llist_ref != NULL) /* In a location window */
2913 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2914 else /* In a quickfix window */
2915 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002916 }
2917
2918 return OK;
2919}
2920
2921/*
2922 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002923 * Returns OK if successfully edited the file, FAIL on failing to open the
2924 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
2925 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002926 */
2927 static int
2928qf_jump_edit_buffer(
2929 qf_info_T *qi,
2930 qfline_T *qf_ptr,
2931 int forceit,
2932 win_T *oldwin,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002933 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002934{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002935 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002936 int retval = OK;
2937
2938 if (qf_ptr->qf_type == 1)
2939 {
2940 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2941 * set b_p_ro flag). */
2942 if (!can_abandon(curbuf, forceit))
2943 {
2944 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002945 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002946 }
2947 else
2948 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2949 ECMD_HIDE + ECMD_SET_HELP,
2950 oldwin == curwin ? curwin : NULL);
2951 }
2952 else
2953 {
2954 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002955 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002956
2957 retval = buflist_getfile(qf_ptr->qf_fnum,
2958 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002959
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002960 if (IS_LL_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002961 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002962 /*
2963 * Location list. Check whether the associated window is still
2964 * present and the list is still valid.
2965 */
2966 if (!win_valid_any_tab(oldwin))
2967 {
2968 EMSG(_("E924: Current window was closed"));
Bram Moolenaar3c097222017-12-21 20:54:49 +01002969 *opened_window = FALSE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002970 return NOTDONE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002971 }
2972 else if (!qflist_valid(oldwin, save_qfid))
2973 {
2974 EMSG(_(e_loc_list_changed));
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002975 return NOTDONE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002976 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002977 }
2978 else if (old_qf_curlist != qi->qf_curlist
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002979 || !is_qf_entry_present(qfl, qf_ptr))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002980 {
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002981 if (IS_QF_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002982 EMSG(_("E925: Current quickfix was changed"));
2983 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002984 EMSG(_(e_loc_list_changed));
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002985 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002986 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002987 }
2988
2989 return retval;
2990}
2991
2992/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002993 * Go to the error line in the current file using either line/column number or
2994 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002995 */
2996 static void
2997qf_jump_goto_line(
2998 linenr_T qf_lnum,
2999 int qf_col,
3000 char_u qf_viscol,
3001 char_u *qf_pattern)
3002{
3003 linenr_T i;
3004 char_u *line;
3005 colnr_T screen_col;
3006 colnr_T char_col;
3007
3008 if (qf_pattern == NULL)
3009 {
3010 /*
3011 * Go to line with error, unless qf_lnum is 0.
3012 */
3013 i = qf_lnum;
3014 if (i > 0)
3015 {
3016 if (i > curbuf->b_ml.ml_line_count)
3017 i = curbuf->b_ml.ml_line_count;
3018 curwin->w_cursor.lnum = i;
3019 }
3020 if (qf_col > 0)
3021 {
3022 curwin->w_cursor.col = qf_col - 1;
3023#ifdef FEAT_VIRTUALEDIT
3024 curwin->w_cursor.coladd = 0;
3025#endif
3026 if (qf_viscol == TRUE)
3027 {
3028 /*
3029 * Check each character from the beginning of the error
3030 * line up to the error column. For each tab character
3031 * found, reduce the error column value by the length of
3032 * a tab character.
3033 */
3034 line = ml_get_curline();
3035 screen_col = 0;
3036 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3037 {
3038 if (*line == NUL)
3039 break;
3040 if (*line++ == '\t')
3041 {
3042 curwin->w_cursor.col -= 7 - (screen_col % 8);
3043 screen_col += 8 - (screen_col % 8);
3044 }
3045 else
3046 ++screen_col;
3047 }
3048 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003049 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003050 check_cursor();
3051 }
3052 else
3053 beginline(BL_WHITE | BL_FIX);
3054 }
3055 else
3056 {
3057 pos_T save_cursor;
3058
3059 /* Move the cursor to the first line in the buffer */
3060 save_cursor = curwin->w_cursor;
3061 curwin->w_cursor.lnum = 0;
3062 if (!do_search(NULL, '/', qf_pattern, (long)1,
3063 SEARCH_KEEP, NULL, NULL))
3064 curwin->w_cursor = save_cursor;
3065 }
3066}
3067
3068/*
3069 * Display quickfix list index and size message
3070 */
3071 static void
3072qf_jump_print_msg(
3073 qf_info_T *qi,
3074 int qf_index,
3075 qfline_T *qf_ptr,
3076 buf_T *old_curbuf,
3077 linenr_T old_lnum)
3078{
3079 linenr_T i;
3080 int len;
3081
3082 /* Update the screen before showing the message, unless the screen
3083 * scrolled up. */
3084 if (!msg_scrolled)
3085 update_topline_redraw();
3086 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3087 qi->qf_lists[qi->qf_curlist].qf_count,
3088 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3089 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
3090 /* Add the message, skipping leading whitespace and newlines. */
3091 len = (int)STRLEN(IObuff);
3092 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3093
3094 /* Output the message. Overwrite to avoid scrolling when the 'O'
3095 * flag is present in 'shortmess'; But when not jumping, print the
3096 * whole message. */
3097 i = msg_scroll;
3098 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3099 msg_scroll = TRUE;
3100 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3101 msg_scroll = FALSE;
3102 msg_attr_keep(IObuff, 0, TRUE);
3103 msg_scroll = i;
3104}
3105
3106/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003107 * Find a usable window for opening a file from the quickfix/location list. If
3108 * a window is not found then open a new window.
3109 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3110 * able to jump/open a window. Returns NOTDONE if a file is not associated
3111 * with the entry.
3112 */
3113 static int
3114qf_jump_open_window(qf_info_T *qi, qfline_T *qf_ptr, int *opened_window)
3115{
3116 // For ":helpgrep" find a help window or open one.
3117 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
3118 if (jump_to_help_window(qi, opened_window) == FAIL)
3119 return FAIL;
3120
3121 // If currently in the quickfix window, find another window to show the
3122 // file in.
3123 if (bt_quickfix(curbuf) && !*opened_window)
3124 {
3125 // If there is no file specified, we don't know where to go.
3126 // But do advance, otherwise ":cn" gets stuck.
3127 if (qf_ptr->qf_fnum == 0)
3128 return NOTDONE;
3129
3130 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, opened_window) == FAIL)
3131 return FAIL;
3132 }
3133
3134 return OK;
3135}
3136
3137/*
3138 * Edit a selected file from the quickfix/location list and jump to a
3139 * particular line/column, adjust the folds and display a message about the
3140 * jump.
3141 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3142 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3143 * the file.
3144 */
3145 static int
3146qf_jump_to_buffer(
3147 qf_info_T *qi,
3148 int qf_index,
3149 qfline_T *qf_ptr,
3150 int forceit,
3151 win_T *oldwin,
3152 int *opened_window,
3153 int openfold,
3154 int print_message)
3155{
3156 buf_T *old_curbuf;
3157 linenr_T old_lnum;
3158 int retval = OK;
3159
3160 // If there is a file name, read the wanted file if needed, and check
3161 // autowrite etc.
3162 old_curbuf = curbuf;
3163 old_lnum = curwin->w_cursor.lnum;
3164
3165 if (qf_ptr->qf_fnum != 0)
3166 {
3167 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3168 opened_window);
3169 if (retval != OK)
3170 return retval;
3171 }
3172
3173 // When not switched to another buffer, still need to set pc mark
3174 if (curbuf == old_curbuf)
3175 setpcmark();
3176
3177 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3178 qf_ptr->qf_pattern);
3179
3180#ifdef FEAT_FOLDING
3181 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3182 foldOpenCursor();
3183#endif
3184 if (print_message)
3185 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3186
3187 return retval;
3188}
3189
3190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 * jump to a quickfix line
3192 * if dir == FORWARD go "errornr" valid entries forward
3193 * if dir == BACKWARD go "errornr" valid entries backward
3194 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3195 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3196 * else if "errornr" is zero, redisplay the same line
3197 * else go to entry "errornr"
3198 */
3199 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003200qf_jump(qf_info_T *qi,
3201 int dir,
3202 int errornr,
3203 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003205 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003206 qfline_T *qf_ptr;
3207 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003210 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003211 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003213 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003216 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003218 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003220 if (qi == NULL)
3221 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003222
3223 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003224 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
3226 EMSG(_(e_quickfix));
3227 return;
3228 }
3229
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003230 qfl = &qi->qf_lists[qi->qf_curlist];
3231
3232 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003234 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003236
3237 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3238 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003240 qf_ptr = old_qf_ptr;
3241 qf_index = old_qf_index;
3242 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003245 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003246 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003247 // No need to print the error message if it's visible in the error
3248 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 print_message = FALSE;
3250
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003251 retval = qf_jump_open_window(qi, qf_ptr, &opened_window);
3252 if (retval == FAIL)
3253 goto failed;
3254 if (retval == NOTDONE)
3255 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003256
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003257 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3258 &opened_window, old_KeyTyped, print_message);
3259 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003261 // Quickfix/location list is freed by an autocmd
3262 qi = NULL;
3263 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003266 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003269 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003270 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003272 // Couldn't open file, so put index back where it was. This could
3273 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 qf_ptr = old_qf_ptr;
3276 qf_index = old_qf_index;
3277 }
3278 }
3279theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003280 if (qi != NULL)
3281 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003282 qfl->qf_ptr = qf_ptr;
3283 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 if (p_swb != old_swb && opened_window)
3286 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003287 // Restore old 'switchbuf' value, but not when an autocommand or
3288 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003292 swb_flags = old_swb_flags;
3293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 else
3295 free_string_option(old_swb);
3296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297}
3298
3299/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003300 * Highlight attributes used for displaying entries from the quickfix list.
3301 */
3302static int qfFileAttr;
3303static int qfSepAttr;
3304static int qfLineAttr;
3305
3306/*
3307 * Display information about a single entry from the quickfix/location list.
3308 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003309 * 'cursel' will be set to TRUE for the currently selected entry in the
3310 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003311 */
3312 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003313qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003314{
3315 char_u *fname;
3316 buf_T *buf;
3317 int filter_entry;
3318
3319 fname = NULL;
3320 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3321 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3322 (char *)qfp->qf_module);
3323 else {
3324 if (qfp->qf_fnum != 0
3325 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3326 {
3327 fname = buf->b_fname;
3328 if (qfp->qf_type == 1) /* :helpgrep */
3329 fname = gettail(fname);
3330 }
3331 if (fname == NULL)
3332 sprintf((char *)IObuff, "%2d", qf_idx);
3333 else
3334 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3335 qf_idx, (char *)fname);
3336 }
3337
3338 // Support for filtering entries using :filter /pat/ clist
3339 // Match against the module name, file name, search pattern and
3340 // text of the entry.
3341 filter_entry = TRUE;
3342 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3343 filter_entry &= message_filtered(qfp->qf_module);
3344 if (filter_entry && fname != NULL)
3345 filter_entry &= message_filtered(fname);
3346 if (filter_entry && qfp->qf_pattern != NULL)
3347 filter_entry &= message_filtered(qfp->qf_pattern);
3348 if (filter_entry)
3349 filter_entry &= message_filtered(qfp->qf_text);
3350 if (filter_entry)
3351 return;
3352
3353 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003354 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003355
3356 if (qfp->qf_lnum != 0)
3357 msg_puts_attr((char_u *)":", qfSepAttr);
3358 if (qfp->qf_lnum == 0)
3359 IObuff[0] = NUL;
3360 else if (qfp->qf_col == 0)
3361 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3362 else
3363 sprintf((char *)IObuff, "%ld col %d",
3364 qfp->qf_lnum, qfp->qf_col);
3365 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3366 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3367 msg_puts_attr(IObuff, qfLineAttr);
3368 msg_puts_attr((char_u *)":", qfSepAttr);
3369 if (qfp->qf_pattern != NULL)
3370 {
3371 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3372 msg_puts(IObuff);
3373 msg_puts_attr((char_u *)":", qfSepAttr);
3374 }
3375 msg_puts((char_u *)" ");
3376
3377 /* Remove newlines and leading whitespace from the text. For an
3378 * unrecognized line keep the indent, the compiler may mark a word
3379 * with ^^^^. */
3380 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3381 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3382 IObuff, IOSIZE);
3383 msg_prt_line(IObuff, FALSE);
3384 out_flush(); /* show one line at a time */
3385}
3386
3387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003389 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 */
3391 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003392qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003394 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003395 qfline_T *qfp;
3396 int i;
3397 int idx1 = 1;
3398 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003399 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003400 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003401 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003403 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaar39665952018-08-15 20:59:48 +02003405 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003406 {
3407 qi = GET_LOC_LIST(curwin);
3408 if (qi == NULL)
3409 {
3410 EMSG(_(e_loclist));
3411 return;
3412 }
3413 }
3414
3415 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003416 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
3418 EMSG(_(e_quickfix));
3419 return;
3420 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003421 if (*arg == '+')
3422 {
3423 ++arg;
3424 plus = TRUE;
3425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3427 {
3428 EMSG(_(e_trailing));
3429 return;
3430 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003431 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003432 if (plus)
3433 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003434 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003435 idx2 = i + idx1;
3436 idx1 = i;
3437 }
3438 else
3439 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003440 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003441 if (idx1 < 0)
3442 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3443 if (idx2 < 0)
3444 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
Bram Moolenaara796d462018-05-01 14:30:36 +02003447 /* Shorten all the file names, so that it is easy to read */
3448 shorten_fnames(FALSE);
3449
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003450 /*
3451 * Get the attributes for the different quickfix highlight items. Note
3452 * that this depends on syntax items defined in the qf.vim syntax file
3453 */
3454 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3455 if (qfFileAttr == 0)
3456 qfFileAttr = HL_ATTR(HLF_D);
3457 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3458 if (qfSepAttr == 0)
3459 qfSepAttr = HL_ATTR(HLF_D);
3460 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3461 if (qfLineAttr == 0)
3462 qfLineAttr = HL_ATTR(HLF_N);
3463
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003464 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003466 qfp = qfl->qf_start;
3467 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
3469 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3470 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003471 if (got_int)
3472 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003473
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003474 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003476
3477 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003478 if (qfp == NULL)
3479 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003480 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 ui_breakcheck();
3482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483}
3484
3485/*
3486 * Remove newlines and leading whitespace from an error message.
3487 * Put the result in "buf[bufsize]".
3488 */
3489 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003490qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491{
3492 int i;
3493 char_u *p = text;
3494
3495 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3496 {
3497 if (*p == '\n')
3498 {
3499 buf[i] = ' ';
3500 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003501 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 break;
3503 }
3504 else
3505 buf[i] = *p++;
3506 }
3507 buf[i] = NUL;
3508}
3509
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003510/*
3511 * Display information (list number, list size and the title) about a
3512 * quickfix/location list.
3513 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003514 static void
3515qf_msg(qf_info_T *qi, int which, char *lead)
3516{
3517 char *title = (char *)qi->qf_lists[which].qf_title;
3518 int count = qi->qf_lists[which].qf_count;
3519 char_u buf[IOSIZE];
3520
3521 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3522 lead,
3523 which + 1,
3524 qi->qf_listcount,
3525 count);
3526
3527 if (title != NULL)
3528 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003529 size_t len = STRLEN(buf);
3530
3531 if (len < 34)
3532 {
3533 vim_memset(buf + len, ' ', 34 - len);
3534 buf[34] = NUL;
3535 }
3536 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003537 }
3538 trunc_string(buf, buf, Columns - 1, IOSIZE);
3539 msg(buf);
3540}
3541
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542/*
3543 * ":colder [count]": Up in the quickfix stack.
3544 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003545 * ":lolder [count]": Up in the location list stack.
3546 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 */
3548 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003549qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003551 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 int count;
3553
Bram Moolenaar39665952018-08-15 20:59:48 +02003554 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003555 {
3556 qi = GET_LOC_LIST(curwin);
3557 if (qi == NULL)
3558 {
3559 EMSG(_(e_loclist));
3560 return;
3561 }
3562 }
3563
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (eap->addr_count != 0)
3565 count = eap->line2;
3566 else
3567 count = 1;
3568 while (count--)
3569 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003570 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003572 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
3574 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003575 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003577 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579 else
3580 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003581 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003584 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003586 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
3588 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003589 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003590 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591}
3592
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003593/*
3594 * Display the information about all the quickfix/location lists in the stack
3595 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003596 void
3597qf_history(exarg_T *eap)
3598{
3599 qf_info_T *qi = &ql_info;
3600 int i;
3601
Bram Moolenaar39665952018-08-15 20:59:48 +02003602 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003603 qi = GET_LOC_LIST(curwin);
3604 if (qi == NULL || (qi->qf_listcount == 0
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003605 && qf_list_empty(qi, qi->qf_curlist)))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003606 MSG(_("No entries"));
3607 else
3608 for (i = 0; i < qi->qf_listcount; ++i)
3609 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3610}
3611
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003613 * Free all the entries in the error list "idx". Note that other information
3614 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 */
3616 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003617qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003619 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003620 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003621 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003623 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003625 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003626 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003627 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003628 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003629 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003630 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003631 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003632 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003633 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003634 if (stop)
3635 /* Somehow qf_count may have an incorrect value, set it to 1
3636 * to avoid crashing when it's wrong.
3637 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003638 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003639 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003640 qfl->qf_start = qfpnext;
3641 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003643
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003644 qfl->qf_index = 0;
3645 qfl->qf_start = NULL;
3646 qfl->qf_last = NULL;
3647 qfl->qf_ptr = NULL;
3648 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003649
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003650 qf_clean_dir_stack(&qfl->qf_dir_stack);
3651 qfl->qf_directory = NULL;
3652 qf_clean_dir_stack(&qfl->qf_file_stack);
3653 qfl->qf_currfile = NULL;
3654 qfl->qf_multiline = FALSE;
3655 qfl->qf_multiignore = FALSE;
3656 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657}
3658
3659/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003660 * Free error list "idx". Frees all the entries in the quickfix list,
3661 * associated context information and the title.
3662 */
3663 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003664qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003665{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003666 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003667
Bram Moolenaard23a8232018-02-10 18:45:26 +01003668 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003669 free_tv(qfl->qf_ctx);
3670 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003671 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003672 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003673}
3674
3675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 * qf_mark_adjust: adjust marks
3677 */
3678 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003679qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003680 win_T *wp,
3681 linenr_T line1,
3682 linenr_T line2,
3683 long amount,
3684 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003686 int i;
3687 qfline_T *qfp;
3688 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003689 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003690 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003691 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
Bram Moolenaarc1542742016-07-20 21:44:37 +02003693 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003694 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003695 if (wp != NULL)
3696 {
3697 if (wp->w_llist == NULL)
3698 return;
3699 qi = wp->w_llist;
3700 }
3701
3702 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003703 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003704 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003705 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3706 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 if (qfp->qf_fnum == curbuf->b_fnum)
3708 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003709 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3711 {
3712 if (amount == MAXLNUM)
3713 qfp->qf_cleared = TRUE;
3714 else
3715 qfp->qf_lnum += amount;
3716 }
3717 else if (amount_after && qfp->qf_lnum > line2)
3718 qfp->qf_lnum += amount_after;
3719 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003720
3721 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003722 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723}
3724
3725/*
3726 * Make a nice message out of the error character and the error number:
3727 * char number message
3728 * e or E 0 " error"
3729 * w or W 0 " warning"
3730 * i or I 0 " info"
3731 * 0 0 ""
3732 * other 0 " c"
3733 * e or E n " error n"
3734 * w or W n " warning n"
3735 * i or I n " info n"
3736 * 0 n " error n"
3737 * other n " c n"
3738 * 1 x "" :helpgrep
3739 */
3740 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003741qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
3743 static char_u buf[20];
3744 static char_u cc[3];
3745 char_u *p;
3746
3747 if (c == 'W' || c == 'w')
3748 p = (char_u *)" warning";
3749 else if (c == 'I' || c == 'i')
3750 p = (char_u *)" info";
3751 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3752 p = (char_u *)" error";
3753 else if (c == 0 || c == 1)
3754 p = (char_u *)"";
3755 else
3756 {
3757 cc[0] = ' ';
3758 cc[1] = c;
3759 cc[2] = NUL;
3760 p = cc;
3761 }
3762
3763 if (nr <= 0)
3764 return p;
3765
3766 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3767 return buf;
3768}
3769
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003771 * When "split" is FALSE: Open the entry/result under the cursor.
3772 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3773 */
3774 void
3775qf_view_result(int split)
3776{
3777 qf_info_T *qi = &ql_info;
3778
3779 if (!bt_quickfix(curbuf))
3780 return;
3781
3782 if (IS_LL_WINDOW(curwin))
3783 qi = GET_LOC_LIST(curwin);
3784
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003785 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003786 {
3787 EMSG(_(e_quickfix));
3788 return;
3789 }
3790
3791 if (split)
3792 {
3793 char_u cmd[32];
3794
3795 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3796 (long)curwin->w_cursor.lnum,
3797 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3798 if (do_cmdline_cmd(cmd) == OK)
3799 do_cmdline_cmd((char_u *) "clearjumps");
3800 return;
3801 }
3802
3803 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3804}
3805
3806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 * ":cwindow": open the quickfix window if we have errors to display,
3808 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003809 * ":lwindow": open the location list window if we have locations to display,
3810 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 */
3812 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003813ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003815 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 win_T *win;
3817
Bram Moolenaar39665952018-08-15 20:59:48 +02003818 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003819 {
3820 qi = GET_LOC_LIST(curwin);
3821 if (qi == NULL)
3822 return;
3823 }
3824
3825 /* Look for an existing quickfix window. */
3826 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
3828 /*
3829 * If a quickfix window is open but we have no errors to display,
3830 * close the window. If a quickfix window is not open, then open
3831 * it if we have errors; otherwise, leave it closed.
3832 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003833 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003834 || qf_list_empty(qi, qi->qf_curlist)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003835 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 {
3837 if (win != NULL)
3838 ex_cclose(eap);
3839 }
3840 else if (win == NULL)
3841 ex_copen(eap);
3842}
3843
3844/*
3845 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003846 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003849ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003851 win_T *win = NULL;
3852 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853
Bram Moolenaar39665952018-08-15 20:59:48 +02003854 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003855 {
3856 qi = GET_LOC_LIST(curwin);
3857 if (qi == NULL)
3858 return;
3859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003861 /* Find existing quickfix window and close it. */
3862 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 if (win != NULL)
3864 win_close(win, FALSE);
3865}
3866
3867/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003868 * Set "w:quickfix_title" if "qi" has a title.
3869 */
3870 static void
3871qf_set_title_var(qf_list_T *qfl)
3872{
3873 if (qfl->qf_title != NULL)
3874 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3875}
3876
3877/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003878 * Goto a quickfix or location list window (if present).
3879 * Returns OK if the window is found, FAIL otherwise.
3880 */
3881 static int
3882qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3883{
3884 win_T *win;
3885
3886 win = qf_find_win(qi);
3887 if (win == NULL)
3888 return FAIL;
3889
3890 win_goto(win);
3891 if (resize)
3892 {
3893 if (vertsplit)
3894 {
3895 if (sz != win->w_width)
3896 win_setwidth(sz);
3897 }
3898 else if (sz != win->w_height)
3899 win_setheight(sz);
3900 }
3901
3902 return OK;
3903}
3904
3905/*
3906 * Open a new quickfix or location list window, load the quickfix buffer and
3907 * set the appropriate options for the window.
3908 * Returns FAIL if the window could not be opened.
3909 */
3910 static int
3911qf_open_new_cwindow(qf_info_T *qi, int height)
3912{
3913 buf_T *qf_buf;
3914 win_T *oldwin = curwin;
3915 tabpage_T *prevtab = curtab;
3916 int flags = 0;
3917 win_T *win;
3918
3919 qf_buf = qf_find_buf(qi);
3920
3921 // The current window becomes the previous window afterwards.
3922 win = curwin;
3923
3924 if (IS_QF_STACK(qi) && cmdmod.split == 0)
3925 // Create the new quickfix window at the very bottom, except when
3926 // :belowright or :aboveleft is used.
3927 win_goto(lastwin);
3928 // Default is to open the window below the current window
3929 if (cmdmod.split == 0)
3930 flags = WSP_BELOW;
3931 flags |= WSP_NEWLOC;
3932 if (win_split(height, flags) == FAIL)
3933 return FAIL; // not enough room for window
3934 RESET_BINDING(curwin);
3935
3936 if (IS_LL_STACK(qi))
3937 {
3938 // For the location list window, create a reference to the
3939 // location list from the window 'win'.
3940 curwin->w_llist_ref = win->w_llist;
3941 win->w_llist->qf_refcount++;
3942 }
3943
3944 if (oldwin != curwin)
3945 oldwin = NULL; // don't store info when in another window
3946 if (qf_buf != NULL)
3947 {
3948 // Use the existing quickfix buffer
3949 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
3950 ECMD_HIDE + ECMD_OLDBUF, oldwin);
3951 }
3952 else
3953 {
3954 // Create a new quickfix buffer
3955 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
3956
3957 // switch off 'swapfile'
3958 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3959 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
3960 OPT_LOCAL);
3961 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
3962 RESET_BINDING(curwin);
3963#ifdef FEAT_DIFF
3964 curwin->w_p_diff = FALSE;
3965#endif
3966#ifdef FEAT_FOLDING
3967 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3968 OPT_LOCAL);
3969#endif
3970 }
3971
3972 // Only set the height when still in the same tab page and there is no
3973 // window to the side.
3974 if (curtab == prevtab && curwin->w_width == Columns)
3975 win_setheight(height);
3976 curwin->w_p_wfh = TRUE; // set 'winfixheight'
3977 if (win_valid(win))
3978 prevwin = win;
3979
3980 return OK;
3981}
3982
3983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003985 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 */
3987 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003988ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003990 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003992 int status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
Bram Moolenaar39665952018-08-15 20:59:48 +02003994 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003995 {
3996 qi = GET_LOC_LIST(curwin);
3997 if (qi == NULL)
3998 {
3999 EMSG(_(e_loclist));
4000 return;
4001 }
4002 }
4003
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (eap->addr_count != 0)
4005 height = eap->line2;
4006 else
4007 height = QF_WINHEIGHT;
4008
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004009 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010#ifdef FEAT_GUI
4011 need_mouse_correct = TRUE;
4012#endif
4013
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004014 // Find an existing quickfix window, or open a new one.
4015 if (cmdmod.tab == 0)
4016 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4017 cmdmod.split & WSP_VERT);
4018 if (status == FAIL)
4019 if (qf_open_new_cwindow(qi, height) == FAIL)
4020 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004022 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004023
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004024 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004025 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004027 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 curwin->w_cursor.col = 0;
4029 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004030 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031}
4032
4033/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004034 * Move the cursor in the quickfix window to "lnum".
4035 */
4036 static void
4037qf_win_goto(win_T *win, linenr_T lnum)
4038{
4039 win_T *old_curwin = curwin;
4040
4041 curwin = win;
4042 curbuf = win->w_buffer;
4043 curwin->w_cursor.lnum = lnum;
4044 curwin->w_cursor.col = 0;
4045#ifdef FEAT_VIRTUALEDIT
4046 curwin->w_cursor.coladd = 0;
4047#endif
4048 curwin->w_curswant = 0;
4049 update_topline(); /* scroll to show the line */
4050 redraw_later(VALID);
4051 curwin->w_redr_status = TRUE; /* update ruler */
4052 curwin = old_curwin;
4053 curbuf = curwin->w_buffer;
4054}
4055
4056/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004057 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004058 */
4059 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004060ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004061{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004062 qf_info_T *qi = &ql_info;
4063 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004064
Bram Moolenaar39665952018-08-15 20:59:48 +02004065 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004066 {
4067 qi = GET_LOC_LIST(curwin);
4068 if (qi == NULL)
4069 {
4070 EMSG(_(e_loclist));
4071 return;
4072 }
4073 }
4074
4075 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004076 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4077 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4078}
4079
4080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 * Return the number of the current entry (line number in the quickfix
4082 * window).
4083 */
4084 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004085qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004087 qf_info_T *qi = &ql_info;
4088
4089 if (IS_LL_WINDOW(wp))
4090 /* In the location list window, use the referenced location list */
4091 qi = wp->w_llist_ref;
4092
4093 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094}
4095
4096/*
4097 * Update the cursor position in the quickfix window to the current error.
4098 * Return TRUE if there is a quickfix window.
4099 */
4100 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004101qf_win_pos_update(
4102 qf_info_T *qi,
4103 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
4105 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004106 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
4108 /*
4109 * Put the cursor on the current error in the quickfix window, so that
4110 * it's viewable.
4111 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004112 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 if (win != NULL
4114 && qf_index <= win->w_buffer->b_ml.ml_line_count
4115 && old_qf_index != qf_index)
4116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (qf_index > old_qf_index)
4118 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004119 win->w_redraw_top = old_qf_index;
4120 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122 else
4123 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004124 win->w_redraw_top = qf_index;
4125 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004127 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 return win != NULL;
4130}
4131
4132/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004133 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004134 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004135 */
4136 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004137is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004138{
4139 /*
4140 * A window displaying the quickfix buffer will have the w_llist_ref field
4141 * set to NULL.
4142 * A window displaying a location list buffer will have the w_llist_ref
4143 * pointing to the location list.
4144 */
4145 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004146 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4147 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004148 return TRUE;
4149
4150 return FALSE;
4151}
4152
4153/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004154 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004155 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004156 */
4157 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004158qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004159{
4160 win_T *win;
4161
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004162 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004163 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004164 return win;
4165 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004166}
4167
4168/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004169 * Find a quickfix buffer.
4170 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 */
4172 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004173qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004175 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004176 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
Bram Moolenaar9c102382006-05-03 21:26:49 +00004178 FOR_ALL_TAB_WINDOWS(tp, win)
4179 if (is_qf_win(win, qi))
4180 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004181
Bram Moolenaar9c102382006-05-03 21:26:49 +00004182 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183}
4184
4185/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004186 * Update the w:quickfix_title variable in the quickfix/location list window
4187 */
4188 static void
4189qf_update_win_titlevar(qf_info_T *qi)
4190{
4191 win_T *win;
4192 win_T *curwin_save;
4193
4194 if ((win = qf_find_win(qi)) != NULL)
4195 {
4196 curwin_save = curwin;
4197 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004198 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004199 curwin = curwin_save;
4200 }
4201}
4202
4203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 * Find the quickfix buffer. If it exists, update the contents.
4205 */
4206 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004207qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
4209 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004210 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
4213 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004214 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 if (buf != NULL)
4216 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004217 linenr_T old_line_count = buf->b_ml.ml_line_count;
4218
4219 if (old_last == NULL)
4220 /* set curwin/curbuf to buf and save a few things */
4221 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
Bram Moolenaard823fa92016-08-12 16:29:27 +02004223 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004224
Bram Moolenaar864293a2016-06-02 13:40:04 +02004225 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004226 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004227
Bram Moolenaar864293a2016-06-02 13:40:04 +02004228 if (old_last == NULL)
4229 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004230 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004231
4232 /* restore curwin/curbuf and a few other things */
4233 aucmd_restbuf(&aco);
4234 }
4235
4236 /* Only redraw when added lines are visible. This avoids flickering
4237 * when the added lines are not visible. */
4238 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4239 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241}
4242
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004243/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004244 * Add an error line to the quickfix buffer.
4245 */
4246 static int
4247qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4248{
4249 int len;
4250 buf_T *errbuf;
4251
4252 if (qfp->qf_module != NULL)
4253 {
4254 STRCPY(IObuff, qfp->qf_module);
4255 len = (int)STRLEN(IObuff);
4256 }
4257 else if (qfp->qf_fnum != 0
4258 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4259 && errbuf->b_fname != NULL)
4260 {
4261 if (qfp->qf_type == 1) /* :helpgrep */
4262 STRCPY(IObuff, gettail(errbuf->b_fname));
4263 else
4264 {
4265 /* shorten the file name if not done already */
4266 if (errbuf->b_sfname == NULL
4267 || mch_isFullName(errbuf->b_sfname))
4268 {
4269 if (*dirname == NUL)
4270 mch_dirname(dirname, MAXPATHL);
4271 shorten_buf_fname(errbuf, dirname, FALSE);
4272 }
4273 STRCPY(IObuff, errbuf->b_fname);
4274 }
4275 len = (int)STRLEN(IObuff);
4276 }
4277 else
4278 len = 0;
4279 IObuff[len++] = '|';
4280
4281 if (qfp->qf_lnum > 0)
4282 {
4283 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4284 len += (int)STRLEN(IObuff + len);
4285
4286 if (qfp->qf_col > 0)
4287 {
4288 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4289 len += (int)STRLEN(IObuff + len);
4290 }
4291
4292 sprintf((char *)IObuff + len, "%s",
4293 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4294 len += (int)STRLEN(IObuff + len);
4295 }
4296 else if (qfp->qf_pattern != NULL)
4297 {
4298 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4299 len += (int)STRLEN(IObuff + len);
4300 }
4301 IObuff[len++] = '|';
4302 IObuff[len++] = ' ';
4303
4304 /* Remove newlines and leading whitespace from the text.
4305 * For an unrecognized line keep the indent, the compiler may
4306 * mark a word with ^^^^. */
4307 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4308 IObuff + len, IOSIZE - len);
4309
4310 if (ml_append_buf(buf, lnum, IObuff,
4311 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4312 return FAIL;
4313
4314 return OK;
4315}
4316
4317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 * Fill current buffer with quickfix errors, replacing any previous contents.
4319 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004320 * If "old_last" is not NULL append the items after this one.
4321 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4322 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 */
4324 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004325qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004327 linenr_T lnum;
4328 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004329 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
Bram Moolenaar864293a2016-06-02 13:40:04 +02004331 if (old_last == NULL)
4332 {
4333 if (buf != curbuf)
4334 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004335 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004336 return;
4337 }
4338
4339 /* delete all existing lines */
4340 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4341 (void)ml_delete((linenr_T)1, FALSE);
4342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343
4344 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004345 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 {
Bram Moolenaara796d462018-05-01 14:30:36 +02004347 char_u dirname[MAXPATHL];
4348
4349 *dirname = NUL;
4350
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004352 if (old_last == NULL)
4353 {
4354 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4355 lnum = 0;
4356 }
4357 else
4358 {
4359 qfp = old_last->qf_next;
4360 lnum = buf->b_ml.ml_line_count;
4361 }
4362 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004364 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004366
Bram Moolenaar864293a2016-06-02 13:40:04 +02004367 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004369 if (qfp == NULL)
4370 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004372
4373 if (old_last == NULL)
4374 /* Delete the empty line which is now at the end */
4375 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
4377
4378 /* correct cursor position */
4379 check_lnums(TRUE);
4380
Bram Moolenaar864293a2016-06-02 13:40:04 +02004381 if (old_last == NULL)
4382 {
4383 /* Set the 'filetype' to "qf" each time after filling the buffer.
4384 * This resembles reading a file into a buffer, it's more logical when
4385 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02004386 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004387 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4388 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
Bram Moolenaar864293a2016-06-02 13:40:04 +02004390 keep_filetype = TRUE; /* don't detect 'filetype' */
4391 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004393 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004395 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004396 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004397
Bram Moolenaar864293a2016-06-02 13:40:04 +02004398 /* make sure it will be redrawn */
4399 redraw_curbuf_later(NOT_VALID);
4400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
4402 /* Restore KeyTyped, setting 'filetype' may reset it. */
4403 KeyTyped = old_KeyTyped;
4404}
4405
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004406/*
4407 * For every change made to the quickfix list, update the changed tick.
4408 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004409 static void
4410qf_list_changed(qf_info_T *qi, int qf_idx)
4411{
4412 qi->qf_lists[qf_idx].qf_changedtick++;
4413}
4414
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004416 * Return the quickfix/location list number with the given identifier.
4417 * Returns -1 if list is not found.
4418 */
4419 static int
4420qf_id2nr(qf_info_T *qi, int_u qfid)
4421{
4422 int qf_idx;
4423
4424 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4425 if (qi->qf_lists[qf_idx].qf_id == qfid)
4426 return qf_idx;
4427 return INVALID_QFIDX;
4428}
4429
4430/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004431 * If the current list is not "save_qfid" and we can find the list with that ID
4432 * then make it the current list.
4433 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004434 * Returns OK if successfully restored the list. Returns FAIL if the list with
4435 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004436 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004437 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004438qf_restore_list(qf_info_T *qi, int_u save_qfid)
4439{
4440 int curlist;
4441
4442 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4443 {
4444 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004445 if (curlist < 0)
4446 // list is not present
4447 return FAIL;
4448 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004449 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004450 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004451}
4452
4453/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004454 * Jump to the first entry if there is one.
4455 */
4456 static void
4457qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4458{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004459 if (qf_restore_list(qi, save_qfid) == FAIL)
4460 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004461
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004462 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004463 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004464 qf_jump(qi, 0, 0, forceit);
4465}
4466
4467/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004468 * Return TRUE when using ":vimgrep" for ":grep".
4469 */
4470 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004471grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004472{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004473 return ((cmdidx == CMD_grep
4474 || cmdidx == CMD_lgrep
4475 || cmdidx == CMD_grepadd
4476 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004477 && STRCMP("internal",
4478 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4479}
4480
4481/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004482 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004484 static char_u *
4485make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004487 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004488 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004489 case CMD_make: return (char_u *)"make";
4490 case CMD_lmake: return (char_u *)"lmake";
4491 case CMD_grep: return (char_u *)"grep";
4492 case CMD_lgrep: return (char_u *)"lgrep";
4493 case CMD_grepadd: return (char_u *)"grepadd";
4494 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4495 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497}
4498
4499/*
4500 * Return the name for the errorfile, in allocated memory.
4501 * Find a new unique name when 'makeef' contains "##".
4502 * Returns NULL for error.
4503 */
4504 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004505get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506{
4507 char_u *p;
4508 char_u *name;
4509 static int start = -1;
4510 static int off = 0;
4511#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004512 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513#endif
4514
4515 if (*p_mef == NUL)
4516 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004517 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 if (name == NULL)
4519 EMSG(_(e_notmp));
4520 return name;
4521 }
4522
4523 for (p = p_mef; *p; ++p)
4524 if (p[0] == '#' && p[1] == '#')
4525 break;
4526
4527 if (*p == NUL)
4528 return vim_strsave(p_mef);
4529
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004530 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 for (;;)
4532 {
4533 if (start == -1)
4534 start = mch_get_pid();
4535 else
4536 off += 19;
4537
4538 name = alloc((unsigned)STRLEN(p_mef) + 30);
4539 if (name == NULL)
4540 break;
4541 STRCPY(name, p_mef);
4542 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4543 STRCAT(name, p + 2);
4544 if (mch_getperm(name) < 0
4545#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004546 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 && mch_lstat((char *)name, &sb) < 0
4548#endif
4549 )
4550 break;
4551 vim_free(name);
4552 }
4553 return name;
4554}
4555
4556/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004557 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4558 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4559 */
4560 static char_u *
4561make_get_fullcmd(char_u *makecmd, char_u *fname)
4562{
4563 char_u *cmd;
4564 unsigned len;
4565
4566 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4567 if (*p_sp != NUL)
4568 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4569 cmd = alloc(len);
4570 if (cmd == NULL)
4571 return NULL;
4572 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4573 (char *)p_shq);
4574
4575 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4576 if (*p_sp != NUL)
4577 append_redir(cmd, len, p_sp, fname);
4578
4579 // Display the fully formed command. Output a newline if there's something
4580 // else than the :make command that was typed (in which case the cursor is
4581 // in column 0).
4582 if (msg_col == 0)
4583 msg_didout = FALSE;
4584 msg_start();
4585 MSG_PUTS(":!");
4586 msg_outtrans(cmd); // show what we are doing
4587
4588 return cmd;
4589}
4590
4591/*
4592 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4593 */
4594 void
4595ex_make(exarg_T *eap)
4596{
4597 char_u *fname;
4598 char_u *cmd;
4599 char_u *enc = NULL;
4600 win_T *wp = NULL;
4601 qf_info_T *qi = &ql_info;
4602 int res;
4603 char_u *au_name = NULL;
4604 int_u save_qfid;
4605
4606 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4607 if (grep_internal(eap->cmdidx))
4608 {
4609 ex_vimgrep(eap);
4610 return;
4611 }
4612
4613 au_name = make_get_auname(eap->cmdidx);
4614 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4615 curbuf->b_fname, TRUE, curbuf))
4616 {
4617#ifdef FEAT_EVAL
4618 if (aborting())
4619 return;
4620#endif
4621 }
4622#ifdef FEAT_MBYTE
4623 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4624#endif
4625
4626 if (is_loclist_cmd(eap->cmdidx))
4627 wp = curwin;
4628
4629 autowrite_all();
4630 fname = get_mef_name();
4631 if (fname == NULL)
4632 return;
4633 mch_remove(fname); // in case it's not unique
4634
4635 cmd = make_get_fullcmd(eap->arg, fname);
4636 if (cmd == NULL)
4637 return;
4638
4639 // let the shell know if we are redirecting output or not
4640 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4641
4642#ifdef AMIGA
4643 out_flush();
4644 // read window status report and redraw before message
4645 (void)char_avail();
4646#endif
4647
4648 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4649 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4650 (eap->cmdidx != CMD_grepadd
4651 && eap->cmdidx != CMD_lgrepadd),
4652 qf_cmdtitle(*eap->cmdlinep), enc);
4653 if (wp != NULL)
4654 {
4655 qi = GET_LOC_LIST(wp);
4656 if (qi == NULL)
4657 goto cleanup;
4658 }
4659 if (res >= 0)
4660 qf_list_changed(qi, qi->qf_curlist);
4661
4662 // Remember the current quickfix list identifier, so that we can
4663 // check for autocommands changing the current quickfix list.
4664 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4665 if (au_name != NULL)
4666 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4667 curbuf->b_fname, TRUE, curbuf);
4668 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4669 // display the first error
4670 qf_jump_first(qi, save_qfid, FALSE);
4671
4672cleanup:
4673 mch_remove(fname);
4674 vim_free(fname);
4675 vim_free(cmd);
4676}
4677
4678/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004679 * Returns the number of valid entries in the current quickfix/location list.
4680 */
4681 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004682qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004683{
4684 qf_info_T *qi = &ql_info;
4685 qfline_T *qfp;
4686 int i, sz = 0;
4687 int prev_fnum = 0;
4688
Bram Moolenaar39665952018-08-15 20:59:48 +02004689 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004690 {
4691 /* Location list */
4692 qi = GET_LOC_LIST(curwin);
4693 if (qi == NULL)
4694 return 0;
4695 }
4696
4697 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004698 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004699 ++i, qfp = qfp->qf_next)
4700 {
4701 if (qfp->qf_valid)
4702 {
4703 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4704 sz++; /* Count all valid entries */
4705 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4706 {
4707 /* Count the number of files */
4708 sz++;
4709 prev_fnum = qfp->qf_fnum;
4710 }
4711 }
4712 }
4713
4714 return sz;
4715}
4716
4717/*
4718 * Returns the current index of the quickfix/location list.
4719 * Returns 0 if there is an error.
4720 */
4721 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004722qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004723{
4724 qf_info_T *qi = &ql_info;
4725
Bram Moolenaar39665952018-08-15 20:59:48 +02004726 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004727 {
4728 /* Location list */
4729 qi = GET_LOC_LIST(curwin);
4730 if (qi == NULL)
4731 return 0;
4732 }
4733
4734 return qi->qf_lists[qi->qf_curlist].qf_index;
4735}
4736
4737/*
4738 * Returns the current index in the quickfix/location list (counting only valid
4739 * entries). If no valid entries are in the list, then returns 1.
4740 */
4741 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004742qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004743{
4744 qf_info_T *qi = &ql_info;
4745 qf_list_T *qfl;
4746 qfline_T *qfp;
4747 int i, eidx = 0;
4748 int prev_fnum = 0;
4749
Bram Moolenaar39665952018-08-15 20:59:48 +02004750 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004751 {
4752 /* Location list */
4753 qi = GET_LOC_LIST(curwin);
4754 if (qi == NULL)
4755 return 1;
4756 }
4757
4758 qfl = &qi->qf_lists[qi->qf_curlist];
4759 qfp = qfl->qf_start;
4760
4761 /* check if the list has valid errors */
4762 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4763 return 1;
4764
4765 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4766 {
4767 if (qfp->qf_valid)
4768 {
4769 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4770 {
4771 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4772 {
4773 /* Count the number of files */
4774 eidx++;
4775 prev_fnum = qfp->qf_fnum;
4776 }
4777 }
4778 else
4779 eidx++;
4780 }
4781 }
4782
4783 return eidx ? eidx : 1;
4784}
4785
4786/*
4787 * Get the 'n'th valid error entry in the quickfix or location list.
4788 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4789 * For :cdo and :ldo returns the 'n'th valid error entry.
4790 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4791 */
4792 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004793qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004794{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004795 qfline_T *qfp = qfl->qf_start;
4796 int i, eidx;
4797 int prev_fnum = 0;
4798
4799 /* check if the list has valid errors */
4800 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4801 return 1;
4802
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004803 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004804 i++, qfp = qfp->qf_next)
4805 {
4806 if (qfp->qf_valid)
4807 {
4808 if (fdo)
4809 {
4810 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4811 {
4812 /* Count the number of files */
4813 eidx++;
4814 prev_fnum = qfp->qf_fnum;
4815 }
4816 }
4817 else
4818 eidx++;
4819 }
4820
4821 if (eidx == n)
4822 break;
4823 }
4824
4825 if (i <= qfl->qf_count)
4826 return i;
4827 else
4828 return 1;
4829}
4830
4831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004833 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004834 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 */
4836 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004837ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004839 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004840 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004841
Bram Moolenaar39665952018-08-15 20:59:48 +02004842 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004843 {
4844 qi = GET_LOC_LIST(curwin);
4845 if (qi == NULL)
4846 {
4847 EMSG(_(e_loclist));
4848 return;
4849 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004850 }
4851
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004852 if (eap->addr_count > 0)
4853 errornr = (int)eap->line2;
4854 else
4855 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004856 switch (eap->cmdidx)
4857 {
4858 case CMD_cc: case CMD_ll:
4859 errornr = 0;
4860 break;
4861 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4862 case CMD_lfirst:
4863 errornr = 1;
4864 break;
4865 default:
4866 errornr = 32767;
4867 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004868 }
4869
4870 /* For cdo and ldo commands, jump to the nth valid error.
4871 * For cfdo and lfdo commands, jump to the nth valid file entry.
4872 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004873 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4874 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004875 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004876 eap->addr_count > 0 ? (int)eap->line1 : 1,
4877 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4878
4879 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880}
4881
4882/*
4883 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004884 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004885 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 */
4887 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004888ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004890 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004891 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004892 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004893
Bram Moolenaar39665952018-08-15 20:59:48 +02004894 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004895 {
4896 qi = GET_LOC_LIST(curwin);
4897 if (qi == NULL)
4898 {
4899 EMSG(_(e_loclist));
4900 return;
4901 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004902 }
4903
Bram Moolenaar55b69262017-08-13 13:42:01 +02004904 if (eap->addr_count > 0
4905 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4906 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004907 errornr = (int)eap->line2;
4908 else
4909 errornr = 1;
4910
Bram Moolenaar39665952018-08-15 20:59:48 +02004911 // Depending on the command jump to either next or previous entry/file.
4912 switch (eap->cmdidx)
4913 {
4914 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4915 dir = FORWARD;
4916 break;
4917 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4918 case CMD_lNext:
4919 dir = BACKWARD;
4920 break;
4921 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4922 dir = FORWARD_FILE;
4923 break;
4924 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4925 dir = BACKWARD_FILE;
4926 break;
4927 default:
4928 dir = FORWARD;
4929 break;
4930 }
4931
4932 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933}
4934
4935/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004936 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004937 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 */
4939 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004940ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004942 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004943 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004944 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004945 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004946 int_u save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004947 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004948
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004949 switch (eap->cmdidx)
4950 {
4951 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4952 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4953 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4954 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4955 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4956 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4957 default: break;
4958 }
4959 if (au_name != NULL)
4960 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004961#ifdef FEAT_MBYTE
4962 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4963#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004964#ifdef FEAT_BROWSE
4965 if (cmdmod.browse)
4966 {
4967 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004968 NULL, NULL,
4969 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004970 if (browse_file == NULL)
4971 return;
4972 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4973 vim_free(browse_file);
4974 }
4975 else
4976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004978 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004979
Bram Moolenaar39665952018-08-15 20:59:48 +02004980 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004981 wp = curwin;
4982
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004983 /*
4984 * This function is used by the :cfile, :cgetfile and :caddfile
4985 * commands.
4986 * :cfile always creates a new quickfix list and jumps to the
4987 * first error.
4988 * :cgetfile creates a new quickfix list but doesn't jump to the
4989 * first error.
4990 * :caddfile adds to an existing quickfix list. If there is no
4991 * quickfix list then a new list is created.
4992 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004993 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004994 && eap->cmdidx != CMD_laddfile),
4995 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004996 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004997 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01004998 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004999 if (qi == NULL)
5000 return;
5001 }
5002 if (res >= 0)
Bram Moolenaarb254af32017-12-18 19:48:58 +01005003 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005004 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005005 if (au_name != NULL)
5006 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005007
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005008 // Jump to the first error for a new list and if autocmds didn't
5009 // free the list.
5010 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5011 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005012 // display the first error
5013 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005014}
5015
5016/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005017 * Return the vimgrep autocmd name.
5018 */
5019 static char_u *
5020vgr_get_auname(cmdidx_T cmdidx)
5021{
5022 switch (cmdidx)
5023 {
5024 case CMD_vimgrep: return (char_u *)"vimgrep";
5025 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5026 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5027 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5028 case CMD_grep: return (char_u *)"grep";
5029 case CMD_lgrep: return (char_u *)"lgrep";
5030 case CMD_grepadd: return (char_u *)"grepadd";
5031 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5032 default: return NULL;
5033 }
5034}
5035
5036/*
5037 * Initialize the regmatch used by vimgrep for pattern "s".
5038 */
5039 static void
5040vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5041{
5042 /* Get the search pattern: either white-separated or enclosed in // */
5043 regmatch->regprog = NULL;
5044
5045 if (s == NULL || *s == NUL)
5046 {
5047 /* Pattern is empty, use last search pattern. */
5048 if (last_search_pat() == NULL)
5049 {
5050 EMSG(_(e_noprevre));
5051 return;
5052 }
5053 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5054 }
5055 else
5056 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5057
5058 regmatch->rmm_ic = p_ic;
5059 regmatch->rmm_maxcol = 0;
5060}
5061
5062/*
5063 * Display a file name when vimgrep is running.
5064 */
5065 static void
5066vgr_display_fname(char_u *fname)
5067{
5068 char_u *p;
5069
5070 msg_start();
5071 p = msg_strtrunc(fname, TRUE);
5072 if (p == NULL)
5073 msg_outtrans(fname);
5074 else
5075 {
5076 msg_outtrans(p);
5077 vim_free(p);
5078 }
5079 msg_clr_eos();
5080 msg_didout = FALSE; /* overwrite this message */
5081 msg_nowait = TRUE; /* don't wait for this message */
5082 msg_col = 0;
5083 out_flush();
5084}
5085
5086/*
5087 * Load a dummy buffer to search for a pattern using vimgrep.
5088 */
5089 static buf_T *
5090vgr_load_dummy_buf(
5091 char_u *fname,
5092 char_u *dirname_start,
5093 char_u *dirname_now)
5094{
5095 int save_mls;
5096#if defined(FEAT_SYN_HL)
5097 char_u *save_ei = NULL;
5098#endif
5099 buf_T *buf;
5100
5101#if defined(FEAT_SYN_HL)
5102 /* Don't do Filetype autocommands to avoid loading syntax and
5103 * indent scripts, a great speed improvement. */
5104 save_ei = au_event_disable(",Filetype");
5105#endif
5106 /* Don't use modelines here, it's useless. */
5107 save_mls = p_mls;
5108 p_mls = 0;
5109
5110 /* Load file into a buffer, so that 'fileencoding' is detected,
5111 * autocommands applied, etc. */
5112 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5113
5114 p_mls = save_mls;
5115#if defined(FEAT_SYN_HL)
5116 au_event_restore(save_ei);
5117#endif
5118
5119 return buf;
5120}
5121
5122/*
5123 * Check whether a quickfix/location list valid. Autocmds may remove or change
5124 * a quickfix list when vimgrep is running. If the list is not found, create a
5125 * new list.
5126 */
5127 static int
5128vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005129 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005130 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005131 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005132 char_u *title)
5133{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005134 /* Verify that the quickfix/location list was not freed by an autocmd */
5135 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005136 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005137 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005138 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005139 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005140 EMSG(_(e_loc_list_changed));
5141 return FALSE;
5142 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005143 else
5144 {
5145 /* Quickfix list is not found, create a new one. */
5146 qf_new_list(qi, title);
5147 return TRUE;
5148 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005149 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005150
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005151 if (qf_restore_list(qi, qfid) == FAIL)
5152 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005153
5154 return TRUE;
5155}
5156
5157/*
5158 * Search for a pattern in all the lines in a buffer and add the matching lines
5159 * to a quickfix list.
5160 */
5161 static int
5162vgr_match_buflines(
5163 qf_info_T *qi,
5164 char_u *fname,
5165 buf_T *buf,
5166 regmmatch_T *regmatch,
5167 long tomatch,
5168 int duplicate_name,
5169 int flags)
5170{
5171 int found_match = FALSE;
5172 long lnum;
5173 colnr_T col;
5174
5175 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5176 {
5177 col = 0;
5178 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5179 col, NULL, NULL) > 0)
5180 {
5181 /* Pass the buffer number so that it gets used even for a
5182 * dummy buffer, unless duplicate_name is set, then the
5183 * buffer will be wiped out below. */
5184 if (qf_add_entry(qi,
5185 qi->qf_curlist,
5186 NULL, /* dir */
5187 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005188 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005189 duplicate_name ? 0 : buf->b_fnum,
5190 ml_get_buf(buf,
5191 regmatch->startpos[0].lnum + lnum, FALSE),
5192 regmatch->startpos[0].lnum + lnum,
5193 regmatch->startpos[0].col + 1,
5194 FALSE, /* vis_col */
5195 NULL, /* search pattern */
5196 0, /* nr */
5197 0, /* type */
5198 TRUE /* valid */
5199 ) == FAIL)
5200 {
5201 got_int = TRUE;
5202 break;
5203 }
5204 found_match = TRUE;
5205 if (--tomatch == 0)
5206 break;
5207 if ((flags & VGR_GLOBAL) == 0
5208 || regmatch->endpos[0].lnum > 0)
5209 break;
5210 col = regmatch->endpos[0].col
5211 + (col == regmatch->endpos[0].col);
5212 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5213 break;
5214 }
5215 line_breakcheck();
5216 if (got_int)
5217 break;
5218 }
5219
5220 return found_match;
5221}
5222
5223/*
5224 * Jump to the first match and update the directory.
5225 */
5226 static void
5227vgr_jump_to_match(
5228 qf_info_T *qi,
5229 int forceit,
5230 int *redraw_for_dummy,
5231 buf_T *first_match_buf,
5232 char_u *target_dir)
5233{
5234 buf_T *buf;
5235
5236 buf = curbuf;
5237 qf_jump(qi, 0, 0, forceit);
5238 if (buf != curbuf)
5239 /* If we jumped to another buffer redrawing will already be
5240 * taken care of. */
5241 *redraw_for_dummy = FALSE;
5242
5243 /* Jump to the directory used after loading the buffer. */
5244 if (curbuf == first_match_buf && target_dir != NULL)
5245 {
5246 exarg_T ea;
5247
5248 ea.arg = target_dir;
5249 ea.cmdidx = CMD_lcd;
5250 ex_cd(&ea);
5251 }
5252}
5253
5254/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005255 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005256 * ":vimgrepadd {pattern} file(s)"
5257 * ":lvimgrep {pattern} file(s)"
5258 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005259 */
5260 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005261ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005262{
Bram Moolenaar81695252004-12-29 20:58:21 +00005263 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005264 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005265 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005266 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005267 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005268 char_u *s;
5269 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005270 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005271 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005272 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005273 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005274 buf_T *buf;
5275 int duplicate_name = FALSE;
5276 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005277 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005278 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005279 buf_T *first_match_buf = NULL;
5280 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005281 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005282 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005283 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005284 char_u *dirname_start = NULL;
5285 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005286 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005287 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005288
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005289 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005290 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5291 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005292 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005293#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005294 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005295 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005296#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005297 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005298
Bram Moolenaar39665952018-08-15 20:59:48 +02005299 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005300 {
5301 qi = ll_get_or_alloc_list(curwin);
5302 if (qi == NULL)
5303 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005304 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005305 }
5306
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005307 if (eap->addr_count > 0)
5308 tomatch = eap->line2;
5309 else
5310 tomatch = MAXLNUM;
5311
Bram Moolenaar81695252004-12-29 20:58:21 +00005312 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005313 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005314 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005315 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005316 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005317 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005318 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005319 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005320 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005321
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005322 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005323 if (regmatch.regprog == NULL)
5324 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005325
5326 p = skipwhite(p);
5327 if (*p == NUL)
5328 {
5329 EMSG(_("E683: File name missing or invalid pattern"));
5330 goto theend;
5331 }
5332
Bram Moolenaar55b69262017-08-13 13:42:01 +02005333 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005334 && eap->cmdidx != CMD_vimgrepadd
5335 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005336 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005337 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005338 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005339
5340 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005341 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005342 goto theend;
5343 if (fcount == 0)
5344 {
5345 EMSG(_(e_nomatch));
5346 goto theend;
5347 }
5348
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005349 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5350 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005351 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005352 {
5353 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005354 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005355 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005356
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005357 /* Remember the current directory, because a BufRead autocommand that does
5358 * ":lcd %:p:h" changes the meaning of short path names. */
5359 mch_dirname(dirname_start, MAXPATHL);
5360
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005361 /* Remember the current quickfix list identifier, so that we can check for
5362 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005363 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005364
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005365 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005366 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005367 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005368 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005369 if (time(NULL) > seconds)
5370 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005371 /* Display the file name every second or so, show the user we are
5372 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005373 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005374 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005375 }
5376
Bram Moolenaar81695252004-12-29 20:58:21 +00005377 buf = buflist_findname_exp(fnames[fi]);
5378 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5379 {
5380 /* Remember that a buffer with this name already exists. */
5381 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005382 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005383 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005384
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005385 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005386 }
5387 else
5388 /* Use existing, loaded buffer. */
5389 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005390
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005391 /* Check whether the quickfix list is still valid. When loading a
5392 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005393 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005394 {
5395 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005396 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005397 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005398 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005399
Bram Moolenaar81695252004-12-29 20:58:21 +00005400 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005401 {
5402 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005403 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005404 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005405 else
5406 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005407 /* Try for a match in all lines of the buffer.
5408 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005409 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5410 tomatch, duplicate_name, flags);
5411
Bram Moolenaar81695252004-12-29 20:58:21 +00005412 if (using_dummy)
5413 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005414 if (found_match && first_match_buf == NULL)
5415 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005416 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005417 {
Bram Moolenaar81695252004-12-29 20:58:21 +00005418 /* Never keep a dummy buffer if there is another buffer
5419 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005420 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005421 buf = NULL;
5422 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005423 else if (!cmdmod.hide
5424 || buf->b_p_bh[0] == 'u' /* "unload" */
5425 || buf->b_p_bh[0] == 'w' /* "wipe" */
5426 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00005427 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005428 /* When no match was found we don't need to remember the
5429 * buffer, wipe it out. If there was a match and it
5430 * wasn't the first one or we won't jump there: only
5431 * unload the buffer.
5432 * Ignore 'hidden' here, because it may lead to having too
5433 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00005434 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005435 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005436 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005437 buf = NULL;
5438 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005439 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005440 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005441 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02005442 /* Keeping the buffer, remove the dummy flag. */
5443 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005444 buf = NULL;
5445 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005446 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005447
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005448 if (buf != NULL)
5449 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02005450 /* Keeping the buffer, remove the dummy flag. */
5451 buf->b_flags &= ~BF_DUMMY;
5452
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005453 /* If the buffer is still loaded we need to use the
5454 * directory we jumped to below. */
5455 if (buf == first_match_buf
5456 && target_dir == NULL
5457 && STRCMP(dirname_start, dirname_now) != 0)
5458 target_dir = vim_strsave(dirname_now);
5459
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005460 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005461 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00005462 * need to be done (again). But not the window-local
5463 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005464 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005465#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005466 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5467 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005468#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005469 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005470 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005471 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005472 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005473 }
5474 }
5475
5476 FreeWild(fcount, fnames);
5477
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005478 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5479 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5480 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005481 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005482
Bram Moolenaar864293a2016-06-02 13:40:04 +02005483 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005484
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005485 if (au_name != NULL)
5486 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5487 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005488 /*
5489 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5490 * is still valid.
5491 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005492 if (!qflist_valid(wp, save_qfid))
5493 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005494
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005495 if (qf_restore_list(qi, save_qfid) == FAIL)
5496 goto theend;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005497
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005498 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005499 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005500 {
5501 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005502 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5503 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005504 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005505 else
5506 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005507
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005508 /* If we loaded a dummy buffer into the current window, the autocommands
5509 * may have messed up things, need to redraw and recompute folds. */
5510 if (redraw_for_dummy)
5511 {
5512#ifdef FEAT_FOLDING
5513 foldUpdateAll(curwin);
5514#else
5515 redraw_later(NOT_VALID);
5516#endif
5517 }
5518
Bram Moolenaar86b68352004-12-27 21:59:20 +00005519theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005520 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005521 vim_free(dirname_now);
5522 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005523 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005524 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005525}
5526
5527/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005528 * Restore current working directory to "dirname_start" if they differ, taking
5529 * into account whether it is set locally or globally.
5530 */
5531 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005532restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005533{
5534 char_u *dirname_now = alloc(MAXPATHL);
5535
5536 if (NULL != dirname_now)
5537 {
5538 mch_dirname(dirname_now, MAXPATHL);
5539 if (STRCMP(dirname_start, dirname_now) != 0)
5540 {
5541 /* If the directory has changed, change it back by building up an
5542 * appropriate ex command and executing it. */
5543 exarg_T ea;
5544
5545 ea.arg = dirname_start;
5546 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5547 ex_cd(&ea);
5548 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005549 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005550 }
5551}
5552
5553/*
5554 * Load file "fname" into a dummy buffer and return the buffer pointer,
5555 * placing the directory resulting from the buffer load into the
5556 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5557 * prior to calling this function. Restores directory to "dirname_start" prior
5558 * to returning, if autocmds or the 'autochdir' option have changed it.
5559 *
5560 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5561 * or wipe_dummy_buffer() later!
5562 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005563 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005564 */
5565 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005566load_dummy_buffer(
5567 char_u *fname,
5568 char_u *dirname_start, /* in: old directory */
5569 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005570{
5571 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005572 bufref_T newbufref;
5573 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005574 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005575 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005576 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005577
5578 /* Allocate a buffer without putting it in the buffer list. */
5579 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5580 if (newbuf == NULL)
5581 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005582 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005583
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005584 /* Init the options. */
5585 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5586
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005587 /* need to open the memfile before putting the buffer in a window */
5588 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005589 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005590 /* Make sure this buffer isn't wiped out by autocommands. */
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005591 ++newbuf->b_locked;
5592
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005593 /* set curwin/curbuf to buf and save a few things */
5594 aucmd_prepbuf(&aco, newbuf);
5595
5596 /* Need to set the filename for autocommands. */
5597 (void)setfname(curbuf, fname, NULL, FALSE);
5598
Bram Moolenaar81695252004-12-29 20:58:21 +00005599 /* Create swap file now to avoid the ATTENTION message. */
5600 check_need_swap(TRUE);
5601
5602 /* Remove the "dummy" flag, otherwise autocommands may not
5603 * work. */
5604 curbuf->b_flags &= ~BF_DUMMY;
5605
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005606 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005607 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005608 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005609 NULL, READ_NEW | READ_DUMMY);
5610 --newbuf->b_locked;
5611 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005612 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005613 && !(curbuf->b_flags & BF_NEW))
5614 {
5615 failed = FALSE;
5616 if (curbuf != newbuf)
5617 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005618 /* Bloody autocommands changed the buffer! Can happen when
5619 * using netrw and editing a remote file. Use the current
5620 * buffer instead, delete the dummy one after restoring the
5621 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005622 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005623 newbuf = curbuf;
5624 }
5625 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005626
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005627 /* restore curwin/curbuf and a few other things */
5628 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005629 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5630 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005631
5632 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5633 * skip it. */
5634 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005635 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005636
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005637 /*
5638 * When autocommands/'autochdir' option changed directory: go back.
5639 * Let the caller know what the resulting dir was first, in case it is
5640 * important.
5641 */
5642 mch_dirname(resulting_dir, MAXPATHL);
5643 restore_start_dir(dirname_start);
5644
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005645 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005646 return NULL;
5647 if (failed)
5648 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005649 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005650 return NULL;
5651 }
5652 return newbuf;
5653}
5654
5655/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005656 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5657 * directory to "dirname_start" prior to returning, if autocmds or the
5658 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005659 */
5660 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005661wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005662{
5663 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005664 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005665#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005666 cleanup_T cs;
5667
5668 /* Reset the error/interrupt/exception state here so that aborting()
5669 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5670 * work when got_int is set. */
5671 enter_cleanup(&cs);
5672#endif
5673
Bram Moolenaar81695252004-12-29 20:58:21 +00005674 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005675
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005676#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005677 /* Restore the error/interrupt/exception state if not discarded by a
5678 * new aborting error, interrupt, or uncaught exception. */
5679 leave_cleanup(&cs);
5680#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005681 /* When autocommands/'autochdir' option changed directory: go back. */
5682 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005683 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005684}
5685
5686/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005687 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5688 * directory to "dirname_start" prior to returning, if autocmds or the
5689 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005690 */
5691 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005692unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005693{
5694 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005695 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005696 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005697
5698 /* When autocommands/'autochdir' option changed directory: go back. */
5699 restore_start_dir(dirname_start);
5700 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005701}
5702
Bram Moolenaar05159a02005-02-26 23:04:13 +00005703#if defined(FEAT_EVAL) || defined(PROTO)
5704/*
5705 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005706 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005707 */
5708 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005709get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005710{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005711 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005712 dict_T *dict;
5713 char_u buf[2];
5714 qfline_T *qfp;
5715 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005716 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005717
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005718 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005719 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005720 qi = &ql_info;
5721 if (wp != NULL)
5722 {
5723 qi = GET_LOC_LIST(wp);
5724 if (qi == NULL)
5725 return FAIL;
5726 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005727 }
5728
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005729 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005730 qf_idx = qi->qf_curlist;
5731
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005732 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005733 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005734
Bram Moolenaard823fa92016-08-12 16:29:27 +02005735 qfp = qi->qf_lists[qf_idx].qf_start;
5736 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005737 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005738 /* Handle entries with a non-existing buffer number. */
5739 bufnum = qfp->qf_fnum;
5740 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5741 bufnum = 0;
5742
Bram Moolenaar05159a02005-02-26 23:04:13 +00005743 if ((dict = dict_alloc()) == NULL)
5744 return FAIL;
5745 if (list_append_dict(list, dict) == FAIL)
5746 return FAIL;
5747
5748 buf[0] = qfp->qf_type;
5749 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005750 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5751 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5752 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5753 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5754 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5755 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5756 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5757 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5758 || dict_add_string(dict, "type", buf) == FAIL
5759 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005760 return FAIL;
5761
5762 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005763 if (qfp == NULL)
5764 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005765 }
5766 return OK;
5767}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005768
5769/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005770 * Flags used by getqflist()/getloclist() to determine which fields to return.
5771 */
5772enum {
5773 QF_GETLIST_NONE = 0x0,
5774 QF_GETLIST_TITLE = 0x1,
5775 QF_GETLIST_ITEMS = 0x2,
5776 QF_GETLIST_NR = 0x4,
5777 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005778 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005779 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005780 QF_GETLIST_IDX = 0x40,
5781 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005782 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005783 QF_GETLIST_FILEWINID = 0x200,
5784 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005785};
5786
5787/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005788 * Parse text from 'di' and return the quickfix list items.
5789 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005790 */
5791 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005792qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005793{
5794 int status = FAIL;
5795 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005796 char_u *errorformat = p_efm;
5797 dictitem_T *efm_di;
5798 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005799
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005800 /* Only a List value is supported */
5801 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005802 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005803 /* If errorformat is supplied then use it, otherwise use the 'efm'
5804 * option setting
5805 */
5806 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5807 {
5808 if (efm_di->di_tv.v_type != VAR_STRING ||
5809 efm_di->di_tv.vval.v_string == NULL)
5810 return FAIL;
5811 errorformat = efm_di->di_tv.vval.v_string;
5812 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005813
Bram Moolenaar36538222017-09-02 19:51:44 +02005814 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005815 if (l == NULL)
5816 return FAIL;
5817
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005818 qi = ll_new_list();
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005819 if (qi != NULL)
5820 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005821 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005822 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5823 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005824 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005825 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005826 }
5827 free(qi);
5828 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005829 dict_add_list(retdict, "items", l);
5830 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005831 }
5832
5833 return status;
5834}
5835
5836/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005837 * Return the quickfix/location list window identifier in the current tabpage.
5838 */
5839 static int
5840qf_winid(qf_info_T *qi)
5841{
5842 win_T *win;
5843
5844 /* The quickfix window can be opened even if the quickfix list is not set
5845 * using ":copen". This is not true for location lists. */
5846 if (qi == NULL)
5847 return 0;
5848 win = qf_find_win(qi);
5849 if (win != NULL)
5850 return win->w_id;
5851 return 0;
5852}
5853
5854/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005855 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005856 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005857 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005858qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005859{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005860 int flags = QF_GETLIST_NONE;
5861
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005862 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005863 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005864 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005865 if (!loclist)
5866 // File window ID is applicable only to location list windows
5867 flags &= ~ QF_GETLIST_FILEWINID;
5868 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005869
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005870 if (dict_find(what, (char_u *)"title", -1) != NULL)
5871 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005872
Bram Moolenaara6d48492017-12-12 22:45:31 +01005873 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5874 flags |= QF_GETLIST_NR;
5875
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005876 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5877 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005878
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005879 if (dict_find(what, (char_u *)"context", -1) != NULL)
5880 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005881
Bram Moolenaara6d48492017-12-12 22:45:31 +01005882 if (dict_find(what, (char_u *)"id", -1) != NULL)
5883 flags |= QF_GETLIST_ID;
5884
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005885 if (dict_find(what, (char_u *)"items", -1) != NULL)
5886 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005887
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005888 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5889 flags |= QF_GETLIST_IDX;
5890
5891 if (dict_find(what, (char_u *)"size", -1) != NULL)
5892 flags |= QF_GETLIST_SIZE;
5893
Bram Moolenaarb254af32017-12-18 19:48:58 +01005894 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5895 flags |= QF_GETLIST_TICK;
5896
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005897 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5898 flags |= QF_GETLIST_FILEWINID;
5899
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005900 return flags;
5901}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005902
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005903/*
5904 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5905 * If 'nr' and 'id' are not present in 'what' then return the current
5906 * quickfix list index.
5907 * If 'nr' is zero then return the current quickfix list index.
5908 * If 'nr' is '$' then return the last quickfix list index.
5909 * If 'id' is present then return the index of the quickfix list with that id.
5910 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5911 * Return -1, if quickfix list is not present or if the stack is empty.
5912 */
5913 static int
5914qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5915{
5916 int qf_idx;
5917 dictitem_T *di;
5918
5919 qf_idx = qi->qf_curlist; /* default is the current list */
5920 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5921 {
5922 /* Use the specified quickfix/location list */
5923 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005924 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005925 /* for zero use the current list */
5926 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005927 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005928 qf_idx = di->di_tv.vval.v_number - 1;
5929 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005930 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005931 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005932 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005933 else if (di->di_tv.v_type == VAR_STRING
5934 && di->di_tv.vval.v_string != NULL
5935 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5936 /* Get the last quickfix list number */
5937 qf_idx = qi->qf_listcount - 1;
5938 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005939 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005940 }
5941
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005942 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5943 {
5944 /* Look for a list with the specified id */
5945 if (di->di_tv.v_type == VAR_NUMBER)
5946 {
5947 /*
5948 * For zero, use the current list or the list specified by 'nr'
5949 */
5950 if (di->di_tv.vval.v_number != 0)
5951 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5952 }
5953 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005954 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005955 }
5956
5957 return qf_idx;
5958}
5959
5960/*
5961 * Return default values for quickfix list properties in retdict.
5962 */
5963 static int
5964qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5965{
5966 int status = OK;
5967
5968 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005969 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005970 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5971 {
5972 list_T *l = list_alloc();
5973 if (l != NULL)
5974 status = dict_add_list(retdict, "items", l);
5975 else
5976 status = FAIL;
5977 }
5978 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005979 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005980 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005981 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005982 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005983 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005984 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005985 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005986 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005987 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005988 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005989 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005990 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005991 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005992 if ((status == OK) && (qi != &ql_info) && (flags & QF_GETLIST_FILEWINID))
5993 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005994
5995 return status;
5996}
5997
5998/*
5999 * Return the quickfix list title as 'title' in retdict
6000 */
6001 static int
6002qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
6003{
Bram Moolenaare0be1672018-07-08 16:50:37 +02006004 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006005}
6006
6007/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006008 * Returns the identifier of the window used to display files from a location
6009 * list. If there is no associated window, then returns 0. Useful only when
6010 * called from a location list window.
6011 */
6012 static int
6013qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6014{
6015 int winid = 0;
6016
6017 if (wp != NULL && IS_LL_WINDOW(wp))
6018 {
6019 win_T *ll_wp = qf_find_win_with_loclist(qi);
6020 if (ll_wp != NULL)
6021 winid = ll_wp->w_id;
6022 }
6023
6024 return dict_add_number(retdict, "filewinid", winid);
6025}
6026
6027/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006028 * Return the quickfix list items/entries as 'items' in retdict
6029 */
6030 static int
6031qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6032{
6033 int status = OK;
6034 list_T *l = list_alloc();
6035 if (l != NULL)
6036 {
6037 (void)get_errorlist(qi, NULL, qf_idx, l);
6038 dict_add_list(retdict, "items", l);
6039 }
6040 else
6041 status = FAIL;
6042
6043 return status;
6044}
6045
6046/*
6047 * Return the quickfix list context (if any) as 'context' in retdict.
6048 */
6049 static int
6050qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6051{
6052 int status;
6053 dictitem_T *di;
6054
6055 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
6056 {
6057 di = dictitem_alloc((char_u *)"context");
6058 if (di != NULL)
6059 {
6060 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
6061 status = dict_add(retdict, di);
6062 if (status == FAIL)
6063 dictitem_free(di);
6064 }
6065 else
6066 status = FAIL;
6067 }
6068 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006069 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006070
6071 return status;
6072}
6073
6074/*
6075 * Return the quickfix list index as 'idx' in retdict
6076 */
6077 static int
6078qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6079{
6080 int idx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006081 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006082 /* For empty lists, qf_index is set to 1 */
6083 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02006084 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006085}
6086
6087/*
6088 * Return quickfix/location list details (title) as a
6089 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6090 * then current list is used. Otherwise the specified list is used.
6091 */
6092 int
6093qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6094{
6095 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006096 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006097 int status = OK;
6098 int qf_idx;
6099 dictitem_T *di;
6100 int flags = QF_GETLIST_NONE;
6101
6102 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6103 return qf_get_list_from_lines(what, di, retdict);
6104
6105 if (wp != NULL)
6106 qi = GET_LOC_LIST(wp);
6107
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006108 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006109
6110 if (qi != NULL && qi->qf_listcount != 0)
6111 qf_idx = qf_getprop_qfidx(qi, what);
6112
Bram Moolenaara6d48492017-12-12 22:45:31 +01006113 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006114 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006115 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006116
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006117 qfl = &qi->qf_lists[qf_idx];
6118
Bram Moolenaard823fa92016-08-12 16:29:27 +02006119 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006120 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006121 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006122 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006123 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006124 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006125 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006126 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006127 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006128 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006129 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006130 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006131 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006132 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006133 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006134 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006135 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006136 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006137 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6138 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006139
Bram Moolenaard823fa92016-08-12 16:29:27 +02006140 return status;
6141}
6142
6143/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006144 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
6145 * items in the dict 'd'.
6146 */
6147 static int
6148qf_add_entry_from_dict(
6149 qf_info_T *qi,
6150 int qf_idx,
6151 dict_T *d,
6152 int first_entry)
6153{
6154 static int did_bufnr_emsg;
6155 char_u *filename, *module, *pattern, *text, *type;
6156 int bufnum, valid, status, col, vcol, nr;
6157 long lnum;
6158
6159 if (first_entry)
6160 did_bufnr_emsg = FALSE;
6161
6162 filename = get_dict_string(d, (char_u *)"filename", TRUE);
6163 module = get_dict_string(d, (char_u *)"module", TRUE);
6164 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6165 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6166 col = (int)get_dict_number(d, (char_u *)"col");
6167 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6168 nr = (int)get_dict_number(d, (char_u *)"nr");
6169 type = get_dict_string(d, (char_u *)"type", TRUE);
6170 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6171 text = get_dict_string(d, (char_u *)"text", TRUE);
6172 if (text == NULL)
6173 text = vim_strsave((char_u *)"");
6174
6175 valid = TRUE;
6176 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6177 valid = FALSE;
6178
6179 // Mark entries with non-existing buffer number as not valid. Give the
6180 // error message only once.
6181 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6182 {
6183 if (!did_bufnr_emsg)
6184 {
6185 did_bufnr_emsg = TRUE;
6186 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6187 }
6188 valid = FALSE;
6189 bufnum = 0;
6190 }
6191
6192 // If the 'valid' field is present it overrules the detected value.
6193 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6194 valid = (int)get_dict_number(d, (char_u *)"valid");
6195
6196 status = qf_add_entry(qi,
6197 qf_idx,
6198 NULL, // dir
6199 filename,
6200 module,
6201 bufnum,
6202 text,
6203 lnum,
6204 col,
6205 vcol, // vis_col
6206 pattern, // search pattern
6207 nr,
6208 type == NULL ? NUL : *type,
6209 valid);
6210
6211 vim_free(filename);
6212 vim_free(module);
6213 vim_free(pattern);
6214 vim_free(text);
6215 vim_free(type);
6216
6217 return status;
6218}
6219
6220/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006221 * Add list of entries to quickfix/location list. Each list entry is
6222 * a dictionary with item information.
6223 */
6224 static int
6225qf_add_entries(
6226 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006227 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006228 list_T *list,
6229 char_u *title,
6230 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006231{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006232 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006233 listitem_T *li;
6234 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006235 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006236 int retval = OK;
6237
Bram Moolenaara3921f42017-06-04 15:30:34 +02006238 if (action == ' ' || qf_idx == qi->qf_listcount)
6239 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006240 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006241 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006242 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006243 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006244 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006245 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006246 /* Adding to existing list, use last entry. */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006247 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006248 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006249 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006250 qf_free_items(qfl);
6251 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006252 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006253
6254 for (li = list->lv_first; li != NULL; li = li->li_next)
6255 {
6256 if (li->li_tv.v_type != VAR_DICT)
6257 continue; /* Skip non-dict items */
6258
6259 d = li->li_tv.vval.v_dict;
6260 if (d == NULL)
6261 continue;
6262
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006263 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first);
6264 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006265 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006266 }
6267
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006268 if (qfl->qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02006269 /* no valid entry */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006270 qfl->qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006271 else
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006272 qfl->qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006273 if (action != 'a')
6274 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006275 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006276 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006277 qfl->qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006278 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006279
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006280 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02006281 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006282
6283 return retval;
6284}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006285
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006286/*
6287 * Get the quickfix list index from 'nr' or 'id'
6288 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006289 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006290qf_setprop_get_qfidx(
6291 qf_info_T *qi,
6292 dict_T *what,
6293 int action,
6294 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006295{
6296 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006297 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006298
Bram Moolenaard823fa92016-08-12 16:29:27 +02006299 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6300 {
6301 /* Use the specified quickfix/location list */
6302 if (di->di_tv.v_type == VAR_NUMBER)
6303 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006304 /* for zero use the current list */
6305 if (di->di_tv.vval.v_number != 0)
6306 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006307
Bram Moolenaar55b69262017-08-13 13:42:01 +02006308 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6309 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006310 /*
6311 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02006312 * non-available list and add the new list at the end of the
6313 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006314 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006315 *newlist = TRUE;
6316 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006317 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006318 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006319 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006320 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006321 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02006322 }
6323 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006324 && di->di_tv.vval.v_string != NULL
6325 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006326 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006327 if (qi->qf_listcount > 0)
6328 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006329 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006330 qf_idx = 0;
6331 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006332 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006333 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006334 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006335 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006336 }
6337
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006338 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006339 {
6340 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006341 if (di->di_tv.v_type != VAR_NUMBER)
6342 return INVALID_QFIDX;
6343
6344 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006345 }
6346
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006347 return qf_idx;
6348}
6349
6350/*
6351 * Set the quickfix list title.
6352 */
6353 static int
6354qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6355{
6356 if (di->di_tv.v_type != VAR_STRING)
6357 return FAIL;
6358
6359 vim_free(qi->qf_lists[qf_idx].qf_title);
6360 qi->qf_lists[qf_idx].qf_title =
6361 get_dict_string(what, (char_u *)"title", TRUE);
6362 if (qf_idx == qi->qf_curlist)
6363 qf_update_win_titlevar(qi);
6364
6365 return OK;
6366}
6367
6368/*
6369 * Set quickfix list items/entries.
6370 */
6371 static int
6372qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6373{
6374 int retval = FAIL;
6375 char_u *title_save;
6376
6377 if (di->di_tv.v_type != VAR_LIST)
6378 return FAIL;
6379
6380 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6381 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6382 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006383 vim_free(title_save);
6384
6385 return retval;
6386}
6387
6388/*
6389 * Set quickfix list items/entries from a list of lines.
6390 */
6391 static int
6392qf_setprop_items_from_lines(
6393 qf_info_T *qi,
6394 int qf_idx,
6395 dict_T *what,
6396 dictitem_T *di,
6397 int action)
6398{
6399 char_u *errorformat = p_efm;
6400 dictitem_T *efm_di;
6401 int retval = FAIL;
6402
6403 /* Use the user supplied errorformat settings (if present) */
6404 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6405 {
6406 if (efm_di->di_tv.v_type != VAR_STRING ||
6407 efm_di->di_tv.vval.v_string == NULL)
6408 return FAIL;
6409 errorformat = efm_di->di_tv.vval.v_string;
6410 }
6411
6412 /* Only a List value is supported */
6413 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6414 return FAIL;
6415
6416 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006417 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006418 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6419 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6420 retval = OK;
6421
6422 return retval;
6423}
6424
6425/*
6426 * Set quickfix list context.
6427 */
6428 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006429qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006430{
6431 typval_T *ctx;
6432
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006433 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006434 ctx = alloc_tv();
6435 if (ctx != NULL)
6436 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006437 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006438
6439 return OK;
6440}
6441
6442/*
6443 * Set quickfix/location list properties (title, items, context).
6444 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006445 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006446 */
6447 static int
6448qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6449{
6450 dictitem_T *di;
6451 int retval = FAIL;
6452 int qf_idx;
6453 int newlist = FALSE;
6454
6455 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
6456 newlist = TRUE;
6457
6458 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6459 if (qf_idx == INVALID_QFIDX) /* List not found */
6460 return FAIL;
6461
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006462 if (newlist)
6463 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006464 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006465 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006466 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006467 }
6468
6469 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006470 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006471 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006472 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006473 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006474 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006475 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006476 retval = qf_setprop_context(&qi->qf_lists[qf_idx], di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006477
Bram Moolenaarb254af32017-12-18 19:48:58 +01006478 if (retval == OK)
6479 qf_list_changed(qi, qf_idx);
6480
Bram Moolenaard823fa92016-08-12 16:29:27 +02006481 return retval;
6482}
6483
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006484/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006485 * Find the non-location list window with the specified location list in the
6486 * current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006487 */
6488 static win_T *
6489find_win_with_ll(qf_info_T *qi)
6490{
6491 win_T *wp = NULL;
6492
6493 FOR_ALL_WINDOWS(wp)
6494 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6495 return wp;
6496
6497 return NULL;
6498}
6499
6500/*
6501 * Free the entire quickfix/location list stack.
6502 * If the quickfix/location list window is open, then clear it.
6503 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006504 static void
6505qf_free_stack(win_T *wp, qf_info_T *qi)
6506{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006507 win_T *qfwin = qf_find_win(qi);
6508 win_T *llwin = NULL;
6509 win_T *orig_wp = wp;
6510
6511 if (qfwin != NULL)
6512 {
6513 /* If the quickfix/location list window is open, then clear it */
6514 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006515 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006516 qf_update_buffer(qi, NULL);
6517 }
6518
6519 if (wp != NULL && IS_LL_WINDOW(wp))
6520 {
6521 /* If in the location list window, then use the non-location list
6522 * window with this location list (if present)
6523 */
6524 llwin = find_win_with_ll(qi);
6525 if (llwin != NULL)
6526 wp = llwin;
6527 }
6528
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006529 qf_free_all(wp);
6530 if (wp == NULL)
6531 {
6532 /* quickfix list */
6533 qi->qf_curlist = 0;
6534 qi->qf_listcount = 0;
6535 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006536 else if (IS_LL_WINDOW(orig_wp))
6537 {
6538 /* If the location list window is open, then create a new empty
6539 * location list */
6540 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006541
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006542 /* first free the list reference in the location list window */
6543 ll_free_all(&orig_wp->w_llist_ref);
6544
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006545 orig_wp->w_llist_ref = new_ll;
6546 if (llwin != NULL)
6547 {
6548 llwin->w_llist = new_ll;
6549 new_ll->qf_refcount++;
6550 }
6551 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006552}
6553
Bram Moolenaard823fa92016-08-12 16:29:27 +02006554/*
6555 * Populate the quickfix list with the items supplied in the list
6556 * of dictionaries. "title" will be copied to w:quickfix_title.
6557 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6558 */
6559 int
6560set_errorlist(
6561 win_T *wp,
6562 list_T *list,
6563 int action,
6564 char_u *title,
6565 dict_T *what)
6566{
6567 qf_info_T *qi = &ql_info;
6568 int retval = OK;
6569
6570 if (wp != NULL)
6571 {
6572 qi = ll_get_or_alloc_list(wp);
6573 if (qi == NULL)
6574 return FAIL;
6575 }
6576
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006577 if (action == 'f')
6578 {
6579 /* Free the entire quickfix or location list stack */
6580 qf_free_stack(wp, qi);
6581 }
6582 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006583 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006584 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006585 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006586 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006587 if (retval == OK)
6588 qf_list_changed(qi, qi->qf_curlist);
6589 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006590
6591 return retval;
6592}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006593
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006594/*
6595 * Mark the context as in use for all the lists in a quickfix stack.
6596 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006597 static int
6598mark_quickfix_ctx(qf_info_T *qi, int copyID)
6599{
6600 int i;
6601 int abort = FALSE;
6602 typval_T *ctx;
6603
6604 for (i = 0; i < LISTCOUNT && !abort; ++i)
6605 {
6606 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006607 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6608 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006609 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6610 }
6611
6612 return abort;
6613}
6614
6615/*
6616 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006617 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006618 */
6619 int
6620set_ref_in_quickfix(int copyID)
6621{
6622 int abort = FALSE;
6623 tabpage_T *tp;
6624 win_T *win;
6625
6626 abort = mark_quickfix_ctx(&ql_info, copyID);
6627 if (abort)
6628 return abort;
6629
6630 FOR_ALL_TAB_WINDOWS(tp, win)
6631 {
6632 if (win->w_llist != NULL)
6633 {
6634 abort = mark_quickfix_ctx(win->w_llist, copyID);
6635 if (abort)
6636 return abort;
6637 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006638 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6639 {
6640 /* In a location list window and none of the other windows is
6641 * referring to this location list. Mark the location list
6642 * context as still in use.
6643 */
6644 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6645 if (abort)
6646 return abort;
6647 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006648 }
6649
6650 return abort;
6651}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006652#endif
6653
Bram Moolenaar81695252004-12-29 20:58:21 +00006654/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006655 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006656 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006657 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006658 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006659 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006660 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006661 */
6662 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006663ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006664{
6665 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006666 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006667 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006668 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006669 int_u save_qfid;
6670 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006671
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006672 switch (eap->cmdidx)
6673 {
6674 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6675 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6676 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6677 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6678 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6679 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6680 default: break;
6681 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006682 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6683 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006684 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006685#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006686 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006687 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006688#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006689 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006690
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006691 /* Must come after autocommands. */
Bram Moolenaar39665952018-08-15 20:59:48 +02006692 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006693 {
6694 qi = ll_get_or_alloc_list(curwin);
6695 if (qi == NULL)
6696 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006697 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006698 }
6699
Bram Moolenaar86b68352004-12-27 21:59:20 +00006700 if (*eap->arg == NUL)
6701 buf = curbuf;
6702 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6703 buf = buflist_findnr(atoi((char *)eap->arg));
6704 if (buf == NULL)
6705 EMSG(_(e_invarg));
6706 else if (buf->b_ml.ml_mfp == NULL)
6707 EMSG(_("E681: Buffer is not loaded"));
6708 else
6709 {
6710 if (eap->addr_count == 0)
6711 {
6712 eap->line1 = 1;
6713 eap->line2 = buf->b_ml.ml_line_count;
6714 }
6715 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6716 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6717 EMSG(_(e_invrange));
6718 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006719 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006720 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006721
6722 if (buf->b_sfname)
6723 {
6724 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6725 (char *)qf_title, (char *)buf->b_sfname);
6726 qf_title = IObuff;
6727 }
6728
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006729 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006730 (eap->cmdidx != CMD_caddbuffer
6731 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006732 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006733 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006734 if (res >= 0)
6735 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006736
6737 // Remember the current quickfix list identifier, so that we can
6738 // check for autocommands changing the current quickfix list.
6739 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006740 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006741 {
6742 buf_T *curbuf_old = curbuf;
6743
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006744 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6745 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006746 if (curbuf != curbuf_old)
6747 // Autocommands changed buffer, don't jump now, "qi" may
6748 // be invalid.
6749 res = 0;
6750 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006751 // Jump to the first error for a new list and if autocmds didn't
6752 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006753 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006754 eap->cmdidx == CMD_lbuffer)
6755 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006756 // display the first error
6757 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar754b5602006-02-09 23:53:20 +00006758 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006759 }
6760}
6761
Bram Moolenaar1e015462005-09-25 22:16:38 +00006762#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006763/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006764 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6765 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006766 */
6767 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006768ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006769{
6770 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006771 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006772 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006773 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006774 int_u save_qfid;
6775 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006776
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006777 switch (eap->cmdidx)
6778 {
6779 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6780 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6781 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6782 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6783 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6784 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6785 default: break;
6786 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006787 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6788 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006789 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006790#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006791 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006792 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006793#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006794 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006795
Bram Moolenaar39665952018-08-15 20:59:48 +02006796 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006797 {
6798 qi = ll_get_or_alloc_list(curwin);
6799 if (qi == NULL)
6800 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006801 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006802 }
6803
Bram Moolenaar4770d092006-01-12 23:22:24 +00006804 /* Evaluate the expression. When the result is a string or a list we can
6805 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006806 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006807 if (tv != NULL)
6808 {
6809 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6810 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6811 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006812 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006813 (eap->cmdidx != CMD_caddexpr
6814 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006815 (linenr_T)0, (linenr_T)0,
6816 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006817 if (res >= 0)
6818 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006819
6820 // Remember the current quickfix list identifier, so that we can
6821 // check for autocommands changing the current quickfix list.
6822 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006823 if (au_name != NULL)
6824 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6825 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006826
6827 // Jump to the first error for a new list and if autocmds didn't
6828 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006829 if (res > 0 && (eap->cmdidx == CMD_cexpr
6830 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006831 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006832 // display the first error
6833 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006834 }
6835 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006836 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006837 free_tv(tv);
6838 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006839}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006840#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006841
6842/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006843 * Get the location list for ":lhelpgrep"
6844 */
6845 static qf_info_T *
6846hgr_get_ll(int *new_ll)
6847{
6848 win_T *wp;
6849 qf_info_T *qi;
6850
6851 /* If the current window is a help window, then use it */
6852 if (bt_help(curwin->w_buffer))
6853 wp = curwin;
6854 else
6855 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006856 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006857
6858 if (wp == NULL) /* Help window not found */
6859 qi = NULL;
6860 else
6861 qi = wp->w_llist;
6862
6863 if (qi == NULL)
6864 {
6865 /* Allocate a new location list for help text matches */
6866 if ((qi = ll_new_list()) == NULL)
6867 return NULL;
6868 *new_ll = TRUE;
6869 }
6870
6871 return qi;
6872}
6873
6874/*
6875 * Search for a pattern in a help file.
6876 */
6877 static void
6878hgr_search_file(
6879 qf_info_T *qi,
6880 char_u *fname,
6881#ifdef FEAT_MBYTE
6882 vimconv_T *p_vc,
6883#endif
6884 regmatch_T *p_regmatch)
6885{
6886 FILE *fd;
6887 long lnum;
6888
6889 fd = mch_fopen((char *)fname, "r");
6890 if (fd == NULL)
6891 return;
6892
6893 lnum = 1;
6894 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6895 {
6896 char_u *line = IObuff;
6897#ifdef FEAT_MBYTE
6898 /* Convert a line if 'encoding' is not utf-8 and
6899 * the line contains a non-ASCII character. */
6900 if (p_vc->vc_type != CONV_NONE
6901 && has_non_ascii(IObuff))
6902 {
6903 line = string_convert(p_vc, IObuff, NULL);
6904 if (line == NULL)
6905 line = IObuff;
6906 }
6907#endif
6908
6909 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6910 {
6911 int l = (int)STRLEN(line);
6912
6913 /* remove trailing CR, LF, spaces, etc. */
6914 while (l > 0 && line[l - 1] <= ' ')
6915 line[--l] = NUL;
6916
6917 if (qf_add_entry(qi,
6918 qi->qf_curlist,
6919 NULL, /* dir */
6920 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006921 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006922 0,
6923 line,
6924 lnum,
6925 (int)(p_regmatch->startp[0] - line)
6926 + 1, /* col */
6927 FALSE, /* vis_col */
6928 NULL, /* search pattern */
6929 0, /* nr */
6930 1, /* type */
6931 TRUE /* valid */
6932 ) == FAIL)
6933 {
6934 got_int = TRUE;
6935#ifdef FEAT_MBYTE
6936 if (line != IObuff)
6937 vim_free(line);
6938#endif
6939 break;
6940 }
6941 }
6942#ifdef FEAT_MBYTE
6943 if (line != IObuff)
6944 vim_free(line);
6945#endif
6946 ++lnum;
6947 line_breakcheck();
6948 }
6949 fclose(fd);
6950}
6951
6952/*
6953 * Search for a pattern in all the help files in the doc directory under
6954 * the given directory.
6955 */
6956 static void
6957hgr_search_files_in_dir(
6958 qf_info_T *qi,
6959 char_u *dirname,
6960 regmatch_T *p_regmatch
6961#ifdef FEAT_MBYTE
6962 , vimconv_T *p_vc
6963#endif
6964#ifdef FEAT_MULTI_LANG
6965 , char_u *lang
6966#endif
6967 )
6968{
6969 int fcount;
6970 char_u **fnames;
6971 int fi;
6972
6973 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6974 add_pathsep(dirname);
6975 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6976 if (gen_expand_wildcards(1, &dirname, &fcount,
6977 &fnames, EW_FILE|EW_SILENT) == OK
6978 && fcount > 0)
6979 {
6980 for (fi = 0; fi < fcount && !got_int; ++fi)
6981 {
6982#ifdef FEAT_MULTI_LANG
6983 /* Skip files for a different language. */
6984 if (lang != NULL
6985 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006986 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006987 && !(STRNICMP(lang, "en", 2) == 0
6988 && STRNICMP("txt", fnames[fi]
6989 + STRLEN(fnames[fi]) - 3, 3) == 0))
6990 continue;
6991#endif
6992
6993 hgr_search_file(qi, fnames[fi],
6994#ifdef FEAT_MBYTE
6995 p_vc,
6996#endif
6997 p_regmatch);
6998 }
6999 FreeWild(fcount, fnames);
7000 }
7001}
7002
7003/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007004 * Search for a pattern in all the help files in the 'runtimepath'
7005 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007006 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007007 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007008 */
7009 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007010hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007011{
7012 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007013
7014#ifdef FEAT_MBYTE
7015 vimconv_T vc;
7016
7017 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
7018 * differs. */
7019 vc.vc_type = CONV_NONE;
7020 if (!enc_utf8)
7021 convert_setup(&vc, (char_u *)"utf-8", p_enc);
7022#endif
7023
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007024
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007025 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007026 p = p_rtp;
7027 while (*p != NUL && !got_int)
7028 {
7029 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7030
7031 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
7032#ifdef FEAT_MBYTE
7033 , &vc
7034#endif
7035#ifdef FEAT_MULTI_LANG
7036 , lang
7037#endif
7038 );
7039 }
7040
7041#ifdef FEAT_MBYTE
7042 if (vc.vc_type != CONV_NONE)
7043 convert_setup(&vc, NULL, NULL);
7044#endif
7045}
7046
7047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 * ":helpgrep {pattern}"
7049 */
7050 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007051ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
7053 regmatch_T regmatch;
7054 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007055 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007056 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007057 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007058 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059
Bram Moolenaar73633f82012-01-20 13:39:07 +01007060 switch (eap->cmdidx)
7061 {
7062 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7063 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7064 default: break;
7065 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007066 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7067 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007068 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007069#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007070 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007071 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007072#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007073 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007074
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007075 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007076 save_cpo = p_cpo;
7077 p_cpo = empty_option;
7078
Bram Moolenaar39665952018-08-15 20:59:48 +02007079 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007080 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007081 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007082 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007083 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007084 }
7085
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007086#ifdef FEAT_MULTI_LANG
7087 // Check for a specified language
7088 lang = check_help_lang(eap->arg);
7089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7091 regmatch.rm_ic = FALSE;
7092 if (regmatch.regprog != NULL)
7093 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007094 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007095 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007097 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007098
Bram Moolenaar473de612013-06-08 18:19:48 +02007099 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007101 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
7102 qi->qf_lists[qi->qf_curlist].qf_ptr =
7103 qi->qf_lists[qi->qf_curlist].qf_start;
7104 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 }
7106
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007107 if (p_cpo == empty_option)
7108 p_cpo = save_cpo;
7109 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007110 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007111 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112
Bram Moolenaarb254af32017-12-18 19:48:58 +01007113 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02007114 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
Bram Moolenaar73633f82012-01-20 13:39:07 +01007116 if (au_name != NULL)
7117 {
7118 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7119 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007120 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01007121 /* autocommands made "qi" invalid */
7122 return;
7123 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007124
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007126 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007127 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007128 else
7129 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007130
7131 if (eap->cmdidx == CMD_lhelpgrep)
7132 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007133 // If the help window is not opened or if it already points to the
7134 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007135 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007136 {
7137 if (new_qi)
7138 ll_free_all(&qi);
7139 }
7140 else if (curwin->w_llist == NULL)
7141 curwin->w_llist = qi;
7142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143}
7144
7145#endif /* FEAT_QUICKFIX */