blob: 9c346519fa1e9227c7b02e54ae442b4e643b9a3a [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 char_u *get_mef_name(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
153static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
154static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
155static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000157/* Quickfix window check helper macro */
158#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
159/* Location list window check helper macro */
160#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200161
162// Quickfix and location list stack check helper macros
163#define IS_QF_STACK(qi) (qi == &ql_info)
164#define IS_LL_STACK(qi) (qi != &ql_info)
165
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000166/*
167 * Return location list for window 'wp'
168 * For location list window, return the referenced location list
169 */
170#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200173 * Looking up a buffer can be slow if there are many. Remember the last one
174 * to make this a lot faster if there are multiple matches in the same file.
175 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200176static char_u *qf_last_bufname = NULL;
177static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200178
Bram Moolenaar3c097222017-12-21 20:54:49 +0100179static char *e_loc_list_changed =
180 N_("E926: Current location list was changed");
181
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200182/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200183 * Maximum number of bytes allowed per line while reading a errorfile.
184 */
185#define LINE_MAXLEN 4096
186
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200187static struct fmtpattern
188{
189 char_u convchar;
190 char *pattern;
191} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200192 {
193 {'f', ".\\+"}, /* only used when at end */
194 {'n', "\\d\\+"},
195 {'l', "\\d\\+"},
196 {'c', "\\d\\+"},
197 {'t', "."},
198 {'m', ".\\+"},
199 {'r', ".*"},
200 {'p', "[- .]*"},
201 {'v', "\\d\\+"},
202 {'s', ".\\+"},
203 {'o', ".\\+"}
204 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200205
206/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200207 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200208 * See fmt_pat definition above for the list of supported patterns. The
209 * pattern specifier is supplied in "efmpat". The converted pattern is stored
210 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200211 */
212 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200213efmpat_to_regpat(
214 char_u *efmpat,
215 char_u *regpat,
216 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200217 int idx,
218 int round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200219 char_u *errmsg)
220{
221 char_u *srcptr;
222
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200223 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200224 {
225 /* Each errorformat pattern can occur only once */
226 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200227 _("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200228 EMSG(errmsg);
229 return NULL;
230 }
231 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200232 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200233 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200234 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200235 {
236 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200237 _("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200238 EMSG(errmsg);
239 return NULL;
240 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200241 efminfo->addr[idx] = (char_u)++round;
242 *regpat++ = '\\';
243 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200244#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200245 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200246 {
247 /* Also match "c:" in the file name, even when
248 * checking for a colon next: "%f:".
249 * "\%(\a:\)\=" */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200250 STRCPY(regpat, "\\%(\\a:\\)\\=");
251 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200252 }
253#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200254 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200255 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200256 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200257 {
258 /* A file name may contain spaces, but this isn't
259 * in "\f". For "%f:%l:%m" there may be a ":" in
260 * the file name. Use ".\{-1,}x" instead (x is
261 * the next character), the requirement that :999:
262 * follows should work. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200263 STRCPY(regpat, ".\\{-1,}");
264 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200265 }
266 else
267 {
268 /* File name followed by '\\' or '%': include as
269 * many file name chars as possible. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200270 STRCPY(regpat, "\\f\\+");
271 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200272 }
273 }
274 else
275 {
276 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200277 while ((*regpat = *srcptr++) != NUL)
278 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200279 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200280 *regpat++ = '\\';
281 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200282
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200283 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200284}
285
286/*
287 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200288 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200289 */
290 static char_u *
291scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200292 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200293 char_u *efm,
294 int len,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200295 char_u *regpat,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200296 char_u *errmsg)
297{
298 char_u *efmp = *pefmp;
299
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200300 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200301 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200302 if ((*regpat++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200303 {
304 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200305 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200306 if (efmp < efm + len)
307 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200308 *regpat++ = *++efmp; /* could be ']' */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200309 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200310 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200311 /* skip */;
312 if (efmp == efm + len)
313 {
314 EMSG(_("E374: Missing ] in format string"));
315 return NULL;
316 }
317 }
318 }
319 else if (efmp < efm + len) /* %*\D, %*\s etc. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200320 *regpat++ = *++efmp;
321 *regpat++ = '\\';
322 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200323 }
324 else
325 {
326 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
327 sprintf((char *)errmsg,
328 _("E375: Unsupported %%%c in format string"), *efmp);
329 EMSG(errmsg);
330 return NULL;
331 }
332
333 *pefmp = efmp;
334
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200335 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200336}
337
338/*
339 * Analyze/parse an errorformat prefix.
340 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200341 static char_u *
342efm_analyze_prefix(char_u *efmp, efm_T *efminfo, char_u *errmsg)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200343{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200344 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200345 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200346 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200347 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200348 else
349 {
350 sprintf((char *)errmsg,
351 _("E376: Invalid %%%c in format string prefix"), *efmp);
352 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200353 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200354 }
355
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200356 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200357}
358
359/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200360 * Converts a 'errorformat' string part in 'efm' to a regular expression
361 * pattern. The resulting regex pattern is returned in "regpat". Additional
362 * information about the 'erroformat' pattern is returned in "fmt_ptr".
363 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200364 */
365 static int
366efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200367 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200368 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200369 efm_T *fmt_ptr,
370 char_u *regpat,
371 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200372{
373 char_u *ptr;
374 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200375 int round;
376 int idx = 0;
377
378 /*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200379 * Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200380 */
381 ptr = regpat;
382 *ptr++ = '^';
383 round = 0;
384 for (efmp = efm; efmp < efm + len; ++efmp)
385 {
386 if (*efmp == '%')
387 {
388 ++efmp;
389 for (idx = 0; idx < FMT_PATTERNS; ++idx)
390 if (fmt_pat[idx].convchar == *efmp)
391 break;
392 if (idx < FMT_PATTERNS)
393 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200394 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200395 errmsg);
396 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200397 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200398 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200399 }
400 else if (*efmp == '*')
401 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200402 ++efmp;
403 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr, errmsg);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200404 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200405 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200406 }
407 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
408 *ptr++ = *efmp; /* regexp magic characters */
409 else if (*efmp == '#')
410 *ptr++ = '*';
411 else if (*efmp == '>')
412 fmt_ptr->conthere = TRUE;
413 else if (efmp == efm + 1) /* analyse prefix */
414 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200415 /*
416 * prefix is allowed only at the beginning of the errorformat
417 * option part
418 */
419 efmp = efm_analyze_prefix(efmp, fmt_ptr, errmsg);
420 if (efmp == NULL)
421 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200422 }
423 else
424 {
425 sprintf((char *)errmsg,
426 _("E377: Invalid %%%c in format string"), *efmp);
427 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200428 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200429 }
430 }
431 else /* copy normal character */
432 {
433 if (*efmp == '\\' && efmp + 1 < efm + len)
434 ++efmp;
435 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
436 *ptr++ = '\\'; /* escape regexp atoms */
437 if (*efmp)
438 *ptr++ = *efmp;
439 }
440 }
441 *ptr++ = '$';
442 *ptr = NUL;
443
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200444 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200445}
446
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200447/*
448 * Free the 'errorformat' information list
449 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200450 static void
451free_efm_list(efm_T **efm_first)
452{
453 efm_T *efm_ptr;
454
455 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
456 {
457 *efm_first = efm_ptr->next;
458 vim_regfree(efm_ptr->prog);
459 vim_free(efm_ptr);
460 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100461 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200462}
463
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200464/*
465 * Compute the size of the buffer used to convert a 'errorformat' pattern into
466 * a regular expression pattern.
467 */
468 static int
469efm_regpat_bufsz(char_u *efm)
470{
471 int sz;
472 int i;
473
474 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
475 for (i = FMT_PATTERNS; i > 0; )
476 sz += (int)STRLEN(fmt_pat[--i].pattern);
477#ifdef BACKSLASH_IN_FILENAME
478 sz += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
479#else
480 sz += 2; /* "%f" can become two chars longer */
481#endif
482
483 return sz;
484}
485
486/*
487 * Return the length of a 'errorformat' option part (separated by ",").
488 */
489 static int
490efm_option_part_len(char_u *efm)
491{
492 int len;
493
494 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
495 if (efm[len] == '\\' && efm[len + 1] != NUL)
496 ++len;
497
498 return len;
499}
500
501/*
502 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
503 * are parsed and converted to regular expressions. Returns information about
504 * the parsed 'errorformat' option.
505 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200506 static efm_T *
507parse_efm_option(char_u *efm)
508{
509 char_u *errmsg = NULL;
510 int errmsglen;
511 efm_T *fmt_ptr = NULL;
512 efm_T *fmt_first = NULL;
513 efm_T *fmt_last = NULL;
514 char_u *fmtstr = NULL;
515 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200516 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200517
518 errmsglen = CMDBUFFSIZE + 1;
519 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
520 if (errmsg == NULL)
521 goto parse_efm_end;
522
523 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200524 * Each part of the format string is copied and modified from errorformat
525 * to regex prog. Only a few % characters are allowed.
526 */
527
528 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200529 * Get some space to modify the format string into.
530 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200531 sz = efm_regpat_bufsz(efm);
532 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200533 goto parse_efm_error;
534
535 while (efm[0] != NUL)
536 {
537 /*
538 * Allocate a new eformat structure and put it at the end of the list
539 */
540 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
541 if (fmt_ptr == NULL)
542 goto parse_efm_error;
543 if (fmt_first == NULL) /* first one */
544 fmt_first = fmt_ptr;
545 else
546 fmt_last->next = fmt_ptr;
547 fmt_last = fmt_ptr;
548
549 /*
550 * Isolate one part in the 'errorformat' option
551 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200552 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200553
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200554 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200555 goto parse_efm_error;
556 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
557 goto parse_efm_error;
558 /*
559 * Advance to next part
560 */
561 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
562 }
563
564 if (fmt_first == NULL) /* nothing found */
565 EMSG(_("E378: 'errorformat' contains no pattern"));
566
567 goto parse_efm_end;
568
569parse_efm_error:
570 free_efm_list(&fmt_first);
571
572parse_efm_end:
573 vim_free(fmtstr);
574 vim_free(errmsg);
575
576 return fmt_first;
577}
578
Bram Moolenaare0d37972016-07-15 22:36:01 +0200579enum {
580 QF_FAIL = 0,
581 QF_OK = 1,
582 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200583 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200584 QF_IGNORE_LINE = 4,
585 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200586};
587
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200588/*
589 * State information used to parse lines and add entries to a quickfix/location
590 * list.
591 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200592typedef struct {
593 char_u *linebuf;
594 int linelen;
595 char_u *growbuf;
596 int growbufsiz;
597 FILE *fd;
598 typval_T *tv;
599 char_u *p_str;
600 listitem_T *p_li;
601 buf_T *buf;
602 linenr_T buflnum;
603 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100604 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200605} qfstate_T;
606
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200607/*
608 * Allocate more memory for the line buffer used for parsing lines.
609 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200610 static char_u *
611qf_grow_linebuf(qfstate_T *state, int newsz)
612{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200613 char_u *p;
614
Bram Moolenaare0d37972016-07-15 22:36:01 +0200615 /*
616 * If the line exceeds LINE_MAXLEN exclude the last
617 * byte since it's not a NL character.
618 */
619 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
620 if (state->growbuf == NULL)
621 {
622 state->growbuf = alloc(state->linelen + 1);
623 if (state->growbuf == NULL)
624 return NULL;
625 state->growbufsiz = state->linelen;
626 }
627 else if (state->linelen > state->growbufsiz)
628 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200629 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200630 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200631 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200632 state->growbufsiz = state->linelen;
633 }
634 return state->growbuf;
635}
636
637/*
638 * Get the next string (separated by newline) from state->p_str.
639 */
640 static int
641qf_get_next_str_line(qfstate_T *state)
642{
643 /* Get the next line from the supplied string */
644 char_u *p_str = state->p_str;
645 char_u *p;
646 int len;
647
648 if (*p_str == NUL) /* Reached the end of the string */
649 return QF_END_OF_INPUT;
650
651 p = vim_strchr(p_str, '\n');
652 if (p != NULL)
653 len = (int)(p - p_str) + 1;
654 else
655 len = (int)STRLEN(p_str);
656
657 if (len > IOSIZE - 2)
658 {
659 state->linebuf = qf_grow_linebuf(state, len);
660 if (state->linebuf == NULL)
661 return QF_NOMEM;
662 }
663 else
664 {
665 state->linebuf = IObuff;
666 state->linelen = len;
667 }
668 vim_strncpy(state->linebuf, p_str, state->linelen);
669
670 /*
671 * Increment using len in order to discard the rest of the
672 * line if it exceeds LINE_MAXLEN.
673 */
674 p_str += len;
675 state->p_str = p_str;
676
677 return QF_OK;
678}
679
680/*
681 * Get the next string from state->p_Li.
682 */
683 static int
684qf_get_next_list_line(qfstate_T *state)
685{
686 listitem_T *p_li = state->p_li;
687 int len;
688
689 while (p_li != NULL
690 && (p_li->li_tv.v_type != VAR_STRING
691 || p_li->li_tv.vval.v_string == NULL))
692 p_li = p_li->li_next; /* Skip non-string items */
693
694 if (p_li == NULL) /* End of the list */
695 {
696 state->p_li = NULL;
697 return QF_END_OF_INPUT;
698 }
699
700 len = (int)STRLEN(p_li->li_tv.vval.v_string);
701 if (len > IOSIZE - 2)
702 {
703 state->linebuf = qf_grow_linebuf(state, len);
704 if (state->linebuf == NULL)
705 return QF_NOMEM;
706 }
707 else
708 {
709 state->linebuf = IObuff;
710 state->linelen = len;
711 }
712
713 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
714
715 state->p_li = p_li->li_next; /* next item */
716 return QF_OK;
717}
718
719/*
720 * Get the next string from state->buf.
721 */
722 static int
723qf_get_next_buf_line(qfstate_T *state)
724{
725 char_u *p_buf = NULL;
726 int len;
727
728 /* Get the next line from the supplied buffer */
729 if (state->buflnum > state->lnumlast)
730 return QF_END_OF_INPUT;
731
732 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
733 state->buflnum += 1;
734
735 len = (int)STRLEN(p_buf);
736 if (len > IOSIZE - 2)
737 {
738 state->linebuf = qf_grow_linebuf(state, len);
739 if (state->linebuf == NULL)
740 return QF_NOMEM;
741 }
742 else
743 {
744 state->linebuf = IObuff;
745 state->linelen = len;
746 }
747 vim_strncpy(state->linebuf, p_buf, state->linelen);
748
749 return QF_OK;
750}
751
752/*
753 * Get the next string from file state->fd.
754 */
755 static int
756qf_get_next_file_line(qfstate_T *state)
757{
758 int discard;
759 int growbuflen;
760
761 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
762 return QF_END_OF_INPUT;
763
764 discard = FALSE;
765 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200766 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200767 {
768 /*
769 * The current line exceeds IObuff, continue reading using
770 * growbuf until EOL or LINE_MAXLEN bytes is read.
771 */
772 if (state->growbuf == NULL)
773 {
774 state->growbufsiz = 2 * (IOSIZE - 1);
775 state->growbuf = alloc(state->growbufsiz);
776 if (state->growbuf == NULL)
777 return QF_NOMEM;
778 }
779
780 /* Copy the read part of the line, excluding null-terminator */
781 memcpy(state->growbuf, IObuff, IOSIZE - 1);
782 growbuflen = state->linelen;
783
784 for (;;)
785 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200786 char_u *p;
787
Bram Moolenaare0d37972016-07-15 22:36:01 +0200788 if (fgets((char *)state->growbuf + growbuflen,
789 state->growbufsiz - growbuflen, state->fd) == NULL)
790 break;
791 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
792 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200793 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200794 break;
795 if (state->growbufsiz == LINE_MAXLEN)
796 {
797 discard = TRUE;
798 break;
799 }
800
801 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
802 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200803 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200804 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200805 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200806 }
807
808 while (discard)
809 {
810 /*
811 * The current line is longer than LINE_MAXLEN, continue
812 * reading but discard everything until EOL or EOF is
813 * reached.
814 */
815 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
816 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200817 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200818 break;
819 }
820
821 state->linebuf = state->growbuf;
822 state->linelen = growbuflen;
823 }
824 else
825 state->linebuf = IObuff;
826
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100827#ifdef FEAT_MBYTE
828 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200829 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
830 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100831 char_u *line;
832
833 line = string_convert(&state->vc, state->linebuf, &state->linelen);
834 if (line != NULL)
835 {
836 if (state->linelen < IOSIZE)
837 {
838 STRCPY(state->linebuf, line);
839 vim_free(line);
840 }
841 else
842 {
843 vim_free(state->growbuf);
844 state->linebuf = state->growbuf = line;
845 state->growbufsiz = state->linelen < LINE_MAXLEN
846 ? state->linelen : LINE_MAXLEN;
847 }
848 }
849 }
850#endif
851
Bram Moolenaare0d37972016-07-15 22:36:01 +0200852 return QF_OK;
853}
854
855/*
856 * Get the next string from a file/buffer/list/string.
857 */
858 static int
859qf_get_nextline(qfstate_T *state)
860{
861 int status = QF_FAIL;
862
863 if (state->fd == NULL)
864 {
865 if (state->tv != NULL)
866 {
867 if (state->tv->v_type == VAR_STRING)
868 /* Get the next line from the supplied string */
869 status = qf_get_next_str_line(state);
870 else if (state->tv->v_type == VAR_LIST)
871 /* Get the next line from the supplied list */
872 status = qf_get_next_list_line(state);
873 }
874 else
875 /* Get the next line from the supplied buffer */
876 status = qf_get_next_buf_line(state);
877 }
878 else
879 /* Get the next line from the supplied file */
880 status = qf_get_next_file_line(state);
881
882 if (status != QF_OK)
883 return status;
884
885 /* remove newline/CR from the line */
886 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200887 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200888 state->linebuf[state->linelen - 1] = NUL;
889#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200890 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
891 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200892#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200893 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200894
895#ifdef FEAT_MBYTE
896 remove_bom(state->linebuf);
897#endif
898
899 return QF_OK;
900}
901
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200902typedef struct {
903 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200904 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200905 char_u *errmsg;
906 int errmsglen;
907 long lnum;
908 int col;
909 char_u use_viscol;
910 char_u *pattern;
911 int enr;
912 int type;
913 int valid;
914} qffields_T;
915
916/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200917 * Parse the match for filename ('%f') pattern in regmatch.
918 * Return the matched value in "fields->namebuf".
919 */
920 static int
921qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
922{
923 int c;
924
925 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
926 return QF_FAIL;
927
928 /* Expand ~/file and $HOME/file to full path. */
929 c = *rmp->endp[midx];
930 *rmp->endp[midx] = NUL;
931 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
932 *rmp->endp[midx] = c;
933
934 /*
935 * For separate filename patterns (%O, %P and %Q), the specified file
936 * should exist.
937 */
938 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
939 && mch_getperm(fields->namebuf) == -1)
940 return QF_FAIL;
941
942 return QF_OK;
943}
944
945/*
946 * Parse the match for error number ('%n') pattern in regmatch.
947 * Return the matched value in "fields->enr".
948 */
949 static int
950qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
951{
952 if (rmp->startp[midx] == NULL)
953 return QF_FAIL;
954 fields->enr = (int)atol((char *)rmp->startp[midx]);
955 return QF_OK;
956}
957
958/*
959 * Parse the match for line number (%l') pattern in regmatch.
960 * Return the matched value in "fields->lnum".
961 */
962 static int
963qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
964{
965 if (rmp->startp[midx] == NULL)
966 return QF_FAIL;
967 fields->lnum = atol((char *)rmp->startp[midx]);
968 return QF_OK;
969}
970
971/*
972 * Parse the match for column number ('%c') pattern in regmatch.
973 * Return the matched value in "fields->col".
974 */
975 static int
976qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
977{
978 if (rmp->startp[midx] == NULL)
979 return QF_FAIL;
980 fields->col = (int)atol((char *)rmp->startp[midx]);
981 return QF_OK;
982}
983
984/*
985 * Parse the match for error type ('%t') pattern in regmatch.
986 * Return the matched value in "fields->type".
987 */
988 static int
989qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
990{
991 if (rmp->startp[midx] == NULL)
992 return QF_FAIL;
993 fields->type = *rmp->startp[midx];
994 return QF_OK;
995}
996
997/*
998 * Parse the match for '%+' format pattern. The whole matching line is included
999 * in the error string. Return the matched line in "fields->errmsg".
1000 */
1001 static int
1002qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
1003{
1004 char_u *p;
1005
1006 if (linelen >= fields->errmsglen)
1007 {
1008 /* linelen + null terminator */
1009 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1010 return QF_NOMEM;
1011 fields->errmsg = p;
1012 fields->errmsglen = linelen + 1;
1013 }
1014 vim_strncpy(fields->errmsg, linebuf, linelen);
1015 return QF_OK;
1016}
1017
1018/*
1019 * Parse the match for error message ('%m') pattern in regmatch.
1020 * Return the matched value in "fields->errmsg".
1021 */
1022 static int
1023qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1024{
1025 char_u *p;
1026 int len;
1027
1028 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1029 return QF_FAIL;
1030 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1031 if (len >= fields->errmsglen)
1032 {
1033 /* len + null terminator */
1034 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1035 return QF_NOMEM;
1036 fields->errmsg = p;
1037 fields->errmsglen = len + 1;
1038 }
1039 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1040 return QF_OK;
1041}
1042
1043/*
1044 * Parse the match for rest of a single-line file message ('%r') pattern.
1045 * Return the matched value in "tail".
1046 */
1047 static int
1048qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1049{
1050 if (rmp->startp[midx] == NULL)
1051 return QF_FAIL;
1052 *tail = rmp->startp[midx];
1053 return QF_OK;
1054}
1055
1056/*
1057 * Parse the match for the pointer line ('%p') pattern in regmatch.
1058 * Return the matched value in "fields->col".
1059 */
1060 static int
1061qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1062{
1063 char_u *match_ptr;
1064
1065 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1066 return QF_FAIL;
1067 fields->col = 0;
1068 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1069 ++match_ptr)
1070 {
1071 ++fields->col;
1072 if (*match_ptr == TAB)
1073 {
1074 fields->col += 7;
1075 fields->col -= fields->col % 8;
1076 }
1077 }
1078 ++fields->col;
1079 fields->use_viscol = TRUE;
1080 return QF_OK;
1081}
1082
1083/*
1084 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1085 * Return the matched value in "fields->col".
1086 */
1087 static int
1088qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1089{
1090 if (rmp->startp[midx] == NULL)
1091 return QF_FAIL;
1092 fields->col = (int)atol((char *)rmp->startp[midx]);
1093 fields->use_viscol = TRUE;
1094 return QF_OK;
1095}
1096
1097/*
1098 * Parse the match for the search text ('%s') pattern in regmatch.
1099 * Return the matched value in "fields->pattern".
1100 */
1101 static int
1102qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1103{
1104 int len;
1105
1106 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1107 return QF_FAIL;
1108 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1109 if (len > CMDBUFFSIZE - 5)
1110 len = CMDBUFFSIZE - 5;
1111 STRCPY(fields->pattern, "^\\V");
1112 STRNCAT(fields->pattern, rmp->startp[midx], len);
1113 fields->pattern[len + 3] = '\\';
1114 fields->pattern[len + 4] = '$';
1115 fields->pattern[len + 5] = NUL;
1116 return QF_OK;
1117}
1118
1119/*
1120 * Parse the match for the module ('%o') pattern in regmatch.
1121 * Return the matched value in "fields->module".
1122 */
1123 static int
1124qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1125{
1126 int len;
1127
1128 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1129 return QF_FAIL;
1130 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1131 if (len > CMDBUFFSIZE)
1132 len = CMDBUFFSIZE;
1133 STRNCAT(fields->module, rmp->startp[midx], len);
1134 return QF_OK;
1135}
1136
1137/*
1138 * 'errorformat' format pattern parser functions.
1139 * The '%f' and '%r' formats are parsed differently from other formats.
1140 * See qf_parse_match() for details.
1141 */
1142static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1143{
1144 NULL,
1145 qf_parse_fmt_n,
1146 qf_parse_fmt_l,
1147 qf_parse_fmt_c,
1148 qf_parse_fmt_t,
1149 qf_parse_fmt_m,
1150 NULL,
1151 qf_parse_fmt_p,
1152 qf_parse_fmt_v,
1153 qf_parse_fmt_s,
1154 qf_parse_fmt_o
1155};
1156
1157/*
1158 * Parse the error format pattern matches in "regmatch" and set the values in
1159 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1160 * match. Returns QF_OK if all the matches are successfully parsed. On
1161 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001162 */
1163 static int
1164qf_parse_match(
1165 char_u *linebuf,
1166 int linelen,
1167 efm_T *fmt_ptr,
1168 regmatch_T *regmatch,
1169 qffields_T *fields,
1170 int qf_multiline,
1171 int qf_multiscan,
1172 char_u **tail)
1173{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001174 int idx = fmt_ptr->prefix;
1175 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001176 int midx;
1177 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001178
1179 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1180 return QF_FAIL;
1181 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1182 fields->type = idx;
1183 else
1184 fields->type = 0;
1185 /*
1186 * Extract error message data from matched line.
1187 * We check for an actual submatch, because "\[" and "\]" in
1188 * the 'errorformat' may cause the wrong submatch to be used.
1189 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001190 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001191 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001192 status = QF_OK;
1193 midx = (int)fmt_ptr->addr[i];
1194 if (i == 0 && midx > 0) /* %f */
1195 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1196 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001197 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001198 if (fmt_ptr->flags == '+' && !qf_multiscan) /* %+ */
1199 status = qf_parse_fmt_plus(linebuf, linelen, fields);
1200 else if (midx > 0) /* %m */
1201 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001202 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001203 else if (i == 6 && midx > 0) /* %r */
1204 status = qf_parse_fmt_r(regmatch, midx, tail);
1205 else if (midx > 0) /* others */
1206 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001207
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001208 if (status != QF_OK)
1209 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001210 }
1211
1212 return QF_OK;
1213}
1214
1215/*
1216 * Parse an error line in 'linebuf' using a single error format string in
1217 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1218 * Returns QF_OK if the efm format matches completely and the fields are
1219 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1220 */
1221 static int
1222qf_parse_get_fields(
1223 char_u *linebuf,
1224 int linelen,
1225 efm_T *fmt_ptr,
1226 qffields_T *fields,
1227 int qf_multiline,
1228 int qf_multiscan,
1229 char_u **tail)
1230{
1231 regmatch_T regmatch;
1232 int status = QF_FAIL;
1233 int r;
1234
1235 if (qf_multiscan &&
1236 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1237 return QF_FAIL;
1238
1239 fields->namebuf[0] = NUL;
1240 fields->module[0] = NUL;
1241 fields->pattern[0] = NUL;
1242 if (!qf_multiscan)
1243 fields->errmsg[0] = NUL;
1244 fields->lnum = 0;
1245 fields->col = 0;
1246 fields->use_viscol = FALSE;
1247 fields->enr = -1;
1248 fields->type = 0;
1249 *tail = NULL;
1250
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001251 /* Always ignore case when looking for a matching error. */
1252 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001253 regmatch.regprog = fmt_ptr->prog;
1254 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1255 fmt_ptr->prog = regmatch.regprog;
1256 if (r)
1257 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1258 fields, qf_multiline, qf_multiscan, tail);
1259
1260 return status;
1261}
1262
1263/*
1264 * Parse directory error format prefixes (%D and %X).
1265 * Push and pop directories from the directory stack when scanning directory
1266 * names.
1267 */
1268 static int
1269qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1270{
1271 if (idx == 'D') /* enter directory */
1272 {
1273 if (*fields->namebuf == NUL)
1274 {
1275 EMSG(_("E379: Missing or empty directory name"));
1276 return QF_FAIL;
1277 }
1278 qfl->qf_directory =
1279 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1280 if (qfl->qf_directory == NULL)
1281 return QF_FAIL;
1282 }
1283 else if (idx == 'X') /* leave directory */
1284 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1285
1286 return QF_OK;
1287}
1288
1289/*
1290 * Parse global file name error format prefixes (%O, %P and %Q).
1291 */
1292 static int
1293qf_parse_file_pfx(
1294 int idx,
1295 qffields_T *fields,
1296 qf_list_T *qfl,
1297 char_u *tail)
1298{
1299 fields->valid = FALSE;
1300 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1301 {
1302 if (*fields->namebuf && idx == 'P')
1303 qfl->qf_currfile =
1304 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1305 else if (idx == 'Q')
1306 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1307 *fields->namebuf = NUL;
1308 if (tail && *tail)
1309 {
1310 STRMOVE(IObuff, skipwhite(tail));
1311 qfl->qf_multiscan = TRUE;
1312 return QF_MULTISCAN;
1313 }
1314 }
1315
1316 return QF_OK;
1317}
1318
1319/*
1320 * Parse a non-error line (a line which doesn't match any of the error
1321 * format in 'efm').
1322 */
1323 static int
1324qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1325{
1326 char_u *p;
1327
1328 fields->namebuf[0] = NUL; /* no match found, remove file name */
1329 fields->lnum = 0; /* don't jump to this line */
1330 fields->valid = FALSE;
1331 if (linelen >= fields->errmsglen)
1332 {
1333 /* linelen + null terminator */
1334 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1335 return QF_NOMEM;
1336 fields->errmsg = p;
1337 fields->errmsglen = linelen + 1;
1338 }
1339 /* copy whole line to error message */
1340 vim_strncpy(fields->errmsg, linebuf, linelen);
1341
1342 return QF_OK;
1343}
1344
1345/*
1346 * Parse multi-line error format prefixes (%C and %Z)
1347 */
1348 static int
1349qf_parse_multiline_pfx(
1350 qf_info_T *qi,
1351 int qf_idx,
1352 int idx,
1353 qf_list_T *qfl,
1354 qffields_T *fields)
1355{
1356 char_u *ptr;
1357 int len;
1358
1359 if (!qfl->qf_multiignore)
1360 {
1361 qfline_T *qfprev = qfl->qf_last;
1362
1363 if (qfprev == NULL)
1364 return QF_FAIL;
1365 if (*fields->errmsg && !qfl->qf_multiignore)
1366 {
1367 len = (int)STRLEN(qfprev->qf_text);
1368 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1369 == NULL)
1370 return QF_FAIL;
1371 STRCPY(ptr, qfprev->qf_text);
1372 vim_free(qfprev->qf_text);
1373 qfprev->qf_text = ptr;
1374 *(ptr += len) = '\n';
1375 STRCPY(++ptr, fields->errmsg);
1376 }
1377 if (qfprev->qf_nr == -1)
1378 qfprev->qf_nr = fields->enr;
1379 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1380 /* only printable chars allowed */
1381 qfprev->qf_type = fields->type;
1382
1383 if (!qfprev->qf_lnum)
1384 qfprev->qf_lnum = fields->lnum;
1385 if (!qfprev->qf_col)
1386 qfprev->qf_col = fields->col;
1387 qfprev->qf_viscol = fields->use_viscol;
1388 if (!qfprev->qf_fnum)
1389 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1390 qfl->qf_directory,
1391 *fields->namebuf || qfl->qf_directory != NULL
1392 ? fields->namebuf
1393 : qfl->qf_currfile != NULL && fields->valid
1394 ? qfl->qf_currfile : 0);
1395 }
1396 if (idx == 'Z')
1397 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1398 line_breakcheck();
1399
1400 return QF_IGNORE_LINE;
1401}
1402
1403/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001404 * Parse a line and get the quickfix fields.
1405 * Return the QF_ status.
1406 */
1407 static int
1408qf_parse_line(
1409 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001410 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001411 char_u *linebuf,
1412 int linelen,
1413 efm_T *fmt_first,
1414 qffields_T *fields)
1415{
1416 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417 int idx = 0;
1418 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001419 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001420 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001421
Bram Moolenaare333e792018-04-08 13:27:39 +02001422restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001423 /* If there was no %> item start at the first pattern */
1424 if (fmt_start == NULL)
1425 fmt_ptr = fmt_first;
1426 else
1427 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001428 /* Otherwise start from the last used pattern */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001429 fmt_ptr = fmt_start;
1430 fmt_start = NULL;
1431 }
1432
1433 /*
1434 * Try to match each part of 'errorformat' until we find a complete
1435 * match or no match.
1436 */
1437 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001438 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1439 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001440 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001441 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1442 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1443 if (status == QF_NOMEM)
1444 return status;
1445 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001446 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001447 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001448 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001449
1450 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1451 {
1452 if (fmt_ptr != NULL)
1453 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001454 /* 'D' and 'X' directory specifiers */
1455 status = qf_parse_dir_pfx(idx, fields, qfl);
1456 if (status != QF_OK)
1457 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001458 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001459
1460 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1461 if (status != QF_OK)
1462 return status;
1463
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001464 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001465 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 }
1467 else if (fmt_ptr != NULL)
1468 {
1469 /* honor %> item */
1470 if (fmt_ptr->conthere)
1471 fmt_start = fmt_ptr;
1472
1473 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1474 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001475 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1476 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001477 }
1478 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1479 { /* continuation of multi-line msg */
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001480 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1481 if (status != QF_OK)
1482 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001483 }
1484 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001485 { /* global file names */
1486 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1487 if (status == QF_MULTISCAN)
1488 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001489 }
1490 if (fmt_ptr->flags == '-') /* generally exclude this line */
1491 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001492 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001493 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001494 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001495 return QF_IGNORE_LINE;
1496 }
1497 }
1498
1499 return QF_OK;
1500}
1501
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001502/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001503 * Returns TRUE if the specified quickfix/location list is empty.
1504 */
1505 static int
1506qf_list_empty(qf_info_T *qi, int qf_idx)
1507{
1508 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1509 return TRUE;
1510 return qi->qf_lists[qf_idx].qf_count <= 0;
1511}
1512
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001513/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001514 * Allocate the fields used for parsing lines and populating a quickfix list.
1515 */
1516 static int
1517qf_alloc_fields(qffields_T *pfields)
1518{
1519 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1520 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1521 pfields->errmsglen = CMDBUFFSIZE + 1;
1522 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1523 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1524 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1525 || pfields->pattern == NULL || pfields->module == NULL)
1526 return FAIL;
1527
1528 return OK;
1529}
1530
1531/*
1532 * Free the fields used for parsing lines and populating a quickfix list.
1533 */
1534 static void
1535qf_free_fields(qffields_T *pfields)
1536{
1537 vim_free(pfields->namebuf);
1538 vim_free(pfields->module);
1539 vim_free(pfields->errmsg);
1540 vim_free(pfields->pattern);
1541}
1542
1543/*
1544 * Setup the state information used for parsing lines and populating a
1545 * quickfix list.
1546 */
1547 static int
1548qf_setup_state(
1549 qfstate_T *pstate,
1550 char_u *enc,
1551 char_u *efile,
1552 typval_T *tv,
1553 buf_T *buf,
1554 linenr_T lnumfirst,
1555 linenr_T lnumlast)
1556{
1557#ifdef FEAT_MBYTE
1558 pstate->vc.vc_type = CONV_NONE;
1559 if (enc != NULL && *enc != NUL)
1560 convert_setup(&pstate->vc, enc, p_enc);
1561#endif
1562
1563 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1564 {
1565 EMSG2(_(e_openerrf), efile);
1566 return FAIL;
1567 }
1568
1569 if (tv != NULL)
1570 {
1571 if (tv->v_type == VAR_STRING)
1572 pstate->p_str = tv->vval.v_string;
1573 else if (tv->v_type == VAR_LIST)
1574 pstate->p_li = tv->vval.v_list->lv_first;
1575 pstate->tv = tv;
1576 }
1577 pstate->buf = buf;
1578 pstate->buflnum = lnumfirst;
1579 pstate->lnumlast = lnumlast;
1580
1581 return OK;
1582}
1583
1584/*
1585 * Cleanup the state information used for parsing lines and populating a
1586 * quickfix list.
1587 */
1588 static void
1589qf_cleanup_state(qfstate_T *pstate)
1590{
1591 if (pstate->fd != NULL)
1592 fclose(pstate->fd);
1593
1594 vim_free(pstate->growbuf);
1595#ifdef FEAT_MBYTE
1596 if (pstate->vc.vc_type != CONV_NONE)
1597 convert_setup(&pstate->vc, NULL, NULL);
1598#endif
1599}
1600
1601/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001602 * Read the errorfile "efile" into memory, line by line, building the error
1603 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001604 * Alternative: when "efile" is NULL read errors from buffer "buf".
1605 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001606 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001607 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1608 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001609 * Return -1 for error, number of errors for success.
1610 */
1611 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001612qf_init_ext(
1613 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001614 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001615 char_u *efile,
1616 buf_T *buf,
1617 typval_T *tv,
1618 char_u *errorformat,
1619 int newlist, /* TRUE: start a new error list */
1620 linenr_T lnumfirst, /* first line number to use */
1621 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001622 char_u *qf_title,
1623 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001624{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001625 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001626 qfstate_T state;
1627 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001628 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001629 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001630 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001632 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001634 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001636 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001637 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001638
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001639 vim_memset(&state, 0, sizeof(state));
1640 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001641 if ((qf_alloc_fields(&fields) == FAIL) ||
1642 (qf_setup_state(&state, enc, efile, tv, buf,
1643 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 goto qf_init_end;
1645
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001646 if (newlist || qf_idx == qi->qf_listcount)
1647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001649 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001650 qf_idx = qi->qf_curlist;
1651 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001652 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001653 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001654 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001655 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001656 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001657 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001660 qfl = &qi->qf_lists[qf_idx];
1661
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001663 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001664 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 else
1666 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001668 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001669 * If the errorformat didn't change between calls, then reuse the
1670 * previously parsed values.
1671 */
1672 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1673 {
1674 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001675 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001676 free_efm_list(&fmt_first);
1677
1678 /* parse the current 'efm' */
1679 fmt_first = parse_efm_option(efm);
1680 if (fmt_first != NULL)
1681 last_efm = vim_strsave(efm);
1682 }
1683
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
1687 /*
1688 * got_int is reset here, because it was probably set when killing the
1689 * ":make" command, but we still want to read the errorfile then.
1690 */
1691 got_int = FALSE;
1692
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 /*
1694 * Read the lines in the error file one by one.
1695 * Try to recognize one of the error formats in each line.
1696 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001697 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001699 /* Get the next line from a file/buffer/list/string */
1700 status = qf_get_nextline(&state);
1701 if (status == QF_NOMEM) /* memory alloc failure */
1702 goto qf_init_end;
1703 if (status == QF_END_OF_INPUT) /* end of input */
1704 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001706 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1707 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001708 if (status == QF_FAIL)
1709 goto error2;
1710 if (status == QF_NOMEM)
1711 goto qf_init_end;
1712 if (status == QF_IGNORE_LINE)
1713 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001715 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001716 qf_idx,
1717 qfl->qf_directory,
1718 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001719 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001720 : ((qfl->qf_currfile != NULL && fields.valid)
1721 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001722 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001723 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001724 fields.errmsg,
1725 fields.lnum,
1726 fields.col,
1727 fields.use_viscol,
1728 fields.pattern,
1729 fields.enr,
1730 fields.type,
1731 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 goto error2;
1733 line_breakcheck();
1734 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001735 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001737 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001739 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001740 qfl->qf_ptr = qfl->qf_start;
1741 qfl->qf_index = 1;
1742 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
1744 else
1745 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001746 qfl->qf_nonevalid = FALSE;
1747 if (qfl->qf_ptr == NULL)
1748 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001750 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001751 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001752 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
1754 EMSG(_(e_readerrf));
1755error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001756 if (!adding)
1757 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001758 /* Error when creating a new list. Free the new list */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001759 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001760 qi->qf_listcount--;
1761 if (qi->qf_curlist > 0)
1762 --qi->qf_curlist;
1763 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001764qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001765 if (qf_idx == qi->qf_curlist)
1766 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001767 qf_cleanup_state(&state);
1768 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
1770 return retval;
1771}
1772
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001773/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001774 * Read the errorfile "efile" into memory, line by line, building the error
1775 * list. Set the error list's title to qf_title.
1776 * Return -1 for error, number of errors for success.
1777 */
1778 int
1779qf_init(win_T *wp,
1780 char_u *efile,
1781 char_u *errorformat,
1782 int newlist, /* TRUE: start a new error list */
1783 char_u *qf_title,
1784 char_u *enc)
1785{
1786 qf_info_T *qi = &ql_info;
1787
1788 if (wp != NULL)
1789 {
1790 qi = ll_get_or_alloc_list(wp);
1791 if (qi == NULL)
1792 return FAIL;
1793 }
1794
1795 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1796 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1797}
1798
1799/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001800 * Set the title of the specified quickfix list. Frees the previous title.
1801 * Prepends ':' to the title.
1802 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001803 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001804qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001805{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001806 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001807
Bram Moolenaarfb604092014-07-23 15:55:00 +02001808 if (title != NULL)
1809 {
1810 char_u *p = alloc((int)STRLEN(title) + 2);
1811
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001812 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001813 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001814 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001815 }
1816}
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001819 * The title of a quickfix/location list is set, by default, to the command
1820 * that created the quickfix list with the ":" prefix.
1821 * Create a quickfix list title string by prepending ":" to a user command.
1822 * Returns a pointer to a static buffer with the title.
1823 */
1824 static char_u *
1825qf_cmdtitle(char_u *cmd)
1826{
1827 static char_u qftitle_str[IOSIZE];
1828
1829 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1830 return qftitle_str;
1831}
1832
1833/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001834 * Prepare for adding a new quickfix list. If the current list is in the
1835 * middle of the stack, then all the following lists are freed and then
1836 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 */
1838 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001839qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 int i;
1842
1843 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001844 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 * the current entry. This makes it possible to browse in a tree-like
1846 * way with ":grep'.
1847 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001848 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001849 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 /*
1852 * When the stack is full, remove to oldest entry
1853 * Otherwise, add a new entry.
1854 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001855 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001857 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001859 qi->qf_lists[i - 1] = qi->qf_lists[i];
1860 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
1862 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001863 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001864 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001865 qf_store_title(&qi->qf_lists[qi->qf_curlist], qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001866 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867}
1868
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001869/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001870 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001871 */
1872 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001873ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001874{
1875 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001876 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001877
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001878 qi = *pqi;
1879 if (qi == NULL)
1880 return;
1881 *pqi = NULL; /* Remove reference to this list */
1882
1883 qi->qf_refcount--;
1884 if (qi->qf_refcount < 1)
1885 {
1886 /* No references to this location list */
1887 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001888 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001889 vim_free(qi);
1890 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001891}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001892
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001893/*
1894 * Free all the quickfix/location lists in the stack.
1895 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001896 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001897qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001898{
1899 int i;
1900 qf_info_T *qi = &ql_info;
1901
1902 if (wp != NULL)
1903 {
1904 /* location list */
1905 ll_free_all(&wp->w_llist);
1906 ll_free_all(&wp->w_llist_ref);
1907 }
1908 else
1909 /* quickfix list */
1910 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001911 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001912}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001913
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914/*
1915 * Add an entry to the end of the list of errors.
1916 * Returns OK or FAIL.
1917 */
1918 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001919qf_add_entry(
1920 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001921 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001922 char_u *dir, /* optional directory name */
1923 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001924 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001925 int bufnum, /* buffer number or zero */
1926 char_u *mesg, /* message */
1927 long lnum, /* line number */
1928 int col, /* column */
1929 int vis_col, /* using visual column */
1930 char_u *pattern, /* search pattern */
1931 int nr, /* error number */
1932 int type, /* type character */
1933 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001935 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001936 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001937 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001939 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001941 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001942 {
1943 buf_T *buf = buflist_findnr(bufnum);
1944
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001945 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001946 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001947 buf->b_has_qf_entry |=
Bram Moolenaar4d77c652018-08-18 19:59:54 +02001948 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001949 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001950 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001951 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1953 {
1954 vim_free(qfp);
1955 return FAIL;
1956 }
1957 qfp->qf_lnum = lnum;
1958 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001959 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001960 if (pattern == NULL || *pattern == NUL)
1961 qfp->qf_pattern = NULL;
1962 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1963 {
1964 vim_free(qfp->qf_text);
1965 vim_free(qfp);
1966 return FAIL;
1967 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001968 if (module == NULL || *module == NUL)
1969 qfp->qf_module = NULL;
1970 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1971 {
1972 vim_free(qfp->qf_text);
1973 vim_free(qfp->qf_pattern);
1974 vim_free(qfp);
1975 return FAIL;
1976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 qfp->qf_nr = nr;
1978 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1979 type = 0;
1980 qfp->qf_type = type;
1981 qfp->qf_valid = valid;
1982
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001983 lastp = &qfl->qf_last;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001984 if (qf_list_empty(qi, qf_idx)) /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001986 qfl->qf_start = qfp;
1987 qfl->qf_ptr = qfp;
1988 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001989 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
1991 else
1992 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001993 qfp->qf_prev = *lastp;
1994 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001996 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001998 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001999 ++qfl->qf_count;
2000 if (qfl->qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002001 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002003 qfl->qf_index = qfl->qf_count;
2004 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
2006
2007 return OK;
2008}
2009
2010/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002011 * Allocate a new location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002012 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002013 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002014ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002015{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002016 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002018 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002019 if (qi != NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002020 qi->qf_refcount++;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002021 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002022}
2023
2024/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002025 * Return the location list stack for window 'wp'.
2026 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002027 */
2028 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002029ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002030{
2031 if (IS_LL_WINDOW(wp))
2032 /* For a location list window, use the referenced location list */
2033 return wp->w_llist_ref;
2034
2035 /*
2036 * For a non-location list window, w_llist_ref should not point to a
2037 * location list.
2038 */
2039 ll_free_all(&wp->w_llist_ref);
2040
2041 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002042 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002043 return wp->w_llist;
2044}
2045
2046/*
2047 * Copy the location list from window "from" to window "to".
2048 */
2049 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002050copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002051{
2052 qf_info_T *qi;
2053 int idx;
2054 int i;
2055
2056 /*
2057 * When copying from a location list window, copy the referenced
2058 * location list. For other windows, copy the location list for
2059 * that window.
2060 */
2061 if (IS_LL_WINDOW(from))
2062 qi = from->w_llist_ref;
2063 else
2064 qi = from->w_llist;
2065
2066 if (qi == NULL) /* no location list to copy */
2067 return;
2068
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002069 /* allocate a new location list */
2070 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002071 return;
2072
2073 to->w_llist->qf_listcount = qi->qf_listcount;
2074
2075 /* Copy the location lists one at a time */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002076 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002077 {
2078 qf_list_T *from_qfl;
2079 qf_list_T *to_qfl;
2080
2081 to->w_llist->qf_curlist = idx;
2082
2083 from_qfl = &qi->qf_lists[idx];
2084 to_qfl = &to->w_llist->qf_lists[idx];
2085
2086 /* Some of the fields are populated by qf_add_entry() */
2087 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2088 to_qfl->qf_count = 0;
2089 to_qfl->qf_index = 0;
2090 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002091 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002092 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002093 if (from_qfl->qf_title != NULL)
2094 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2095 else
2096 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002097 if (from_qfl->qf_ctx != NULL)
2098 {
2099 to_qfl->qf_ctx = alloc_tv();
2100 if (to_qfl->qf_ctx != NULL)
2101 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2102 }
2103 else
2104 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002105
2106 if (from_qfl->qf_count)
2107 {
2108 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002109 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002110
2111 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002112 for (i = 0, from_qfp = from_qfl->qf_start;
2113 i < from_qfl->qf_count && from_qfp != NULL;
2114 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002115 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002116 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02002117 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002118 NULL,
2119 NULL,
Bram Moolenaard76ce852018-05-01 15:02:04 +02002120 from_qfp->qf_module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002121 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002122 from_qfp->qf_text,
2123 from_qfp->qf_lnum,
2124 from_qfp->qf_col,
2125 from_qfp->qf_viscol,
2126 from_qfp->qf_pattern,
2127 from_qfp->qf_nr,
2128 0,
2129 from_qfp->qf_valid) == FAIL)
2130 {
2131 qf_free_all(to);
2132 return;
2133 }
2134 /*
2135 * qf_add_entry() will not set the qf_num field, as the
2136 * directory and file names are not supplied. So the qf_fnum
2137 * field is copied here.
2138 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002139 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002140 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
2141 prevp->qf_type = from_qfp->qf_type; /* error type */
2142 if (from_qfl->qf_ptr == from_qfp)
2143 to_qfl->qf_ptr = prevp; /* current location */
2144 }
2145 }
2146
2147 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
2148
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002149 /* Assign a new ID for the location list */
2150 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01002151 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002152
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002153 /* When no valid entries are present in the list, qf_ptr points to
2154 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02002155 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002156 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002157 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002158 to_qfl->qf_index = 1;
2159 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002160 }
2161
2162 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
2163}
2164
2165/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002166 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002167 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 */
2169 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002170qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
Bram Moolenaar82404332016-07-10 17:00:38 +02002172 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002173 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002174 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002175
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (fname == NULL || *fname == NUL) /* no file name */
2177 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
Bram Moolenaare60acc12011-05-10 16:41:25 +02002179#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002180 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002181#endif
2182#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002183 if (directory != NULL)
2184 slash_adjust(directory);
2185 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002186#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002187 if (directory != NULL && !vim_isAbsName(fname)
2188 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2189 {
2190 /*
2191 * Here we check if the file really exists.
2192 * This should normally be true, but if make works without
2193 * "leaving directory"-messages we might have missed a
2194 * directory change.
2195 */
2196 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 vim_free(ptr);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002199 directory = qf_guess_filepath(&qi->qf_lists[qf_idx], fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002200 if (directory)
2201 ptr = concat_fnames(directory, fname, TRUE);
2202 else
2203 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002205 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02002206 bufname = ptr;
2207 }
2208 else
2209 bufname = fname;
2210
2211 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002212 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002213 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002214 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002215 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002217 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002218 {
2219 vim_free(qf_last_bufname);
2220 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2221 if (bufname == ptr)
2222 qf_last_bufname = bufname;
2223 else
2224 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002225 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002226 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002227 if (buf == NULL)
2228 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002229
Bram Moolenaarc1542742016-07-20 21:44:37 +02002230 buf->b_has_qf_entry =
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002231 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002232 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233}
2234
2235/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002236 * Push dirbuf onto the directory stack and return pointer to actual dir or
2237 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 */
2239 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002240qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
2242 struct dir_stack_T *ds_new;
2243 struct dir_stack_T *ds_ptr;
2244
2245 /* allocate new stack element and hook it in */
2246 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2247 if (ds_new == NULL)
2248 return NULL;
2249
2250 ds_new->next = *stackptr;
2251 *stackptr = ds_new;
2252
2253 /* store directory on the stack */
2254 if (vim_isAbsName(dirbuf)
2255 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002256 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 (*stackptr)->dirname = vim_strsave(dirbuf);
2258 else
2259 {
2260 /* Okay we don't have an absolute path.
2261 * dirbuf must be a subdir of one of the directories on the stack.
2262 * Let's search...
2263 */
2264 ds_new = (*stackptr)->next;
2265 (*stackptr)->dirname = NULL;
2266 while (ds_new)
2267 {
2268 vim_free((*stackptr)->dirname);
2269 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2270 TRUE);
2271 if (mch_isdir((*stackptr)->dirname) == TRUE)
2272 break;
2273
2274 ds_new = ds_new->next;
2275 }
2276
2277 /* clean up all dirs we already left */
2278 while ((*stackptr)->next != ds_new)
2279 {
2280 ds_ptr = (*stackptr)->next;
2281 (*stackptr)->next = (*stackptr)->next->next;
2282 vim_free(ds_ptr->dirname);
2283 vim_free(ds_ptr);
2284 }
2285
2286 /* Nothing found -> it must be on top level */
2287 if (ds_new == NULL)
2288 {
2289 vim_free((*stackptr)->dirname);
2290 (*stackptr)->dirname = vim_strsave(dirbuf);
2291 }
2292 }
2293
2294 if ((*stackptr)->dirname != NULL)
2295 return (*stackptr)->dirname;
2296 else
2297 {
2298 ds_ptr = *stackptr;
2299 *stackptr = (*stackptr)->next;
2300 vim_free(ds_ptr);
2301 return NULL;
2302 }
2303}
2304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305/*
2306 * pop dirbuf from the directory stack and return previous directory or NULL if
2307 * stack is empty
2308 */
2309 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002310qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
2312 struct dir_stack_T *ds_ptr;
2313
2314 /* TODO: Should we check if dirbuf is the directory on top of the stack?
2315 * What to do if it isn't? */
2316
2317 /* pop top element and free it */
2318 if (*stackptr != NULL)
2319 {
2320 ds_ptr = *stackptr;
2321 *stackptr = (*stackptr)->next;
2322 vim_free(ds_ptr->dirname);
2323 vim_free(ds_ptr);
2324 }
2325
2326 /* return NEW top element as current dir or NULL if stack is empty*/
2327 return *stackptr ? (*stackptr)->dirname : NULL;
2328}
2329
2330/*
2331 * clean up directory stack
2332 */
2333 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002334qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336 struct dir_stack_T *ds_ptr;
2337
2338 while ((ds_ptr = *stackptr) != NULL)
2339 {
2340 *stackptr = (*stackptr)->next;
2341 vim_free(ds_ptr->dirname);
2342 vim_free(ds_ptr);
2343 }
2344}
2345
2346/*
2347 * Check in which directory of the directory stack the given file can be
2348 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002349 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 * Cleans up intermediate directory entries.
2351 *
2352 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002353 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 * ./
2355 * ./aa
2356 * ./aa/bb
2357 * ./bb
2358 * ./bb/x.c
2359 * and make says:
2360 * making all in aa
2361 * making all in bb
2362 * x.c:9: Error
2363 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2364 * qf_guess_filepath will return NULL.
2365 */
2366 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002367qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368{
2369 struct dir_stack_T *ds_ptr;
2370 struct dir_stack_T *ds_tmp;
2371 char_u *fullname;
2372
2373 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002374 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 return NULL;
2376
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002377 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 fullname = NULL;
2379 while (ds_ptr)
2380 {
2381 vim_free(fullname);
2382 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2383
2384 /* If concat_fnames failed, just go on. The worst thing that can happen
2385 * is that we delete the entire stack.
2386 */
2387 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2388 break;
2389
2390 ds_ptr = ds_ptr->next;
2391 }
2392
2393 vim_free(fullname);
2394
2395 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002396 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002398 ds_tmp = qfl->qf_dir_stack->next;
2399 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 vim_free(ds_tmp->dirname);
2401 vim_free(ds_tmp);
2402 }
2403
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002404 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405}
2406
2407/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002408 * Returns TRUE if a quickfix/location list with the given identifier exists.
2409 */
2410 static int
2411qflist_valid (win_T *wp, int_u qf_id)
2412{
2413 qf_info_T *qi = &ql_info;
2414 int i;
2415
2416 if (wp != NULL)
2417 {
2418 qi = GET_LOC_LIST(wp); /* Location list */
2419 if (qi == NULL)
2420 return FALSE;
2421 }
2422
2423 for (i = 0; i < qi->qf_listcount; ++i)
2424 if (qi->qf_lists[i].qf_id == qf_id)
2425 return TRUE;
2426
2427 return FALSE;
2428}
2429
2430/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002431 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002432 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002433 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002434 * Similar to location list.
2435 */
2436 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002437is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002438{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002439 qfline_T *qfp;
2440 int i;
2441
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002442 /* Search for the entry in the current list */
2443 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2444 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002445 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002446 break;
2447
2448 if (i == qfl->qf_count) /* Entry is not found */
2449 return FALSE;
2450
2451 return TRUE;
2452}
2453
2454/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002455 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002456 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002457 */
2458 static qfline_T *
2459get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002460 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002461 qfline_T *qf_ptr,
2462 int *qf_index,
2463 int dir)
2464{
2465 int idx;
2466 int old_qf_fnum;
2467
2468 idx = *qf_index;
2469 old_qf_fnum = qf_ptr->qf_fnum;
2470
2471 do
2472 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002473 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002474 return NULL;
2475 ++idx;
2476 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002477 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002478 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2479
2480 *qf_index = idx;
2481 return qf_ptr;
2482}
2483
2484/*
2485 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002486 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002487 */
2488 static qfline_T *
2489get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002490 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002491 qfline_T *qf_ptr,
2492 int *qf_index,
2493 int dir)
2494{
2495 int idx;
2496 int old_qf_fnum;
2497
2498 idx = *qf_index;
2499 old_qf_fnum = qf_ptr->qf_fnum;
2500
2501 do
2502 {
2503 if (idx == 1 || qf_ptr->qf_prev == NULL)
2504 return NULL;
2505 --idx;
2506 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002507 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002508 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2509
2510 *qf_index = idx;
2511 return qf_ptr;
2512}
2513
2514/*
2515 * Get the n'th (errornr) previous/next valid entry from the current entry in
2516 * the quickfix list.
2517 * dir == FORWARD or FORWARD_FILE: next valid entry
2518 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2519 */
2520 static qfline_T *
2521get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002522 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002523 int errornr,
2524 qfline_T *qf_ptr,
2525 int *qf_index,
2526 int dir)
2527{
2528 qfline_T *prev_qf_ptr;
2529 int prev_index;
2530 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2531 char_u *err = e_no_more_items;
2532
2533 while (errornr--)
2534 {
2535 prev_qf_ptr = qf_ptr;
2536 prev_index = *qf_index;
2537
2538 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002539 qf_ptr = get_next_valid_entry(qfl, qf_ptr, qf_index, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002540 else
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002541 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, qf_index, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002542 if (qf_ptr == NULL)
2543 {
2544 qf_ptr = prev_qf_ptr;
2545 *qf_index = prev_index;
2546 if (err != NULL)
2547 {
2548 EMSG(_(err));
2549 return NULL;
2550 }
2551 break;
2552 }
2553
2554 err = NULL;
2555 }
2556
2557 return qf_ptr;
2558}
2559
2560/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002561 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002562 */
2563 static qfline_T *
2564get_nth_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002565 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002566 int errornr,
2567 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002568 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002569{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002570 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002571
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002572 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002573 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2574 {
2575 --qf_idx;
2576 qf_ptr = qf_ptr->qf_prev;
2577 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002578 /* New error number is greater than the current error number */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002579 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2580 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002581 {
2582 ++qf_idx;
2583 qf_ptr = qf_ptr->qf_next;
2584 }
2585
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002586 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002587 return qf_ptr;
2588}
2589
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002590/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002591 * Find a window displaying a Vim help file.
2592 */
2593 static win_T *
2594qf_find_help_win(void)
2595{
2596 win_T *wp;
2597
2598 FOR_ALL_WINDOWS(wp)
2599 if (bt_help(wp->w_buffer))
2600 return wp;
2601
2602 return NULL;
2603}
2604
2605/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002606 * Find a help window or open one.
2607 */
2608 static int
2609jump_to_help_window(qf_info_T *qi, int *opened_window)
2610{
2611 win_T *wp;
2612 int flags;
2613
2614 if (cmdmod.tab != 0)
2615 wp = NULL;
2616 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002617 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002618 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2619 win_enter(wp, TRUE);
2620 else
2621 {
2622 /*
2623 * Split off help window; put it at far top if no position
2624 * specified, the current window is vertically split and narrow.
2625 */
2626 flags = WSP_HELP;
2627 if (cmdmod.split == 0 && curwin->w_width != Columns
2628 && curwin->w_width < 80)
2629 flags |= WSP_TOP;
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002630 if (IS_LL_STACK(qi))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002631 flags |= WSP_NEWLOC; /* don't copy the location list */
2632
2633 if (win_split(0, flags) == FAIL)
2634 return FAIL;
2635
2636 *opened_window = TRUE;
2637
2638 if (curwin->w_height < p_hh)
2639 win_setheight((int)p_hh);
2640
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002641 if (IS_LL_STACK(qi)) // not a quickfix list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002642 {
2643 /* The new window should use the supplied location list */
2644 curwin->w_llist = qi;
2645 qi->qf_refcount++;
2646 }
2647 }
2648
2649 if (!p_im)
2650 restart_edit = 0; /* don't want insert mode in help file */
2651
2652 return OK;
2653}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002654
2655/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002656 * Find a non-quickfix window using the given location list.
2657 * Returns NULL if a matching window is not found.
2658 */
2659 static win_T *
2660qf_find_win_with_loclist(qf_info_T *ll)
2661{
2662 win_T *wp;
2663
2664 FOR_ALL_WINDOWS(wp)
2665 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2666 return wp;
2667
2668 return NULL;
2669}
2670
2671/*
2672 * Find a window containing a normal buffer
2673 */
2674 static win_T *
2675qf_find_win_with_normal_buf(void)
2676{
2677 win_T *wp;
2678
2679 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002680 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002681 return wp;
2682
2683 return NULL;
2684}
2685
2686/*
2687 * Go to a window in any tabpage containing the specified file. Returns TRUE
2688 * if successfully jumped to the window. Otherwise returns FALSE.
2689 */
2690 static int
2691qf_goto_tabwin_with_file(int fnum)
2692{
2693 tabpage_T *tp;
2694 win_T *wp;
2695
2696 FOR_ALL_TAB_WINDOWS(tp, wp)
2697 if (wp->w_buffer->b_fnum == fnum)
2698 {
2699 goto_tabpage_win(tp, wp);
2700 return TRUE;
2701 }
2702
2703 return FALSE;
2704}
2705
2706/*
2707 * Create a new window to show a file above the quickfix window. Called when
2708 * only the quickfix window is present.
2709 */
2710 static int
2711qf_open_new_file_win(qf_info_T *ll_ref)
2712{
2713 int flags;
2714
2715 flags = WSP_ABOVE;
2716 if (ll_ref != NULL)
2717 flags |= WSP_NEWLOC;
2718 if (win_split(0, flags) == FAIL)
2719 return FAIL; /* not enough room for window */
2720 p_swb = empty_option; /* don't split again */
2721 swb_flags = 0;
2722 RESET_BINDING(curwin);
2723 if (ll_ref != NULL)
2724 {
2725 /* The new window should use the location list from the
2726 * location list window */
2727 curwin->w_llist = ll_ref;
2728 ll_ref->qf_refcount++;
2729 }
2730 return OK;
2731}
2732
2733/*
2734 * Go to a window that shows the right buffer. If the window is not found, go
2735 * to the window just above the location list window. This is used for opening
2736 * a file from a location window and not from a quickfix window. If some usable
2737 * window is previously found, then it is supplied in 'use_win'.
2738 */
2739 static void
2740qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2741{
2742 win_T *win = use_win;
2743
2744 if (win == NULL)
2745 {
2746 /* Find the window showing the selected file */
2747 FOR_ALL_WINDOWS(win)
2748 if (win->w_buffer->b_fnum == qf_fnum)
2749 break;
2750 if (win == NULL)
2751 {
2752 /* Find a previous usable window */
2753 win = curwin;
2754 do
2755 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002756 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002757 break;
2758 if (win->w_prev == NULL)
2759 win = lastwin; /* wrap around the top */
2760 else
2761 win = win->w_prev; /* go to previous window */
2762 } while (win != curwin);
2763 }
2764 }
2765 win_goto(win);
2766
2767 /* If the location list for the window is not set, then set it
2768 * to the location list from the location window */
2769 if (win->w_llist == NULL)
2770 {
2771 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002772 if (ll_ref != NULL)
2773 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002774 }
2775}
2776
2777/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002778 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2779 * not found, then go to the window just above the quickfix window. This is
2780 * used for opening a file from a quickfix window and not from a location
2781 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002782 */
2783 static void
2784qf_goto_win_with_qfl_file(int qf_fnum)
2785{
2786 win_T *win;
2787 win_T *altwin;
2788
2789 win = curwin;
2790 altwin = NULL;
2791 for (;;)
2792 {
2793 if (win->w_buffer->b_fnum == qf_fnum)
2794 break;
2795 if (win->w_prev == NULL)
2796 win = lastwin; /* wrap around the top */
2797 else
2798 win = win->w_prev; /* go to previous window */
2799
2800 if (IS_QF_WINDOW(win))
2801 {
2802 /* Didn't find it, go to the window before the quickfix
2803 * window. */
2804 if (altwin != NULL)
2805 win = altwin;
2806 else if (curwin->w_prev != NULL)
2807 win = curwin->w_prev;
2808 else
2809 win = curwin->w_next;
2810 break;
2811 }
2812
2813 /* Remember a usable window. */
Bram Moolenaar91335e52018-08-01 17:53:12 +02002814 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002815 altwin = win;
2816 }
2817
2818 win_goto(win);
2819}
2820
2821/*
2822 * Find a suitable window for opening a file (qf_fnum) from the
2823 * quickfix/location list and jump to it. If the file is already opened in a
2824 * window, jump to it. Otherwise open a new window to display the file. This is
2825 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002826 */
2827 static int
2828qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2829{
2830 win_T *usable_win_ptr = NULL;
2831 int usable_win;
2832 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002833 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002834
2835 usable_win = 0;
2836
2837 ll_ref = curwin->w_llist_ref;
2838 if (ll_ref != NULL)
2839 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002840 /* Find a non-quickfix window with this location list */
2841 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2842 if (usable_win_ptr != NULL)
2843 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002844 }
2845
2846 if (!usable_win)
2847 {
2848 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002849 win = qf_find_win_with_normal_buf();
2850 if (win != NULL)
2851 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002852 }
2853
2854 /*
2855 * If no usable window is found and 'switchbuf' contains "usetab"
2856 * then search in other tabs.
2857 */
2858 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002859 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002860
2861 /*
2862 * If there is only one window and it is the quickfix window, create a
2863 * new one above the quickfix window.
2864 */
2865 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2866 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002867 if (qf_open_new_file_win(ll_ref) != OK)
2868 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002869 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002870 }
2871 else
2872 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002873 if (curwin->w_llist_ref != NULL) /* In a location window */
2874 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2875 else /* In a quickfix window */
2876 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002877 }
2878
2879 return OK;
2880}
2881
2882/*
2883 * Edit the selected file or help file.
2884 */
2885 static int
2886qf_jump_edit_buffer(
2887 qf_info_T *qi,
2888 qfline_T *qf_ptr,
2889 int forceit,
2890 win_T *oldwin,
2891 int *opened_window,
2892 int *abort)
2893{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002894 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002895 int retval = OK;
2896
2897 if (qf_ptr->qf_type == 1)
2898 {
2899 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2900 * set b_p_ro flag). */
2901 if (!can_abandon(curbuf, forceit))
2902 {
2903 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002904 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002905 }
2906 else
2907 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2908 ECMD_HIDE + ECMD_SET_HELP,
2909 oldwin == curwin ? curwin : NULL);
2910 }
2911 else
2912 {
2913 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002914 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002915
2916 retval = buflist_getfile(qf_ptr->qf_fnum,
2917 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002918
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002919 if (IS_LL_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002920 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002921 /*
2922 * Location list. Check whether the associated window is still
2923 * present and the list is still valid.
2924 */
2925 if (!win_valid_any_tab(oldwin))
2926 {
2927 EMSG(_("E924: Current window was closed"));
2928 *abort = TRUE;
2929 *opened_window = FALSE;
2930 }
2931 else if (!qflist_valid(oldwin, save_qfid))
2932 {
2933 EMSG(_(e_loc_list_changed));
2934 *abort = TRUE;
2935 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002936 }
2937 else if (old_qf_curlist != qi->qf_curlist
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002938 || !is_qf_entry_present(qfl, qf_ptr))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002939 {
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002940 if (IS_QF_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002941 EMSG(_("E925: Current quickfix was changed"));
2942 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002943 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002944 *abort = TRUE;
2945 }
2946
2947 if (*abort)
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002948 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002949 }
2950
2951 return retval;
2952}
2953
2954/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002955 * Go to the error line in the current file using either line/column number or
2956 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002957 */
2958 static void
2959qf_jump_goto_line(
2960 linenr_T qf_lnum,
2961 int qf_col,
2962 char_u qf_viscol,
2963 char_u *qf_pattern)
2964{
2965 linenr_T i;
2966 char_u *line;
2967 colnr_T screen_col;
2968 colnr_T char_col;
2969
2970 if (qf_pattern == NULL)
2971 {
2972 /*
2973 * Go to line with error, unless qf_lnum is 0.
2974 */
2975 i = qf_lnum;
2976 if (i > 0)
2977 {
2978 if (i > curbuf->b_ml.ml_line_count)
2979 i = curbuf->b_ml.ml_line_count;
2980 curwin->w_cursor.lnum = i;
2981 }
2982 if (qf_col > 0)
2983 {
2984 curwin->w_cursor.col = qf_col - 1;
2985#ifdef FEAT_VIRTUALEDIT
2986 curwin->w_cursor.coladd = 0;
2987#endif
2988 if (qf_viscol == TRUE)
2989 {
2990 /*
2991 * Check each character from the beginning of the error
2992 * line up to the error column. For each tab character
2993 * found, reduce the error column value by the length of
2994 * a tab character.
2995 */
2996 line = ml_get_curline();
2997 screen_col = 0;
2998 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2999 {
3000 if (*line == NUL)
3001 break;
3002 if (*line++ == '\t')
3003 {
3004 curwin->w_cursor.col -= 7 - (screen_col % 8);
3005 screen_col += 8 - (screen_col % 8);
3006 }
3007 else
3008 ++screen_col;
3009 }
3010 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003011 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003012 check_cursor();
3013 }
3014 else
3015 beginline(BL_WHITE | BL_FIX);
3016 }
3017 else
3018 {
3019 pos_T save_cursor;
3020
3021 /* Move the cursor to the first line in the buffer */
3022 save_cursor = curwin->w_cursor;
3023 curwin->w_cursor.lnum = 0;
3024 if (!do_search(NULL, '/', qf_pattern, (long)1,
3025 SEARCH_KEEP, NULL, NULL))
3026 curwin->w_cursor = save_cursor;
3027 }
3028}
3029
3030/*
3031 * Display quickfix list index and size message
3032 */
3033 static void
3034qf_jump_print_msg(
3035 qf_info_T *qi,
3036 int qf_index,
3037 qfline_T *qf_ptr,
3038 buf_T *old_curbuf,
3039 linenr_T old_lnum)
3040{
3041 linenr_T i;
3042 int len;
3043
3044 /* Update the screen before showing the message, unless the screen
3045 * scrolled up. */
3046 if (!msg_scrolled)
3047 update_topline_redraw();
3048 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3049 qi->qf_lists[qi->qf_curlist].qf_count,
3050 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3051 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
3052 /* Add the message, skipping leading whitespace and newlines. */
3053 len = (int)STRLEN(IObuff);
3054 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3055
3056 /* Output the message. Overwrite to avoid scrolling when the 'O'
3057 * flag is present in 'shortmess'; But when not jumping, print the
3058 * whole message. */
3059 i = msg_scroll;
3060 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3061 msg_scroll = TRUE;
3062 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3063 msg_scroll = FALSE;
3064 msg_attr_keep(IObuff, 0, TRUE);
3065 msg_scroll = i;
3066}
3067
3068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 * jump to a quickfix line
3070 * if dir == FORWARD go "errornr" valid entries forward
3071 * if dir == BACKWARD go "errornr" valid entries backward
3072 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3073 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3074 * else if "errornr" is zero, redisplay the same line
3075 * else go to entry "errornr"
3076 */
3077 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003078qf_jump(qf_info_T *qi,
3079 int dir,
3080 int errornr,
3081 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003083 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003084 qfline_T *qf_ptr;
3085 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 buf_T *old_curbuf;
3089 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003090 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003091 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003093 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#ifdef FEAT_FOLDING
3096 int old_KeyTyped = KeyTyped; /* getting file may reset it */
3097#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003098 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003100 if (qi == NULL)
3101 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003102
3103 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003104 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
3106 EMSG(_(e_quickfix));
3107 return;
3108 }
3109
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003110 qfl = &qi->qf_lists[qi->qf_curlist];
3111
3112 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003114 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02003116 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003118 qf_ptr = get_nth_valid_entry(qfl, errornr, qf_ptr, &qf_index, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003119 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003121 qf_ptr = old_qf_ptr;
3122 qf_index = old_qf_index;
3123 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 }
3125 }
3126 else if (errornr != 0) /* go to specified number */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003127 qf_ptr = get_nth_entry(qfl, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003129 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003130 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 /* No need to print the error message if it's visible in the error
3132 * window */
3133 print_message = FALSE;
3134
3135 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003136 * For ":helpgrep" find a help window or open one.
3137 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003138 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003139 if (jump_to_help_window(qi, &opened_window) == FAIL)
3140 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003141
3142 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 * If currently in the quickfix window, find another window to show the
3144 * file in.
3145 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003146 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 /*
3149 * If there is no file specified, we don't know where to go.
3150 * But do advance, otherwise ":cn" gets stuck.
3151 */
3152 if (qf_ptr->qf_fnum == 0)
3153 goto theend;
3154
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003155 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
3156 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
3159 /*
3160 * If there is a file name,
3161 * read the wanted file if needed, and check autowrite etc.
3162 */
3163 old_curbuf = curbuf;
3164 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003165
3166 if (qf_ptr->qf_fnum != 0)
3167 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003168 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003169
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003170 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3171 &opened_window, &abort);
3172 if (abort)
3173 {
3174 qi = NULL;
3175 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003176 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003177 }
3178
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003179 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 {
3181 /* When not switched to another buffer, still need to set pc mark */
3182 if (curbuf == old_curbuf)
3183 setpcmark();
3184
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003185 if (qf_ptr != NULL)
3186 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col,
3187 qf_ptr->qf_viscol, qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189#ifdef FEAT_FOLDING
3190 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
3191 foldOpenCursor();
3192#endif
3193 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003194 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196 else
3197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 if (opened_window)
3199 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01003200 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
3202 /*
3203 * Couldn't open file, so put index back where it was. This could
3204 * happen if the file was readonly and we changed something.
3205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 qf_ptr = old_qf_ptr;
3208 qf_index = old_qf_index;
3209 }
3210 }
3211theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003212 if (qi != NULL)
3213 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003214 qfl->qf_ptr = qf_ptr;
3215 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 if (p_swb != old_swb && opened_window)
3218 {
3219 /* Restore old 'switchbuf' value, but not when an autocommand or
3220 * modeline has changed the value. */
3221 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003224 swb_flags = old_swb_flags;
3225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 else
3227 free_string_option(old_swb);
3228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229}
3230
3231/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003232 * Highlight attributes used for displaying entries from the quickfix list.
3233 */
3234static int qfFileAttr;
3235static int qfSepAttr;
3236static int qfLineAttr;
3237
3238/*
3239 * Display information about a single entry from the quickfix/location list.
3240 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003241 * 'cursel' will be set to TRUE for the currently selected entry in the
3242 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003243 */
3244 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003245qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003246{
3247 char_u *fname;
3248 buf_T *buf;
3249 int filter_entry;
3250
3251 fname = NULL;
3252 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3253 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3254 (char *)qfp->qf_module);
3255 else {
3256 if (qfp->qf_fnum != 0
3257 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3258 {
3259 fname = buf->b_fname;
3260 if (qfp->qf_type == 1) /* :helpgrep */
3261 fname = gettail(fname);
3262 }
3263 if (fname == NULL)
3264 sprintf((char *)IObuff, "%2d", qf_idx);
3265 else
3266 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3267 qf_idx, (char *)fname);
3268 }
3269
3270 // Support for filtering entries using :filter /pat/ clist
3271 // Match against the module name, file name, search pattern and
3272 // text of the entry.
3273 filter_entry = TRUE;
3274 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3275 filter_entry &= message_filtered(qfp->qf_module);
3276 if (filter_entry && fname != NULL)
3277 filter_entry &= message_filtered(fname);
3278 if (filter_entry && qfp->qf_pattern != NULL)
3279 filter_entry &= message_filtered(qfp->qf_pattern);
3280 if (filter_entry)
3281 filter_entry &= message_filtered(qfp->qf_text);
3282 if (filter_entry)
3283 return;
3284
3285 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003286 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003287
3288 if (qfp->qf_lnum != 0)
3289 msg_puts_attr((char_u *)":", qfSepAttr);
3290 if (qfp->qf_lnum == 0)
3291 IObuff[0] = NUL;
3292 else if (qfp->qf_col == 0)
3293 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3294 else
3295 sprintf((char *)IObuff, "%ld col %d",
3296 qfp->qf_lnum, qfp->qf_col);
3297 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3298 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3299 msg_puts_attr(IObuff, qfLineAttr);
3300 msg_puts_attr((char_u *)":", qfSepAttr);
3301 if (qfp->qf_pattern != NULL)
3302 {
3303 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3304 msg_puts(IObuff);
3305 msg_puts_attr((char_u *)":", qfSepAttr);
3306 }
3307 msg_puts((char_u *)" ");
3308
3309 /* Remove newlines and leading whitespace from the text. For an
3310 * unrecognized line keep the indent, the compiler may mark a word
3311 * with ^^^^. */
3312 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3313 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3314 IObuff, IOSIZE);
3315 msg_prt_line(IObuff, FALSE);
3316 out_flush(); /* show one line at a time */
3317}
3318
3319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003321 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 */
3323 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003324qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003326 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003327 qfline_T *qfp;
3328 int i;
3329 int idx1 = 1;
3330 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003331 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003332 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003333 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003335 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
Bram Moolenaar39665952018-08-15 20:59:48 +02003337 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003338 {
3339 qi = GET_LOC_LIST(curwin);
3340 if (qi == NULL)
3341 {
3342 EMSG(_(e_loclist));
3343 return;
3344 }
3345 }
3346
3347 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003348 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
3350 EMSG(_(e_quickfix));
3351 return;
3352 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003353 if (*arg == '+')
3354 {
3355 ++arg;
3356 plus = TRUE;
3357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3359 {
3360 EMSG(_(e_trailing));
3361 return;
3362 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003363 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003364 if (plus)
3365 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003366 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003367 idx2 = i + idx1;
3368 idx1 = i;
3369 }
3370 else
3371 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003372 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003373 if (idx1 < 0)
3374 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3375 if (idx2 < 0)
3376 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
Bram Moolenaara796d462018-05-01 14:30:36 +02003379 /* Shorten all the file names, so that it is easy to read */
3380 shorten_fnames(FALSE);
3381
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003382 /*
3383 * Get the attributes for the different quickfix highlight items. Note
3384 * that this depends on syntax items defined in the qf.vim syntax file
3385 */
3386 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3387 if (qfFileAttr == 0)
3388 qfFileAttr = HL_ATTR(HLF_D);
3389 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3390 if (qfSepAttr == 0)
3391 qfSepAttr = HL_ATTR(HLF_D);
3392 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3393 if (qfLineAttr == 0)
3394 qfLineAttr = HL_ATTR(HLF_N);
3395
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003396 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003398 qfp = qfl->qf_start;
3399 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
3401 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3402 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003403 if (got_int)
3404 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003405
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003406 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003408
3409 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003410 if (qfp == NULL)
3411 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003412 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 ui_breakcheck();
3414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415}
3416
3417/*
3418 * Remove newlines and leading whitespace from an error message.
3419 * Put the result in "buf[bufsize]".
3420 */
3421 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003422qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
3424 int i;
3425 char_u *p = text;
3426
3427 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3428 {
3429 if (*p == '\n')
3430 {
3431 buf[i] = ' ';
3432 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003433 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 break;
3435 }
3436 else
3437 buf[i] = *p++;
3438 }
3439 buf[i] = NUL;
3440}
3441
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003442/*
3443 * Display information (list number, list size and the title) about a
3444 * quickfix/location list.
3445 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003446 static void
3447qf_msg(qf_info_T *qi, int which, char *lead)
3448{
3449 char *title = (char *)qi->qf_lists[which].qf_title;
3450 int count = qi->qf_lists[which].qf_count;
3451 char_u buf[IOSIZE];
3452
3453 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3454 lead,
3455 which + 1,
3456 qi->qf_listcount,
3457 count);
3458
3459 if (title != NULL)
3460 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003461 size_t len = STRLEN(buf);
3462
3463 if (len < 34)
3464 {
3465 vim_memset(buf + len, ' ', 34 - len);
3466 buf[34] = NUL;
3467 }
3468 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003469 }
3470 trunc_string(buf, buf, Columns - 1, IOSIZE);
3471 msg(buf);
3472}
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474/*
3475 * ":colder [count]": Up in the quickfix stack.
3476 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003477 * ":lolder [count]": Up in the location list stack.
3478 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 */
3480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003481qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003483 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 int count;
3485
Bram Moolenaar39665952018-08-15 20:59:48 +02003486 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003487 {
3488 qi = GET_LOC_LIST(curwin);
3489 if (qi == NULL)
3490 {
3491 EMSG(_(e_loclist));
3492 return;
3493 }
3494 }
3495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (eap->addr_count != 0)
3497 count = eap->line2;
3498 else
3499 count = 1;
3500 while (count--)
3501 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003502 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003504 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003507 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003509 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511 else
3512 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003513 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
3515 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003516 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003518 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
3520 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003521 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003522 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523}
3524
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003525/*
3526 * Display the information about all the quickfix/location lists in the stack
3527 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003528 void
3529qf_history(exarg_T *eap)
3530{
3531 qf_info_T *qi = &ql_info;
3532 int i;
3533
Bram Moolenaar39665952018-08-15 20:59:48 +02003534 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003535 qi = GET_LOC_LIST(curwin);
3536 if (qi == NULL || (qi->qf_listcount == 0
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003537 && qf_list_empty(qi, qi->qf_curlist)))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003538 MSG(_("No entries"));
3539 else
3540 for (i = 0; i < qi->qf_listcount; ++i)
3541 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3542}
3543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003545 * Free all the entries in the error list "idx". Note that other information
3546 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 */
3548 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003549qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003551 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003552 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003553 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003555 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003557 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003558 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003559 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003560 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003561 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003562 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003563 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003564 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003565 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003566 if (stop)
3567 /* Somehow qf_count may have an incorrect value, set it to 1
3568 * to avoid crashing when it's wrong.
3569 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003570 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003571 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003572 qfl->qf_start = qfpnext;
3573 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003575
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003576 qfl->qf_index = 0;
3577 qfl->qf_start = NULL;
3578 qfl->qf_last = NULL;
3579 qfl->qf_ptr = NULL;
3580 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003581
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003582 qf_clean_dir_stack(&qfl->qf_dir_stack);
3583 qfl->qf_directory = NULL;
3584 qf_clean_dir_stack(&qfl->qf_file_stack);
3585 qfl->qf_currfile = NULL;
3586 qfl->qf_multiline = FALSE;
3587 qfl->qf_multiignore = FALSE;
3588 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589}
3590
3591/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003592 * Free error list "idx". Frees all the entries in the quickfix list,
3593 * associated context information and the title.
3594 */
3595 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003596qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003597{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003598 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003599
Bram Moolenaard23a8232018-02-10 18:45:26 +01003600 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003601 free_tv(qfl->qf_ctx);
3602 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003603 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003604 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003605}
3606
3607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 * qf_mark_adjust: adjust marks
3609 */
3610 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003611qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003612 win_T *wp,
3613 linenr_T line1,
3614 linenr_T line2,
3615 long amount,
3616 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003618 int i;
3619 qfline_T *qfp;
3620 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003621 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003622 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003623 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
Bram Moolenaarc1542742016-07-20 21:44:37 +02003625 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003626 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003627 if (wp != NULL)
3628 {
3629 if (wp->w_llist == NULL)
3630 return;
3631 qi = wp->w_llist;
3632 }
3633
3634 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003635 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003636 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003637 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3638 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 if (qfp->qf_fnum == curbuf->b_fnum)
3640 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003641 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3643 {
3644 if (amount == MAXLNUM)
3645 qfp->qf_cleared = TRUE;
3646 else
3647 qfp->qf_lnum += amount;
3648 }
3649 else if (amount_after && qfp->qf_lnum > line2)
3650 qfp->qf_lnum += amount_after;
3651 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003652
3653 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003654 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655}
3656
3657/*
3658 * Make a nice message out of the error character and the error number:
3659 * char number message
3660 * e or E 0 " error"
3661 * w or W 0 " warning"
3662 * i or I 0 " info"
3663 * 0 0 ""
3664 * other 0 " c"
3665 * e or E n " error n"
3666 * w or W n " warning n"
3667 * i or I n " info n"
3668 * 0 n " error n"
3669 * other n " c n"
3670 * 1 x "" :helpgrep
3671 */
3672 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003673qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
3675 static char_u buf[20];
3676 static char_u cc[3];
3677 char_u *p;
3678
3679 if (c == 'W' || c == 'w')
3680 p = (char_u *)" warning";
3681 else if (c == 'I' || c == 'i')
3682 p = (char_u *)" info";
3683 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3684 p = (char_u *)" error";
3685 else if (c == 0 || c == 1)
3686 p = (char_u *)"";
3687 else
3688 {
3689 cc[0] = ' ';
3690 cc[1] = c;
3691 cc[2] = NUL;
3692 p = cc;
3693 }
3694
3695 if (nr <= 0)
3696 return p;
3697
3698 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3699 return buf;
3700}
3701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003703 * When "split" is FALSE: Open the entry/result under the cursor.
3704 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3705 */
3706 void
3707qf_view_result(int split)
3708{
3709 qf_info_T *qi = &ql_info;
3710
3711 if (!bt_quickfix(curbuf))
3712 return;
3713
3714 if (IS_LL_WINDOW(curwin))
3715 qi = GET_LOC_LIST(curwin);
3716
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003717 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003718 {
3719 EMSG(_(e_quickfix));
3720 return;
3721 }
3722
3723 if (split)
3724 {
3725 char_u cmd[32];
3726
3727 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3728 (long)curwin->w_cursor.lnum,
3729 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3730 if (do_cmdline_cmd(cmd) == OK)
3731 do_cmdline_cmd((char_u *) "clearjumps");
3732 return;
3733 }
3734
3735 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3736}
3737
3738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 * ":cwindow": open the quickfix window if we have errors to display,
3740 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003741 * ":lwindow": open the location list window if we have locations to display,
3742 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 */
3744 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003745ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003747 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 win_T *win;
3749
Bram Moolenaar39665952018-08-15 20:59:48 +02003750 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003751 {
3752 qi = GET_LOC_LIST(curwin);
3753 if (qi == NULL)
3754 return;
3755 }
3756
3757 /* Look for an existing quickfix window. */
3758 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
3760 /*
3761 * If a quickfix window is open but we have no errors to display,
3762 * close the window. If a quickfix window is not open, then open
3763 * it if we have errors; otherwise, leave it closed.
3764 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003765 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003766 || qf_list_empty(qi, qi->qf_curlist)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003767 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
3769 if (win != NULL)
3770 ex_cclose(eap);
3771 }
3772 else if (win == NULL)
3773 ex_copen(eap);
3774}
3775
3776/*
3777 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003778 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003781ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003783 win_T *win = NULL;
3784 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
Bram Moolenaar39665952018-08-15 20:59:48 +02003786 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003787 {
3788 qi = GET_LOC_LIST(curwin);
3789 if (qi == NULL)
3790 return;
3791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003793 /* Find existing quickfix window and close it. */
3794 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (win != NULL)
3796 win_close(win, FALSE);
3797}
3798
3799/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003800 * Set "w:quickfix_title" if "qi" has a title.
3801 */
3802 static void
3803qf_set_title_var(qf_list_T *qfl)
3804{
3805 if (qfl->qf_title != NULL)
3806 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3807}
3808
3809/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003810 * Goto a quickfix or location list window (if present).
3811 * Returns OK if the window is found, FAIL otherwise.
3812 */
3813 static int
3814qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3815{
3816 win_T *win;
3817
3818 win = qf_find_win(qi);
3819 if (win == NULL)
3820 return FAIL;
3821
3822 win_goto(win);
3823 if (resize)
3824 {
3825 if (vertsplit)
3826 {
3827 if (sz != win->w_width)
3828 win_setwidth(sz);
3829 }
3830 else if (sz != win->w_height)
3831 win_setheight(sz);
3832 }
3833
3834 return OK;
3835}
3836
3837/*
3838 * Open a new quickfix or location list window, load the quickfix buffer and
3839 * set the appropriate options for the window.
3840 * Returns FAIL if the window could not be opened.
3841 */
3842 static int
3843qf_open_new_cwindow(qf_info_T *qi, int height)
3844{
3845 buf_T *qf_buf;
3846 win_T *oldwin = curwin;
3847 tabpage_T *prevtab = curtab;
3848 int flags = 0;
3849 win_T *win;
3850
3851 qf_buf = qf_find_buf(qi);
3852
3853 // The current window becomes the previous window afterwards.
3854 win = curwin;
3855
3856 if (IS_QF_STACK(qi) && cmdmod.split == 0)
3857 // Create the new quickfix window at the very bottom, except when
3858 // :belowright or :aboveleft is used.
3859 win_goto(lastwin);
3860 // Default is to open the window below the current window
3861 if (cmdmod.split == 0)
3862 flags = WSP_BELOW;
3863 flags |= WSP_NEWLOC;
3864 if (win_split(height, flags) == FAIL)
3865 return FAIL; // not enough room for window
3866 RESET_BINDING(curwin);
3867
3868 if (IS_LL_STACK(qi))
3869 {
3870 // For the location list window, create a reference to the
3871 // location list from the window 'win'.
3872 curwin->w_llist_ref = win->w_llist;
3873 win->w_llist->qf_refcount++;
3874 }
3875
3876 if (oldwin != curwin)
3877 oldwin = NULL; // don't store info when in another window
3878 if (qf_buf != NULL)
3879 {
3880 // Use the existing quickfix buffer
3881 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
3882 ECMD_HIDE + ECMD_OLDBUF, oldwin);
3883 }
3884 else
3885 {
3886 // Create a new quickfix buffer
3887 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
3888
3889 // switch off 'swapfile'
3890 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3891 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
3892 OPT_LOCAL);
3893 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
3894 RESET_BINDING(curwin);
3895#ifdef FEAT_DIFF
3896 curwin->w_p_diff = FALSE;
3897#endif
3898#ifdef FEAT_FOLDING
3899 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3900 OPT_LOCAL);
3901#endif
3902 }
3903
3904 // Only set the height when still in the same tab page and there is no
3905 // window to the side.
3906 if (curtab == prevtab && curwin->w_width == Columns)
3907 win_setheight(height);
3908 curwin->w_p_wfh = TRUE; // set 'winfixheight'
3909 if (win_valid(win))
3910 prevwin = win;
3911
3912 return OK;
3913}
3914
3915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003917 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 */
3919 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003920ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003922 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003924 int status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
Bram Moolenaar39665952018-08-15 20:59:48 +02003926 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003927 {
3928 qi = GET_LOC_LIST(curwin);
3929 if (qi == NULL)
3930 {
3931 EMSG(_(e_loclist));
3932 return;
3933 }
3934 }
3935
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 if (eap->addr_count != 0)
3937 height = eap->line2;
3938 else
3939 height = QF_WINHEIGHT;
3940
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003941 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942#ifdef FEAT_GUI
3943 need_mouse_correct = TRUE;
3944#endif
3945
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003946 // Find an existing quickfix window, or open a new one.
3947 if (cmdmod.tab == 0)
3948 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
3949 cmdmod.split & WSP_VERT);
3950 if (status == FAIL)
3951 if (qf_open_new_cwindow(qi, height) == FAIL)
3952 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003954 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003955
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003956 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02003957 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003959 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 curwin->w_cursor.col = 0;
3961 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003962 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963}
3964
3965/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003966 * Move the cursor in the quickfix window to "lnum".
3967 */
3968 static void
3969qf_win_goto(win_T *win, linenr_T lnum)
3970{
3971 win_T *old_curwin = curwin;
3972
3973 curwin = win;
3974 curbuf = win->w_buffer;
3975 curwin->w_cursor.lnum = lnum;
3976 curwin->w_cursor.col = 0;
3977#ifdef FEAT_VIRTUALEDIT
3978 curwin->w_cursor.coladd = 0;
3979#endif
3980 curwin->w_curswant = 0;
3981 update_topline(); /* scroll to show the line */
3982 redraw_later(VALID);
3983 curwin->w_redr_status = TRUE; /* update ruler */
3984 curwin = old_curwin;
3985 curbuf = curwin->w_buffer;
3986}
3987
3988/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003989 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003990 */
3991 void
Bram Moolenaar39665952018-08-15 20:59:48 +02003992ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02003993{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003994 qf_info_T *qi = &ql_info;
3995 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003996
Bram Moolenaar39665952018-08-15 20:59:48 +02003997 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02003998 {
3999 qi = GET_LOC_LIST(curwin);
4000 if (qi == NULL)
4001 {
4002 EMSG(_(e_loclist));
4003 return;
4004 }
4005 }
4006
4007 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004008 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4009 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4010}
4011
4012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 * Return the number of the current entry (line number in the quickfix
4014 * window).
4015 */
4016 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004017qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004019 qf_info_T *qi = &ql_info;
4020
4021 if (IS_LL_WINDOW(wp))
4022 /* In the location list window, use the referenced location list */
4023 qi = wp->w_llist_ref;
4024
4025 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026}
4027
4028/*
4029 * Update the cursor position in the quickfix window to the current error.
4030 * Return TRUE if there is a quickfix window.
4031 */
4032 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004033qf_win_pos_update(
4034 qf_info_T *qi,
4035 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036{
4037 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004038 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
4040 /*
4041 * Put the cursor on the current error in the quickfix window, so that
4042 * it's viewable.
4043 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004044 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 if (win != NULL
4046 && qf_index <= win->w_buffer->b_ml.ml_line_count
4047 && old_qf_index != qf_index)
4048 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 if (qf_index > old_qf_index)
4050 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004051 win->w_redraw_top = old_qf_index;
4052 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
4054 else
4055 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004056 win->w_redraw_top = qf_index;
4057 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004059 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061 return win != NULL;
4062}
4063
4064/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004065 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004066 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004067 */
4068 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004069is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004070{
4071 /*
4072 * A window displaying the quickfix buffer will have the w_llist_ref field
4073 * set to NULL.
4074 * A window displaying a location list buffer will have the w_llist_ref
4075 * pointing to the location list.
4076 */
4077 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004078 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4079 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004080 return TRUE;
4081
4082 return FALSE;
4083}
4084
4085/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004086 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004087 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004088 */
4089 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004090qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004091{
4092 win_T *win;
4093
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004094 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004095 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004096 return win;
4097 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004098}
4099
4100/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004101 * Find a quickfix buffer.
4102 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 */
4104 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004105qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004107 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004108 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
Bram Moolenaar9c102382006-05-03 21:26:49 +00004110 FOR_ALL_TAB_WINDOWS(tp, win)
4111 if (is_qf_win(win, qi))
4112 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004113
Bram Moolenaar9c102382006-05-03 21:26:49 +00004114 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115}
4116
4117/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004118 * Update the w:quickfix_title variable in the quickfix/location list window
4119 */
4120 static void
4121qf_update_win_titlevar(qf_info_T *qi)
4122{
4123 win_T *win;
4124 win_T *curwin_save;
4125
4126 if ((win = qf_find_win(qi)) != NULL)
4127 {
4128 curwin_save = curwin;
4129 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004130 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004131 curwin = curwin_save;
4132 }
4133}
4134
4135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 * Find the quickfix buffer. If it exists, update the contents.
4137 */
4138 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004139qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140{
4141 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004142 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004146 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 if (buf != NULL)
4148 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004149 linenr_T old_line_count = buf->b_ml.ml_line_count;
4150
4151 if (old_last == NULL)
4152 /* set curwin/curbuf to buf and save a few things */
4153 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
Bram Moolenaard823fa92016-08-12 16:29:27 +02004155 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004156
Bram Moolenaar864293a2016-06-02 13:40:04 +02004157 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004158 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004159
Bram Moolenaar864293a2016-06-02 13:40:04 +02004160 if (old_last == NULL)
4161 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004162 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004163
4164 /* restore curwin/curbuf and a few other things */
4165 aucmd_restbuf(&aco);
4166 }
4167
4168 /* Only redraw when added lines are visible. This avoids flickering
4169 * when the added lines are not visible. */
4170 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4171 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173}
4174
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004175/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004176 * Add an error line to the quickfix buffer.
4177 */
4178 static int
4179qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4180{
4181 int len;
4182 buf_T *errbuf;
4183
4184 if (qfp->qf_module != NULL)
4185 {
4186 STRCPY(IObuff, qfp->qf_module);
4187 len = (int)STRLEN(IObuff);
4188 }
4189 else if (qfp->qf_fnum != 0
4190 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4191 && errbuf->b_fname != NULL)
4192 {
4193 if (qfp->qf_type == 1) /* :helpgrep */
4194 STRCPY(IObuff, gettail(errbuf->b_fname));
4195 else
4196 {
4197 /* shorten the file name if not done already */
4198 if (errbuf->b_sfname == NULL
4199 || mch_isFullName(errbuf->b_sfname))
4200 {
4201 if (*dirname == NUL)
4202 mch_dirname(dirname, MAXPATHL);
4203 shorten_buf_fname(errbuf, dirname, FALSE);
4204 }
4205 STRCPY(IObuff, errbuf->b_fname);
4206 }
4207 len = (int)STRLEN(IObuff);
4208 }
4209 else
4210 len = 0;
4211 IObuff[len++] = '|';
4212
4213 if (qfp->qf_lnum > 0)
4214 {
4215 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4216 len += (int)STRLEN(IObuff + len);
4217
4218 if (qfp->qf_col > 0)
4219 {
4220 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4221 len += (int)STRLEN(IObuff + len);
4222 }
4223
4224 sprintf((char *)IObuff + len, "%s",
4225 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4226 len += (int)STRLEN(IObuff + len);
4227 }
4228 else if (qfp->qf_pattern != NULL)
4229 {
4230 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4231 len += (int)STRLEN(IObuff + len);
4232 }
4233 IObuff[len++] = '|';
4234 IObuff[len++] = ' ';
4235
4236 /* Remove newlines and leading whitespace from the text.
4237 * For an unrecognized line keep the indent, the compiler may
4238 * mark a word with ^^^^. */
4239 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4240 IObuff + len, IOSIZE - len);
4241
4242 if (ml_append_buf(buf, lnum, IObuff,
4243 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4244 return FAIL;
4245
4246 return OK;
4247}
4248
4249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 * Fill current buffer with quickfix errors, replacing any previous contents.
4251 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004252 * If "old_last" is not NULL append the items after this one.
4253 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4254 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 */
4256 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004257qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004259 linenr_T lnum;
4260 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004261 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
Bram Moolenaar864293a2016-06-02 13:40:04 +02004263 if (old_last == NULL)
4264 {
4265 if (buf != curbuf)
4266 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004267 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004268 return;
4269 }
4270
4271 /* delete all existing lines */
4272 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4273 (void)ml_delete((linenr_T)1, FALSE);
4274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
4276 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004277 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 {
Bram Moolenaara796d462018-05-01 14:30:36 +02004279 char_u dirname[MAXPATHL];
4280
4281 *dirname = NUL;
4282
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004284 if (old_last == NULL)
4285 {
4286 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4287 lnum = 0;
4288 }
4289 else
4290 {
4291 qfp = old_last->qf_next;
4292 lnum = buf->b_ml.ml_line_count;
4293 }
4294 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004296 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004298
Bram Moolenaar864293a2016-06-02 13:40:04 +02004299 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004301 if (qfp == NULL)
4302 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004304
4305 if (old_last == NULL)
4306 /* Delete the empty line which is now at the end */
4307 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 }
4309
4310 /* correct cursor position */
4311 check_lnums(TRUE);
4312
Bram Moolenaar864293a2016-06-02 13:40:04 +02004313 if (old_last == NULL)
4314 {
4315 /* Set the 'filetype' to "qf" each time after filling the buffer.
4316 * This resembles reading a file into a buffer, it's more logical when
4317 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02004318 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004319 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4320 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
Bram Moolenaar864293a2016-06-02 13:40:04 +02004322 keep_filetype = TRUE; /* don't detect 'filetype' */
4323 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004325 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004327 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004328 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004329
Bram Moolenaar864293a2016-06-02 13:40:04 +02004330 /* make sure it will be redrawn */
4331 redraw_curbuf_later(NOT_VALID);
4332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
4334 /* Restore KeyTyped, setting 'filetype' may reset it. */
4335 KeyTyped = old_KeyTyped;
4336}
4337
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004338/*
4339 * For every change made to the quickfix list, update the changed tick.
4340 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004341 static void
4342qf_list_changed(qf_info_T *qi, int qf_idx)
4343{
4344 qi->qf_lists[qf_idx].qf_changedtick++;
4345}
4346
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004348 * Return the quickfix/location list number with the given identifier.
4349 * Returns -1 if list is not found.
4350 */
4351 static int
4352qf_id2nr(qf_info_T *qi, int_u qfid)
4353{
4354 int qf_idx;
4355
4356 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4357 if (qi->qf_lists[qf_idx].qf_id == qfid)
4358 return qf_idx;
4359 return INVALID_QFIDX;
4360}
4361
4362/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004363 * If the current list is not "save_qfid" and we can find the list with that ID
4364 * then make it the current list.
4365 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004366 * Returns OK if successfully restored the list. Returns FAIL if the list with
4367 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004368 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004369 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004370qf_restore_list(qf_info_T *qi, int_u save_qfid)
4371{
4372 int curlist;
4373
4374 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4375 {
4376 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004377 if (curlist < 0)
4378 // list is not present
4379 return FAIL;
4380 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004381 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004382 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004383}
4384
4385/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004386 * Jump to the first entry if there is one.
4387 */
4388 static void
4389qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4390{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004391 if (qf_restore_list(qi, save_qfid) == FAIL)
4392 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004393
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004394 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004395 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004396 qf_jump(qi, 0, 0, forceit);
4397}
4398
4399/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004400 * Return TRUE when using ":vimgrep" for ":grep".
4401 */
4402 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004403grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004404{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004405 return ((cmdidx == CMD_grep
4406 || cmdidx == CMD_lgrep
4407 || cmdidx == CMD_grepadd
4408 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004409 && STRCMP("internal",
4410 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4411}
4412
4413/*
Bram Moolenaara6557602006-02-04 22:43:20 +00004414 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 */
4416 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004417ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418{
Bram Moolenaar7c626922005-02-07 22:01:03 +00004419 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004421 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00004423 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004424 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004425 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004426 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004427 int_u save_qfid;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004428
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004429 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
4430 if (grep_internal(eap->cmdidx))
4431 {
4432 ex_vimgrep(eap);
4433 return;
4434 }
4435
Bram Moolenaar7c626922005-02-07 22:01:03 +00004436 switch (eap->cmdidx)
4437 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00004438 case CMD_make: au_name = (char_u *)"make"; break;
4439 case CMD_lmake: au_name = (char_u *)"lmake"; break;
4440 case CMD_grep: au_name = (char_u *)"grep"; break;
4441 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4442 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4443 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004444 default: break;
4445 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004446 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4447 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004448 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004449#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004450 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004451 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004452#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004453 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004454#ifdef FEAT_MBYTE
4455 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
Bram Moolenaar39665952018-08-15 20:59:48 +02004458 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00004459 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004460
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00004462 fname = get_mef_name();
4463 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004465 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467 /*
4468 * If 'shellpipe' empty: don't redirect to 'errorfile'.
4469 */
4470 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
4471 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00004472 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 cmd = alloc(len);
4474 if (cmd == NULL)
4475 return;
4476 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
4477 (char *)p_shq);
4478 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004479 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 /*
4481 * Output a newline if there's something else than the :make command that
4482 * was typed (in which case the cursor is in column 0).
4483 */
4484 if (msg_col == 0)
4485 msg_didout = FALSE;
4486 msg_start();
4487 MSG_PUTS(":!");
4488 msg_outtrans(cmd); /* show what we are doing */
4489
4490 /* let the shell know if we are redirecting output or not */
4491 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4492
4493#ifdef AMIGA
4494 out_flush();
4495 /* read window status report and redraw before message */
4496 (void)char_avail();
4497#endif
4498
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004499 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00004500 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4501 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004502 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004503 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004504 if (wp != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004505 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004506 qi = GET_LOC_LIST(wp);
4507 if (qi == NULL)
4508 goto cleanup;
4509 }
4510 if (res >= 0)
4511 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004512
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004513 // Remember the current quickfix list identifier, so that we can
4514 // check for autocommands changing the current quickfix list.
4515 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4516 if (au_name != NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004517 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4518 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004519 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004520 // display the first error
4521 qf_jump_first(qi, save_qfid, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004523cleanup:
Bram Moolenaar7c626922005-02-07 22:01:03 +00004524 mch_remove(fname);
4525 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 vim_free(cmd);
4527}
4528
4529/*
4530 * Return the name for the errorfile, in allocated memory.
4531 * Find a new unique name when 'makeef' contains "##".
4532 * Returns NULL for error.
4533 */
4534 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004535get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536{
4537 char_u *p;
4538 char_u *name;
4539 static int start = -1;
4540 static int off = 0;
4541#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004542 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543#endif
4544
4545 if (*p_mef == NUL)
4546 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004547 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (name == NULL)
4549 EMSG(_(e_notmp));
4550 return name;
4551 }
4552
4553 for (p = p_mef; *p; ++p)
4554 if (p[0] == '#' && p[1] == '#')
4555 break;
4556
4557 if (*p == NUL)
4558 return vim_strsave(p_mef);
4559
4560 /* Keep trying until the name doesn't exist yet. */
4561 for (;;)
4562 {
4563 if (start == -1)
4564 start = mch_get_pid();
4565 else
4566 off += 19;
4567
4568 name = alloc((unsigned)STRLEN(p_mef) + 30);
4569 if (name == NULL)
4570 break;
4571 STRCPY(name, p_mef);
4572 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4573 STRCAT(name, p + 2);
4574 if (mch_getperm(name) < 0
4575#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02004576 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 && mch_lstat((char *)name, &sb) < 0
4578#endif
4579 )
4580 break;
4581 vim_free(name);
4582 }
4583 return name;
4584}
4585
4586/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004587 * Returns the number of valid entries in the current quickfix/location list.
4588 */
4589 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004590qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004591{
4592 qf_info_T *qi = &ql_info;
4593 qfline_T *qfp;
4594 int i, sz = 0;
4595 int prev_fnum = 0;
4596
Bram Moolenaar39665952018-08-15 20:59:48 +02004597 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004598 {
4599 /* Location list */
4600 qi = GET_LOC_LIST(curwin);
4601 if (qi == NULL)
4602 return 0;
4603 }
4604
4605 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004606 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004607 ++i, qfp = qfp->qf_next)
4608 {
4609 if (qfp->qf_valid)
4610 {
4611 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4612 sz++; /* Count all valid entries */
4613 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4614 {
4615 /* Count the number of files */
4616 sz++;
4617 prev_fnum = qfp->qf_fnum;
4618 }
4619 }
4620 }
4621
4622 return sz;
4623}
4624
4625/*
4626 * Returns the current index of the quickfix/location list.
4627 * Returns 0 if there is an error.
4628 */
4629 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004630qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004631{
4632 qf_info_T *qi = &ql_info;
4633
Bram Moolenaar39665952018-08-15 20:59:48 +02004634 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004635 {
4636 /* Location list */
4637 qi = GET_LOC_LIST(curwin);
4638 if (qi == NULL)
4639 return 0;
4640 }
4641
4642 return qi->qf_lists[qi->qf_curlist].qf_index;
4643}
4644
4645/*
4646 * Returns the current index in the quickfix/location list (counting only valid
4647 * entries). If no valid entries are in the list, then returns 1.
4648 */
4649 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004650qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004651{
4652 qf_info_T *qi = &ql_info;
4653 qf_list_T *qfl;
4654 qfline_T *qfp;
4655 int i, eidx = 0;
4656 int prev_fnum = 0;
4657
Bram Moolenaar39665952018-08-15 20:59:48 +02004658 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004659 {
4660 /* Location list */
4661 qi = GET_LOC_LIST(curwin);
4662 if (qi == NULL)
4663 return 1;
4664 }
4665
4666 qfl = &qi->qf_lists[qi->qf_curlist];
4667 qfp = qfl->qf_start;
4668
4669 /* check if the list has valid errors */
4670 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4671 return 1;
4672
4673 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4674 {
4675 if (qfp->qf_valid)
4676 {
4677 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4678 {
4679 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4680 {
4681 /* Count the number of files */
4682 eidx++;
4683 prev_fnum = qfp->qf_fnum;
4684 }
4685 }
4686 else
4687 eidx++;
4688 }
4689 }
4690
4691 return eidx ? eidx : 1;
4692}
4693
4694/*
4695 * Get the 'n'th valid error entry in the quickfix or location list.
4696 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4697 * For :cdo and :ldo returns the 'n'th valid error entry.
4698 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4699 */
4700 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004701qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004702{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004703 qfline_T *qfp = qfl->qf_start;
4704 int i, eidx;
4705 int prev_fnum = 0;
4706
4707 /* check if the list has valid errors */
4708 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4709 return 1;
4710
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004711 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004712 i++, qfp = qfp->qf_next)
4713 {
4714 if (qfp->qf_valid)
4715 {
4716 if (fdo)
4717 {
4718 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4719 {
4720 /* Count the number of files */
4721 eidx++;
4722 prev_fnum = qfp->qf_fnum;
4723 }
4724 }
4725 else
4726 eidx++;
4727 }
4728
4729 if (eidx == n)
4730 break;
4731 }
4732
4733 if (i <= qfl->qf_count)
4734 return i;
4735 else
4736 return 1;
4737}
4738
4739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004741 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004742 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 */
4744 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004745ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004747 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004748 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004749
Bram Moolenaar39665952018-08-15 20:59:48 +02004750 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004751 {
4752 qi = GET_LOC_LIST(curwin);
4753 if (qi == NULL)
4754 {
4755 EMSG(_(e_loclist));
4756 return;
4757 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004758 }
4759
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004760 if (eap->addr_count > 0)
4761 errornr = (int)eap->line2;
4762 else
4763 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004764 switch (eap->cmdidx)
4765 {
4766 case CMD_cc: case CMD_ll:
4767 errornr = 0;
4768 break;
4769 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4770 case CMD_lfirst:
4771 errornr = 1;
4772 break;
4773 default:
4774 errornr = 32767;
4775 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004776 }
4777
4778 /* For cdo and ldo commands, jump to the nth valid error.
4779 * For cfdo and lfdo commands, jump to the nth valid file entry.
4780 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004781 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4782 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004783 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004784 eap->addr_count > 0 ? (int)eap->line1 : 1,
4785 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4786
4787 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788}
4789
4790/*
4791 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004792 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004793 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 */
4795 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004796ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004798 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004799 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004800 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004801
Bram Moolenaar39665952018-08-15 20:59:48 +02004802 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004803 {
4804 qi = GET_LOC_LIST(curwin);
4805 if (qi == NULL)
4806 {
4807 EMSG(_(e_loclist));
4808 return;
4809 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004810 }
4811
Bram Moolenaar55b69262017-08-13 13:42:01 +02004812 if (eap->addr_count > 0
4813 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4814 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004815 errornr = (int)eap->line2;
4816 else
4817 errornr = 1;
4818
Bram Moolenaar39665952018-08-15 20:59:48 +02004819 // Depending on the command jump to either next or previous entry/file.
4820 switch (eap->cmdidx)
4821 {
4822 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4823 dir = FORWARD;
4824 break;
4825 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4826 case CMD_lNext:
4827 dir = BACKWARD;
4828 break;
4829 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4830 dir = FORWARD_FILE;
4831 break;
4832 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4833 dir = BACKWARD_FILE;
4834 break;
4835 default:
4836 dir = FORWARD;
4837 break;
4838 }
4839
4840 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841}
4842
4843/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004844 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004845 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 */
4847 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004848ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004850 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004851 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004852 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004853 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004854 int_u save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004855 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004856
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004857 switch (eap->cmdidx)
4858 {
4859 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4860 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4861 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4862 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4863 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4864 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4865 default: break;
4866 }
4867 if (au_name != NULL)
4868 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004869#ifdef FEAT_MBYTE
4870 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4871#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004872#ifdef FEAT_BROWSE
4873 if (cmdmod.browse)
4874 {
4875 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004876 NULL, NULL,
4877 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004878 if (browse_file == NULL)
4879 return;
4880 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4881 vim_free(browse_file);
4882 }
4883 else
4884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004886 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004887
Bram Moolenaar39665952018-08-15 20:59:48 +02004888 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004889 wp = curwin;
4890
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004891 /*
4892 * This function is used by the :cfile, :cgetfile and :caddfile
4893 * commands.
4894 * :cfile always creates a new quickfix list and jumps to the
4895 * first error.
4896 * :cgetfile creates a new quickfix list but doesn't jump to the
4897 * first error.
4898 * :caddfile adds to an existing quickfix list. If there is no
4899 * quickfix list then a new list is created.
4900 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004901 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004902 && eap->cmdidx != CMD_laddfile),
4903 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004904 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004905 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01004906 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004907 if (qi == NULL)
4908 return;
4909 }
4910 if (res >= 0)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004911 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004912 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004913 if (au_name != NULL)
4914 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004915
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004916 // Jump to the first error for a new list and if autocmds didn't
4917 // free the list.
4918 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
4919 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004920 // display the first error
4921 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004922}
4923
4924/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004925 * Return the vimgrep autocmd name.
4926 */
4927 static char_u *
4928vgr_get_auname(cmdidx_T cmdidx)
4929{
4930 switch (cmdidx)
4931 {
4932 case CMD_vimgrep: return (char_u *)"vimgrep";
4933 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4934 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4935 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4936 case CMD_grep: return (char_u *)"grep";
4937 case CMD_lgrep: return (char_u *)"lgrep";
4938 case CMD_grepadd: return (char_u *)"grepadd";
4939 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4940 default: return NULL;
4941 }
4942}
4943
4944/*
4945 * Initialize the regmatch used by vimgrep for pattern "s".
4946 */
4947 static void
4948vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4949{
4950 /* Get the search pattern: either white-separated or enclosed in // */
4951 regmatch->regprog = NULL;
4952
4953 if (s == NULL || *s == NUL)
4954 {
4955 /* Pattern is empty, use last search pattern. */
4956 if (last_search_pat() == NULL)
4957 {
4958 EMSG(_(e_noprevre));
4959 return;
4960 }
4961 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4962 }
4963 else
4964 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4965
4966 regmatch->rmm_ic = p_ic;
4967 regmatch->rmm_maxcol = 0;
4968}
4969
4970/*
4971 * Display a file name when vimgrep is running.
4972 */
4973 static void
4974vgr_display_fname(char_u *fname)
4975{
4976 char_u *p;
4977
4978 msg_start();
4979 p = msg_strtrunc(fname, TRUE);
4980 if (p == NULL)
4981 msg_outtrans(fname);
4982 else
4983 {
4984 msg_outtrans(p);
4985 vim_free(p);
4986 }
4987 msg_clr_eos();
4988 msg_didout = FALSE; /* overwrite this message */
4989 msg_nowait = TRUE; /* don't wait for this message */
4990 msg_col = 0;
4991 out_flush();
4992}
4993
4994/*
4995 * Load a dummy buffer to search for a pattern using vimgrep.
4996 */
4997 static buf_T *
4998vgr_load_dummy_buf(
4999 char_u *fname,
5000 char_u *dirname_start,
5001 char_u *dirname_now)
5002{
5003 int save_mls;
5004#if defined(FEAT_SYN_HL)
5005 char_u *save_ei = NULL;
5006#endif
5007 buf_T *buf;
5008
5009#if defined(FEAT_SYN_HL)
5010 /* Don't do Filetype autocommands to avoid loading syntax and
5011 * indent scripts, a great speed improvement. */
5012 save_ei = au_event_disable(",Filetype");
5013#endif
5014 /* Don't use modelines here, it's useless. */
5015 save_mls = p_mls;
5016 p_mls = 0;
5017
5018 /* Load file into a buffer, so that 'fileencoding' is detected,
5019 * autocommands applied, etc. */
5020 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5021
5022 p_mls = save_mls;
5023#if defined(FEAT_SYN_HL)
5024 au_event_restore(save_ei);
5025#endif
5026
5027 return buf;
5028}
5029
5030/*
5031 * Check whether a quickfix/location list valid. Autocmds may remove or change
5032 * a quickfix list when vimgrep is running. If the list is not found, create a
5033 * new list.
5034 */
5035 static int
5036vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005037 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005038 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005039 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005040 char_u *title)
5041{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005042 /* Verify that the quickfix/location list was not freed by an autocmd */
5043 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005044 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005045 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005046 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005047 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005048 EMSG(_(e_loc_list_changed));
5049 return FALSE;
5050 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005051 else
5052 {
5053 /* Quickfix list is not found, create a new one. */
5054 qf_new_list(qi, title);
5055 return TRUE;
5056 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005057 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005058
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005059 if (qf_restore_list(qi, qfid) == FAIL)
5060 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005061
5062 return TRUE;
5063}
5064
5065/*
5066 * Search for a pattern in all the lines in a buffer and add the matching lines
5067 * to a quickfix list.
5068 */
5069 static int
5070vgr_match_buflines(
5071 qf_info_T *qi,
5072 char_u *fname,
5073 buf_T *buf,
5074 regmmatch_T *regmatch,
5075 long tomatch,
5076 int duplicate_name,
5077 int flags)
5078{
5079 int found_match = FALSE;
5080 long lnum;
5081 colnr_T col;
5082
5083 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5084 {
5085 col = 0;
5086 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5087 col, NULL, NULL) > 0)
5088 {
5089 /* Pass the buffer number so that it gets used even for a
5090 * dummy buffer, unless duplicate_name is set, then the
5091 * buffer will be wiped out below. */
5092 if (qf_add_entry(qi,
5093 qi->qf_curlist,
5094 NULL, /* dir */
5095 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005096 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005097 duplicate_name ? 0 : buf->b_fnum,
5098 ml_get_buf(buf,
5099 regmatch->startpos[0].lnum + lnum, FALSE),
5100 regmatch->startpos[0].lnum + lnum,
5101 regmatch->startpos[0].col + 1,
5102 FALSE, /* vis_col */
5103 NULL, /* search pattern */
5104 0, /* nr */
5105 0, /* type */
5106 TRUE /* valid */
5107 ) == FAIL)
5108 {
5109 got_int = TRUE;
5110 break;
5111 }
5112 found_match = TRUE;
5113 if (--tomatch == 0)
5114 break;
5115 if ((flags & VGR_GLOBAL) == 0
5116 || regmatch->endpos[0].lnum > 0)
5117 break;
5118 col = regmatch->endpos[0].col
5119 + (col == regmatch->endpos[0].col);
5120 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5121 break;
5122 }
5123 line_breakcheck();
5124 if (got_int)
5125 break;
5126 }
5127
5128 return found_match;
5129}
5130
5131/*
5132 * Jump to the first match and update the directory.
5133 */
5134 static void
5135vgr_jump_to_match(
5136 qf_info_T *qi,
5137 int forceit,
5138 int *redraw_for_dummy,
5139 buf_T *first_match_buf,
5140 char_u *target_dir)
5141{
5142 buf_T *buf;
5143
5144 buf = curbuf;
5145 qf_jump(qi, 0, 0, forceit);
5146 if (buf != curbuf)
5147 /* If we jumped to another buffer redrawing will already be
5148 * taken care of. */
5149 *redraw_for_dummy = FALSE;
5150
5151 /* Jump to the directory used after loading the buffer. */
5152 if (curbuf == first_match_buf && target_dir != NULL)
5153 {
5154 exarg_T ea;
5155
5156 ea.arg = target_dir;
5157 ea.cmdidx = CMD_lcd;
5158 ex_cd(&ea);
5159 }
5160}
5161
5162/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005163 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005164 * ":vimgrepadd {pattern} file(s)"
5165 * ":lvimgrep {pattern} file(s)"
5166 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005167 */
5168 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005169ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005170{
Bram Moolenaar81695252004-12-29 20:58:21 +00005171 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005172 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005173 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005174 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005175 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005176 char_u *s;
5177 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005178 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005179 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005180 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005181 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005182 buf_T *buf;
5183 int duplicate_name = FALSE;
5184 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005185 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005186 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005187 buf_T *first_match_buf = NULL;
5188 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005189 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005190 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005191 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005192 char_u *dirname_start = NULL;
5193 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005194 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005195 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005196
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005197 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005198 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5199 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005200 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005201#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005202 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005203 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005204#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005205 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005206
Bram Moolenaar39665952018-08-15 20:59:48 +02005207 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005208 {
5209 qi = ll_get_or_alloc_list(curwin);
5210 if (qi == NULL)
5211 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005212 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005213 }
5214
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005215 if (eap->addr_count > 0)
5216 tomatch = eap->line2;
5217 else
5218 tomatch = MAXLNUM;
5219
Bram Moolenaar81695252004-12-29 20:58:21 +00005220 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005221 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005222 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005223 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005224 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005225 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005226 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005227 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005228 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005229
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005230 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005231 if (regmatch.regprog == NULL)
5232 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005233
5234 p = skipwhite(p);
5235 if (*p == NUL)
5236 {
5237 EMSG(_("E683: File name missing or invalid pattern"));
5238 goto theend;
5239 }
5240
Bram Moolenaar55b69262017-08-13 13:42:01 +02005241 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005242 && eap->cmdidx != CMD_vimgrepadd
5243 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005244 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005245 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005246 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005247
5248 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005249 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005250 goto theend;
5251 if (fcount == 0)
5252 {
5253 EMSG(_(e_nomatch));
5254 goto theend;
5255 }
5256
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005257 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5258 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005259 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005260 {
5261 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005262 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005263 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005264
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005265 /* Remember the current directory, because a BufRead autocommand that does
5266 * ":lcd %:p:h" changes the meaning of short path names. */
5267 mch_dirname(dirname_start, MAXPATHL);
5268
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005269 /* Remember the current quickfix list identifier, so that we can check for
5270 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005271 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005272
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005273 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005274 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005275 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005276 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005277 if (time(NULL) > seconds)
5278 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005279 /* Display the file name every second or so, show the user we are
5280 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005281 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005282 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005283 }
5284
Bram Moolenaar81695252004-12-29 20:58:21 +00005285 buf = buflist_findname_exp(fnames[fi]);
5286 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5287 {
5288 /* Remember that a buffer with this name already exists. */
5289 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005290 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005291 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005292
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005293 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005294 }
5295 else
5296 /* Use existing, loaded buffer. */
5297 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005298
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005299 /* Check whether the quickfix list is still valid. When loading a
5300 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005301 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005302 {
5303 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005304 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005305 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005306 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005307
Bram Moolenaar81695252004-12-29 20:58:21 +00005308 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005309 {
5310 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005311 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005312 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005313 else
5314 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005315 /* Try for a match in all lines of the buffer.
5316 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005317 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5318 tomatch, duplicate_name, flags);
5319
Bram Moolenaar81695252004-12-29 20:58:21 +00005320 if (using_dummy)
5321 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005322 if (found_match && first_match_buf == NULL)
5323 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005324 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005325 {
Bram Moolenaar81695252004-12-29 20:58:21 +00005326 /* Never keep a dummy buffer if there is another buffer
5327 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005328 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005329 buf = NULL;
5330 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005331 else if (!cmdmod.hide
5332 || buf->b_p_bh[0] == 'u' /* "unload" */
5333 || buf->b_p_bh[0] == 'w' /* "wipe" */
5334 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00005335 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005336 /* When no match was found we don't need to remember the
5337 * buffer, wipe it out. If there was a match and it
5338 * wasn't the first one or we won't jump there: only
5339 * unload the buffer.
5340 * Ignore 'hidden' here, because it may lead to having too
5341 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00005342 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005343 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005344 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005345 buf = NULL;
5346 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005347 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005348 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005349 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02005350 /* Keeping the buffer, remove the dummy flag. */
5351 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005352 buf = NULL;
5353 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005354 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005355
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005356 if (buf != NULL)
5357 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02005358 /* Keeping the buffer, remove the dummy flag. */
5359 buf->b_flags &= ~BF_DUMMY;
5360
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005361 /* If the buffer is still loaded we need to use the
5362 * directory we jumped to below. */
5363 if (buf == first_match_buf
5364 && target_dir == NULL
5365 && STRCMP(dirname_start, dirname_now) != 0)
5366 target_dir = vim_strsave(dirname_now);
5367
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005368 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005369 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00005370 * need to be done (again). But not the window-local
5371 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005372 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005373#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005374 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5375 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005376#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005377 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005378 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005379 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005380 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005381 }
5382 }
5383
5384 FreeWild(fcount, fnames);
5385
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005386 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5387 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5388 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005389 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005390
Bram Moolenaar864293a2016-06-02 13:40:04 +02005391 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005392
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005393 if (au_name != NULL)
5394 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5395 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005396 /*
5397 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5398 * is still valid.
5399 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005400 if (!qflist_valid(wp, save_qfid))
5401 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005402
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005403 if (qf_restore_list(qi, save_qfid) == FAIL)
5404 goto theend;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005405
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005406 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005407 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005408 {
5409 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005410 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5411 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005412 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005413 else
5414 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005415
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005416 /* If we loaded a dummy buffer into the current window, the autocommands
5417 * may have messed up things, need to redraw and recompute folds. */
5418 if (redraw_for_dummy)
5419 {
5420#ifdef FEAT_FOLDING
5421 foldUpdateAll(curwin);
5422#else
5423 redraw_later(NOT_VALID);
5424#endif
5425 }
5426
Bram Moolenaar86b68352004-12-27 21:59:20 +00005427theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005428 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005429 vim_free(dirname_now);
5430 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005431 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005432 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005433}
5434
5435/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005436 * Restore current working directory to "dirname_start" if they differ, taking
5437 * into account whether it is set locally or globally.
5438 */
5439 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005440restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005441{
5442 char_u *dirname_now = alloc(MAXPATHL);
5443
5444 if (NULL != dirname_now)
5445 {
5446 mch_dirname(dirname_now, MAXPATHL);
5447 if (STRCMP(dirname_start, dirname_now) != 0)
5448 {
5449 /* If the directory has changed, change it back by building up an
5450 * appropriate ex command and executing it. */
5451 exarg_T ea;
5452
5453 ea.arg = dirname_start;
5454 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5455 ex_cd(&ea);
5456 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005457 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005458 }
5459}
5460
5461/*
5462 * Load file "fname" into a dummy buffer and return the buffer pointer,
5463 * placing the directory resulting from the buffer load into the
5464 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5465 * prior to calling this function. Restores directory to "dirname_start" prior
5466 * to returning, if autocmds or the 'autochdir' option have changed it.
5467 *
5468 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5469 * or wipe_dummy_buffer() later!
5470 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005471 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005472 */
5473 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005474load_dummy_buffer(
5475 char_u *fname,
5476 char_u *dirname_start, /* in: old directory */
5477 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005478{
5479 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005480 bufref_T newbufref;
5481 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005482 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005483 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005484 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005485
5486 /* Allocate a buffer without putting it in the buffer list. */
5487 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5488 if (newbuf == NULL)
5489 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005490 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005491
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005492 /* Init the options. */
5493 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5494
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005495 /* need to open the memfile before putting the buffer in a window */
5496 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005497 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005498 /* Make sure this buffer isn't wiped out by autocommands. */
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005499 ++newbuf->b_locked;
5500
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005501 /* set curwin/curbuf to buf and save a few things */
5502 aucmd_prepbuf(&aco, newbuf);
5503
5504 /* Need to set the filename for autocommands. */
5505 (void)setfname(curbuf, fname, NULL, FALSE);
5506
Bram Moolenaar81695252004-12-29 20:58:21 +00005507 /* Create swap file now to avoid the ATTENTION message. */
5508 check_need_swap(TRUE);
5509
5510 /* Remove the "dummy" flag, otherwise autocommands may not
5511 * work. */
5512 curbuf->b_flags &= ~BF_DUMMY;
5513
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005514 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005515 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005516 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005517 NULL, READ_NEW | READ_DUMMY);
5518 --newbuf->b_locked;
5519 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005520 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005521 && !(curbuf->b_flags & BF_NEW))
5522 {
5523 failed = FALSE;
5524 if (curbuf != newbuf)
5525 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005526 /* Bloody autocommands changed the buffer! Can happen when
5527 * using netrw and editing a remote file. Use the current
5528 * buffer instead, delete the dummy one after restoring the
5529 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005530 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005531 newbuf = curbuf;
5532 }
5533 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005534
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005535 /* restore curwin/curbuf and a few other things */
5536 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005537 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5538 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005539
5540 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5541 * skip it. */
5542 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005543 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005544
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005545 /*
5546 * When autocommands/'autochdir' option changed directory: go back.
5547 * Let the caller know what the resulting dir was first, in case it is
5548 * important.
5549 */
5550 mch_dirname(resulting_dir, MAXPATHL);
5551 restore_start_dir(dirname_start);
5552
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005553 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005554 return NULL;
5555 if (failed)
5556 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005557 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005558 return NULL;
5559 }
5560 return newbuf;
5561}
5562
5563/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005564 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5565 * directory to "dirname_start" prior to returning, if autocmds or the
5566 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005567 */
5568 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005569wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005570{
5571 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005572 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005573#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005574 cleanup_T cs;
5575
5576 /* Reset the error/interrupt/exception state here so that aborting()
5577 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5578 * work when got_int is set. */
5579 enter_cleanup(&cs);
5580#endif
5581
Bram Moolenaar81695252004-12-29 20:58:21 +00005582 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005583
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005584#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005585 /* Restore the error/interrupt/exception state if not discarded by a
5586 * new aborting error, interrupt, or uncaught exception. */
5587 leave_cleanup(&cs);
5588#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005589 /* When autocommands/'autochdir' option changed directory: go back. */
5590 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005591 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005592}
5593
5594/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005595 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5596 * directory to "dirname_start" prior to returning, if autocmds or the
5597 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005598 */
5599 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005600unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005601{
5602 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005603 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005604 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005605
5606 /* When autocommands/'autochdir' option changed directory: go back. */
5607 restore_start_dir(dirname_start);
5608 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005609}
5610
Bram Moolenaar05159a02005-02-26 23:04:13 +00005611#if defined(FEAT_EVAL) || defined(PROTO)
5612/*
5613 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005614 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005615 */
5616 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005617get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005618{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005619 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005620 dict_T *dict;
5621 char_u buf[2];
5622 qfline_T *qfp;
5623 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005624 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005625
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005626 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005627 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005628 qi = &ql_info;
5629 if (wp != NULL)
5630 {
5631 qi = GET_LOC_LIST(wp);
5632 if (qi == NULL)
5633 return FAIL;
5634 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005635 }
5636
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005637 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005638 qf_idx = qi->qf_curlist;
5639
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005640 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005641 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005642
Bram Moolenaard823fa92016-08-12 16:29:27 +02005643 qfp = qi->qf_lists[qf_idx].qf_start;
5644 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005645 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005646 /* Handle entries with a non-existing buffer number. */
5647 bufnum = qfp->qf_fnum;
5648 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5649 bufnum = 0;
5650
Bram Moolenaar05159a02005-02-26 23:04:13 +00005651 if ((dict = dict_alloc()) == NULL)
5652 return FAIL;
5653 if (list_append_dict(list, dict) == FAIL)
5654 return FAIL;
5655
5656 buf[0] = qfp->qf_type;
5657 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005658 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5659 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5660 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5661 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5662 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5663 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5664 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5665 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5666 || dict_add_string(dict, "type", buf) == FAIL
5667 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005668 return FAIL;
5669
5670 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005671 if (qfp == NULL)
5672 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005673 }
5674 return OK;
5675}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005676
5677/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005678 * Flags used by getqflist()/getloclist() to determine which fields to return.
5679 */
5680enum {
5681 QF_GETLIST_NONE = 0x0,
5682 QF_GETLIST_TITLE = 0x1,
5683 QF_GETLIST_ITEMS = 0x2,
5684 QF_GETLIST_NR = 0x4,
5685 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005686 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005687 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005688 QF_GETLIST_IDX = 0x40,
5689 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005690 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005691 QF_GETLIST_FILEWINID = 0x200,
5692 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005693};
5694
5695/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005696 * Parse text from 'di' and return the quickfix list items.
5697 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005698 */
5699 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005700qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005701{
5702 int status = FAIL;
5703 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005704 char_u *errorformat = p_efm;
5705 dictitem_T *efm_di;
5706 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005707
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005708 /* Only a List value is supported */
5709 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005710 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005711 /* If errorformat is supplied then use it, otherwise use the 'efm'
5712 * option setting
5713 */
5714 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5715 {
5716 if (efm_di->di_tv.v_type != VAR_STRING ||
5717 efm_di->di_tv.vval.v_string == NULL)
5718 return FAIL;
5719 errorformat = efm_di->di_tv.vval.v_string;
5720 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005721
Bram Moolenaar36538222017-09-02 19:51:44 +02005722 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005723 if (l == NULL)
5724 return FAIL;
5725
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005726 qi = ll_new_list();
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005727 if (qi != NULL)
5728 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005729 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005730 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5731 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005732 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005733 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005734 }
5735 free(qi);
5736 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005737 dict_add_list(retdict, "items", l);
5738 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005739 }
5740
5741 return status;
5742}
5743
5744/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005745 * Return the quickfix/location list window identifier in the current tabpage.
5746 */
5747 static int
5748qf_winid(qf_info_T *qi)
5749{
5750 win_T *win;
5751
5752 /* The quickfix window can be opened even if the quickfix list is not set
5753 * using ":copen". This is not true for location lists. */
5754 if (qi == NULL)
5755 return 0;
5756 win = qf_find_win(qi);
5757 if (win != NULL)
5758 return win->w_id;
5759 return 0;
5760}
5761
5762/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005763 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005764 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005765 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005766qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005767{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005768 int flags = QF_GETLIST_NONE;
5769
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005770 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005771 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005772 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005773 if (!loclist)
5774 // File window ID is applicable only to location list windows
5775 flags &= ~ QF_GETLIST_FILEWINID;
5776 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005777
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005778 if (dict_find(what, (char_u *)"title", -1) != NULL)
5779 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005780
Bram Moolenaara6d48492017-12-12 22:45:31 +01005781 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5782 flags |= QF_GETLIST_NR;
5783
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005784 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5785 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005786
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005787 if (dict_find(what, (char_u *)"context", -1) != NULL)
5788 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005789
Bram Moolenaara6d48492017-12-12 22:45:31 +01005790 if (dict_find(what, (char_u *)"id", -1) != NULL)
5791 flags |= QF_GETLIST_ID;
5792
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005793 if (dict_find(what, (char_u *)"items", -1) != NULL)
5794 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005795
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005796 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5797 flags |= QF_GETLIST_IDX;
5798
5799 if (dict_find(what, (char_u *)"size", -1) != NULL)
5800 flags |= QF_GETLIST_SIZE;
5801
Bram Moolenaarb254af32017-12-18 19:48:58 +01005802 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5803 flags |= QF_GETLIST_TICK;
5804
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005805 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5806 flags |= QF_GETLIST_FILEWINID;
5807
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005808 return flags;
5809}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005810
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005811/*
5812 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5813 * If 'nr' and 'id' are not present in 'what' then return the current
5814 * quickfix list index.
5815 * If 'nr' is zero then return the current quickfix list index.
5816 * If 'nr' is '$' then return the last quickfix list index.
5817 * If 'id' is present then return the index of the quickfix list with that id.
5818 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5819 * Return -1, if quickfix list is not present or if the stack is empty.
5820 */
5821 static int
5822qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5823{
5824 int qf_idx;
5825 dictitem_T *di;
5826
5827 qf_idx = qi->qf_curlist; /* default is the current list */
5828 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5829 {
5830 /* Use the specified quickfix/location list */
5831 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005832 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005833 /* for zero use the current list */
5834 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005835 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005836 qf_idx = di->di_tv.vval.v_number - 1;
5837 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005838 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005839 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005840 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005841 else if (di->di_tv.v_type == VAR_STRING
5842 && di->di_tv.vval.v_string != NULL
5843 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5844 /* Get the last quickfix list number */
5845 qf_idx = qi->qf_listcount - 1;
5846 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005847 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005848 }
5849
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005850 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5851 {
5852 /* Look for a list with the specified id */
5853 if (di->di_tv.v_type == VAR_NUMBER)
5854 {
5855 /*
5856 * For zero, use the current list or the list specified by 'nr'
5857 */
5858 if (di->di_tv.vval.v_number != 0)
5859 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5860 }
5861 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005862 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005863 }
5864
5865 return qf_idx;
5866}
5867
5868/*
5869 * Return default values for quickfix list properties in retdict.
5870 */
5871 static int
5872qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5873{
5874 int status = OK;
5875
5876 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005877 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005878 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5879 {
5880 list_T *l = list_alloc();
5881 if (l != NULL)
5882 status = dict_add_list(retdict, "items", l);
5883 else
5884 status = FAIL;
5885 }
5886 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005887 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005888 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005889 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005890 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005891 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005892 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005893 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005894 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005895 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005896 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005897 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005898 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005899 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005900 if ((status == OK) && (qi != &ql_info) && (flags & QF_GETLIST_FILEWINID))
5901 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005902
5903 return status;
5904}
5905
5906/*
5907 * Return the quickfix list title as 'title' in retdict
5908 */
5909 static int
5910qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5911{
Bram Moolenaare0be1672018-07-08 16:50:37 +02005912 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005913}
5914
5915/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005916 * Returns the identifier of the window used to display files from a location
5917 * list. If there is no associated window, then returns 0. Useful only when
5918 * called from a location list window.
5919 */
5920 static int
5921qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
5922{
5923 int winid = 0;
5924
5925 if (wp != NULL && IS_LL_WINDOW(wp))
5926 {
5927 win_T *ll_wp = qf_find_win_with_loclist(qi);
5928 if (ll_wp != NULL)
5929 winid = ll_wp->w_id;
5930 }
5931
5932 return dict_add_number(retdict, "filewinid", winid);
5933}
5934
5935/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005936 * Return the quickfix list items/entries as 'items' in retdict
5937 */
5938 static int
5939qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5940{
5941 int status = OK;
5942 list_T *l = list_alloc();
5943 if (l != NULL)
5944 {
5945 (void)get_errorlist(qi, NULL, qf_idx, l);
5946 dict_add_list(retdict, "items", l);
5947 }
5948 else
5949 status = FAIL;
5950
5951 return status;
5952}
5953
5954/*
5955 * Return the quickfix list context (if any) as 'context' in retdict.
5956 */
5957 static int
5958qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5959{
5960 int status;
5961 dictitem_T *di;
5962
5963 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5964 {
5965 di = dictitem_alloc((char_u *)"context");
5966 if (di != NULL)
5967 {
5968 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5969 status = dict_add(retdict, di);
5970 if (status == FAIL)
5971 dictitem_free(di);
5972 }
5973 else
5974 status = FAIL;
5975 }
5976 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02005977 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005978
5979 return status;
5980}
5981
5982/*
5983 * Return the quickfix list index as 'idx' in retdict
5984 */
5985 static int
5986qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5987{
5988 int idx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005989 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005990 /* For empty lists, qf_index is set to 1 */
5991 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005992 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005993}
5994
5995/*
5996 * Return quickfix/location list details (title) as a
5997 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5998 * then current list is used. Otherwise the specified list is used.
5999 */
6000 int
6001qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6002{
6003 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006004 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006005 int status = OK;
6006 int qf_idx;
6007 dictitem_T *di;
6008 int flags = QF_GETLIST_NONE;
6009
6010 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6011 return qf_get_list_from_lines(what, di, retdict);
6012
6013 if (wp != NULL)
6014 qi = GET_LOC_LIST(wp);
6015
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006016 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006017
6018 if (qi != NULL && qi->qf_listcount != 0)
6019 qf_idx = qf_getprop_qfidx(qi, what);
6020
Bram Moolenaara6d48492017-12-12 22:45:31 +01006021 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006022 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006023 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006024
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006025 qfl = &qi->qf_lists[qf_idx];
6026
Bram Moolenaard823fa92016-08-12 16:29:27 +02006027 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006028 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006029 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006030 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006031 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006032 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006033 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006034 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006035 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006036 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006037 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006038 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006039 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006040 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006041 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006042 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006043 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006044 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006045 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6046 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006047
Bram Moolenaard823fa92016-08-12 16:29:27 +02006048 return status;
6049}
6050
6051/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006052 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
6053 * items in the dict 'd'.
6054 */
6055 static int
6056qf_add_entry_from_dict(
6057 qf_info_T *qi,
6058 int qf_idx,
6059 dict_T *d,
6060 int first_entry)
6061{
6062 static int did_bufnr_emsg;
6063 char_u *filename, *module, *pattern, *text, *type;
6064 int bufnum, valid, status, col, vcol, nr;
6065 long lnum;
6066
6067 if (first_entry)
6068 did_bufnr_emsg = FALSE;
6069
6070 filename = get_dict_string(d, (char_u *)"filename", TRUE);
6071 module = get_dict_string(d, (char_u *)"module", TRUE);
6072 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6073 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6074 col = (int)get_dict_number(d, (char_u *)"col");
6075 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6076 nr = (int)get_dict_number(d, (char_u *)"nr");
6077 type = get_dict_string(d, (char_u *)"type", TRUE);
6078 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6079 text = get_dict_string(d, (char_u *)"text", TRUE);
6080 if (text == NULL)
6081 text = vim_strsave((char_u *)"");
6082
6083 valid = TRUE;
6084 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6085 valid = FALSE;
6086
6087 // Mark entries with non-existing buffer number as not valid. Give the
6088 // error message only once.
6089 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6090 {
6091 if (!did_bufnr_emsg)
6092 {
6093 did_bufnr_emsg = TRUE;
6094 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6095 }
6096 valid = FALSE;
6097 bufnum = 0;
6098 }
6099
6100 // If the 'valid' field is present it overrules the detected value.
6101 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6102 valid = (int)get_dict_number(d, (char_u *)"valid");
6103
6104 status = qf_add_entry(qi,
6105 qf_idx,
6106 NULL, // dir
6107 filename,
6108 module,
6109 bufnum,
6110 text,
6111 lnum,
6112 col,
6113 vcol, // vis_col
6114 pattern, // search pattern
6115 nr,
6116 type == NULL ? NUL : *type,
6117 valid);
6118
6119 vim_free(filename);
6120 vim_free(module);
6121 vim_free(pattern);
6122 vim_free(text);
6123 vim_free(type);
6124
6125 return status;
6126}
6127
6128/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006129 * Add list of entries to quickfix/location list. Each list entry is
6130 * a dictionary with item information.
6131 */
6132 static int
6133qf_add_entries(
6134 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006135 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006136 list_T *list,
6137 char_u *title,
6138 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006139{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006140 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006141 listitem_T *li;
6142 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006143 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006144 int retval = OK;
6145
Bram Moolenaara3921f42017-06-04 15:30:34 +02006146 if (action == ' ' || qf_idx == qi->qf_listcount)
6147 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006148 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006149 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006150 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006151 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006152 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006153 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006154 /* Adding to existing list, use last entry. */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006155 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006156 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006157 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006158 qf_free_items(qfl);
6159 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006160 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006161
6162 for (li = list->lv_first; li != NULL; li = li->li_next)
6163 {
6164 if (li->li_tv.v_type != VAR_DICT)
6165 continue; /* Skip non-dict items */
6166
6167 d = li->li_tv.vval.v_dict;
6168 if (d == NULL)
6169 continue;
6170
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006171 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first);
6172 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006173 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006174 }
6175
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006176 if (qfl->qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02006177 /* no valid entry */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006178 qfl->qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006179 else
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006180 qfl->qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006181 if (action != 'a')
6182 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006183 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006184 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006185 qfl->qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006186 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006187
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006188 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02006189 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006190
6191 return retval;
6192}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006193
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006194/*
6195 * Get the quickfix list index from 'nr' or 'id'
6196 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006197 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006198qf_setprop_get_qfidx(
6199 qf_info_T *qi,
6200 dict_T *what,
6201 int action,
6202 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006203{
6204 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006205 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006206
Bram Moolenaard823fa92016-08-12 16:29:27 +02006207 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6208 {
6209 /* Use the specified quickfix/location list */
6210 if (di->di_tv.v_type == VAR_NUMBER)
6211 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006212 /* for zero use the current list */
6213 if (di->di_tv.vval.v_number != 0)
6214 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006215
Bram Moolenaar55b69262017-08-13 13:42:01 +02006216 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6217 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006218 /*
6219 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02006220 * non-available list and add the new list at the end of the
6221 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006222 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006223 *newlist = TRUE;
6224 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006225 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006226 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006227 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006228 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006229 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02006230 }
6231 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006232 && di->di_tv.vval.v_string != NULL
6233 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006234 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006235 if (qi->qf_listcount > 0)
6236 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006237 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006238 qf_idx = 0;
6239 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006240 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006241 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006242 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006243 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006244 }
6245
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006246 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006247 {
6248 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006249 if (di->di_tv.v_type != VAR_NUMBER)
6250 return INVALID_QFIDX;
6251
6252 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006253 }
6254
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006255 return qf_idx;
6256}
6257
6258/*
6259 * Set the quickfix list title.
6260 */
6261 static int
6262qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6263{
6264 if (di->di_tv.v_type != VAR_STRING)
6265 return FAIL;
6266
6267 vim_free(qi->qf_lists[qf_idx].qf_title);
6268 qi->qf_lists[qf_idx].qf_title =
6269 get_dict_string(what, (char_u *)"title", TRUE);
6270 if (qf_idx == qi->qf_curlist)
6271 qf_update_win_titlevar(qi);
6272
6273 return OK;
6274}
6275
6276/*
6277 * Set quickfix list items/entries.
6278 */
6279 static int
6280qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6281{
6282 int retval = FAIL;
6283 char_u *title_save;
6284
6285 if (di->di_tv.v_type != VAR_LIST)
6286 return FAIL;
6287
6288 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6289 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6290 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006291 vim_free(title_save);
6292
6293 return retval;
6294}
6295
6296/*
6297 * Set quickfix list items/entries from a list of lines.
6298 */
6299 static int
6300qf_setprop_items_from_lines(
6301 qf_info_T *qi,
6302 int qf_idx,
6303 dict_T *what,
6304 dictitem_T *di,
6305 int action)
6306{
6307 char_u *errorformat = p_efm;
6308 dictitem_T *efm_di;
6309 int retval = FAIL;
6310
6311 /* Use the user supplied errorformat settings (if present) */
6312 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6313 {
6314 if (efm_di->di_tv.v_type != VAR_STRING ||
6315 efm_di->di_tv.vval.v_string == NULL)
6316 return FAIL;
6317 errorformat = efm_di->di_tv.vval.v_string;
6318 }
6319
6320 /* Only a List value is supported */
6321 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6322 return FAIL;
6323
6324 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006325 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006326 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6327 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6328 retval = OK;
6329
6330 return retval;
6331}
6332
6333/*
6334 * Set quickfix list context.
6335 */
6336 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006337qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006338{
6339 typval_T *ctx;
6340
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006341 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006342 ctx = alloc_tv();
6343 if (ctx != NULL)
6344 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006345 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006346
6347 return OK;
6348}
6349
6350/*
6351 * Set quickfix/location list properties (title, items, context).
6352 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006353 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006354 */
6355 static int
6356qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6357{
6358 dictitem_T *di;
6359 int retval = FAIL;
6360 int qf_idx;
6361 int newlist = FALSE;
6362
6363 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
6364 newlist = TRUE;
6365
6366 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6367 if (qf_idx == INVALID_QFIDX) /* List not found */
6368 return FAIL;
6369
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006370 if (newlist)
6371 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006372 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006373 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006374 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006375 }
6376
6377 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006378 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006379 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006380 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006381 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006382 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006383 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006384 retval = qf_setprop_context(&qi->qf_lists[qf_idx], di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006385
Bram Moolenaarb254af32017-12-18 19:48:58 +01006386 if (retval == OK)
6387 qf_list_changed(qi, qf_idx);
6388
Bram Moolenaard823fa92016-08-12 16:29:27 +02006389 return retval;
6390}
6391
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006392/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006393 * Find the non-location list window with the specified location list in the
6394 * current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006395 */
6396 static win_T *
6397find_win_with_ll(qf_info_T *qi)
6398{
6399 win_T *wp = NULL;
6400
6401 FOR_ALL_WINDOWS(wp)
6402 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6403 return wp;
6404
6405 return NULL;
6406}
6407
6408/*
6409 * Free the entire quickfix/location list stack.
6410 * If the quickfix/location list window is open, then clear it.
6411 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006412 static void
6413qf_free_stack(win_T *wp, qf_info_T *qi)
6414{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006415 win_T *qfwin = qf_find_win(qi);
6416 win_T *llwin = NULL;
6417 win_T *orig_wp = wp;
6418
6419 if (qfwin != NULL)
6420 {
6421 /* If the quickfix/location list window is open, then clear it */
6422 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006423 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006424 qf_update_buffer(qi, NULL);
6425 }
6426
6427 if (wp != NULL && IS_LL_WINDOW(wp))
6428 {
6429 /* If in the location list window, then use the non-location list
6430 * window with this location list (if present)
6431 */
6432 llwin = find_win_with_ll(qi);
6433 if (llwin != NULL)
6434 wp = llwin;
6435 }
6436
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006437 qf_free_all(wp);
6438 if (wp == NULL)
6439 {
6440 /* quickfix list */
6441 qi->qf_curlist = 0;
6442 qi->qf_listcount = 0;
6443 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006444 else if (IS_LL_WINDOW(orig_wp))
6445 {
6446 /* If the location list window is open, then create a new empty
6447 * location list */
6448 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006449
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006450 /* first free the list reference in the location list window */
6451 ll_free_all(&orig_wp->w_llist_ref);
6452
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006453 orig_wp->w_llist_ref = new_ll;
6454 if (llwin != NULL)
6455 {
6456 llwin->w_llist = new_ll;
6457 new_ll->qf_refcount++;
6458 }
6459 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006460}
6461
Bram Moolenaard823fa92016-08-12 16:29:27 +02006462/*
6463 * Populate the quickfix list with the items supplied in the list
6464 * of dictionaries. "title" will be copied to w:quickfix_title.
6465 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6466 */
6467 int
6468set_errorlist(
6469 win_T *wp,
6470 list_T *list,
6471 int action,
6472 char_u *title,
6473 dict_T *what)
6474{
6475 qf_info_T *qi = &ql_info;
6476 int retval = OK;
6477
6478 if (wp != NULL)
6479 {
6480 qi = ll_get_or_alloc_list(wp);
6481 if (qi == NULL)
6482 return FAIL;
6483 }
6484
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006485 if (action == 'f')
6486 {
6487 /* Free the entire quickfix or location list stack */
6488 qf_free_stack(wp, qi);
6489 }
6490 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006491 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006492 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006493 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006494 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006495 if (retval == OK)
6496 qf_list_changed(qi, qi->qf_curlist);
6497 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006498
6499 return retval;
6500}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006501
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006502/*
6503 * Mark the context as in use for all the lists in a quickfix stack.
6504 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006505 static int
6506mark_quickfix_ctx(qf_info_T *qi, int copyID)
6507{
6508 int i;
6509 int abort = FALSE;
6510 typval_T *ctx;
6511
6512 for (i = 0; i < LISTCOUNT && !abort; ++i)
6513 {
6514 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006515 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6516 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006517 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6518 }
6519
6520 return abort;
6521}
6522
6523/*
6524 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006525 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006526 */
6527 int
6528set_ref_in_quickfix(int copyID)
6529{
6530 int abort = FALSE;
6531 tabpage_T *tp;
6532 win_T *win;
6533
6534 abort = mark_quickfix_ctx(&ql_info, copyID);
6535 if (abort)
6536 return abort;
6537
6538 FOR_ALL_TAB_WINDOWS(tp, win)
6539 {
6540 if (win->w_llist != NULL)
6541 {
6542 abort = mark_quickfix_ctx(win->w_llist, copyID);
6543 if (abort)
6544 return abort;
6545 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006546 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6547 {
6548 /* In a location list window and none of the other windows is
6549 * referring to this location list. Mark the location list
6550 * context as still in use.
6551 */
6552 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6553 if (abort)
6554 return abort;
6555 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006556 }
6557
6558 return abort;
6559}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006560#endif
6561
Bram Moolenaar81695252004-12-29 20:58:21 +00006562/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006563 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006564 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006565 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006566 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006567 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006568 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006569 */
6570 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006571ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006572{
6573 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006574 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006575 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006576 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006577 int_u save_qfid;
6578 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006579
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006580 switch (eap->cmdidx)
6581 {
6582 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6583 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6584 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6585 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6586 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6587 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6588 default: break;
6589 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006590 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6591 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006592 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006593#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006594 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006595 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006596#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006597 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006598
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006599 /* Must come after autocommands. */
Bram Moolenaar39665952018-08-15 20:59:48 +02006600 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006601 {
6602 qi = ll_get_or_alloc_list(curwin);
6603 if (qi == NULL)
6604 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006605 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006606 }
6607
Bram Moolenaar86b68352004-12-27 21:59:20 +00006608 if (*eap->arg == NUL)
6609 buf = curbuf;
6610 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6611 buf = buflist_findnr(atoi((char *)eap->arg));
6612 if (buf == NULL)
6613 EMSG(_(e_invarg));
6614 else if (buf->b_ml.ml_mfp == NULL)
6615 EMSG(_("E681: Buffer is not loaded"));
6616 else
6617 {
6618 if (eap->addr_count == 0)
6619 {
6620 eap->line1 = 1;
6621 eap->line2 = buf->b_ml.ml_line_count;
6622 }
6623 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6624 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6625 EMSG(_(e_invrange));
6626 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006627 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006628 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006629
6630 if (buf->b_sfname)
6631 {
6632 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6633 (char *)qf_title, (char *)buf->b_sfname);
6634 qf_title = IObuff;
6635 }
6636
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006637 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006638 (eap->cmdidx != CMD_caddbuffer
6639 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006640 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006641 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006642 if (res >= 0)
6643 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006644
6645 // Remember the current quickfix list identifier, so that we can
6646 // check for autocommands changing the current quickfix list.
6647 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006648 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006649 {
6650 buf_T *curbuf_old = curbuf;
6651
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006652 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6653 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006654 if (curbuf != curbuf_old)
6655 // Autocommands changed buffer, don't jump now, "qi" may
6656 // be invalid.
6657 res = 0;
6658 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006659 // Jump to the first error for a new list and if autocmds didn't
6660 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006661 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006662 eap->cmdidx == CMD_lbuffer)
6663 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006664 // display the first error
6665 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar754b5602006-02-09 23:53:20 +00006666 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006667 }
6668}
6669
Bram Moolenaar1e015462005-09-25 22:16:38 +00006670#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006671/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006672 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6673 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006674 */
6675 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006676ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006677{
6678 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006679 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006680 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006681 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006682 int_u save_qfid;
6683 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006684
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006685 switch (eap->cmdidx)
6686 {
6687 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6688 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6689 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6690 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6691 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6692 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6693 default: break;
6694 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006695 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6696 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006697 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006698#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006699 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006700 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006701#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006702 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006703
Bram Moolenaar39665952018-08-15 20:59:48 +02006704 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006705 {
6706 qi = ll_get_or_alloc_list(curwin);
6707 if (qi == NULL)
6708 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006709 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006710 }
6711
Bram Moolenaar4770d092006-01-12 23:22:24 +00006712 /* Evaluate the expression. When the result is a string or a list we can
6713 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006714 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006715 if (tv != NULL)
6716 {
6717 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6718 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6719 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006720 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006721 (eap->cmdidx != CMD_caddexpr
6722 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006723 (linenr_T)0, (linenr_T)0,
6724 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006725 if (res >= 0)
6726 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006727
6728 // Remember the current quickfix list identifier, so that we can
6729 // check for autocommands changing the current quickfix list.
6730 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006731 if (au_name != NULL)
6732 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6733 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006734
6735 // Jump to the first error for a new list and if autocmds didn't
6736 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006737 if (res > 0 && (eap->cmdidx == CMD_cexpr
6738 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006739 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006740 // display the first error
6741 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006742 }
6743 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006744 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006745 free_tv(tv);
6746 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006747}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006748#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006749
6750/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006751 * Get the location list for ":lhelpgrep"
6752 */
6753 static qf_info_T *
6754hgr_get_ll(int *new_ll)
6755{
6756 win_T *wp;
6757 qf_info_T *qi;
6758
6759 /* If the current window is a help window, then use it */
6760 if (bt_help(curwin->w_buffer))
6761 wp = curwin;
6762 else
6763 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006764 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006765
6766 if (wp == NULL) /* Help window not found */
6767 qi = NULL;
6768 else
6769 qi = wp->w_llist;
6770
6771 if (qi == NULL)
6772 {
6773 /* Allocate a new location list for help text matches */
6774 if ((qi = ll_new_list()) == NULL)
6775 return NULL;
6776 *new_ll = TRUE;
6777 }
6778
6779 return qi;
6780}
6781
6782/*
6783 * Search for a pattern in a help file.
6784 */
6785 static void
6786hgr_search_file(
6787 qf_info_T *qi,
6788 char_u *fname,
6789#ifdef FEAT_MBYTE
6790 vimconv_T *p_vc,
6791#endif
6792 regmatch_T *p_regmatch)
6793{
6794 FILE *fd;
6795 long lnum;
6796
6797 fd = mch_fopen((char *)fname, "r");
6798 if (fd == NULL)
6799 return;
6800
6801 lnum = 1;
6802 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6803 {
6804 char_u *line = IObuff;
6805#ifdef FEAT_MBYTE
6806 /* Convert a line if 'encoding' is not utf-8 and
6807 * the line contains a non-ASCII character. */
6808 if (p_vc->vc_type != CONV_NONE
6809 && has_non_ascii(IObuff))
6810 {
6811 line = string_convert(p_vc, IObuff, NULL);
6812 if (line == NULL)
6813 line = IObuff;
6814 }
6815#endif
6816
6817 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6818 {
6819 int l = (int)STRLEN(line);
6820
6821 /* remove trailing CR, LF, spaces, etc. */
6822 while (l > 0 && line[l - 1] <= ' ')
6823 line[--l] = NUL;
6824
6825 if (qf_add_entry(qi,
6826 qi->qf_curlist,
6827 NULL, /* dir */
6828 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006829 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006830 0,
6831 line,
6832 lnum,
6833 (int)(p_regmatch->startp[0] - line)
6834 + 1, /* col */
6835 FALSE, /* vis_col */
6836 NULL, /* search pattern */
6837 0, /* nr */
6838 1, /* type */
6839 TRUE /* valid */
6840 ) == FAIL)
6841 {
6842 got_int = TRUE;
6843#ifdef FEAT_MBYTE
6844 if (line != IObuff)
6845 vim_free(line);
6846#endif
6847 break;
6848 }
6849 }
6850#ifdef FEAT_MBYTE
6851 if (line != IObuff)
6852 vim_free(line);
6853#endif
6854 ++lnum;
6855 line_breakcheck();
6856 }
6857 fclose(fd);
6858}
6859
6860/*
6861 * Search for a pattern in all the help files in the doc directory under
6862 * the given directory.
6863 */
6864 static void
6865hgr_search_files_in_dir(
6866 qf_info_T *qi,
6867 char_u *dirname,
6868 regmatch_T *p_regmatch
6869#ifdef FEAT_MBYTE
6870 , vimconv_T *p_vc
6871#endif
6872#ifdef FEAT_MULTI_LANG
6873 , char_u *lang
6874#endif
6875 )
6876{
6877 int fcount;
6878 char_u **fnames;
6879 int fi;
6880
6881 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6882 add_pathsep(dirname);
6883 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6884 if (gen_expand_wildcards(1, &dirname, &fcount,
6885 &fnames, EW_FILE|EW_SILENT) == OK
6886 && fcount > 0)
6887 {
6888 for (fi = 0; fi < fcount && !got_int; ++fi)
6889 {
6890#ifdef FEAT_MULTI_LANG
6891 /* Skip files for a different language. */
6892 if (lang != NULL
6893 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006894 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006895 && !(STRNICMP(lang, "en", 2) == 0
6896 && STRNICMP("txt", fnames[fi]
6897 + STRLEN(fnames[fi]) - 3, 3) == 0))
6898 continue;
6899#endif
6900
6901 hgr_search_file(qi, fnames[fi],
6902#ifdef FEAT_MBYTE
6903 p_vc,
6904#endif
6905 p_regmatch);
6906 }
6907 FreeWild(fcount, fnames);
6908 }
6909}
6910
6911/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006912 * Search for a pattern in all the help files in the 'runtimepath'
6913 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006914 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006915 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006916 */
6917 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006918hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006919{
6920 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006921
6922#ifdef FEAT_MBYTE
6923 vimconv_T vc;
6924
6925 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6926 * differs. */
6927 vc.vc_type = CONV_NONE;
6928 if (!enc_utf8)
6929 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6930#endif
6931
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006932
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006933 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006934 p = p_rtp;
6935 while (*p != NUL && !got_int)
6936 {
6937 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6938
6939 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6940#ifdef FEAT_MBYTE
6941 , &vc
6942#endif
6943#ifdef FEAT_MULTI_LANG
6944 , lang
6945#endif
6946 );
6947 }
6948
6949#ifdef FEAT_MBYTE
6950 if (vc.vc_type != CONV_NONE)
6951 convert_setup(&vc, NULL, NULL);
6952#endif
6953}
6954
6955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 * ":helpgrep {pattern}"
6957 */
6958 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006959ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
6961 regmatch_T regmatch;
6962 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006963 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006964 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006965 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006966 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967
Bram Moolenaar73633f82012-01-20 13:39:07 +01006968 switch (eap->cmdidx)
6969 {
6970 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6971 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6972 default: break;
6973 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006974 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6975 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006976 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006977#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006978 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006979 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006980#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006981 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006982
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006983 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01006984 save_cpo = p_cpo;
6985 p_cpo = empty_option;
6986
Bram Moolenaar39665952018-08-15 20:59:48 +02006987 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006988 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006989 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006990 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006991 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006992 }
6993
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006994#ifdef FEAT_MULTI_LANG
6995 // Check for a specified language
6996 lang = check_help_lang(eap->arg);
6997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6999 regmatch.rm_ic = FALSE;
7000 if (regmatch.regprog != NULL)
7001 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007002 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007003 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007005 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007006
Bram Moolenaar473de612013-06-08 18:19:48 +02007007 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007009 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
7010 qi->qf_lists[qi->qf_curlist].qf_ptr =
7011 qi->qf_lists[qi->qf_curlist].qf_start;
7012 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 }
7014
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007015 if (p_cpo == empty_option)
7016 p_cpo = save_cpo;
7017 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007018 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007019 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020
Bram Moolenaarb254af32017-12-18 19:48:58 +01007021 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02007022 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
Bram Moolenaar73633f82012-01-20 13:39:07 +01007024 if (au_name != NULL)
7025 {
7026 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7027 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007028 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01007029 /* autocommands made "qi" invalid */
7030 return;
7031 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007032
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007034 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007035 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007036 else
7037 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007038
7039 if (eap->cmdidx == CMD_lhelpgrep)
7040 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007041 // If the help window is not opened or if it already points to the
7042 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007043 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007044 {
7045 if (new_qi)
7046 ll_free_all(&qi);
7047 }
7048 else if (curwin->w_llist == NULL)
7049 curwin->w_llist = qi;
7050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051}
7052
7053#endif /* FEAT_QUICKFIX */