blob: 59b3a227c8a7126b500bb1810dfb57bace73f4a8 [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,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002524 int dir,
2525 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002526{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002527 qfline_T *qf_ptr = qfl->qf_ptr;
2528 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002529 qfline_T *prev_qf_ptr;
2530 int prev_index;
2531 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2532 char_u *err = e_no_more_items;
2533
2534 while (errornr--)
2535 {
2536 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002537 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002538
2539 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002540 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002541 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002542 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002543 if (qf_ptr == NULL)
2544 {
2545 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002546 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002547 if (err != NULL)
2548 {
2549 EMSG(_(err));
2550 return NULL;
2551 }
2552 break;
2553 }
2554
2555 err = NULL;
2556 }
2557
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002558 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002559 return qf_ptr;
2560}
2561
2562/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002563 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2564 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002565 */
2566 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002567get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002568{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002569 qfline_T *qf_ptr = qfl->qf_ptr;
2570 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002571
Bram Moolenaar6dae96e2018-09-24 21:50:12 +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 Moolenaar6dae96e2018-09-24 21:50:12 +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 Moolenaar6dae96e2018-09-24 21:50:12 +02002586 *new_qfidx = qf_idx;
2587 return qf_ptr;
2588}
2589
2590/*
2591 * Get a entry specied by 'errornr' and 'dir' from the current
2592 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2593 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2594 * Returns a pointer to the entry and the index of the new entry is stored in
2595 * 'new_qfidx'.
2596 */
2597 static qfline_T *
2598qf_get_entry(
2599 qf_list_T *qfl,
2600 int errornr,
2601 int dir,
2602 int *new_qfidx)
2603{
2604 qfline_T *qf_ptr = qfl->qf_ptr;
2605 int qfidx = qfl->qf_index;
2606
2607 if (dir != 0) // next/prev valid entry
2608 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2609 else if (errornr != 0) // go to specified number
2610 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2611
2612 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002613 return qf_ptr;
2614}
2615
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002616/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002617 * Find a window displaying a Vim help file.
2618 */
2619 static win_T *
2620qf_find_help_win(void)
2621{
2622 win_T *wp;
2623
2624 FOR_ALL_WINDOWS(wp)
2625 if (bt_help(wp->w_buffer))
2626 return wp;
2627
2628 return NULL;
2629}
2630
2631/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002632 * Find a help window or open one.
2633 */
2634 static int
2635jump_to_help_window(qf_info_T *qi, int *opened_window)
2636{
2637 win_T *wp;
2638 int flags;
2639
2640 if (cmdmod.tab != 0)
2641 wp = NULL;
2642 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002643 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002644 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2645 win_enter(wp, TRUE);
2646 else
2647 {
2648 /*
2649 * Split off help window; put it at far top if no position
2650 * specified, the current window is vertically split and narrow.
2651 */
2652 flags = WSP_HELP;
2653 if (cmdmod.split == 0 && curwin->w_width != Columns
2654 && curwin->w_width < 80)
2655 flags |= WSP_TOP;
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002656 if (IS_LL_STACK(qi))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002657 flags |= WSP_NEWLOC; /* don't copy the location list */
2658
2659 if (win_split(0, flags) == FAIL)
2660 return FAIL;
2661
2662 *opened_window = TRUE;
2663
2664 if (curwin->w_height < p_hh)
2665 win_setheight((int)p_hh);
2666
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002667 if (IS_LL_STACK(qi)) // not a quickfix list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002668 {
2669 /* The new window should use the supplied location list */
2670 curwin->w_llist = qi;
2671 qi->qf_refcount++;
2672 }
2673 }
2674
2675 if (!p_im)
2676 restart_edit = 0; /* don't want insert mode in help file */
2677
2678 return OK;
2679}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002680
2681/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002682 * Find a non-quickfix window using the given location list.
2683 * Returns NULL if a matching window is not found.
2684 */
2685 static win_T *
2686qf_find_win_with_loclist(qf_info_T *ll)
2687{
2688 win_T *wp;
2689
2690 FOR_ALL_WINDOWS(wp)
2691 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2692 return wp;
2693
2694 return NULL;
2695}
2696
2697/*
2698 * Find a window containing a normal buffer
2699 */
2700 static win_T *
2701qf_find_win_with_normal_buf(void)
2702{
2703 win_T *wp;
2704
2705 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002706 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002707 return wp;
2708
2709 return NULL;
2710}
2711
2712/*
2713 * Go to a window in any tabpage containing the specified file. Returns TRUE
2714 * if successfully jumped to the window. Otherwise returns FALSE.
2715 */
2716 static int
2717qf_goto_tabwin_with_file(int fnum)
2718{
2719 tabpage_T *tp;
2720 win_T *wp;
2721
2722 FOR_ALL_TAB_WINDOWS(tp, wp)
2723 if (wp->w_buffer->b_fnum == fnum)
2724 {
2725 goto_tabpage_win(tp, wp);
2726 return TRUE;
2727 }
2728
2729 return FALSE;
2730}
2731
2732/*
2733 * Create a new window to show a file above the quickfix window. Called when
2734 * only the quickfix window is present.
2735 */
2736 static int
2737qf_open_new_file_win(qf_info_T *ll_ref)
2738{
2739 int flags;
2740
2741 flags = WSP_ABOVE;
2742 if (ll_ref != NULL)
2743 flags |= WSP_NEWLOC;
2744 if (win_split(0, flags) == FAIL)
2745 return FAIL; /* not enough room for window */
2746 p_swb = empty_option; /* don't split again */
2747 swb_flags = 0;
2748 RESET_BINDING(curwin);
2749 if (ll_ref != NULL)
2750 {
2751 /* The new window should use the location list from the
2752 * location list window */
2753 curwin->w_llist = ll_ref;
2754 ll_ref->qf_refcount++;
2755 }
2756 return OK;
2757}
2758
2759/*
2760 * Go to a window that shows the right buffer. If the window is not found, go
2761 * to the window just above the location list window. This is used for opening
2762 * a file from a location window and not from a quickfix window. If some usable
2763 * window is previously found, then it is supplied in 'use_win'.
2764 */
2765 static void
2766qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2767{
2768 win_T *win = use_win;
2769
2770 if (win == NULL)
2771 {
2772 /* Find the window showing the selected file */
2773 FOR_ALL_WINDOWS(win)
2774 if (win->w_buffer->b_fnum == qf_fnum)
2775 break;
2776 if (win == NULL)
2777 {
2778 /* Find a previous usable window */
2779 win = curwin;
2780 do
2781 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002782 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002783 break;
2784 if (win->w_prev == NULL)
2785 win = lastwin; /* wrap around the top */
2786 else
2787 win = win->w_prev; /* go to previous window */
2788 } while (win != curwin);
2789 }
2790 }
2791 win_goto(win);
2792
2793 /* If the location list for the window is not set, then set it
2794 * to the location list from the location window */
2795 if (win->w_llist == NULL)
2796 {
2797 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002798 if (ll_ref != NULL)
2799 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002800 }
2801}
2802
2803/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002804 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2805 * not found, then go to the window just above the quickfix window. This is
2806 * used for opening a file from a quickfix window and not from a location
2807 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002808 */
2809 static void
2810qf_goto_win_with_qfl_file(int qf_fnum)
2811{
2812 win_T *win;
2813 win_T *altwin;
2814
2815 win = curwin;
2816 altwin = NULL;
2817 for (;;)
2818 {
2819 if (win->w_buffer->b_fnum == qf_fnum)
2820 break;
2821 if (win->w_prev == NULL)
2822 win = lastwin; /* wrap around the top */
2823 else
2824 win = win->w_prev; /* go to previous window */
2825
2826 if (IS_QF_WINDOW(win))
2827 {
2828 /* Didn't find it, go to the window before the quickfix
2829 * window. */
2830 if (altwin != NULL)
2831 win = altwin;
2832 else if (curwin->w_prev != NULL)
2833 win = curwin->w_prev;
2834 else
2835 win = curwin->w_next;
2836 break;
2837 }
2838
2839 /* Remember a usable window. */
Bram Moolenaar91335e52018-08-01 17:53:12 +02002840 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002841 altwin = win;
2842 }
2843
2844 win_goto(win);
2845}
2846
2847/*
2848 * Find a suitable window for opening a file (qf_fnum) from the
2849 * quickfix/location list and jump to it. If the file is already opened in a
2850 * window, jump to it. Otherwise open a new window to display the file. This is
2851 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002852 */
2853 static int
2854qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2855{
2856 win_T *usable_win_ptr = NULL;
2857 int usable_win;
2858 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002859 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002860
2861 usable_win = 0;
2862
2863 ll_ref = curwin->w_llist_ref;
2864 if (ll_ref != NULL)
2865 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002866 /* Find a non-quickfix window with this location list */
2867 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2868 if (usable_win_ptr != NULL)
2869 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002870 }
2871
2872 if (!usable_win)
2873 {
2874 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002875 win = qf_find_win_with_normal_buf();
2876 if (win != NULL)
2877 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002878 }
2879
2880 /*
2881 * If no usable window is found and 'switchbuf' contains "usetab"
2882 * then search in other tabs.
2883 */
2884 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002885 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002886
2887 /*
2888 * If there is only one window and it is the quickfix window, create a
2889 * new one above the quickfix window.
2890 */
2891 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2892 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002893 if (qf_open_new_file_win(ll_ref) != OK)
2894 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002895 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002896 }
2897 else
2898 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002899 if (curwin->w_llist_ref != NULL) /* In a location window */
2900 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2901 else /* In a quickfix window */
2902 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002903 }
2904
2905 return OK;
2906}
2907
2908/*
2909 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002910 * Returns OK if successfully edited the file, FAIL on failing to open the
2911 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
2912 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002913 */
2914 static int
2915qf_jump_edit_buffer(
2916 qf_info_T *qi,
2917 qfline_T *qf_ptr,
2918 int forceit,
2919 win_T *oldwin,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002920 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002921{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002922 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002923 int retval = OK;
2924
2925 if (qf_ptr->qf_type == 1)
2926 {
2927 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2928 * set b_p_ro flag). */
2929 if (!can_abandon(curbuf, forceit))
2930 {
2931 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002932 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002933 }
2934 else
2935 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2936 ECMD_HIDE + ECMD_SET_HELP,
2937 oldwin == curwin ? curwin : NULL);
2938 }
2939 else
2940 {
2941 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002942 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002943
2944 retval = buflist_getfile(qf_ptr->qf_fnum,
2945 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002946
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002947 if (IS_LL_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002948 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002949 /*
2950 * Location list. Check whether the associated window is still
2951 * present and the list is still valid.
2952 */
2953 if (!win_valid_any_tab(oldwin))
2954 {
2955 EMSG(_("E924: Current window was closed"));
Bram Moolenaar3c097222017-12-21 20:54:49 +01002956 *opened_window = FALSE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002957 return NOTDONE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002958 }
2959 else if (!qflist_valid(oldwin, save_qfid))
2960 {
2961 EMSG(_(e_loc_list_changed));
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002962 return NOTDONE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002963 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002964 }
2965 else if (old_qf_curlist != qi->qf_curlist
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002966 || !is_qf_entry_present(qfl, qf_ptr))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002967 {
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002968 if (IS_QF_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002969 EMSG(_("E925: Current quickfix was changed"));
2970 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002971 EMSG(_(e_loc_list_changed));
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002972 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002973 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002974 }
2975
2976 return retval;
2977}
2978
2979/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002980 * Go to the error line in the current file using either line/column number or
2981 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002982 */
2983 static void
2984qf_jump_goto_line(
2985 linenr_T qf_lnum,
2986 int qf_col,
2987 char_u qf_viscol,
2988 char_u *qf_pattern)
2989{
2990 linenr_T i;
2991 char_u *line;
2992 colnr_T screen_col;
2993 colnr_T char_col;
2994
2995 if (qf_pattern == NULL)
2996 {
2997 /*
2998 * Go to line with error, unless qf_lnum is 0.
2999 */
3000 i = qf_lnum;
3001 if (i > 0)
3002 {
3003 if (i > curbuf->b_ml.ml_line_count)
3004 i = curbuf->b_ml.ml_line_count;
3005 curwin->w_cursor.lnum = i;
3006 }
3007 if (qf_col > 0)
3008 {
3009 curwin->w_cursor.col = qf_col - 1;
3010#ifdef FEAT_VIRTUALEDIT
3011 curwin->w_cursor.coladd = 0;
3012#endif
3013 if (qf_viscol == TRUE)
3014 {
3015 /*
3016 * Check each character from the beginning of the error
3017 * line up to the error column. For each tab character
3018 * found, reduce the error column value by the length of
3019 * a tab character.
3020 */
3021 line = ml_get_curline();
3022 screen_col = 0;
3023 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3024 {
3025 if (*line == NUL)
3026 break;
3027 if (*line++ == '\t')
3028 {
3029 curwin->w_cursor.col -= 7 - (screen_col % 8);
3030 screen_col += 8 - (screen_col % 8);
3031 }
3032 else
3033 ++screen_col;
3034 }
3035 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003036 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003037 check_cursor();
3038 }
3039 else
3040 beginline(BL_WHITE | BL_FIX);
3041 }
3042 else
3043 {
3044 pos_T save_cursor;
3045
3046 /* Move the cursor to the first line in the buffer */
3047 save_cursor = curwin->w_cursor;
3048 curwin->w_cursor.lnum = 0;
3049 if (!do_search(NULL, '/', qf_pattern, (long)1,
3050 SEARCH_KEEP, NULL, NULL))
3051 curwin->w_cursor = save_cursor;
3052 }
3053}
3054
3055/*
3056 * Display quickfix list index and size message
3057 */
3058 static void
3059qf_jump_print_msg(
3060 qf_info_T *qi,
3061 int qf_index,
3062 qfline_T *qf_ptr,
3063 buf_T *old_curbuf,
3064 linenr_T old_lnum)
3065{
3066 linenr_T i;
3067 int len;
3068
3069 /* Update the screen before showing the message, unless the screen
3070 * scrolled up. */
3071 if (!msg_scrolled)
3072 update_topline_redraw();
3073 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3074 qi->qf_lists[qi->qf_curlist].qf_count,
3075 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3076 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
3077 /* Add the message, skipping leading whitespace and newlines. */
3078 len = (int)STRLEN(IObuff);
3079 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3080
3081 /* Output the message. Overwrite to avoid scrolling when the 'O'
3082 * flag is present in 'shortmess'; But when not jumping, print the
3083 * whole message. */
3084 i = msg_scroll;
3085 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3086 msg_scroll = TRUE;
3087 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3088 msg_scroll = FALSE;
3089 msg_attr_keep(IObuff, 0, TRUE);
3090 msg_scroll = i;
3091}
3092
3093/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003094 * Find a usable window for opening a file from the quickfix/location list. If
3095 * a window is not found then open a new window.
3096 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3097 * able to jump/open a window. Returns NOTDONE if a file is not associated
3098 * with the entry.
3099 */
3100 static int
3101qf_jump_open_window(qf_info_T *qi, qfline_T *qf_ptr, int *opened_window)
3102{
3103 // For ":helpgrep" find a help window or open one.
3104 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
3105 if (jump_to_help_window(qi, opened_window) == FAIL)
3106 return FAIL;
3107
3108 // If currently in the quickfix window, find another window to show the
3109 // file in.
3110 if (bt_quickfix(curbuf) && !*opened_window)
3111 {
3112 // If there is no file specified, we don't know where to go.
3113 // But do advance, otherwise ":cn" gets stuck.
3114 if (qf_ptr->qf_fnum == 0)
3115 return NOTDONE;
3116
3117 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, opened_window) == FAIL)
3118 return FAIL;
3119 }
3120
3121 return OK;
3122}
3123
3124/*
3125 * Edit a selected file from the quickfix/location list and jump to a
3126 * particular line/column, adjust the folds and display a message about the
3127 * jump.
3128 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3129 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3130 * the file.
3131 */
3132 static int
3133qf_jump_to_buffer(
3134 qf_info_T *qi,
3135 int qf_index,
3136 qfline_T *qf_ptr,
3137 int forceit,
3138 win_T *oldwin,
3139 int *opened_window,
3140 int openfold,
3141 int print_message)
3142{
3143 buf_T *old_curbuf;
3144 linenr_T old_lnum;
3145 int retval = OK;
3146
3147 // If there is a file name, read the wanted file if needed, and check
3148 // autowrite etc.
3149 old_curbuf = curbuf;
3150 old_lnum = curwin->w_cursor.lnum;
3151
3152 if (qf_ptr->qf_fnum != 0)
3153 {
3154 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3155 opened_window);
3156 if (retval != OK)
3157 return retval;
3158 }
3159
3160 // When not switched to another buffer, still need to set pc mark
3161 if (curbuf == old_curbuf)
3162 setpcmark();
3163
3164 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3165 qf_ptr->qf_pattern);
3166
3167#ifdef FEAT_FOLDING
3168 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3169 foldOpenCursor();
3170#endif
3171 if (print_message)
3172 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3173
3174 return retval;
3175}
3176
3177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 * jump to a quickfix line
3179 * if dir == FORWARD go "errornr" valid entries forward
3180 * if dir == BACKWARD go "errornr" valid entries backward
3181 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3182 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3183 * else if "errornr" is zero, redisplay the same line
3184 * else go to entry "errornr"
3185 */
3186 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003187qf_jump(qf_info_T *qi,
3188 int dir,
3189 int errornr,
3190 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003192 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003193 qfline_T *qf_ptr;
3194 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003197 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003198 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003200 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003203 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003205 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003207 if (qi == NULL)
3208 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003209
3210 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003211 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 {
3213 EMSG(_(e_quickfix));
3214 return;
3215 }
3216
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003217 qfl = &qi->qf_lists[qi->qf_curlist];
3218
3219 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003221 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003223
3224 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3225 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003227 qf_ptr = old_qf_ptr;
3228 qf_index = old_qf_index;
3229 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003232 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003233 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003234 // No need to print the error message if it's visible in the error
3235 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 print_message = FALSE;
3237
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003238 retval = qf_jump_open_window(qi, qf_ptr, &opened_window);
3239 if (retval == FAIL)
3240 goto failed;
3241 if (retval == NOTDONE)
3242 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003243
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003244 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3245 &opened_window, old_KeyTyped, print_message);
3246 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003248 // Quickfix/location list is freed by an autocmd
3249 qi = NULL;
3250 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003253 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003256 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003257 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003259 // Couldn't open file, so put index back where it was. This could
3260 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 qf_ptr = old_qf_ptr;
3263 qf_index = old_qf_index;
3264 }
3265 }
3266theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003267 if (qi != NULL)
3268 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003269 qfl->qf_ptr = qf_ptr;
3270 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 if (p_swb != old_swb && opened_window)
3273 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003274 // Restore old 'switchbuf' value, but not when an autocommand or
3275 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003279 swb_flags = old_swb_flags;
3280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 else
3282 free_string_option(old_swb);
3283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284}
3285
3286/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003287 * Highlight attributes used for displaying entries from the quickfix list.
3288 */
3289static int qfFileAttr;
3290static int qfSepAttr;
3291static int qfLineAttr;
3292
3293/*
3294 * Display information about a single entry from the quickfix/location list.
3295 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003296 * 'cursel' will be set to TRUE for the currently selected entry in the
3297 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003298 */
3299 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003300qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003301{
3302 char_u *fname;
3303 buf_T *buf;
3304 int filter_entry;
3305
3306 fname = NULL;
3307 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3308 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3309 (char *)qfp->qf_module);
3310 else {
3311 if (qfp->qf_fnum != 0
3312 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3313 {
3314 fname = buf->b_fname;
3315 if (qfp->qf_type == 1) /* :helpgrep */
3316 fname = gettail(fname);
3317 }
3318 if (fname == NULL)
3319 sprintf((char *)IObuff, "%2d", qf_idx);
3320 else
3321 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3322 qf_idx, (char *)fname);
3323 }
3324
3325 // Support for filtering entries using :filter /pat/ clist
3326 // Match against the module name, file name, search pattern and
3327 // text of the entry.
3328 filter_entry = TRUE;
3329 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3330 filter_entry &= message_filtered(qfp->qf_module);
3331 if (filter_entry && fname != NULL)
3332 filter_entry &= message_filtered(fname);
3333 if (filter_entry && qfp->qf_pattern != NULL)
3334 filter_entry &= message_filtered(qfp->qf_pattern);
3335 if (filter_entry)
3336 filter_entry &= message_filtered(qfp->qf_text);
3337 if (filter_entry)
3338 return;
3339
3340 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003341 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003342
3343 if (qfp->qf_lnum != 0)
3344 msg_puts_attr((char_u *)":", qfSepAttr);
3345 if (qfp->qf_lnum == 0)
3346 IObuff[0] = NUL;
3347 else if (qfp->qf_col == 0)
3348 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3349 else
3350 sprintf((char *)IObuff, "%ld col %d",
3351 qfp->qf_lnum, qfp->qf_col);
3352 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3353 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3354 msg_puts_attr(IObuff, qfLineAttr);
3355 msg_puts_attr((char_u *)":", qfSepAttr);
3356 if (qfp->qf_pattern != NULL)
3357 {
3358 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3359 msg_puts(IObuff);
3360 msg_puts_attr((char_u *)":", qfSepAttr);
3361 }
3362 msg_puts((char_u *)" ");
3363
3364 /* Remove newlines and leading whitespace from the text. For an
3365 * unrecognized line keep the indent, the compiler may mark a word
3366 * with ^^^^. */
3367 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3368 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3369 IObuff, IOSIZE);
3370 msg_prt_line(IObuff, FALSE);
3371 out_flush(); /* show one line at a time */
3372}
3373
3374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003376 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 */
3378 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003379qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003381 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003382 qfline_T *qfp;
3383 int i;
3384 int idx1 = 1;
3385 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003386 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003387 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003388 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003390 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
Bram Moolenaar39665952018-08-15 20:59:48 +02003392 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003393 {
3394 qi = GET_LOC_LIST(curwin);
3395 if (qi == NULL)
3396 {
3397 EMSG(_(e_loclist));
3398 return;
3399 }
3400 }
3401
3402 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003403 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 {
3405 EMSG(_(e_quickfix));
3406 return;
3407 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003408 if (*arg == '+')
3409 {
3410 ++arg;
3411 plus = TRUE;
3412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3414 {
3415 EMSG(_(e_trailing));
3416 return;
3417 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003418 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003419 if (plus)
3420 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003421 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003422 idx2 = i + idx1;
3423 idx1 = i;
3424 }
3425 else
3426 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003427 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003428 if (idx1 < 0)
3429 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3430 if (idx2 < 0)
3431 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
Bram Moolenaara796d462018-05-01 14:30:36 +02003434 /* Shorten all the file names, so that it is easy to read */
3435 shorten_fnames(FALSE);
3436
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003437 /*
3438 * Get the attributes for the different quickfix highlight items. Note
3439 * that this depends on syntax items defined in the qf.vim syntax file
3440 */
3441 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3442 if (qfFileAttr == 0)
3443 qfFileAttr = HL_ATTR(HLF_D);
3444 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3445 if (qfSepAttr == 0)
3446 qfSepAttr = HL_ATTR(HLF_D);
3447 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3448 if (qfLineAttr == 0)
3449 qfLineAttr = HL_ATTR(HLF_N);
3450
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003451 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003453 qfp = qfl->qf_start;
3454 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 {
3456 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3457 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003458 if (got_int)
3459 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003460
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003461 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003463
3464 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003465 if (qfp == NULL)
3466 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003467 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 ui_breakcheck();
3469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470}
3471
3472/*
3473 * Remove newlines and leading whitespace from an error message.
3474 * Put the result in "buf[bufsize]".
3475 */
3476 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003477qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
3479 int i;
3480 char_u *p = text;
3481
3482 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3483 {
3484 if (*p == '\n')
3485 {
3486 buf[i] = ' ';
3487 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003488 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 break;
3490 }
3491 else
3492 buf[i] = *p++;
3493 }
3494 buf[i] = NUL;
3495}
3496
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003497/*
3498 * Display information (list number, list size and the title) about a
3499 * quickfix/location list.
3500 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003501 static void
3502qf_msg(qf_info_T *qi, int which, char *lead)
3503{
3504 char *title = (char *)qi->qf_lists[which].qf_title;
3505 int count = qi->qf_lists[which].qf_count;
3506 char_u buf[IOSIZE];
3507
3508 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3509 lead,
3510 which + 1,
3511 qi->qf_listcount,
3512 count);
3513
3514 if (title != NULL)
3515 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003516 size_t len = STRLEN(buf);
3517
3518 if (len < 34)
3519 {
3520 vim_memset(buf + len, ' ', 34 - len);
3521 buf[34] = NUL;
3522 }
3523 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003524 }
3525 trunc_string(buf, buf, Columns - 1, IOSIZE);
3526 msg(buf);
3527}
3528
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529/*
3530 * ":colder [count]": Up in the quickfix stack.
3531 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003532 * ":lolder [count]": Up in the location list stack.
3533 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 */
3535 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003536qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003538 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 int count;
3540
Bram Moolenaar39665952018-08-15 20:59:48 +02003541 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003542 {
3543 qi = GET_LOC_LIST(curwin);
3544 if (qi == NULL)
3545 {
3546 EMSG(_(e_loclist));
3547 return;
3548 }
3549 }
3550
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (eap->addr_count != 0)
3552 count = eap->line2;
3553 else
3554 count = 1;
3555 while (count--)
3556 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003557 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003559 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
3561 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003562 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003564 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566 else
3567 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003568 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
3570 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003571 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003573 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003576 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003577 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578}
3579
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003580/*
3581 * Display the information about all the quickfix/location lists in the stack
3582 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003583 void
3584qf_history(exarg_T *eap)
3585{
3586 qf_info_T *qi = &ql_info;
3587 int i;
3588
Bram Moolenaar39665952018-08-15 20:59:48 +02003589 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003590 qi = GET_LOC_LIST(curwin);
3591 if (qi == NULL || (qi->qf_listcount == 0
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003592 && qf_list_empty(qi, qi->qf_curlist)))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003593 MSG(_("No entries"));
3594 else
3595 for (i = 0; i < qi->qf_listcount; ++i)
3596 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3597}
3598
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003600 * Free all the entries in the error list "idx". Note that other information
3601 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 */
3603 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003604qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003606 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003607 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003608 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003610 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003612 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003613 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003614 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003615 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003616 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003617 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003618 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003619 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003620 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003621 if (stop)
3622 /* Somehow qf_count may have an incorrect value, set it to 1
3623 * to avoid crashing when it's wrong.
3624 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003625 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003626 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003627 qfl->qf_start = qfpnext;
3628 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003630
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003631 qfl->qf_index = 0;
3632 qfl->qf_start = NULL;
3633 qfl->qf_last = NULL;
3634 qfl->qf_ptr = NULL;
3635 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003636
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003637 qf_clean_dir_stack(&qfl->qf_dir_stack);
3638 qfl->qf_directory = NULL;
3639 qf_clean_dir_stack(&qfl->qf_file_stack);
3640 qfl->qf_currfile = NULL;
3641 qfl->qf_multiline = FALSE;
3642 qfl->qf_multiignore = FALSE;
3643 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644}
3645
3646/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003647 * Free error list "idx". Frees all the entries in the quickfix list,
3648 * associated context information and the title.
3649 */
3650 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003651qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003652{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003653 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003654
Bram Moolenaard23a8232018-02-10 18:45:26 +01003655 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003656 free_tv(qfl->qf_ctx);
3657 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003658 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003659 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003660}
3661
3662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 * qf_mark_adjust: adjust marks
3664 */
3665 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003666qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003667 win_T *wp,
3668 linenr_T line1,
3669 linenr_T line2,
3670 long amount,
3671 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003673 int i;
3674 qfline_T *qfp;
3675 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003676 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003677 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003678 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
Bram Moolenaarc1542742016-07-20 21:44:37 +02003680 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003681 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003682 if (wp != NULL)
3683 {
3684 if (wp->w_llist == NULL)
3685 return;
3686 qi = wp->w_llist;
3687 }
3688
3689 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003690 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003691 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003692 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3693 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 if (qfp->qf_fnum == curbuf->b_fnum)
3695 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003696 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3698 {
3699 if (amount == MAXLNUM)
3700 qfp->qf_cleared = TRUE;
3701 else
3702 qfp->qf_lnum += amount;
3703 }
3704 else if (amount_after && qfp->qf_lnum > line2)
3705 qfp->qf_lnum += amount_after;
3706 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003707
3708 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003709 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710}
3711
3712/*
3713 * Make a nice message out of the error character and the error number:
3714 * char number message
3715 * e or E 0 " error"
3716 * w or W 0 " warning"
3717 * i or I 0 " info"
3718 * 0 0 ""
3719 * other 0 " c"
3720 * e or E n " error n"
3721 * w or W n " warning n"
3722 * i or I n " info n"
3723 * 0 n " error n"
3724 * other n " c n"
3725 * 1 x "" :helpgrep
3726 */
3727 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003728qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
3730 static char_u buf[20];
3731 static char_u cc[3];
3732 char_u *p;
3733
3734 if (c == 'W' || c == 'w')
3735 p = (char_u *)" warning";
3736 else if (c == 'I' || c == 'i')
3737 p = (char_u *)" info";
3738 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3739 p = (char_u *)" error";
3740 else if (c == 0 || c == 1)
3741 p = (char_u *)"";
3742 else
3743 {
3744 cc[0] = ' ';
3745 cc[1] = c;
3746 cc[2] = NUL;
3747 p = cc;
3748 }
3749
3750 if (nr <= 0)
3751 return p;
3752
3753 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3754 return buf;
3755}
3756
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003758 * When "split" is FALSE: Open the entry/result under the cursor.
3759 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3760 */
3761 void
3762qf_view_result(int split)
3763{
3764 qf_info_T *qi = &ql_info;
3765
3766 if (!bt_quickfix(curbuf))
3767 return;
3768
3769 if (IS_LL_WINDOW(curwin))
3770 qi = GET_LOC_LIST(curwin);
3771
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003772 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003773 {
3774 EMSG(_(e_quickfix));
3775 return;
3776 }
3777
3778 if (split)
3779 {
3780 char_u cmd[32];
3781
3782 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3783 (long)curwin->w_cursor.lnum,
3784 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3785 if (do_cmdline_cmd(cmd) == OK)
3786 do_cmdline_cmd((char_u *) "clearjumps");
3787 return;
3788 }
3789
3790 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3791}
3792
3793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 * ":cwindow": open the quickfix window if we have errors to display,
3795 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003796 * ":lwindow": open the location list window if we have locations to display,
3797 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 */
3799 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003800ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003802 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 win_T *win;
3804
Bram Moolenaar39665952018-08-15 20:59:48 +02003805 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003806 {
3807 qi = GET_LOC_LIST(curwin);
3808 if (qi == NULL)
3809 return;
3810 }
3811
3812 /* Look for an existing quickfix window. */
3813 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
3815 /*
3816 * If a quickfix window is open but we have no errors to display,
3817 * close the window. If a quickfix window is not open, then open
3818 * it if we have errors; otherwise, leave it closed.
3819 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003820 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003821 || qf_list_empty(qi, qi->qf_curlist)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003822 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
3824 if (win != NULL)
3825 ex_cclose(eap);
3826 }
3827 else if (win == NULL)
3828 ex_copen(eap);
3829}
3830
3831/*
3832 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003833 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003836ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003838 win_T *win = NULL;
3839 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
Bram Moolenaar39665952018-08-15 20:59:48 +02003841 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003842 {
3843 qi = GET_LOC_LIST(curwin);
3844 if (qi == NULL)
3845 return;
3846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003848 /* Find existing quickfix window and close it. */
3849 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 if (win != NULL)
3851 win_close(win, FALSE);
3852}
3853
3854/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003855 * Set "w:quickfix_title" if "qi" has a title.
3856 */
3857 static void
3858qf_set_title_var(qf_list_T *qfl)
3859{
3860 if (qfl->qf_title != NULL)
3861 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3862}
3863
3864/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003865 * Goto a quickfix or location list window (if present).
3866 * Returns OK if the window is found, FAIL otherwise.
3867 */
3868 static int
3869qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3870{
3871 win_T *win;
3872
3873 win = qf_find_win(qi);
3874 if (win == NULL)
3875 return FAIL;
3876
3877 win_goto(win);
3878 if (resize)
3879 {
3880 if (vertsplit)
3881 {
3882 if (sz != win->w_width)
3883 win_setwidth(sz);
3884 }
3885 else if (sz != win->w_height)
3886 win_setheight(sz);
3887 }
3888
3889 return OK;
3890}
3891
3892/*
3893 * Open a new quickfix or location list window, load the quickfix buffer and
3894 * set the appropriate options for the window.
3895 * Returns FAIL if the window could not be opened.
3896 */
3897 static int
3898qf_open_new_cwindow(qf_info_T *qi, int height)
3899{
3900 buf_T *qf_buf;
3901 win_T *oldwin = curwin;
3902 tabpage_T *prevtab = curtab;
3903 int flags = 0;
3904 win_T *win;
3905
3906 qf_buf = qf_find_buf(qi);
3907
3908 // The current window becomes the previous window afterwards.
3909 win = curwin;
3910
3911 if (IS_QF_STACK(qi) && cmdmod.split == 0)
3912 // Create the new quickfix window at the very bottom, except when
3913 // :belowright or :aboveleft is used.
3914 win_goto(lastwin);
3915 // Default is to open the window below the current window
3916 if (cmdmod.split == 0)
3917 flags = WSP_BELOW;
3918 flags |= WSP_NEWLOC;
3919 if (win_split(height, flags) == FAIL)
3920 return FAIL; // not enough room for window
3921 RESET_BINDING(curwin);
3922
3923 if (IS_LL_STACK(qi))
3924 {
3925 // For the location list window, create a reference to the
3926 // location list from the window 'win'.
3927 curwin->w_llist_ref = win->w_llist;
3928 win->w_llist->qf_refcount++;
3929 }
3930
3931 if (oldwin != curwin)
3932 oldwin = NULL; // don't store info when in another window
3933 if (qf_buf != NULL)
3934 {
3935 // Use the existing quickfix buffer
3936 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
3937 ECMD_HIDE + ECMD_OLDBUF, oldwin);
3938 }
3939 else
3940 {
3941 // Create a new quickfix buffer
3942 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
3943
3944 // switch off 'swapfile'
3945 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3946 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
3947 OPT_LOCAL);
3948 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
3949 RESET_BINDING(curwin);
3950#ifdef FEAT_DIFF
3951 curwin->w_p_diff = FALSE;
3952#endif
3953#ifdef FEAT_FOLDING
3954 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3955 OPT_LOCAL);
3956#endif
3957 }
3958
3959 // Only set the height when still in the same tab page and there is no
3960 // window to the side.
3961 if (curtab == prevtab && curwin->w_width == Columns)
3962 win_setheight(height);
3963 curwin->w_p_wfh = TRUE; // set 'winfixheight'
3964 if (win_valid(win))
3965 prevwin = win;
3966
3967 return OK;
3968}
3969
3970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003972 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 */
3974 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003975ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003977 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003979 int status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
Bram Moolenaar39665952018-08-15 20:59:48 +02003981 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003982 {
3983 qi = GET_LOC_LIST(curwin);
3984 if (qi == NULL)
3985 {
3986 EMSG(_(e_loclist));
3987 return;
3988 }
3989 }
3990
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 if (eap->addr_count != 0)
3992 height = eap->line2;
3993 else
3994 height = QF_WINHEIGHT;
3995
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003996 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997#ifdef FEAT_GUI
3998 need_mouse_correct = TRUE;
3999#endif
4000
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004001 // Find an existing quickfix window, or open a new one.
4002 if (cmdmod.tab == 0)
4003 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4004 cmdmod.split & WSP_VERT);
4005 if (status == FAIL)
4006 if (qf_open_new_cwindow(qi, height) == FAIL)
4007 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004009 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004010
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004011 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004012 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004014 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 curwin->w_cursor.col = 0;
4016 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004017 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018}
4019
4020/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004021 * Move the cursor in the quickfix window to "lnum".
4022 */
4023 static void
4024qf_win_goto(win_T *win, linenr_T lnum)
4025{
4026 win_T *old_curwin = curwin;
4027
4028 curwin = win;
4029 curbuf = win->w_buffer;
4030 curwin->w_cursor.lnum = lnum;
4031 curwin->w_cursor.col = 0;
4032#ifdef FEAT_VIRTUALEDIT
4033 curwin->w_cursor.coladd = 0;
4034#endif
4035 curwin->w_curswant = 0;
4036 update_topline(); /* scroll to show the line */
4037 redraw_later(VALID);
4038 curwin->w_redr_status = TRUE; /* update ruler */
4039 curwin = old_curwin;
4040 curbuf = curwin->w_buffer;
4041}
4042
4043/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004044 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004045 */
4046 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004047ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004048{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004049 qf_info_T *qi = &ql_info;
4050 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004051
Bram Moolenaar39665952018-08-15 20:59:48 +02004052 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004053 {
4054 qi = GET_LOC_LIST(curwin);
4055 if (qi == NULL)
4056 {
4057 EMSG(_(e_loclist));
4058 return;
4059 }
4060 }
4061
4062 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004063 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4064 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4065}
4066
4067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 * Return the number of the current entry (line number in the quickfix
4069 * window).
4070 */
4071 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004072qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004074 qf_info_T *qi = &ql_info;
4075
4076 if (IS_LL_WINDOW(wp))
4077 /* In the location list window, use the referenced location list */
4078 qi = wp->w_llist_ref;
4079
4080 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081}
4082
4083/*
4084 * Update the cursor position in the quickfix window to the current error.
4085 * Return TRUE if there is a quickfix window.
4086 */
4087 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004088qf_win_pos_update(
4089 qf_info_T *qi,
4090 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
4092 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004093 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
4095 /*
4096 * Put the cursor on the current error in the quickfix window, so that
4097 * it's viewable.
4098 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004099 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (win != NULL
4101 && qf_index <= win->w_buffer->b_ml.ml_line_count
4102 && old_qf_index != qf_index)
4103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 if (qf_index > old_qf_index)
4105 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004106 win->w_redraw_top = old_qf_index;
4107 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109 else
4110 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004111 win->w_redraw_top = qf_index;
4112 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004114 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116 return win != NULL;
4117}
4118
4119/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004120 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004121 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004122 */
4123 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004124is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004125{
4126 /*
4127 * A window displaying the quickfix buffer will have the w_llist_ref field
4128 * set to NULL.
4129 * A window displaying a location list buffer will have the w_llist_ref
4130 * pointing to the location list.
4131 */
4132 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004133 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4134 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004135 return TRUE;
4136
4137 return FALSE;
4138}
4139
4140/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004141 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004142 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004143 */
4144 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004145qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004146{
4147 win_T *win;
4148
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004149 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004150 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004151 return win;
4152 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004153}
4154
4155/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004156 * Find a quickfix buffer.
4157 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 */
4159 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004160qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004162 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004163 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
Bram Moolenaar9c102382006-05-03 21:26:49 +00004165 FOR_ALL_TAB_WINDOWS(tp, win)
4166 if (is_qf_win(win, qi))
4167 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004168
Bram Moolenaar9c102382006-05-03 21:26:49 +00004169 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170}
4171
4172/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004173 * Update the w:quickfix_title variable in the quickfix/location list window
4174 */
4175 static void
4176qf_update_win_titlevar(qf_info_T *qi)
4177{
4178 win_T *win;
4179 win_T *curwin_save;
4180
4181 if ((win = qf_find_win(qi)) != NULL)
4182 {
4183 curwin_save = curwin;
4184 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004185 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004186 curwin = curwin_save;
4187 }
4188}
4189
4190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 * Find the quickfix buffer. If it exists, update the contents.
4192 */
4193 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004194qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195{
4196 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004197 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
4200 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004201 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 if (buf != NULL)
4203 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004204 linenr_T old_line_count = buf->b_ml.ml_line_count;
4205
4206 if (old_last == NULL)
4207 /* set curwin/curbuf to buf and save a few things */
4208 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
Bram Moolenaard823fa92016-08-12 16:29:27 +02004210 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004211
Bram Moolenaar864293a2016-06-02 13:40:04 +02004212 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004213 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004214
Bram Moolenaar864293a2016-06-02 13:40:04 +02004215 if (old_last == NULL)
4216 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004217 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004218
4219 /* restore curwin/curbuf and a few other things */
4220 aucmd_restbuf(&aco);
4221 }
4222
4223 /* Only redraw when added lines are visible. This avoids flickering
4224 * when the added lines are not visible. */
4225 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4226 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228}
4229
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004230/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004231 * Add an error line to the quickfix buffer.
4232 */
4233 static int
4234qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4235{
4236 int len;
4237 buf_T *errbuf;
4238
4239 if (qfp->qf_module != NULL)
4240 {
4241 STRCPY(IObuff, qfp->qf_module);
4242 len = (int)STRLEN(IObuff);
4243 }
4244 else if (qfp->qf_fnum != 0
4245 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4246 && errbuf->b_fname != NULL)
4247 {
4248 if (qfp->qf_type == 1) /* :helpgrep */
4249 STRCPY(IObuff, gettail(errbuf->b_fname));
4250 else
4251 {
4252 /* shorten the file name if not done already */
4253 if (errbuf->b_sfname == NULL
4254 || mch_isFullName(errbuf->b_sfname))
4255 {
4256 if (*dirname == NUL)
4257 mch_dirname(dirname, MAXPATHL);
4258 shorten_buf_fname(errbuf, dirname, FALSE);
4259 }
4260 STRCPY(IObuff, errbuf->b_fname);
4261 }
4262 len = (int)STRLEN(IObuff);
4263 }
4264 else
4265 len = 0;
4266 IObuff[len++] = '|';
4267
4268 if (qfp->qf_lnum > 0)
4269 {
4270 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4271 len += (int)STRLEN(IObuff + len);
4272
4273 if (qfp->qf_col > 0)
4274 {
4275 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4276 len += (int)STRLEN(IObuff + len);
4277 }
4278
4279 sprintf((char *)IObuff + len, "%s",
4280 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4281 len += (int)STRLEN(IObuff + len);
4282 }
4283 else if (qfp->qf_pattern != NULL)
4284 {
4285 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4286 len += (int)STRLEN(IObuff + len);
4287 }
4288 IObuff[len++] = '|';
4289 IObuff[len++] = ' ';
4290
4291 /* Remove newlines and leading whitespace from the text.
4292 * For an unrecognized line keep the indent, the compiler may
4293 * mark a word with ^^^^. */
4294 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4295 IObuff + len, IOSIZE - len);
4296
4297 if (ml_append_buf(buf, lnum, IObuff,
4298 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4299 return FAIL;
4300
4301 return OK;
4302}
4303
4304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 * Fill current buffer with quickfix errors, replacing any previous contents.
4306 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004307 * If "old_last" is not NULL append the items after this one.
4308 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4309 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 */
4311 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004312qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004314 linenr_T lnum;
4315 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004316 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317
Bram Moolenaar864293a2016-06-02 13:40:04 +02004318 if (old_last == NULL)
4319 {
4320 if (buf != curbuf)
4321 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004322 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004323 return;
4324 }
4325
4326 /* delete all existing lines */
4327 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4328 (void)ml_delete((linenr_T)1, FALSE);
4329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
4331 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004332 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 {
Bram Moolenaara796d462018-05-01 14:30:36 +02004334 char_u dirname[MAXPATHL];
4335
4336 *dirname = NUL;
4337
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004339 if (old_last == NULL)
4340 {
4341 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4342 lnum = 0;
4343 }
4344 else
4345 {
4346 qfp = old_last->qf_next;
4347 lnum = buf->b_ml.ml_line_count;
4348 }
4349 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004351 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004353
Bram Moolenaar864293a2016-06-02 13:40:04 +02004354 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004356 if (qfp == NULL)
4357 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004359
4360 if (old_last == NULL)
4361 /* Delete the empty line which is now at the end */
4362 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364
4365 /* correct cursor position */
4366 check_lnums(TRUE);
4367
Bram Moolenaar864293a2016-06-02 13:40:04 +02004368 if (old_last == NULL)
4369 {
4370 /* Set the 'filetype' to "qf" each time after filling the buffer.
4371 * This resembles reading a file into a buffer, it's more logical when
4372 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02004373 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004374 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4375 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
Bram Moolenaar864293a2016-06-02 13:40:04 +02004377 keep_filetype = TRUE; /* don't detect 'filetype' */
4378 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004380 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004382 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004383 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004384
Bram Moolenaar864293a2016-06-02 13:40:04 +02004385 /* make sure it will be redrawn */
4386 redraw_curbuf_later(NOT_VALID);
4387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
4389 /* Restore KeyTyped, setting 'filetype' may reset it. */
4390 KeyTyped = old_KeyTyped;
4391}
4392
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004393/*
4394 * For every change made to the quickfix list, update the changed tick.
4395 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004396 static void
4397qf_list_changed(qf_info_T *qi, int qf_idx)
4398{
4399 qi->qf_lists[qf_idx].qf_changedtick++;
4400}
4401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004403 * Return the quickfix/location list number with the given identifier.
4404 * Returns -1 if list is not found.
4405 */
4406 static int
4407qf_id2nr(qf_info_T *qi, int_u qfid)
4408{
4409 int qf_idx;
4410
4411 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4412 if (qi->qf_lists[qf_idx].qf_id == qfid)
4413 return qf_idx;
4414 return INVALID_QFIDX;
4415}
4416
4417/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004418 * If the current list is not "save_qfid" and we can find the list with that ID
4419 * then make it the current list.
4420 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004421 * Returns OK if successfully restored the list. Returns FAIL if the list with
4422 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004423 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004424 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004425qf_restore_list(qf_info_T *qi, int_u save_qfid)
4426{
4427 int curlist;
4428
4429 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4430 {
4431 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004432 if (curlist < 0)
4433 // list is not present
4434 return FAIL;
4435 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004436 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004437 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004438}
4439
4440/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004441 * Jump to the first entry if there is one.
4442 */
4443 static void
4444qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4445{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004446 if (qf_restore_list(qi, save_qfid) == FAIL)
4447 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004448
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004449 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004450 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004451 qf_jump(qi, 0, 0, forceit);
4452}
4453
4454/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004455 * Return TRUE when using ":vimgrep" for ":grep".
4456 */
4457 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004458grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004459{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004460 return ((cmdidx == CMD_grep
4461 || cmdidx == CMD_lgrep
4462 || cmdidx == CMD_grepadd
4463 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004464 && STRCMP("internal",
4465 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4466}
4467
4468/*
Bram Moolenaara6557602006-02-04 22:43:20 +00004469 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 */
4471 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004472ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473{
Bram Moolenaar7c626922005-02-07 22:01:03 +00004474 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004476 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00004478 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004479 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004480 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004481 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004482 int_u save_qfid;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004483
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004484 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
4485 if (grep_internal(eap->cmdidx))
4486 {
4487 ex_vimgrep(eap);
4488 return;
4489 }
4490
Bram Moolenaar7c626922005-02-07 22:01:03 +00004491 switch (eap->cmdidx)
4492 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00004493 case CMD_make: au_name = (char_u *)"make"; break;
4494 case CMD_lmake: au_name = (char_u *)"lmake"; break;
4495 case CMD_grep: au_name = (char_u *)"grep"; break;
4496 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4497 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4498 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004499 default: break;
4500 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004501 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4502 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004503 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004504#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004505 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004506 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004507#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004508 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004509#ifdef FEAT_MBYTE
4510 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512
Bram Moolenaar39665952018-08-15 20:59:48 +02004513 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00004514 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004515
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00004517 fname = get_mef_name();
4518 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004520 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
4522 /*
4523 * If 'shellpipe' empty: don't redirect to 'errorfile'.
4524 */
4525 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
4526 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00004527 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 cmd = alloc(len);
4529 if (cmd == NULL)
4530 return;
4531 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
4532 (char *)p_shq);
4533 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004534 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 /*
4536 * Output a newline if there's something else than the :make command that
4537 * was typed (in which case the cursor is in column 0).
4538 */
4539 if (msg_col == 0)
4540 msg_didout = FALSE;
4541 msg_start();
4542 MSG_PUTS(":!");
4543 msg_outtrans(cmd); /* show what we are doing */
4544
4545 /* let the shell know if we are redirecting output or not */
4546 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4547
4548#ifdef AMIGA
4549 out_flush();
4550 /* read window status report and redraw before message */
4551 (void)char_avail();
4552#endif
4553
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004554 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00004555 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4556 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004557 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004558 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004559 if (wp != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004560 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004561 qi = GET_LOC_LIST(wp);
4562 if (qi == NULL)
4563 goto cleanup;
4564 }
4565 if (res >= 0)
4566 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004567
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004568 // Remember the current quickfix list identifier, so that we can
4569 // check for autocommands changing the current quickfix list.
4570 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4571 if (au_name != NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004572 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4573 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004574 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004575 // display the first error
4576 qf_jump_first(qi, save_qfid, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004578cleanup:
Bram Moolenaar7c626922005-02-07 22:01:03 +00004579 mch_remove(fname);
4580 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 vim_free(cmd);
4582}
4583
4584/*
4585 * Return the name for the errorfile, in allocated memory.
4586 * Find a new unique name when 'makeef' contains "##".
4587 * Returns NULL for error.
4588 */
4589 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004590get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591{
4592 char_u *p;
4593 char_u *name;
4594 static int start = -1;
4595 static int off = 0;
4596#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004597 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598#endif
4599
4600 if (*p_mef == NUL)
4601 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004602 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 if (name == NULL)
4604 EMSG(_(e_notmp));
4605 return name;
4606 }
4607
4608 for (p = p_mef; *p; ++p)
4609 if (p[0] == '#' && p[1] == '#')
4610 break;
4611
4612 if (*p == NUL)
4613 return vim_strsave(p_mef);
4614
4615 /* Keep trying until the name doesn't exist yet. */
4616 for (;;)
4617 {
4618 if (start == -1)
4619 start = mch_get_pid();
4620 else
4621 off += 19;
4622
4623 name = alloc((unsigned)STRLEN(p_mef) + 30);
4624 if (name == NULL)
4625 break;
4626 STRCPY(name, p_mef);
4627 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4628 STRCAT(name, p + 2);
4629 if (mch_getperm(name) < 0
4630#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02004631 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 && mch_lstat((char *)name, &sb) < 0
4633#endif
4634 )
4635 break;
4636 vim_free(name);
4637 }
4638 return name;
4639}
4640
4641/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004642 * Returns the number of valid entries in the current quickfix/location list.
4643 */
4644 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004645qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004646{
4647 qf_info_T *qi = &ql_info;
4648 qfline_T *qfp;
4649 int i, sz = 0;
4650 int prev_fnum = 0;
4651
Bram Moolenaar39665952018-08-15 20:59:48 +02004652 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004653 {
4654 /* Location list */
4655 qi = GET_LOC_LIST(curwin);
4656 if (qi == NULL)
4657 return 0;
4658 }
4659
4660 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004661 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004662 ++i, qfp = qfp->qf_next)
4663 {
4664 if (qfp->qf_valid)
4665 {
4666 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4667 sz++; /* Count all valid entries */
4668 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4669 {
4670 /* Count the number of files */
4671 sz++;
4672 prev_fnum = qfp->qf_fnum;
4673 }
4674 }
4675 }
4676
4677 return sz;
4678}
4679
4680/*
4681 * Returns the current index of the quickfix/location list.
4682 * Returns 0 if there is an error.
4683 */
4684 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004685qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004686{
4687 qf_info_T *qi = &ql_info;
4688
Bram Moolenaar39665952018-08-15 20:59:48 +02004689 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004690 {
4691 /* Location list */
4692 qi = GET_LOC_LIST(curwin);
4693 if (qi == NULL)
4694 return 0;
4695 }
4696
4697 return qi->qf_lists[qi->qf_curlist].qf_index;
4698}
4699
4700/*
4701 * Returns the current index in the quickfix/location list (counting only valid
4702 * entries). If no valid entries are in the list, then returns 1.
4703 */
4704 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004705qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004706{
4707 qf_info_T *qi = &ql_info;
4708 qf_list_T *qfl;
4709 qfline_T *qfp;
4710 int i, eidx = 0;
4711 int prev_fnum = 0;
4712
Bram Moolenaar39665952018-08-15 20:59:48 +02004713 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004714 {
4715 /* Location list */
4716 qi = GET_LOC_LIST(curwin);
4717 if (qi == NULL)
4718 return 1;
4719 }
4720
4721 qfl = &qi->qf_lists[qi->qf_curlist];
4722 qfp = qfl->qf_start;
4723
4724 /* check if the list has valid errors */
4725 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4726 return 1;
4727
4728 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4729 {
4730 if (qfp->qf_valid)
4731 {
4732 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4733 {
4734 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4735 {
4736 /* Count the number of files */
4737 eidx++;
4738 prev_fnum = qfp->qf_fnum;
4739 }
4740 }
4741 else
4742 eidx++;
4743 }
4744 }
4745
4746 return eidx ? eidx : 1;
4747}
4748
4749/*
4750 * Get the 'n'th valid error entry in the quickfix or location list.
4751 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4752 * For :cdo and :ldo returns the 'n'th valid error entry.
4753 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4754 */
4755 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004756qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004757{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004758 qfline_T *qfp = qfl->qf_start;
4759 int i, eidx;
4760 int prev_fnum = 0;
4761
4762 /* check if the list has valid errors */
4763 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4764 return 1;
4765
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004766 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004767 i++, qfp = qfp->qf_next)
4768 {
4769 if (qfp->qf_valid)
4770 {
4771 if (fdo)
4772 {
4773 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4774 {
4775 /* Count the number of files */
4776 eidx++;
4777 prev_fnum = qfp->qf_fnum;
4778 }
4779 }
4780 else
4781 eidx++;
4782 }
4783
4784 if (eidx == n)
4785 break;
4786 }
4787
4788 if (i <= qfl->qf_count)
4789 return i;
4790 else
4791 return 1;
4792}
4793
4794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004796 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004797 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 */
4799 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004800ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004802 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004803 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004804
Bram Moolenaar39665952018-08-15 20:59:48 +02004805 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004806 {
4807 qi = GET_LOC_LIST(curwin);
4808 if (qi == NULL)
4809 {
4810 EMSG(_(e_loclist));
4811 return;
4812 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004813 }
4814
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004815 if (eap->addr_count > 0)
4816 errornr = (int)eap->line2;
4817 else
4818 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004819 switch (eap->cmdidx)
4820 {
4821 case CMD_cc: case CMD_ll:
4822 errornr = 0;
4823 break;
4824 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4825 case CMD_lfirst:
4826 errornr = 1;
4827 break;
4828 default:
4829 errornr = 32767;
4830 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004831 }
4832
4833 /* For cdo and ldo commands, jump to the nth valid error.
4834 * For cfdo and lfdo commands, jump to the nth valid file entry.
4835 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004836 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4837 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004838 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004839 eap->addr_count > 0 ? (int)eap->line1 : 1,
4840 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4841
4842 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843}
4844
4845/*
4846 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004847 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004848 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 */
4850 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004851ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004853 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004854 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004855 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004856
Bram Moolenaar39665952018-08-15 20:59:48 +02004857 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004858 {
4859 qi = GET_LOC_LIST(curwin);
4860 if (qi == NULL)
4861 {
4862 EMSG(_(e_loclist));
4863 return;
4864 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004865 }
4866
Bram Moolenaar55b69262017-08-13 13:42:01 +02004867 if (eap->addr_count > 0
4868 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4869 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004870 errornr = (int)eap->line2;
4871 else
4872 errornr = 1;
4873
Bram Moolenaar39665952018-08-15 20:59:48 +02004874 // Depending on the command jump to either next or previous entry/file.
4875 switch (eap->cmdidx)
4876 {
4877 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4878 dir = FORWARD;
4879 break;
4880 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4881 case CMD_lNext:
4882 dir = BACKWARD;
4883 break;
4884 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4885 dir = FORWARD_FILE;
4886 break;
4887 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4888 dir = BACKWARD_FILE;
4889 break;
4890 default:
4891 dir = FORWARD;
4892 break;
4893 }
4894
4895 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896}
4897
4898/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004899 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004900 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 */
4902 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004903ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004905 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004906 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004907 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004908 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004909 int_u save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004910 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004911
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004912 switch (eap->cmdidx)
4913 {
4914 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4915 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4916 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4917 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4918 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4919 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4920 default: break;
4921 }
4922 if (au_name != NULL)
4923 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004924#ifdef FEAT_MBYTE
4925 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4926#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004927#ifdef FEAT_BROWSE
4928 if (cmdmod.browse)
4929 {
4930 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004931 NULL, NULL,
4932 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004933 if (browse_file == NULL)
4934 return;
4935 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4936 vim_free(browse_file);
4937 }
4938 else
4939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004941 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004942
Bram Moolenaar39665952018-08-15 20:59:48 +02004943 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004944 wp = curwin;
4945
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004946 /*
4947 * This function is used by the :cfile, :cgetfile and :caddfile
4948 * commands.
4949 * :cfile always creates a new quickfix list and jumps to the
4950 * first error.
4951 * :cgetfile creates a new quickfix list but doesn't jump to the
4952 * first error.
4953 * :caddfile adds to an existing quickfix list. If there is no
4954 * quickfix list then a new list is created.
4955 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004956 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004957 && eap->cmdidx != CMD_laddfile),
4958 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004959 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004960 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01004961 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004962 if (qi == NULL)
4963 return;
4964 }
4965 if (res >= 0)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004966 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004967 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004968 if (au_name != NULL)
4969 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004970
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004971 // Jump to the first error for a new list and if autocmds didn't
4972 // free the list.
4973 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
4974 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004975 // display the first error
4976 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004977}
4978
4979/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004980 * Return the vimgrep autocmd name.
4981 */
4982 static char_u *
4983vgr_get_auname(cmdidx_T cmdidx)
4984{
4985 switch (cmdidx)
4986 {
4987 case CMD_vimgrep: return (char_u *)"vimgrep";
4988 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4989 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4990 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4991 case CMD_grep: return (char_u *)"grep";
4992 case CMD_lgrep: return (char_u *)"lgrep";
4993 case CMD_grepadd: return (char_u *)"grepadd";
4994 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4995 default: return NULL;
4996 }
4997}
4998
4999/*
5000 * Initialize the regmatch used by vimgrep for pattern "s".
5001 */
5002 static void
5003vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5004{
5005 /* Get the search pattern: either white-separated or enclosed in // */
5006 regmatch->regprog = NULL;
5007
5008 if (s == NULL || *s == NUL)
5009 {
5010 /* Pattern is empty, use last search pattern. */
5011 if (last_search_pat() == NULL)
5012 {
5013 EMSG(_(e_noprevre));
5014 return;
5015 }
5016 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5017 }
5018 else
5019 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5020
5021 regmatch->rmm_ic = p_ic;
5022 regmatch->rmm_maxcol = 0;
5023}
5024
5025/*
5026 * Display a file name when vimgrep is running.
5027 */
5028 static void
5029vgr_display_fname(char_u *fname)
5030{
5031 char_u *p;
5032
5033 msg_start();
5034 p = msg_strtrunc(fname, TRUE);
5035 if (p == NULL)
5036 msg_outtrans(fname);
5037 else
5038 {
5039 msg_outtrans(p);
5040 vim_free(p);
5041 }
5042 msg_clr_eos();
5043 msg_didout = FALSE; /* overwrite this message */
5044 msg_nowait = TRUE; /* don't wait for this message */
5045 msg_col = 0;
5046 out_flush();
5047}
5048
5049/*
5050 * Load a dummy buffer to search for a pattern using vimgrep.
5051 */
5052 static buf_T *
5053vgr_load_dummy_buf(
5054 char_u *fname,
5055 char_u *dirname_start,
5056 char_u *dirname_now)
5057{
5058 int save_mls;
5059#if defined(FEAT_SYN_HL)
5060 char_u *save_ei = NULL;
5061#endif
5062 buf_T *buf;
5063
5064#if defined(FEAT_SYN_HL)
5065 /* Don't do Filetype autocommands to avoid loading syntax and
5066 * indent scripts, a great speed improvement. */
5067 save_ei = au_event_disable(",Filetype");
5068#endif
5069 /* Don't use modelines here, it's useless. */
5070 save_mls = p_mls;
5071 p_mls = 0;
5072
5073 /* Load file into a buffer, so that 'fileencoding' is detected,
5074 * autocommands applied, etc. */
5075 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5076
5077 p_mls = save_mls;
5078#if defined(FEAT_SYN_HL)
5079 au_event_restore(save_ei);
5080#endif
5081
5082 return buf;
5083}
5084
5085/*
5086 * Check whether a quickfix/location list valid. Autocmds may remove or change
5087 * a quickfix list when vimgrep is running. If the list is not found, create a
5088 * new list.
5089 */
5090 static int
5091vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005092 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005093 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005094 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005095 char_u *title)
5096{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005097 /* Verify that the quickfix/location list was not freed by an autocmd */
5098 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005099 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005100 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005101 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005102 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005103 EMSG(_(e_loc_list_changed));
5104 return FALSE;
5105 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005106 else
5107 {
5108 /* Quickfix list is not found, create a new one. */
5109 qf_new_list(qi, title);
5110 return TRUE;
5111 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005112 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005113
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005114 if (qf_restore_list(qi, qfid) == FAIL)
5115 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005116
5117 return TRUE;
5118}
5119
5120/*
5121 * Search for a pattern in all the lines in a buffer and add the matching lines
5122 * to a quickfix list.
5123 */
5124 static int
5125vgr_match_buflines(
5126 qf_info_T *qi,
5127 char_u *fname,
5128 buf_T *buf,
5129 regmmatch_T *regmatch,
5130 long tomatch,
5131 int duplicate_name,
5132 int flags)
5133{
5134 int found_match = FALSE;
5135 long lnum;
5136 colnr_T col;
5137
5138 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5139 {
5140 col = 0;
5141 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5142 col, NULL, NULL) > 0)
5143 {
5144 /* Pass the buffer number so that it gets used even for a
5145 * dummy buffer, unless duplicate_name is set, then the
5146 * buffer will be wiped out below. */
5147 if (qf_add_entry(qi,
5148 qi->qf_curlist,
5149 NULL, /* dir */
5150 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005151 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005152 duplicate_name ? 0 : buf->b_fnum,
5153 ml_get_buf(buf,
5154 regmatch->startpos[0].lnum + lnum, FALSE),
5155 regmatch->startpos[0].lnum + lnum,
5156 regmatch->startpos[0].col + 1,
5157 FALSE, /* vis_col */
5158 NULL, /* search pattern */
5159 0, /* nr */
5160 0, /* type */
5161 TRUE /* valid */
5162 ) == FAIL)
5163 {
5164 got_int = TRUE;
5165 break;
5166 }
5167 found_match = TRUE;
5168 if (--tomatch == 0)
5169 break;
5170 if ((flags & VGR_GLOBAL) == 0
5171 || regmatch->endpos[0].lnum > 0)
5172 break;
5173 col = regmatch->endpos[0].col
5174 + (col == regmatch->endpos[0].col);
5175 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5176 break;
5177 }
5178 line_breakcheck();
5179 if (got_int)
5180 break;
5181 }
5182
5183 return found_match;
5184}
5185
5186/*
5187 * Jump to the first match and update the directory.
5188 */
5189 static void
5190vgr_jump_to_match(
5191 qf_info_T *qi,
5192 int forceit,
5193 int *redraw_for_dummy,
5194 buf_T *first_match_buf,
5195 char_u *target_dir)
5196{
5197 buf_T *buf;
5198
5199 buf = curbuf;
5200 qf_jump(qi, 0, 0, forceit);
5201 if (buf != curbuf)
5202 /* If we jumped to another buffer redrawing will already be
5203 * taken care of. */
5204 *redraw_for_dummy = FALSE;
5205
5206 /* Jump to the directory used after loading the buffer. */
5207 if (curbuf == first_match_buf && target_dir != NULL)
5208 {
5209 exarg_T ea;
5210
5211 ea.arg = target_dir;
5212 ea.cmdidx = CMD_lcd;
5213 ex_cd(&ea);
5214 }
5215}
5216
5217/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005218 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005219 * ":vimgrepadd {pattern} file(s)"
5220 * ":lvimgrep {pattern} file(s)"
5221 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005222 */
5223 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005224ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005225{
Bram Moolenaar81695252004-12-29 20:58:21 +00005226 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005227 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005228 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005229 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005230 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005231 char_u *s;
5232 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005233 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005234 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005235 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005236 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005237 buf_T *buf;
5238 int duplicate_name = FALSE;
5239 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005240 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005241 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005242 buf_T *first_match_buf = NULL;
5243 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005244 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005245 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005246 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005247 char_u *dirname_start = NULL;
5248 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005249 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005250 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005251
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005252 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005253 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5254 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005255 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005256#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005257 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005258 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005259#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005260 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005261
Bram Moolenaar39665952018-08-15 20:59:48 +02005262 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005263 {
5264 qi = ll_get_or_alloc_list(curwin);
5265 if (qi == NULL)
5266 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005267 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005268 }
5269
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005270 if (eap->addr_count > 0)
5271 tomatch = eap->line2;
5272 else
5273 tomatch = MAXLNUM;
5274
Bram Moolenaar81695252004-12-29 20:58:21 +00005275 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005276 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005277 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005278 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005279 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005280 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005281 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005282 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005283 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005284
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005285 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005286 if (regmatch.regprog == NULL)
5287 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005288
5289 p = skipwhite(p);
5290 if (*p == NUL)
5291 {
5292 EMSG(_("E683: File name missing or invalid pattern"));
5293 goto theend;
5294 }
5295
Bram Moolenaar55b69262017-08-13 13:42:01 +02005296 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005297 && eap->cmdidx != CMD_vimgrepadd
5298 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005299 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005300 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005301 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005302
5303 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005304 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005305 goto theend;
5306 if (fcount == 0)
5307 {
5308 EMSG(_(e_nomatch));
5309 goto theend;
5310 }
5311
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005312 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5313 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005314 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005315 {
5316 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005317 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005318 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005319
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005320 /* Remember the current directory, because a BufRead autocommand that does
5321 * ":lcd %:p:h" changes the meaning of short path names. */
5322 mch_dirname(dirname_start, MAXPATHL);
5323
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005324 /* Remember the current quickfix list identifier, so that we can check for
5325 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005326 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005327
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005328 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005329 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005330 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005331 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005332 if (time(NULL) > seconds)
5333 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005334 /* Display the file name every second or so, show the user we are
5335 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005336 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005337 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005338 }
5339
Bram Moolenaar81695252004-12-29 20:58:21 +00005340 buf = buflist_findname_exp(fnames[fi]);
5341 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5342 {
5343 /* Remember that a buffer with this name already exists. */
5344 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005345 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005346 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005347
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005348 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005349 }
5350 else
5351 /* Use existing, loaded buffer. */
5352 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005353
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005354 /* Check whether the quickfix list is still valid. When loading a
5355 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005356 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005357 {
5358 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005359 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005360 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005361 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005362
Bram Moolenaar81695252004-12-29 20:58:21 +00005363 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005364 {
5365 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005366 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005367 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005368 else
5369 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005370 /* Try for a match in all lines of the buffer.
5371 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005372 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5373 tomatch, duplicate_name, flags);
5374
Bram Moolenaar81695252004-12-29 20:58:21 +00005375 if (using_dummy)
5376 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005377 if (found_match && first_match_buf == NULL)
5378 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005379 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005380 {
Bram Moolenaar81695252004-12-29 20:58:21 +00005381 /* Never keep a dummy buffer if there is another buffer
5382 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005383 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005384 buf = NULL;
5385 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005386 else if (!cmdmod.hide
5387 || buf->b_p_bh[0] == 'u' /* "unload" */
5388 || buf->b_p_bh[0] == 'w' /* "wipe" */
5389 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00005390 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005391 /* When no match was found we don't need to remember the
5392 * buffer, wipe it out. If there was a match and it
5393 * wasn't the first one or we won't jump there: only
5394 * unload the buffer.
5395 * Ignore 'hidden' here, because it may lead to having too
5396 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00005397 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005398 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005399 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005400 buf = NULL;
5401 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005402 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005403 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005404 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02005405 /* Keeping the buffer, remove the dummy flag. */
5406 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005407 buf = NULL;
5408 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005409 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005410
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005411 if (buf != NULL)
5412 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02005413 /* Keeping the buffer, remove the dummy flag. */
5414 buf->b_flags &= ~BF_DUMMY;
5415
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005416 /* If the buffer is still loaded we need to use the
5417 * directory we jumped to below. */
5418 if (buf == first_match_buf
5419 && target_dir == NULL
5420 && STRCMP(dirname_start, dirname_now) != 0)
5421 target_dir = vim_strsave(dirname_now);
5422
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005423 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005424 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00005425 * need to be done (again). But not the window-local
5426 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005427 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005428#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005429 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5430 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005431#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005432 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005433 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005434 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005435 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005436 }
5437 }
5438
5439 FreeWild(fcount, fnames);
5440
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005441 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5442 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5443 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005444 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005445
Bram Moolenaar864293a2016-06-02 13:40:04 +02005446 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005447
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005448 if (au_name != NULL)
5449 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5450 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005451 /*
5452 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5453 * is still valid.
5454 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005455 if (!qflist_valid(wp, save_qfid))
5456 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005457
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005458 if (qf_restore_list(qi, save_qfid) == FAIL)
5459 goto theend;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005460
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005461 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005462 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005463 {
5464 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005465 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5466 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005467 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005468 else
5469 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005470
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005471 /* If we loaded a dummy buffer into the current window, the autocommands
5472 * may have messed up things, need to redraw and recompute folds. */
5473 if (redraw_for_dummy)
5474 {
5475#ifdef FEAT_FOLDING
5476 foldUpdateAll(curwin);
5477#else
5478 redraw_later(NOT_VALID);
5479#endif
5480 }
5481
Bram Moolenaar86b68352004-12-27 21:59:20 +00005482theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005483 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005484 vim_free(dirname_now);
5485 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005486 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005487 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005488}
5489
5490/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005491 * Restore current working directory to "dirname_start" if they differ, taking
5492 * into account whether it is set locally or globally.
5493 */
5494 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005495restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005496{
5497 char_u *dirname_now = alloc(MAXPATHL);
5498
5499 if (NULL != dirname_now)
5500 {
5501 mch_dirname(dirname_now, MAXPATHL);
5502 if (STRCMP(dirname_start, dirname_now) != 0)
5503 {
5504 /* If the directory has changed, change it back by building up an
5505 * appropriate ex command and executing it. */
5506 exarg_T ea;
5507
5508 ea.arg = dirname_start;
5509 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5510 ex_cd(&ea);
5511 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005512 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005513 }
5514}
5515
5516/*
5517 * Load file "fname" into a dummy buffer and return the buffer pointer,
5518 * placing the directory resulting from the buffer load into the
5519 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5520 * prior to calling this function. Restores directory to "dirname_start" prior
5521 * to returning, if autocmds or the 'autochdir' option have changed it.
5522 *
5523 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5524 * or wipe_dummy_buffer() later!
5525 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005526 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005527 */
5528 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005529load_dummy_buffer(
5530 char_u *fname,
5531 char_u *dirname_start, /* in: old directory */
5532 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005533{
5534 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005535 bufref_T newbufref;
5536 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005537 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005538 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005539 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005540
5541 /* Allocate a buffer without putting it in the buffer list. */
5542 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5543 if (newbuf == NULL)
5544 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005545 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005546
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005547 /* Init the options. */
5548 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5549
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005550 /* need to open the memfile before putting the buffer in a window */
5551 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005552 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005553 /* Make sure this buffer isn't wiped out by autocommands. */
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005554 ++newbuf->b_locked;
5555
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005556 /* set curwin/curbuf to buf and save a few things */
5557 aucmd_prepbuf(&aco, newbuf);
5558
5559 /* Need to set the filename for autocommands. */
5560 (void)setfname(curbuf, fname, NULL, FALSE);
5561
Bram Moolenaar81695252004-12-29 20:58:21 +00005562 /* Create swap file now to avoid the ATTENTION message. */
5563 check_need_swap(TRUE);
5564
5565 /* Remove the "dummy" flag, otherwise autocommands may not
5566 * work. */
5567 curbuf->b_flags &= ~BF_DUMMY;
5568
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005569 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005570 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005571 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005572 NULL, READ_NEW | READ_DUMMY);
5573 --newbuf->b_locked;
5574 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005575 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005576 && !(curbuf->b_flags & BF_NEW))
5577 {
5578 failed = FALSE;
5579 if (curbuf != newbuf)
5580 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005581 /* Bloody autocommands changed the buffer! Can happen when
5582 * using netrw and editing a remote file. Use the current
5583 * buffer instead, delete the dummy one after restoring the
5584 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005585 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005586 newbuf = curbuf;
5587 }
5588 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005589
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005590 /* restore curwin/curbuf and a few other things */
5591 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005592 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5593 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005594
5595 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5596 * skip it. */
5597 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005598 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005599
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005600 /*
5601 * When autocommands/'autochdir' option changed directory: go back.
5602 * Let the caller know what the resulting dir was first, in case it is
5603 * important.
5604 */
5605 mch_dirname(resulting_dir, MAXPATHL);
5606 restore_start_dir(dirname_start);
5607
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005608 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005609 return NULL;
5610 if (failed)
5611 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005612 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005613 return NULL;
5614 }
5615 return newbuf;
5616}
5617
5618/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005619 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5620 * directory to "dirname_start" prior to returning, if autocmds or the
5621 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005622 */
5623 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005624wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005625{
5626 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005627 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005628#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005629 cleanup_T cs;
5630
5631 /* Reset the error/interrupt/exception state here so that aborting()
5632 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5633 * work when got_int is set. */
5634 enter_cleanup(&cs);
5635#endif
5636
Bram Moolenaar81695252004-12-29 20:58:21 +00005637 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005638
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005639#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005640 /* Restore the error/interrupt/exception state if not discarded by a
5641 * new aborting error, interrupt, or uncaught exception. */
5642 leave_cleanup(&cs);
5643#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005644 /* When autocommands/'autochdir' option changed directory: go back. */
5645 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005646 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005647}
5648
5649/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005650 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5651 * directory to "dirname_start" prior to returning, if autocmds or the
5652 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005653 */
5654 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005655unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005656{
5657 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005658 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005659 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005660
5661 /* When autocommands/'autochdir' option changed directory: go back. */
5662 restore_start_dir(dirname_start);
5663 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005664}
5665
Bram Moolenaar05159a02005-02-26 23:04:13 +00005666#if defined(FEAT_EVAL) || defined(PROTO)
5667/*
5668 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005669 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005670 */
5671 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005672get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005673{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005674 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005675 dict_T *dict;
5676 char_u buf[2];
5677 qfline_T *qfp;
5678 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005679 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005680
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005681 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005682 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005683 qi = &ql_info;
5684 if (wp != NULL)
5685 {
5686 qi = GET_LOC_LIST(wp);
5687 if (qi == NULL)
5688 return FAIL;
5689 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005690 }
5691
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005692 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005693 qf_idx = qi->qf_curlist;
5694
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005695 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005696 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005697
Bram Moolenaard823fa92016-08-12 16:29:27 +02005698 qfp = qi->qf_lists[qf_idx].qf_start;
5699 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005700 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005701 /* Handle entries with a non-existing buffer number. */
5702 bufnum = qfp->qf_fnum;
5703 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5704 bufnum = 0;
5705
Bram Moolenaar05159a02005-02-26 23:04:13 +00005706 if ((dict = dict_alloc()) == NULL)
5707 return FAIL;
5708 if (list_append_dict(list, dict) == FAIL)
5709 return FAIL;
5710
5711 buf[0] = qfp->qf_type;
5712 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005713 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5714 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5715 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5716 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5717 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5718 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5719 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5720 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5721 || dict_add_string(dict, "type", buf) == FAIL
5722 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005723 return FAIL;
5724
5725 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005726 if (qfp == NULL)
5727 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005728 }
5729 return OK;
5730}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005731
5732/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005733 * Flags used by getqflist()/getloclist() to determine which fields to return.
5734 */
5735enum {
5736 QF_GETLIST_NONE = 0x0,
5737 QF_GETLIST_TITLE = 0x1,
5738 QF_GETLIST_ITEMS = 0x2,
5739 QF_GETLIST_NR = 0x4,
5740 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005741 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005742 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005743 QF_GETLIST_IDX = 0x40,
5744 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005745 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005746 QF_GETLIST_FILEWINID = 0x200,
5747 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005748};
5749
5750/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005751 * Parse text from 'di' and return the quickfix list items.
5752 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005753 */
5754 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005755qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005756{
5757 int status = FAIL;
5758 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005759 char_u *errorformat = p_efm;
5760 dictitem_T *efm_di;
5761 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005762
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005763 /* Only a List value is supported */
5764 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005765 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005766 /* If errorformat is supplied then use it, otherwise use the 'efm'
5767 * option setting
5768 */
5769 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5770 {
5771 if (efm_di->di_tv.v_type != VAR_STRING ||
5772 efm_di->di_tv.vval.v_string == NULL)
5773 return FAIL;
5774 errorformat = efm_di->di_tv.vval.v_string;
5775 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005776
Bram Moolenaar36538222017-09-02 19:51:44 +02005777 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005778 if (l == NULL)
5779 return FAIL;
5780
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005781 qi = ll_new_list();
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005782 if (qi != NULL)
5783 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005784 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005785 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5786 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005787 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005788 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005789 }
5790 free(qi);
5791 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005792 dict_add_list(retdict, "items", l);
5793 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005794 }
5795
5796 return status;
5797}
5798
5799/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005800 * Return the quickfix/location list window identifier in the current tabpage.
5801 */
5802 static int
5803qf_winid(qf_info_T *qi)
5804{
5805 win_T *win;
5806
5807 /* The quickfix window can be opened even if the quickfix list is not set
5808 * using ":copen". This is not true for location lists. */
5809 if (qi == NULL)
5810 return 0;
5811 win = qf_find_win(qi);
5812 if (win != NULL)
5813 return win->w_id;
5814 return 0;
5815}
5816
5817/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005818 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005819 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005820 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005821qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005822{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005823 int flags = QF_GETLIST_NONE;
5824
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005825 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005826 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005827 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005828 if (!loclist)
5829 // File window ID is applicable only to location list windows
5830 flags &= ~ QF_GETLIST_FILEWINID;
5831 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005832
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005833 if (dict_find(what, (char_u *)"title", -1) != NULL)
5834 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005835
Bram Moolenaara6d48492017-12-12 22:45:31 +01005836 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5837 flags |= QF_GETLIST_NR;
5838
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005839 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5840 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005841
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005842 if (dict_find(what, (char_u *)"context", -1) != NULL)
5843 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005844
Bram Moolenaara6d48492017-12-12 22:45:31 +01005845 if (dict_find(what, (char_u *)"id", -1) != NULL)
5846 flags |= QF_GETLIST_ID;
5847
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005848 if (dict_find(what, (char_u *)"items", -1) != NULL)
5849 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005850
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005851 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5852 flags |= QF_GETLIST_IDX;
5853
5854 if (dict_find(what, (char_u *)"size", -1) != NULL)
5855 flags |= QF_GETLIST_SIZE;
5856
Bram Moolenaarb254af32017-12-18 19:48:58 +01005857 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5858 flags |= QF_GETLIST_TICK;
5859
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005860 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5861 flags |= QF_GETLIST_FILEWINID;
5862
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005863 return flags;
5864}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005865
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005866/*
5867 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5868 * If 'nr' and 'id' are not present in 'what' then return the current
5869 * quickfix list index.
5870 * If 'nr' is zero then return the current quickfix list index.
5871 * If 'nr' is '$' then return the last quickfix list index.
5872 * If 'id' is present then return the index of the quickfix list with that id.
5873 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5874 * Return -1, if quickfix list is not present or if the stack is empty.
5875 */
5876 static int
5877qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5878{
5879 int qf_idx;
5880 dictitem_T *di;
5881
5882 qf_idx = qi->qf_curlist; /* default is the current list */
5883 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5884 {
5885 /* Use the specified quickfix/location list */
5886 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005887 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005888 /* for zero use the current list */
5889 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005890 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005891 qf_idx = di->di_tv.vval.v_number - 1;
5892 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005893 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005894 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005895 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005896 else if (di->di_tv.v_type == VAR_STRING
5897 && di->di_tv.vval.v_string != NULL
5898 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5899 /* Get the last quickfix list number */
5900 qf_idx = qi->qf_listcount - 1;
5901 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005902 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005903 }
5904
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005905 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5906 {
5907 /* Look for a list with the specified id */
5908 if (di->di_tv.v_type == VAR_NUMBER)
5909 {
5910 /*
5911 * For zero, use the current list or the list specified by 'nr'
5912 */
5913 if (di->di_tv.vval.v_number != 0)
5914 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5915 }
5916 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005917 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005918 }
5919
5920 return qf_idx;
5921}
5922
5923/*
5924 * Return default values for quickfix list properties in retdict.
5925 */
5926 static int
5927qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5928{
5929 int status = OK;
5930
5931 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005932 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005933 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5934 {
5935 list_T *l = list_alloc();
5936 if (l != NULL)
5937 status = dict_add_list(retdict, "items", l);
5938 else
5939 status = FAIL;
5940 }
5941 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005942 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005943 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005944 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005945 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005946 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005947 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005948 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005949 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005950 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005951 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005952 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005953 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005954 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005955 if ((status == OK) && (qi != &ql_info) && (flags & QF_GETLIST_FILEWINID))
5956 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005957
5958 return status;
5959}
5960
5961/*
5962 * Return the quickfix list title as 'title' in retdict
5963 */
5964 static int
5965qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5966{
Bram Moolenaare0be1672018-07-08 16:50:37 +02005967 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005968}
5969
5970/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005971 * Returns the identifier of the window used to display files from a location
5972 * list. If there is no associated window, then returns 0. Useful only when
5973 * called from a location list window.
5974 */
5975 static int
5976qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
5977{
5978 int winid = 0;
5979
5980 if (wp != NULL && IS_LL_WINDOW(wp))
5981 {
5982 win_T *ll_wp = qf_find_win_with_loclist(qi);
5983 if (ll_wp != NULL)
5984 winid = ll_wp->w_id;
5985 }
5986
5987 return dict_add_number(retdict, "filewinid", winid);
5988}
5989
5990/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005991 * Return the quickfix list items/entries as 'items' in retdict
5992 */
5993 static int
5994qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5995{
5996 int status = OK;
5997 list_T *l = list_alloc();
5998 if (l != NULL)
5999 {
6000 (void)get_errorlist(qi, NULL, qf_idx, l);
6001 dict_add_list(retdict, "items", l);
6002 }
6003 else
6004 status = FAIL;
6005
6006 return status;
6007}
6008
6009/*
6010 * Return the quickfix list context (if any) as 'context' in retdict.
6011 */
6012 static int
6013qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6014{
6015 int status;
6016 dictitem_T *di;
6017
6018 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
6019 {
6020 di = dictitem_alloc((char_u *)"context");
6021 if (di != NULL)
6022 {
6023 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
6024 status = dict_add(retdict, di);
6025 if (status == FAIL)
6026 dictitem_free(di);
6027 }
6028 else
6029 status = FAIL;
6030 }
6031 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006032 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006033
6034 return status;
6035}
6036
6037/*
6038 * Return the quickfix list index as 'idx' in retdict
6039 */
6040 static int
6041qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6042{
6043 int idx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006044 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006045 /* For empty lists, qf_index is set to 1 */
6046 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02006047 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006048}
6049
6050/*
6051 * Return quickfix/location list details (title) as a
6052 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6053 * then current list is used. Otherwise the specified list is used.
6054 */
6055 int
6056qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6057{
6058 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006059 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006060 int status = OK;
6061 int qf_idx;
6062 dictitem_T *di;
6063 int flags = QF_GETLIST_NONE;
6064
6065 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6066 return qf_get_list_from_lines(what, di, retdict);
6067
6068 if (wp != NULL)
6069 qi = GET_LOC_LIST(wp);
6070
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006071 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006072
6073 if (qi != NULL && qi->qf_listcount != 0)
6074 qf_idx = qf_getprop_qfidx(qi, what);
6075
Bram Moolenaara6d48492017-12-12 22:45:31 +01006076 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006077 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006078 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006079
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006080 qfl = &qi->qf_lists[qf_idx];
6081
Bram Moolenaard823fa92016-08-12 16:29:27 +02006082 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006083 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006084 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006085 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006086 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006087 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006088 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006089 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006090 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006091 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006092 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006093 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006094 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006095 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006096 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006097 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006098 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006099 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006100 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6101 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006102
Bram Moolenaard823fa92016-08-12 16:29:27 +02006103 return status;
6104}
6105
6106/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006107 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
6108 * items in the dict 'd'.
6109 */
6110 static int
6111qf_add_entry_from_dict(
6112 qf_info_T *qi,
6113 int qf_idx,
6114 dict_T *d,
6115 int first_entry)
6116{
6117 static int did_bufnr_emsg;
6118 char_u *filename, *module, *pattern, *text, *type;
6119 int bufnum, valid, status, col, vcol, nr;
6120 long lnum;
6121
6122 if (first_entry)
6123 did_bufnr_emsg = FALSE;
6124
6125 filename = get_dict_string(d, (char_u *)"filename", TRUE);
6126 module = get_dict_string(d, (char_u *)"module", TRUE);
6127 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6128 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6129 col = (int)get_dict_number(d, (char_u *)"col");
6130 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6131 nr = (int)get_dict_number(d, (char_u *)"nr");
6132 type = get_dict_string(d, (char_u *)"type", TRUE);
6133 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6134 text = get_dict_string(d, (char_u *)"text", TRUE);
6135 if (text == NULL)
6136 text = vim_strsave((char_u *)"");
6137
6138 valid = TRUE;
6139 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6140 valid = FALSE;
6141
6142 // Mark entries with non-existing buffer number as not valid. Give the
6143 // error message only once.
6144 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6145 {
6146 if (!did_bufnr_emsg)
6147 {
6148 did_bufnr_emsg = TRUE;
6149 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6150 }
6151 valid = FALSE;
6152 bufnum = 0;
6153 }
6154
6155 // If the 'valid' field is present it overrules the detected value.
6156 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6157 valid = (int)get_dict_number(d, (char_u *)"valid");
6158
6159 status = qf_add_entry(qi,
6160 qf_idx,
6161 NULL, // dir
6162 filename,
6163 module,
6164 bufnum,
6165 text,
6166 lnum,
6167 col,
6168 vcol, // vis_col
6169 pattern, // search pattern
6170 nr,
6171 type == NULL ? NUL : *type,
6172 valid);
6173
6174 vim_free(filename);
6175 vim_free(module);
6176 vim_free(pattern);
6177 vim_free(text);
6178 vim_free(type);
6179
6180 return status;
6181}
6182
6183/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006184 * Add list of entries to quickfix/location list. Each list entry is
6185 * a dictionary with item information.
6186 */
6187 static int
6188qf_add_entries(
6189 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006190 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006191 list_T *list,
6192 char_u *title,
6193 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006194{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006195 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006196 listitem_T *li;
6197 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006198 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006199 int retval = OK;
6200
Bram Moolenaara3921f42017-06-04 15:30:34 +02006201 if (action == ' ' || qf_idx == qi->qf_listcount)
6202 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006203 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006204 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006205 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006206 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006207 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006208 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006209 /* Adding to existing list, use last entry. */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006210 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006211 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006212 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006213 qf_free_items(qfl);
6214 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006215 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006216
6217 for (li = list->lv_first; li != NULL; li = li->li_next)
6218 {
6219 if (li->li_tv.v_type != VAR_DICT)
6220 continue; /* Skip non-dict items */
6221
6222 d = li->li_tv.vval.v_dict;
6223 if (d == NULL)
6224 continue;
6225
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006226 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first);
6227 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006228 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006229 }
6230
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006231 if (qfl->qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02006232 /* no valid entry */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006233 qfl->qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006234 else
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006235 qfl->qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006236 if (action != 'a')
6237 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006238 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006239 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006240 qfl->qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006241 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006242
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006243 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02006244 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006245
6246 return retval;
6247}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006248
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006249/*
6250 * Get the quickfix list index from 'nr' or 'id'
6251 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006252 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006253qf_setprop_get_qfidx(
6254 qf_info_T *qi,
6255 dict_T *what,
6256 int action,
6257 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006258{
6259 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006260 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006261
Bram Moolenaard823fa92016-08-12 16:29:27 +02006262 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6263 {
6264 /* Use the specified quickfix/location list */
6265 if (di->di_tv.v_type == VAR_NUMBER)
6266 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006267 /* for zero use the current list */
6268 if (di->di_tv.vval.v_number != 0)
6269 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006270
Bram Moolenaar55b69262017-08-13 13:42:01 +02006271 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6272 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006273 /*
6274 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02006275 * non-available list and add the new list at the end of the
6276 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006277 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006278 *newlist = TRUE;
6279 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006280 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006281 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006282 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006283 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006284 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02006285 }
6286 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006287 && di->di_tv.vval.v_string != NULL
6288 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006289 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006290 if (qi->qf_listcount > 0)
6291 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006292 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006293 qf_idx = 0;
6294 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006295 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006296 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006297 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006298 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006299 }
6300
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006301 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006302 {
6303 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006304 if (di->di_tv.v_type != VAR_NUMBER)
6305 return INVALID_QFIDX;
6306
6307 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006308 }
6309
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006310 return qf_idx;
6311}
6312
6313/*
6314 * Set the quickfix list title.
6315 */
6316 static int
6317qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6318{
6319 if (di->di_tv.v_type != VAR_STRING)
6320 return FAIL;
6321
6322 vim_free(qi->qf_lists[qf_idx].qf_title);
6323 qi->qf_lists[qf_idx].qf_title =
6324 get_dict_string(what, (char_u *)"title", TRUE);
6325 if (qf_idx == qi->qf_curlist)
6326 qf_update_win_titlevar(qi);
6327
6328 return OK;
6329}
6330
6331/*
6332 * Set quickfix list items/entries.
6333 */
6334 static int
6335qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6336{
6337 int retval = FAIL;
6338 char_u *title_save;
6339
6340 if (di->di_tv.v_type != VAR_LIST)
6341 return FAIL;
6342
6343 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6344 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6345 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006346 vim_free(title_save);
6347
6348 return retval;
6349}
6350
6351/*
6352 * Set quickfix list items/entries from a list of lines.
6353 */
6354 static int
6355qf_setprop_items_from_lines(
6356 qf_info_T *qi,
6357 int qf_idx,
6358 dict_T *what,
6359 dictitem_T *di,
6360 int action)
6361{
6362 char_u *errorformat = p_efm;
6363 dictitem_T *efm_di;
6364 int retval = FAIL;
6365
6366 /* Use the user supplied errorformat settings (if present) */
6367 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6368 {
6369 if (efm_di->di_tv.v_type != VAR_STRING ||
6370 efm_di->di_tv.vval.v_string == NULL)
6371 return FAIL;
6372 errorformat = efm_di->di_tv.vval.v_string;
6373 }
6374
6375 /* Only a List value is supported */
6376 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6377 return FAIL;
6378
6379 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006380 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006381 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6382 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6383 retval = OK;
6384
6385 return retval;
6386}
6387
6388/*
6389 * Set quickfix list context.
6390 */
6391 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006392qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006393{
6394 typval_T *ctx;
6395
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006396 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006397 ctx = alloc_tv();
6398 if (ctx != NULL)
6399 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006400 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006401
6402 return OK;
6403}
6404
6405/*
6406 * Set quickfix/location list properties (title, items, context).
6407 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006408 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006409 */
6410 static int
6411qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6412{
6413 dictitem_T *di;
6414 int retval = FAIL;
6415 int qf_idx;
6416 int newlist = FALSE;
6417
6418 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
6419 newlist = TRUE;
6420
6421 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6422 if (qf_idx == INVALID_QFIDX) /* List not found */
6423 return FAIL;
6424
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006425 if (newlist)
6426 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006427 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006428 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006429 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006430 }
6431
6432 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006433 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006434 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006435 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006436 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006437 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006438 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006439 retval = qf_setprop_context(&qi->qf_lists[qf_idx], di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006440
Bram Moolenaarb254af32017-12-18 19:48:58 +01006441 if (retval == OK)
6442 qf_list_changed(qi, qf_idx);
6443
Bram Moolenaard823fa92016-08-12 16:29:27 +02006444 return retval;
6445}
6446
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006447/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006448 * Find the non-location list window with the specified location list in the
6449 * current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006450 */
6451 static win_T *
6452find_win_with_ll(qf_info_T *qi)
6453{
6454 win_T *wp = NULL;
6455
6456 FOR_ALL_WINDOWS(wp)
6457 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6458 return wp;
6459
6460 return NULL;
6461}
6462
6463/*
6464 * Free the entire quickfix/location list stack.
6465 * If the quickfix/location list window is open, then clear it.
6466 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006467 static void
6468qf_free_stack(win_T *wp, qf_info_T *qi)
6469{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006470 win_T *qfwin = qf_find_win(qi);
6471 win_T *llwin = NULL;
6472 win_T *orig_wp = wp;
6473
6474 if (qfwin != NULL)
6475 {
6476 /* If the quickfix/location list window is open, then clear it */
6477 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006478 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006479 qf_update_buffer(qi, NULL);
6480 }
6481
6482 if (wp != NULL && IS_LL_WINDOW(wp))
6483 {
6484 /* If in the location list window, then use the non-location list
6485 * window with this location list (if present)
6486 */
6487 llwin = find_win_with_ll(qi);
6488 if (llwin != NULL)
6489 wp = llwin;
6490 }
6491
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006492 qf_free_all(wp);
6493 if (wp == NULL)
6494 {
6495 /* quickfix list */
6496 qi->qf_curlist = 0;
6497 qi->qf_listcount = 0;
6498 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006499 else if (IS_LL_WINDOW(orig_wp))
6500 {
6501 /* If the location list window is open, then create a new empty
6502 * location list */
6503 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006504
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006505 /* first free the list reference in the location list window */
6506 ll_free_all(&orig_wp->w_llist_ref);
6507
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006508 orig_wp->w_llist_ref = new_ll;
6509 if (llwin != NULL)
6510 {
6511 llwin->w_llist = new_ll;
6512 new_ll->qf_refcount++;
6513 }
6514 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006515}
6516
Bram Moolenaard823fa92016-08-12 16:29:27 +02006517/*
6518 * Populate the quickfix list with the items supplied in the list
6519 * of dictionaries. "title" will be copied to w:quickfix_title.
6520 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6521 */
6522 int
6523set_errorlist(
6524 win_T *wp,
6525 list_T *list,
6526 int action,
6527 char_u *title,
6528 dict_T *what)
6529{
6530 qf_info_T *qi = &ql_info;
6531 int retval = OK;
6532
6533 if (wp != NULL)
6534 {
6535 qi = ll_get_or_alloc_list(wp);
6536 if (qi == NULL)
6537 return FAIL;
6538 }
6539
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006540 if (action == 'f')
6541 {
6542 /* Free the entire quickfix or location list stack */
6543 qf_free_stack(wp, qi);
6544 }
6545 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006546 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006547 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006548 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006549 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006550 if (retval == OK)
6551 qf_list_changed(qi, qi->qf_curlist);
6552 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006553
6554 return retval;
6555}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006556
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006557/*
6558 * Mark the context as in use for all the lists in a quickfix stack.
6559 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006560 static int
6561mark_quickfix_ctx(qf_info_T *qi, int copyID)
6562{
6563 int i;
6564 int abort = FALSE;
6565 typval_T *ctx;
6566
6567 for (i = 0; i < LISTCOUNT && !abort; ++i)
6568 {
6569 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006570 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6571 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006572 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6573 }
6574
6575 return abort;
6576}
6577
6578/*
6579 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006580 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006581 */
6582 int
6583set_ref_in_quickfix(int copyID)
6584{
6585 int abort = FALSE;
6586 tabpage_T *tp;
6587 win_T *win;
6588
6589 abort = mark_quickfix_ctx(&ql_info, copyID);
6590 if (abort)
6591 return abort;
6592
6593 FOR_ALL_TAB_WINDOWS(tp, win)
6594 {
6595 if (win->w_llist != NULL)
6596 {
6597 abort = mark_quickfix_ctx(win->w_llist, copyID);
6598 if (abort)
6599 return abort;
6600 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006601 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6602 {
6603 /* In a location list window and none of the other windows is
6604 * referring to this location list. Mark the location list
6605 * context as still in use.
6606 */
6607 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6608 if (abort)
6609 return abort;
6610 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006611 }
6612
6613 return abort;
6614}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006615#endif
6616
Bram Moolenaar81695252004-12-29 20:58:21 +00006617/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006618 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006619 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006620 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006621 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006622 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006623 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006624 */
6625 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006626ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006627{
6628 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006629 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006630 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006631 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006632 int_u save_qfid;
6633 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006634
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006635 switch (eap->cmdidx)
6636 {
6637 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6638 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6639 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6640 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6641 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6642 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6643 default: break;
6644 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006645 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6646 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006647 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006648#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006649 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006650 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006651#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006652 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006653
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006654 /* Must come after autocommands. */
Bram Moolenaar39665952018-08-15 20:59:48 +02006655 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006656 {
6657 qi = ll_get_or_alloc_list(curwin);
6658 if (qi == NULL)
6659 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006660 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006661 }
6662
Bram Moolenaar86b68352004-12-27 21:59:20 +00006663 if (*eap->arg == NUL)
6664 buf = curbuf;
6665 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6666 buf = buflist_findnr(atoi((char *)eap->arg));
6667 if (buf == NULL)
6668 EMSG(_(e_invarg));
6669 else if (buf->b_ml.ml_mfp == NULL)
6670 EMSG(_("E681: Buffer is not loaded"));
6671 else
6672 {
6673 if (eap->addr_count == 0)
6674 {
6675 eap->line1 = 1;
6676 eap->line2 = buf->b_ml.ml_line_count;
6677 }
6678 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6679 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6680 EMSG(_(e_invrange));
6681 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006682 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006683 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006684
6685 if (buf->b_sfname)
6686 {
6687 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6688 (char *)qf_title, (char *)buf->b_sfname);
6689 qf_title = IObuff;
6690 }
6691
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006692 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006693 (eap->cmdidx != CMD_caddbuffer
6694 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006695 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006696 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006697 if (res >= 0)
6698 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006699
6700 // Remember the current quickfix list identifier, so that we can
6701 // check for autocommands changing the current quickfix list.
6702 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006703 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006704 {
6705 buf_T *curbuf_old = curbuf;
6706
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006707 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6708 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006709 if (curbuf != curbuf_old)
6710 // Autocommands changed buffer, don't jump now, "qi" may
6711 // be invalid.
6712 res = 0;
6713 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006714 // Jump to the first error for a new list and if autocmds didn't
6715 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006716 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006717 eap->cmdidx == CMD_lbuffer)
6718 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006719 // display the first error
6720 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar754b5602006-02-09 23:53:20 +00006721 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006722 }
6723}
6724
Bram Moolenaar1e015462005-09-25 22:16:38 +00006725#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006726/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006727 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6728 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006729 */
6730 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006731ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006732{
6733 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006734 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006735 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006736 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006737 int_u save_qfid;
6738 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006739
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006740 switch (eap->cmdidx)
6741 {
6742 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6743 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6744 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6745 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6746 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6747 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6748 default: break;
6749 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006750 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6751 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006752 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006753#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006754 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006755 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006756#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006757 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006758
Bram Moolenaar39665952018-08-15 20:59:48 +02006759 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006760 {
6761 qi = ll_get_or_alloc_list(curwin);
6762 if (qi == NULL)
6763 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006764 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006765 }
6766
Bram Moolenaar4770d092006-01-12 23:22:24 +00006767 /* Evaluate the expression. When the result is a string or a list we can
6768 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006769 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006770 if (tv != NULL)
6771 {
6772 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6773 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6774 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006775 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006776 (eap->cmdidx != CMD_caddexpr
6777 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006778 (linenr_T)0, (linenr_T)0,
6779 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006780 if (res >= 0)
6781 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006782
6783 // Remember the current quickfix list identifier, so that we can
6784 // check for autocommands changing the current quickfix list.
6785 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006786 if (au_name != NULL)
6787 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6788 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006789
6790 // Jump to the first error for a new list and if autocmds didn't
6791 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006792 if (res > 0 && (eap->cmdidx == CMD_cexpr
6793 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006794 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006795 // display the first error
6796 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006797 }
6798 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006799 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006800 free_tv(tv);
6801 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006802}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006803#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006804
6805/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006806 * Get the location list for ":lhelpgrep"
6807 */
6808 static qf_info_T *
6809hgr_get_ll(int *new_ll)
6810{
6811 win_T *wp;
6812 qf_info_T *qi;
6813
6814 /* If the current window is a help window, then use it */
6815 if (bt_help(curwin->w_buffer))
6816 wp = curwin;
6817 else
6818 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006819 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006820
6821 if (wp == NULL) /* Help window not found */
6822 qi = NULL;
6823 else
6824 qi = wp->w_llist;
6825
6826 if (qi == NULL)
6827 {
6828 /* Allocate a new location list for help text matches */
6829 if ((qi = ll_new_list()) == NULL)
6830 return NULL;
6831 *new_ll = TRUE;
6832 }
6833
6834 return qi;
6835}
6836
6837/*
6838 * Search for a pattern in a help file.
6839 */
6840 static void
6841hgr_search_file(
6842 qf_info_T *qi,
6843 char_u *fname,
6844#ifdef FEAT_MBYTE
6845 vimconv_T *p_vc,
6846#endif
6847 regmatch_T *p_regmatch)
6848{
6849 FILE *fd;
6850 long lnum;
6851
6852 fd = mch_fopen((char *)fname, "r");
6853 if (fd == NULL)
6854 return;
6855
6856 lnum = 1;
6857 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6858 {
6859 char_u *line = IObuff;
6860#ifdef FEAT_MBYTE
6861 /* Convert a line if 'encoding' is not utf-8 and
6862 * the line contains a non-ASCII character. */
6863 if (p_vc->vc_type != CONV_NONE
6864 && has_non_ascii(IObuff))
6865 {
6866 line = string_convert(p_vc, IObuff, NULL);
6867 if (line == NULL)
6868 line = IObuff;
6869 }
6870#endif
6871
6872 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6873 {
6874 int l = (int)STRLEN(line);
6875
6876 /* remove trailing CR, LF, spaces, etc. */
6877 while (l > 0 && line[l - 1] <= ' ')
6878 line[--l] = NUL;
6879
6880 if (qf_add_entry(qi,
6881 qi->qf_curlist,
6882 NULL, /* dir */
6883 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006884 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006885 0,
6886 line,
6887 lnum,
6888 (int)(p_regmatch->startp[0] - line)
6889 + 1, /* col */
6890 FALSE, /* vis_col */
6891 NULL, /* search pattern */
6892 0, /* nr */
6893 1, /* type */
6894 TRUE /* valid */
6895 ) == FAIL)
6896 {
6897 got_int = TRUE;
6898#ifdef FEAT_MBYTE
6899 if (line != IObuff)
6900 vim_free(line);
6901#endif
6902 break;
6903 }
6904 }
6905#ifdef FEAT_MBYTE
6906 if (line != IObuff)
6907 vim_free(line);
6908#endif
6909 ++lnum;
6910 line_breakcheck();
6911 }
6912 fclose(fd);
6913}
6914
6915/*
6916 * Search for a pattern in all the help files in the doc directory under
6917 * the given directory.
6918 */
6919 static void
6920hgr_search_files_in_dir(
6921 qf_info_T *qi,
6922 char_u *dirname,
6923 regmatch_T *p_regmatch
6924#ifdef FEAT_MBYTE
6925 , vimconv_T *p_vc
6926#endif
6927#ifdef FEAT_MULTI_LANG
6928 , char_u *lang
6929#endif
6930 )
6931{
6932 int fcount;
6933 char_u **fnames;
6934 int fi;
6935
6936 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6937 add_pathsep(dirname);
6938 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6939 if (gen_expand_wildcards(1, &dirname, &fcount,
6940 &fnames, EW_FILE|EW_SILENT) == OK
6941 && fcount > 0)
6942 {
6943 for (fi = 0; fi < fcount && !got_int; ++fi)
6944 {
6945#ifdef FEAT_MULTI_LANG
6946 /* Skip files for a different language. */
6947 if (lang != NULL
6948 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006949 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006950 && !(STRNICMP(lang, "en", 2) == 0
6951 && STRNICMP("txt", fnames[fi]
6952 + STRLEN(fnames[fi]) - 3, 3) == 0))
6953 continue;
6954#endif
6955
6956 hgr_search_file(qi, fnames[fi],
6957#ifdef FEAT_MBYTE
6958 p_vc,
6959#endif
6960 p_regmatch);
6961 }
6962 FreeWild(fcount, fnames);
6963 }
6964}
6965
6966/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006967 * Search for a pattern in all the help files in the 'runtimepath'
6968 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006969 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006970 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006971 */
6972 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006973hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006974{
6975 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006976
6977#ifdef FEAT_MBYTE
6978 vimconv_T vc;
6979
6980 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6981 * differs. */
6982 vc.vc_type = CONV_NONE;
6983 if (!enc_utf8)
6984 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6985#endif
6986
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006987
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006988 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006989 p = p_rtp;
6990 while (*p != NUL && !got_int)
6991 {
6992 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6993
6994 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6995#ifdef FEAT_MBYTE
6996 , &vc
6997#endif
6998#ifdef FEAT_MULTI_LANG
6999 , lang
7000#endif
7001 );
7002 }
7003
7004#ifdef FEAT_MBYTE
7005 if (vc.vc_type != CONV_NONE)
7006 convert_setup(&vc, NULL, NULL);
7007#endif
7008}
7009
7010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 * ":helpgrep {pattern}"
7012 */
7013 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007014ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015{
7016 regmatch_T regmatch;
7017 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007018 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007019 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007020 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007021 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
Bram Moolenaar73633f82012-01-20 13:39:07 +01007023 switch (eap->cmdidx)
7024 {
7025 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7026 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7027 default: break;
7028 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007029 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7030 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007031 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007032#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007033 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007034 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007035#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007036 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007037
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007038 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007039 save_cpo = p_cpo;
7040 p_cpo = empty_option;
7041
Bram Moolenaar39665952018-08-15 20:59:48 +02007042 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007043 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007044 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007045 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007046 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007047 }
7048
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007049#ifdef FEAT_MULTI_LANG
7050 // Check for a specified language
7051 lang = check_help_lang(eap->arg);
7052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7054 regmatch.rm_ic = FALSE;
7055 if (regmatch.regprog != NULL)
7056 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007057 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007058 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007060 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007061
Bram Moolenaar473de612013-06-08 18:19:48 +02007062 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007064 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
7065 qi->qf_lists[qi->qf_curlist].qf_ptr =
7066 qi->qf_lists[qi->qf_curlist].qf_start;
7067 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 }
7069
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007070 if (p_cpo == empty_option)
7071 p_cpo = save_cpo;
7072 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007073 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007074 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
Bram Moolenaarb254af32017-12-18 19:48:58 +01007076 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02007077 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
Bram Moolenaar73633f82012-01-20 13:39:07 +01007079 if (au_name != NULL)
7080 {
7081 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7082 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007083 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01007084 /* autocommands made "qi" invalid */
7085 return;
7086 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007087
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007089 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007090 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007091 else
7092 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007093
7094 if (eap->cmdidx == CMD_lhelpgrep)
7095 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007096 // If the help window is not opened or if it already points to the
7097 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007098 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007099 {
7100 if (new_qi)
7101 ll_free_all(&qi);
7102 }
7103 else if (curwin->w_llist == NULL)
7104 curwin->w_llist = qi;
7105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106}
7107
7108#endif /* FEAT_QUICKFIX */