blob: c43ad60435d6c1d527b914a33827d83ea824c463 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
Bram Moolenaard76ce852018-05-01 15:02:04 +020036 char_u *qf_module; /* module name for this error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000037 char_u *qf_pattern; /* search pattern for the error */
38 char_u *qf_text; /* description of the error */
39 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
40 char_u qf_cleared; /* set to TRUE if line has been deleted */
41 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000043 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044};
45
46/*
47 * There is a stack of error lists.
48 */
49#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020050#define INVALID_QFIDX (-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020052/*
53 * Quickfix/Location list definition
54 * Contains a list of entries (qfline_T). qf_start points to the first entry
55 * and qf_last points to the last entry. qf_count contains the list size.
56 *
57 * Usually the list contains one or more entries. But an empty list can be
58 * created using setqflist()/setloclist() with a title and/or user context
59 * information and entries can be added later using setqflist()/setloclist().
60 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000062{
Bram Moolenaara539f4f2017-08-30 20:33:55 +020063 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020065 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020067 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000068 int qf_index; /* current index in the error list */
69 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020070 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020071 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020072 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020073
74 struct dir_stack_T *qf_dir_stack;
75 char_u *qf_directory;
76 struct dir_stack_T *qf_file_stack;
77 char_u *qf_currfile;
78 int qf_multiline;
79 int qf_multiignore;
80 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010081 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000082} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020084/*
85 * Quickfix/Location list stack definition
86 * Contains a list of quickfix/location lists (qf_list_T)
87 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000088struct qf_info_S
89{
90 /*
91 * Count of references to this list. Used only for location lists.
92 * When a location list window reference this list, qf_refcount
93 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
94 * reaches 0, the list is freed.
95 */
96 int qf_refcount;
97 int qf_listcount; /* current number of lists */
98 int qf_curlist; /* current error list */
99 qf_list_T qf_lists[LISTCOUNT];
100};
101
102static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200103static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
Bram Moolenaard76ce852018-05-01 15:02:04 +0200105#define FMT_PATTERNS 11 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
107/*
108 * Structure used to hold the info of one part of 'errorformat'
109 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000110typedef struct efm_S efm_T;
111struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112{
113 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
116 char_u prefix; /* prefix of this format line: */
117 /* 'D' enter directory */
118 /* 'X' leave directory */
119 /* 'A' start of multi-line message */
120 /* 'E' error message */
121 /* 'W' warning message */
122 /* 'I' informational message */
123 /* 'C' continuation line */
124 /* 'Z' end of multi-line message */
125 /* 'G' general, unspecific message */
126 /* 'P' push file (partial) message */
127 /* 'Q' pop/quit file (partial) message */
128 /* 'O' overread (partial) message */
129 char_u flags; /* additional flags given in prefix */
130 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000131 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000132 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133};
134
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100135static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
136
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaard76ce852018-05-01 15:02:04 +0200138static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200139static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200141static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200142static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200144static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static win_T *qf_find_win(qf_info_T *qi);
148static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200149static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200150static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static char_u *get_mef_name(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
153static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
154static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
155static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000157/* Quickfix window check helper macro */
158#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
159/* Location list window check helper macro */
160#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200161
162// Quickfix and location list stack check helper macros
163#define IS_QF_STACK(qi) (qi == &ql_info)
164#define IS_LL_STACK(qi) (qi != &ql_info)
165
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000166/*
167 * Return location list for window 'wp'
168 * For location list window, return the referenced location list
169 */
170#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200173 * Looking up a buffer can be slow if there are many. Remember the last one
174 * to make this a lot faster if there are multiple matches in the same file.
175 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200176static char_u *qf_last_bufname = NULL;
177static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200178
Bram Moolenaar3c097222017-12-21 20:54:49 +0100179static char *e_loc_list_changed =
180 N_("E926: Current location list was changed");
181
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200182/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200183 * Maximum number of bytes allowed per line while reading a errorfile.
184 */
185#define LINE_MAXLEN 4096
186
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200187static struct fmtpattern
188{
189 char_u convchar;
190 char *pattern;
191} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200192 {
193 {'f', ".\\+"}, /* only used when at end */
194 {'n', "\\d\\+"},
195 {'l', "\\d\\+"},
196 {'c', "\\d\\+"},
197 {'t', "."},
198 {'m', ".\\+"},
199 {'r', ".*"},
200 {'p', "[- .]*"},
201 {'v', "\\d\\+"},
202 {'s', ".\\+"},
203 {'o', ".\\+"}
204 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200205
206/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200207 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200208 * See fmt_pat definition above for the list of supported patterns. The
209 * pattern specifier is supplied in "efmpat". The converted pattern is stored
210 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200211 */
212 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200213efmpat_to_regpat(
214 char_u *efmpat,
215 char_u *regpat,
216 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200217 int idx,
218 int round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200219 char_u *errmsg)
220{
221 char_u *srcptr;
222
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200223 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200224 {
225 /* Each errorformat pattern can occur only once */
226 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200227 _("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200228 EMSG(errmsg);
229 return NULL;
230 }
231 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200232 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200233 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200234 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200235 {
236 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200237 _("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200238 EMSG(errmsg);
239 return NULL;
240 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200241 efminfo->addr[idx] = (char_u)++round;
242 *regpat++ = '\\';
243 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200244#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200245 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200246 {
247 /* Also match "c:" in the file name, even when
248 * checking for a colon next: "%f:".
249 * "\%(\a:\)\=" */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200250 STRCPY(regpat, "\\%(\\a:\\)\\=");
251 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200252 }
253#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200254 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200255 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200256 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200257 {
258 /* A file name may contain spaces, but this isn't
259 * in "\f". For "%f:%l:%m" there may be a ":" in
260 * the file name. Use ".\{-1,}x" instead (x is
261 * the next character), the requirement that :999:
262 * follows should work. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200263 STRCPY(regpat, ".\\{-1,}");
264 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200265 }
266 else
267 {
268 /* File name followed by '\\' or '%': include as
269 * many file name chars as possible. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200270 STRCPY(regpat, "\\f\\+");
271 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200272 }
273 }
274 else
275 {
276 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200277 while ((*regpat = *srcptr++) != NUL)
278 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200279 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200280 *regpat++ = '\\';
281 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200282
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200283 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200284}
285
286/*
287 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200288 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200289 */
290 static char_u *
291scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200292 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200293 char_u *efm,
294 int len,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200295 char_u *regpat,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200296 char_u *errmsg)
297{
298 char_u *efmp = *pefmp;
299
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200300 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200301 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200302 if ((*regpat++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200303 {
304 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200305 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200306 if (efmp < efm + len)
307 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200308 *regpat++ = *++efmp; /* could be ']' */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200309 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200310 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200311 /* skip */;
312 if (efmp == efm + len)
313 {
314 EMSG(_("E374: Missing ] in format string"));
315 return NULL;
316 }
317 }
318 }
319 else if (efmp < efm + len) /* %*\D, %*\s etc. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200320 *regpat++ = *++efmp;
321 *regpat++ = '\\';
322 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200323 }
324 else
325 {
326 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
327 sprintf((char *)errmsg,
328 _("E375: Unsupported %%%c in format string"), *efmp);
329 EMSG(errmsg);
330 return NULL;
331 }
332
333 *pefmp = efmp;
334
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200335 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200336}
337
338/*
339 * Analyze/parse an errorformat prefix.
340 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200341 static char_u *
342efm_analyze_prefix(char_u *efmp, efm_T *efminfo, char_u *errmsg)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200343{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200344 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200345 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200346 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200347 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200348 else
349 {
350 sprintf((char *)errmsg,
351 _("E376: Invalid %%%c in format string prefix"), *efmp);
352 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200353 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200354 }
355
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200356 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200357}
358
359/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200360 * Converts a 'errorformat' string part in 'efm' to a regular expression
361 * pattern. The resulting regex pattern is returned in "regpat". Additional
362 * information about the 'erroformat' pattern is returned in "fmt_ptr".
363 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200364 */
365 static int
366efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200367 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200368 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200369 efm_T *fmt_ptr,
370 char_u *regpat,
371 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200372{
373 char_u *ptr;
374 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200375 int round;
376 int idx = 0;
377
378 /*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200379 * Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200380 */
381 ptr = regpat;
382 *ptr++ = '^';
383 round = 0;
384 for (efmp = efm; efmp < efm + len; ++efmp)
385 {
386 if (*efmp == '%')
387 {
388 ++efmp;
389 for (idx = 0; idx < FMT_PATTERNS; ++idx)
390 if (fmt_pat[idx].convchar == *efmp)
391 break;
392 if (idx < FMT_PATTERNS)
393 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200394 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200395 errmsg);
396 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200397 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200398 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200399 }
400 else if (*efmp == '*')
401 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200402 ++efmp;
403 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr, errmsg);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200404 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200405 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200406 }
407 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
408 *ptr++ = *efmp; /* regexp magic characters */
409 else if (*efmp == '#')
410 *ptr++ = '*';
411 else if (*efmp == '>')
412 fmt_ptr->conthere = TRUE;
413 else if (efmp == efm + 1) /* analyse prefix */
414 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200415 /*
416 * prefix is allowed only at the beginning of the errorformat
417 * option part
418 */
419 efmp = efm_analyze_prefix(efmp, fmt_ptr, errmsg);
420 if (efmp == NULL)
421 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200422 }
423 else
424 {
425 sprintf((char *)errmsg,
426 _("E377: Invalid %%%c in format string"), *efmp);
427 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200428 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200429 }
430 }
431 else /* copy normal character */
432 {
433 if (*efmp == '\\' && efmp + 1 < efm + len)
434 ++efmp;
435 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
436 *ptr++ = '\\'; /* escape regexp atoms */
437 if (*efmp)
438 *ptr++ = *efmp;
439 }
440 }
441 *ptr++ = '$';
442 *ptr = NUL;
443
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200444 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200445}
446
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200447/*
448 * Free the 'errorformat' information list
449 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200450 static void
451free_efm_list(efm_T **efm_first)
452{
453 efm_T *efm_ptr;
454
455 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
456 {
457 *efm_first = efm_ptr->next;
458 vim_regfree(efm_ptr->prog);
459 vim_free(efm_ptr);
460 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100461 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200462}
463
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200464/*
465 * Compute the size of the buffer used to convert a 'errorformat' pattern into
466 * a regular expression pattern.
467 */
468 static int
469efm_regpat_bufsz(char_u *efm)
470{
471 int sz;
472 int i;
473
474 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
475 for (i = FMT_PATTERNS; i > 0; )
476 sz += (int)STRLEN(fmt_pat[--i].pattern);
477#ifdef BACKSLASH_IN_FILENAME
478 sz += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
479#else
480 sz += 2; /* "%f" can become two chars longer */
481#endif
482
483 return sz;
484}
485
486/*
487 * Return the length of a 'errorformat' option part (separated by ",").
488 */
489 static int
490efm_option_part_len(char_u *efm)
491{
492 int len;
493
494 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
495 if (efm[len] == '\\' && efm[len + 1] != NUL)
496 ++len;
497
498 return len;
499}
500
501/*
502 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
503 * are parsed and converted to regular expressions. Returns information about
504 * the parsed 'errorformat' option.
505 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200506 static efm_T *
507parse_efm_option(char_u *efm)
508{
509 char_u *errmsg = NULL;
510 int errmsglen;
511 efm_T *fmt_ptr = NULL;
512 efm_T *fmt_first = NULL;
513 efm_T *fmt_last = NULL;
514 char_u *fmtstr = NULL;
515 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200516 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200517
518 errmsglen = CMDBUFFSIZE + 1;
519 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
520 if (errmsg == NULL)
521 goto parse_efm_end;
522
523 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200524 * Each part of the format string is copied and modified from errorformat
525 * to regex prog. Only a few % characters are allowed.
526 */
527
528 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200529 * Get some space to modify the format string into.
530 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200531 sz = efm_regpat_bufsz(efm);
532 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200533 goto parse_efm_error;
534
535 while (efm[0] != NUL)
536 {
537 /*
538 * Allocate a new eformat structure and put it at the end of the list
539 */
540 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
541 if (fmt_ptr == NULL)
542 goto parse_efm_error;
543 if (fmt_first == NULL) /* first one */
544 fmt_first = fmt_ptr;
545 else
546 fmt_last->next = fmt_ptr;
547 fmt_last = fmt_ptr;
548
549 /*
550 * Isolate one part in the 'errorformat' option
551 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200552 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200553
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200554 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200555 goto parse_efm_error;
556 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
557 goto parse_efm_error;
558 /*
559 * Advance to next part
560 */
561 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
562 }
563
564 if (fmt_first == NULL) /* nothing found */
565 EMSG(_("E378: 'errorformat' contains no pattern"));
566
567 goto parse_efm_end;
568
569parse_efm_error:
570 free_efm_list(&fmt_first);
571
572parse_efm_end:
573 vim_free(fmtstr);
574 vim_free(errmsg);
575
576 return fmt_first;
577}
578
Bram Moolenaare0d37972016-07-15 22:36:01 +0200579enum {
580 QF_FAIL = 0,
581 QF_OK = 1,
582 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200583 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200584 QF_IGNORE_LINE = 4,
585 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200586};
587
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200588/*
589 * State information used to parse lines and add entries to a quickfix/location
590 * list.
591 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200592typedef struct {
593 char_u *linebuf;
594 int linelen;
595 char_u *growbuf;
596 int growbufsiz;
597 FILE *fd;
598 typval_T *tv;
599 char_u *p_str;
600 listitem_T *p_li;
601 buf_T *buf;
602 linenr_T buflnum;
603 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100604 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200605} qfstate_T;
606
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200607/*
608 * Allocate more memory for the line buffer used for parsing lines.
609 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200610 static char_u *
611qf_grow_linebuf(qfstate_T *state, int newsz)
612{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200613 char_u *p;
614
Bram Moolenaare0d37972016-07-15 22:36:01 +0200615 /*
616 * If the line exceeds LINE_MAXLEN exclude the last
617 * byte since it's not a NL character.
618 */
619 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
620 if (state->growbuf == NULL)
621 {
622 state->growbuf = alloc(state->linelen + 1);
623 if (state->growbuf == NULL)
624 return NULL;
625 state->growbufsiz = state->linelen;
626 }
627 else if (state->linelen > state->growbufsiz)
628 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200629 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200630 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200631 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200632 state->growbufsiz = state->linelen;
633 }
634 return state->growbuf;
635}
636
637/*
638 * Get the next string (separated by newline) from state->p_str.
639 */
640 static int
641qf_get_next_str_line(qfstate_T *state)
642{
643 /* Get the next line from the supplied string */
644 char_u *p_str = state->p_str;
645 char_u *p;
646 int len;
647
648 if (*p_str == NUL) /* Reached the end of the string */
649 return QF_END_OF_INPUT;
650
651 p = vim_strchr(p_str, '\n');
652 if (p != NULL)
653 len = (int)(p - p_str) + 1;
654 else
655 len = (int)STRLEN(p_str);
656
657 if (len > IOSIZE - 2)
658 {
659 state->linebuf = qf_grow_linebuf(state, len);
660 if (state->linebuf == NULL)
661 return QF_NOMEM;
662 }
663 else
664 {
665 state->linebuf = IObuff;
666 state->linelen = len;
667 }
668 vim_strncpy(state->linebuf, p_str, state->linelen);
669
670 /*
671 * Increment using len in order to discard the rest of the
672 * line if it exceeds LINE_MAXLEN.
673 */
674 p_str += len;
675 state->p_str = p_str;
676
677 return QF_OK;
678}
679
680/*
681 * Get the next string from state->p_Li.
682 */
683 static int
684qf_get_next_list_line(qfstate_T *state)
685{
686 listitem_T *p_li = state->p_li;
687 int len;
688
689 while (p_li != NULL
690 && (p_li->li_tv.v_type != VAR_STRING
691 || p_li->li_tv.vval.v_string == NULL))
692 p_li = p_li->li_next; /* Skip non-string items */
693
694 if (p_li == NULL) /* End of the list */
695 {
696 state->p_li = NULL;
697 return QF_END_OF_INPUT;
698 }
699
700 len = (int)STRLEN(p_li->li_tv.vval.v_string);
701 if (len > IOSIZE - 2)
702 {
703 state->linebuf = qf_grow_linebuf(state, len);
704 if (state->linebuf == NULL)
705 return QF_NOMEM;
706 }
707 else
708 {
709 state->linebuf = IObuff;
710 state->linelen = len;
711 }
712
713 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
714
715 state->p_li = p_li->li_next; /* next item */
716 return QF_OK;
717}
718
719/*
720 * Get the next string from state->buf.
721 */
722 static int
723qf_get_next_buf_line(qfstate_T *state)
724{
725 char_u *p_buf = NULL;
726 int len;
727
728 /* Get the next line from the supplied buffer */
729 if (state->buflnum > state->lnumlast)
730 return QF_END_OF_INPUT;
731
732 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
733 state->buflnum += 1;
734
735 len = (int)STRLEN(p_buf);
736 if (len > IOSIZE - 2)
737 {
738 state->linebuf = qf_grow_linebuf(state, len);
739 if (state->linebuf == NULL)
740 return QF_NOMEM;
741 }
742 else
743 {
744 state->linebuf = IObuff;
745 state->linelen = len;
746 }
747 vim_strncpy(state->linebuf, p_buf, state->linelen);
748
749 return QF_OK;
750}
751
752/*
753 * Get the next string from file state->fd.
754 */
755 static int
756qf_get_next_file_line(qfstate_T *state)
757{
758 int discard;
759 int growbuflen;
760
761 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
762 return QF_END_OF_INPUT;
763
764 discard = FALSE;
765 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200766 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200767 {
768 /*
769 * The current line exceeds IObuff, continue reading using
770 * growbuf until EOL or LINE_MAXLEN bytes is read.
771 */
772 if (state->growbuf == NULL)
773 {
774 state->growbufsiz = 2 * (IOSIZE - 1);
775 state->growbuf = alloc(state->growbufsiz);
776 if (state->growbuf == NULL)
777 return QF_NOMEM;
778 }
779
780 /* Copy the read part of the line, excluding null-terminator */
781 memcpy(state->growbuf, IObuff, IOSIZE - 1);
782 growbuflen = state->linelen;
783
784 for (;;)
785 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200786 char_u *p;
787
Bram Moolenaare0d37972016-07-15 22:36:01 +0200788 if (fgets((char *)state->growbuf + growbuflen,
789 state->growbufsiz - growbuflen, state->fd) == NULL)
790 break;
791 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
792 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200793 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200794 break;
795 if (state->growbufsiz == LINE_MAXLEN)
796 {
797 discard = TRUE;
798 break;
799 }
800
801 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
802 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200803 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200804 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200805 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200806 }
807
808 while (discard)
809 {
810 /*
811 * The current line is longer than LINE_MAXLEN, continue
812 * reading but discard everything until EOL or EOF is
813 * reached.
814 */
815 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
816 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200817 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200818 break;
819 }
820
821 state->linebuf = state->growbuf;
822 state->linelen = growbuflen;
823 }
824 else
825 state->linebuf = IObuff;
826
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100827#ifdef FEAT_MBYTE
828 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200829 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
830 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100831 char_u *line;
832
833 line = string_convert(&state->vc, state->linebuf, &state->linelen);
834 if (line != NULL)
835 {
836 if (state->linelen < IOSIZE)
837 {
838 STRCPY(state->linebuf, line);
839 vim_free(line);
840 }
841 else
842 {
843 vim_free(state->growbuf);
844 state->linebuf = state->growbuf = line;
845 state->growbufsiz = state->linelen < LINE_MAXLEN
846 ? state->linelen : LINE_MAXLEN;
847 }
848 }
849 }
850#endif
851
Bram Moolenaare0d37972016-07-15 22:36:01 +0200852 return QF_OK;
853}
854
855/*
856 * Get the next string from a file/buffer/list/string.
857 */
858 static int
859qf_get_nextline(qfstate_T *state)
860{
861 int status = QF_FAIL;
862
863 if (state->fd == NULL)
864 {
865 if (state->tv != NULL)
866 {
867 if (state->tv->v_type == VAR_STRING)
868 /* Get the next line from the supplied string */
869 status = qf_get_next_str_line(state);
870 else if (state->tv->v_type == VAR_LIST)
871 /* Get the next line from the supplied list */
872 status = qf_get_next_list_line(state);
873 }
874 else
875 /* Get the next line from the supplied buffer */
876 status = qf_get_next_buf_line(state);
877 }
878 else
879 /* Get the next line from the supplied file */
880 status = qf_get_next_file_line(state);
881
882 if (status != QF_OK)
883 return status;
884
885 /* remove newline/CR from the line */
886 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200887 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200888 state->linebuf[state->linelen - 1] = NUL;
889#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200890 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
891 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200892#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200893 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200894
895#ifdef FEAT_MBYTE
896 remove_bom(state->linebuf);
897#endif
898
899 return QF_OK;
900}
901
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200902typedef struct {
903 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200904 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200905 char_u *errmsg;
906 int errmsglen;
907 long lnum;
908 int col;
909 char_u use_viscol;
910 char_u *pattern;
911 int enr;
912 int type;
913 int valid;
914} qffields_T;
915
916/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200917 * Parse the match for filename ('%f') pattern in regmatch.
918 * Return the matched value in "fields->namebuf".
919 */
920 static int
921qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
922{
923 int c;
924
925 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
926 return QF_FAIL;
927
928 /* Expand ~/file and $HOME/file to full path. */
929 c = *rmp->endp[midx];
930 *rmp->endp[midx] = NUL;
931 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
932 *rmp->endp[midx] = c;
933
934 /*
935 * For separate filename patterns (%O, %P and %Q), the specified file
936 * should exist.
937 */
938 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
939 && mch_getperm(fields->namebuf) == -1)
940 return QF_FAIL;
941
942 return QF_OK;
943}
944
945/*
946 * Parse the match for error number ('%n') pattern in regmatch.
947 * Return the matched value in "fields->enr".
948 */
949 static int
950qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
951{
952 if (rmp->startp[midx] == NULL)
953 return QF_FAIL;
954 fields->enr = (int)atol((char *)rmp->startp[midx]);
955 return QF_OK;
956}
957
958/*
959 * Parse the match for line number (%l') pattern in regmatch.
960 * Return the matched value in "fields->lnum".
961 */
962 static int
963qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
964{
965 if (rmp->startp[midx] == NULL)
966 return QF_FAIL;
967 fields->lnum = atol((char *)rmp->startp[midx]);
968 return QF_OK;
969}
970
971/*
972 * Parse the match for column number ('%c') pattern in regmatch.
973 * Return the matched value in "fields->col".
974 */
975 static int
976qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
977{
978 if (rmp->startp[midx] == NULL)
979 return QF_FAIL;
980 fields->col = (int)atol((char *)rmp->startp[midx]);
981 return QF_OK;
982}
983
984/*
985 * Parse the match for error type ('%t') pattern in regmatch.
986 * Return the matched value in "fields->type".
987 */
988 static int
989qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
990{
991 if (rmp->startp[midx] == NULL)
992 return QF_FAIL;
993 fields->type = *rmp->startp[midx];
994 return QF_OK;
995}
996
997/*
998 * Parse the match for '%+' format pattern. The whole matching line is included
999 * in the error string. Return the matched line in "fields->errmsg".
1000 */
1001 static int
1002qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
1003{
1004 char_u *p;
1005
1006 if (linelen >= fields->errmsglen)
1007 {
1008 /* linelen + null terminator */
1009 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1010 return QF_NOMEM;
1011 fields->errmsg = p;
1012 fields->errmsglen = linelen + 1;
1013 }
1014 vim_strncpy(fields->errmsg, linebuf, linelen);
1015 return QF_OK;
1016}
1017
1018/*
1019 * Parse the match for error message ('%m') pattern in regmatch.
1020 * Return the matched value in "fields->errmsg".
1021 */
1022 static int
1023qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1024{
1025 char_u *p;
1026 int len;
1027
1028 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1029 return QF_FAIL;
1030 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1031 if (len >= fields->errmsglen)
1032 {
1033 /* len + null terminator */
1034 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1035 return QF_NOMEM;
1036 fields->errmsg = p;
1037 fields->errmsglen = len + 1;
1038 }
1039 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1040 return QF_OK;
1041}
1042
1043/*
1044 * Parse the match for rest of a single-line file message ('%r') pattern.
1045 * Return the matched value in "tail".
1046 */
1047 static int
1048qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1049{
1050 if (rmp->startp[midx] == NULL)
1051 return QF_FAIL;
1052 *tail = rmp->startp[midx];
1053 return QF_OK;
1054}
1055
1056/*
1057 * Parse the match for the pointer line ('%p') pattern in regmatch.
1058 * Return the matched value in "fields->col".
1059 */
1060 static int
1061qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1062{
1063 char_u *match_ptr;
1064
1065 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1066 return QF_FAIL;
1067 fields->col = 0;
1068 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1069 ++match_ptr)
1070 {
1071 ++fields->col;
1072 if (*match_ptr == TAB)
1073 {
1074 fields->col += 7;
1075 fields->col -= fields->col % 8;
1076 }
1077 }
1078 ++fields->col;
1079 fields->use_viscol = TRUE;
1080 return QF_OK;
1081}
1082
1083/*
1084 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1085 * Return the matched value in "fields->col".
1086 */
1087 static int
1088qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1089{
1090 if (rmp->startp[midx] == NULL)
1091 return QF_FAIL;
1092 fields->col = (int)atol((char *)rmp->startp[midx]);
1093 fields->use_viscol = TRUE;
1094 return QF_OK;
1095}
1096
1097/*
1098 * Parse the match for the search text ('%s') pattern in regmatch.
1099 * Return the matched value in "fields->pattern".
1100 */
1101 static int
1102qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1103{
1104 int len;
1105
1106 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1107 return QF_FAIL;
1108 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1109 if (len > CMDBUFFSIZE - 5)
1110 len = CMDBUFFSIZE - 5;
1111 STRCPY(fields->pattern, "^\\V");
1112 STRNCAT(fields->pattern, rmp->startp[midx], len);
1113 fields->pattern[len + 3] = '\\';
1114 fields->pattern[len + 4] = '$';
1115 fields->pattern[len + 5] = NUL;
1116 return QF_OK;
1117}
1118
1119/*
1120 * Parse the match for the module ('%o') pattern in regmatch.
1121 * Return the matched value in "fields->module".
1122 */
1123 static int
1124qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1125{
1126 int len;
1127
1128 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1129 return QF_FAIL;
1130 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1131 if (len > CMDBUFFSIZE)
1132 len = CMDBUFFSIZE;
1133 STRNCAT(fields->module, rmp->startp[midx], len);
1134 return QF_OK;
1135}
1136
1137/*
1138 * 'errorformat' format pattern parser functions.
1139 * The '%f' and '%r' formats are parsed differently from other formats.
1140 * See qf_parse_match() for details.
1141 */
1142static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1143{
1144 NULL,
1145 qf_parse_fmt_n,
1146 qf_parse_fmt_l,
1147 qf_parse_fmt_c,
1148 qf_parse_fmt_t,
1149 qf_parse_fmt_m,
1150 NULL,
1151 qf_parse_fmt_p,
1152 qf_parse_fmt_v,
1153 qf_parse_fmt_s,
1154 qf_parse_fmt_o
1155};
1156
1157/*
1158 * Parse the error format pattern matches in "regmatch" and set the values in
1159 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1160 * match. Returns QF_OK if all the matches are successfully parsed. On
1161 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001162 */
1163 static int
1164qf_parse_match(
1165 char_u *linebuf,
1166 int linelen,
1167 efm_T *fmt_ptr,
1168 regmatch_T *regmatch,
1169 qffields_T *fields,
1170 int qf_multiline,
1171 int qf_multiscan,
1172 char_u **tail)
1173{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001174 int idx = fmt_ptr->prefix;
1175 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001176 int midx;
1177 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001178
1179 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1180 return QF_FAIL;
1181 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1182 fields->type = idx;
1183 else
1184 fields->type = 0;
1185 /*
1186 * Extract error message data from matched line.
1187 * We check for an actual submatch, because "\[" and "\]" in
1188 * the 'errorformat' may cause the wrong submatch to be used.
1189 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001190 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001191 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001192 status = QF_OK;
1193 midx = (int)fmt_ptr->addr[i];
1194 if (i == 0 && midx > 0) /* %f */
1195 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1196 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001197 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001198 if (fmt_ptr->flags == '+' && !qf_multiscan) /* %+ */
1199 status = qf_parse_fmt_plus(linebuf, linelen, fields);
1200 else if (midx > 0) /* %m */
1201 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001202 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001203 else if (i == 6 && midx > 0) /* %r */
1204 status = qf_parse_fmt_r(regmatch, midx, tail);
1205 else if (midx > 0) /* others */
1206 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001207
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001208 if (status != QF_OK)
1209 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001210 }
1211
1212 return QF_OK;
1213}
1214
1215/*
1216 * Parse an error line in 'linebuf' using a single error format string in
1217 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1218 * Returns QF_OK if the efm format matches completely and the fields are
1219 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1220 */
1221 static int
1222qf_parse_get_fields(
1223 char_u *linebuf,
1224 int linelen,
1225 efm_T *fmt_ptr,
1226 qffields_T *fields,
1227 int qf_multiline,
1228 int qf_multiscan,
1229 char_u **tail)
1230{
1231 regmatch_T regmatch;
1232 int status = QF_FAIL;
1233 int r;
1234
1235 if (qf_multiscan &&
1236 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1237 return QF_FAIL;
1238
1239 fields->namebuf[0] = NUL;
1240 fields->module[0] = NUL;
1241 fields->pattern[0] = NUL;
1242 if (!qf_multiscan)
1243 fields->errmsg[0] = NUL;
1244 fields->lnum = 0;
1245 fields->col = 0;
1246 fields->use_viscol = FALSE;
1247 fields->enr = -1;
1248 fields->type = 0;
1249 *tail = NULL;
1250
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001251 /* Always ignore case when looking for a matching error. */
1252 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001253 regmatch.regprog = fmt_ptr->prog;
1254 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1255 fmt_ptr->prog = regmatch.regprog;
1256 if (r)
1257 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1258 fields, qf_multiline, qf_multiscan, tail);
1259
1260 return status;
1261}
1262
1263/*
1264 * Parse directory error format prefixes (%D and %X).
1265 * Push and pop directories from the directory stack when scanning directory
1266 * names.
1267 */
1268 static int
1269qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1270{
1271 if (idx == 'D') /* enter directory */
1272 {
1273 if (*fields->namebuf == NUL)
1274 {
1275 EMSG(_("E379: Missing or empty directory name"));
1276 return QF_FAIL;
1277 }
1278 qfl->qf_directory =
1279 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1280 if (qfl->qf_directory == NULL)
1281 return QF_FAIL;
1282 }
1283 else if (idx == 'X') /* leave directory */
1284 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1285
1286 return QF_OK;
1287}
1288
1289/*
1290 * Parse global file name error format prefixes (%O, %P and %Q).
1291 */
1292 static int
1293qf_parse_file_pfx(
1294 int idx,
1295 qffields_T *fields,
1296 qf_list_T *qfl,
1297 char_u *tail)
1298{
1299 fields->valid = FALSE;
1300 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1301 {
1302 if (*fields->namebuf && idx == 'P')
1303 qfl->qf_currfile =
1304 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1305 else if (idx == 'Q')
1306 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1307 *fields->namebuf = NUL;
1308 if (tail && *tail)
1309 {
1310 STRMOVE(IObuff, skipwhite(tail));
1311 qfl->qf_multiscan = TRUE;
1312 return QF_MULTISCAN;
1313 }
1314 }
1315
1316 return QF_OK;
1317}
1318
1319/*
1320 * Parse a non-error line (a line which doesn't match any of the error
1321 * format in 'efm').
1322 */
1323 static int
1324qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1325{
1326 char_u *p;
1327
1328 fields->namebuf[0] = NUL; /* no match found, remove file name */
1329 fields->lnum = 0; /* don't jump to this line */
1330 fields->valid = FALSE;
1331 if (linelen >= fields->errmsglen)
1332 {
1333 /* linelen + null terminator */
1334 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1335 return QF_NOMEM;
1336 fields->errmsg = p;
1337 fields->errmsglen = linelen + 1;
1338 }
1339 /* copy whole line to error message */
1340 vim_strncpy(fields->errmsg, linebuf, linelen);
1341
1342 return QF_OK;
1343}
1344
1345/*
1346 * Parse multi-line error format prefixes (%C and %Z)
1347 */
1348 static int
1349qf_parse_multiline_pfx(
1350 qf_info_T *qi,
1351 int qf_idx,
1352 int idx,
1353 qf_list_T *qfl,
1354 qffields_T *fields)
1355{
1356 char_u *ptr;
1357 int len;
1358
1359 if (!qfl->qf_multiignore)
1360 {
1361 qfline_T *qfprev = qfl->qf_last;
1362
1363 if (qfprev == NULL)
1364 return QF_FAIL;
1365 if (*fields->errmsg && !qfl->qf_multiignore)
1366 {
1367 len = (int)STRLEN(qfprev->qf_text);
1368 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1369 == NULL)
1370 return QF_FAIL;
1371 STRCPY(ptr, qfprev->qf_text);
1372 vim_free(qfprev->qf_text);
1373 qfprev->qf_text = ptr;
1374 *(ptr += len) = '\n';
1375 STRCPY(++ptr, fields->errmsg);
1376 }
1377 if (qfprev->qf_nr == -1)
1378 qfprev->qf_nr = fields->enr;
1379 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1380 /* only printable chars allowed */
1381 qfprev->qf_type = fields->type;
1382
1383 if (!qfprev->qf_lnum)
1384 qfprev->qf_lnum = fields->lnum;
1385 if (!qfprev->qf_col)
1386 qfprev->qf_col = fields->col;
1387 qfprev->qf_viscol = fields->use_viscol;
1388 if (!qfprev->qf_fnum)
1389 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1390 qfl->qf_directory,
1391 *fields->namebuf || qfl->qf_directory != NULL
1392 ? fields->namebuf
1393 : qfl->qf_currfile != NULL && fields->valid
1394 ? qfl->qf_currfile : 0);
1395 }
1396 if (idx == 'Z')
1397 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1398 line_breakcheck();
1399
1400 return QF_IGNORE_LINE;
1401}
1402
1403/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001404 * Parse a line and get the quickfix fields.
1405 * Return the QF_ status.
1406 */
1407 static int
1408qf_parse_line(
1409 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001410 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001411 char_u *linebuf,
1412 int linelen,
1413 efm_T *fmt_first,
1414 qffields_T *fields)
1415{
1416 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417 int idx = 0;
1418 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001419 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001420 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001421
Bram Moolenaare333e792018-04-08 13:27:39 +02001422restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001423 /* If there was no %> item start at the first pattern */
1424 if (fmt_start == NULL)
1425 fmt_ptr = fmt_first;
1426 else
1427 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001428 /* Otherwise start from the last used pattern */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001429 fmt_ptr = fmt_start;
1430 fmt_start = NULL;
1431 }
1432
1433 /*
1434 * Try to match each part of 'errorformat' until we find a complete
1435 * match or no match.
1436 */
1437 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001438 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1439 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001440 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001441 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1442 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1443 if (status == QF_NOMEM)
1444 return status;
1445 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001446 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001447 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001448 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001449
1450 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1451 {
1452 if (fmt_ptr != NULL)
1453 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001454 /* 'D' and 'X' directory specifiers */
1455 status = qf_parse_dir_pfx(idx, fields, qfl);
1456 if (status != QF_OK)
1457 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001458 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001459
1460 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1461 if (status != QF_OK)
1462 return status;
1463
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001464 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001465 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 }
1467 else if (fmt_ptr != NULL)
1468 {
1469 /* honor %> item */
1470 if (fmt_ptr->conthere)
1471 fmt_start = fmt_ptr;
1472
1473 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1474 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001475 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1476 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001477 }
1478 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1479 { /* continuation of multi-line msg */
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001480 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1481 if (status != QF_OK)
1482 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001483 }
1484 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001485 { /* global file names */
1486 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1487 if (status == QF_MULTISCAN)
1488 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001489 }
1490 if (fmt_ptr->flags == '-') /* generally exclude this line */
1491 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001492 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001493 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001494 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001495 return QF_IGNORE_LINE;
1496 }
1497 }
1498
1499 return QF_OK;
1500}
1501
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001502/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001503 * Returns TRUE if the specified quickfix/location list is empty.
1504 */
1505 static int
1506qf_list_empty(qf_info_T *qi, int qf_idx)
1507{
1508 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1509 return TRUE;
1510 return qi->qf_lists[qf_idx].qf_count <= 0;
1511}
1512
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001513/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001514 * Allocate the fields used for parsing lines and populating a quickfix list.
1515 */
1516 static int
1517qf_alloc_fields(qffields_T *pfields)
1518{
1519 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1520 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1521 pfields->errmsglen = CMDBUFFSIZE + 1;
1522 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1523 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1524 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1525 || pfields->pattern == NULL || pfields->module == NULL)
1526 return FAIL;
1527
1528 return OK;
1529}
1530
1531/*
1532 * Free the fields used for parsing lines and populating a quickfix list.
1533 */
1534 static void
1535qf_free_fields(qffields_T *pfields)
1536{
1537 vim_free(pfields->namebuf);
1538 vim_free(pfields->module);
1539 vim_free(pfields->errmsg);
1540 vim_free(pfields->pattern);
1541}
1542
1543/*
1544 * Setup the state information used for parsing lines and populating a
1545 * quickfix list.
1546 */
1547 static int
1548qf_setup_state(
1549 qfstate_T *pstate,
1550 char_u *enc,
1551 char_u *efile,
1552 typval_T *tv,
1553 buf_T *buf,
1554 linenr_T lnumfirst,
1555 linenr_T lnumlast)
1556{
1557#ifdef FEAT_MBYTE
1558 pstate->vc.vc_type = CONV_NONE;
1559 if (enc != NULL && *enc != NUL)
1560 convert_setup(&pstate->vc, enc, p_enc);
1561#endif
1562
1563 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1564 {
1565 EMSG2(_(e_openerrf), efile);
1566 return FAIL;
1567 }
1568
1569 if (tv != NULL)
1570 {
1571 if (tv->v_type == VAR_STRING)
1572 pstate->p_str = tv->vval.v_string;
1573 else if (tv->v_type == VAR_LIST)
1574 pstate->p_li = tv->vval.v_list->lv_first;
1575 pstate->tv = tv;
1576 }
1577 pstate->buf = buf;
1578 pstate->buflnum = lnumfirst;
1579 pstate->lnumlast = lnumlast;
1580
1581 return OK;
1582}
1583
1584/*
1585 * Cleanup the state information used for parsing lines and populating a
1586 * quickfix list.
1587 */
1588 static void
1589qf_cleanup_state(qfstate_T *pstate)
1590{
1591 if (pstate->fd != NULL)
1592 fclose(pstate->fd);
1593
1594 vim_free(pstate->growbuf);
1595#ifdef FEAT_MBYTE
1596 if (pstate->vc.vc_type != CONV_NONE)
1597 convert_setup(&pstate->vc, NULL, NULL);
1598#endif
1599}
1600
1601/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001602 * Read the errorfile "efile" into memory, line by line, building the error
1603 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001604 * Alternative: when "efile" is NULL read errors from buffer "buf".
1605 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001606 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001607 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1608 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001609 * Return -1 for error, number of errors for success.
1610 */
1611 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001612qf_init_ext(
1613 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001614 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001615 char_u *efile,
1616 buf_T *buf,
1617 typval_T *tv,
1618 char_u *errorformat,
1619 int newlist, /* TRUE: start a new error list */
1620 linenr_T lnumfirst, /* first line number to use */
1621 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001622 char_u *qf_title,
1623 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001624{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001625 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001626 qfstate_T state;
1627 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001628 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001629 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001630 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001632 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001634 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001636 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001637 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001638
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001639 vim_memset(&state, 0, sizeof(state));
1640 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001641 if ((qf_alloc_fields(&fields) == FAIL) ||
1642 (qf_setup_state(&state, enc, efile, tv, buf,
1643 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 goto qf_init_end;
1645
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001646 if (newlist || qf_idx == qi->qf_listcount)
1647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001649 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001650 qf_idx = qi->qf_curlist;
1651 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001652 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001653 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001654 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001655 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001656 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001657 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001660 qfl = &qi->qf_lists[qf_idx];
1661
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001663 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001664 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 else
1666 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001668 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001669 * If the errorformat didn't change between calls, then reuse the
1670 * previously parsed values.
1671 */
1672 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1673 {
1674 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001675 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001676 free_efm_list(&fmt_first);
1677
1678 /* parse the current 'efm' */
1679 fmt_first = parse_efm_option(efm);
1680 if (fmt_first != NULL)
1681 last_efm = vim_strsave(efm);
1682 }
1683
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
1687 /*
1688 * got_int is reset here, because it was probably set when killing the
1689 * ":make" command, but we still want to read the errorfile then.
1690 */
1691 got_int = FALSE;
1692
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 /*
1694 * Read the lines in the error file one by one.
1695 * Try to recognize one of the error formats in each line.
1696 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001697 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001699 /* Get the next line from a file/buffer/list/string */
1700 status = qf_get_nextline(&state);
1701 if (status == QF_NOMEM) /* memory alloc failure */
1702 goto qf_init_end;
1703 if (status == QF_END_OF_INPUT) /* end of input */
1704 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001706 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1707 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001708 if (status == QF_FAIL)
1709 goto error2;
1710 if (status == QF_NOMEM)
1711 goto qf_init_end;
1712 if (status == QF_IGNORE_LINE)
1713 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001715 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001716 qf_idx,
1717 qfl->qf_directory,
1718 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001719 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001720 : ((qfl->qf_currfile != NULL && fields.valid)
1721 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001722 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001723 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001724 fields.errmsg,
1725 fields.lnum,
1726 fields.col,
1727 fields.use_viscol,
1728 fields.pattern,
1729 fields.enr,
1730 fields.type,
1731 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 goto error2;
1733 line_breakcheck();
1734 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001735 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001737 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001739 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001740 qfl->qf_ptr = qfl->qf_start;
1741 qfl->qf_index = 1;
1742 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
1744 else
1745 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001746 qfl->qf_nonevalid = FALSE;
1747 if (qfl->qf_ptr == NULL)
1748 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001750 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001751 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001752 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
1754 EMSG(_(e_readerrf));
1755error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001756 if (!adding)
1757 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001758 /* Error when creating a new list. Free the new list */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001759 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001760 qi->qf_listcount--;
1761 if (qi->qf_curlist > 0)
1762 --qi->qf_curlist;
1763 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001764qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001765 if (qf_idx == qi->qf_curlist)
1766 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001767 qf_cleanup_state(&state);
1768 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
1770 return retval;
1771}
1772
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001773/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001774 * Read the errorfile "efile" into memory, line by line, building the error
1775 * list. Set the error list's title to qf_title.
1776 * Return -1 for error, number of errors for success.
1777 */
1778 int
1779qf_init(win_T *wp,
1780 char_u *efile,
1781 char_u *errorformat,
1782 int newlist, /* TRUE: start a new error list */
1783 char_u *qf_title,
1784 char_u *enc)
1785{
1786 qf_info_T *qi = &ql_info;
1787
1788 if (wp != NULL)
1789 {
1790 qi = ll_get_or_alloc_list(wp);
1791 if (qi == NULL)
1792 return FAIL;
1793 }
1794
1795 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1796 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1797}
1798
1799/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001800 * Set the title of the specified quickfix list. Frees the previous title.
1801 * Prepends ':' to the title.
1802 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001803 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001804qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001805{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001806 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001807
Bram Moolenaarfb604092014-07-23 15:55:00 +02001808 if (title != NULL)
1809 {
1810 char_u *p = alloc((int)STRLEN(title) + 2);
1811
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001812 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001813 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001814 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001815 }
1816}
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001819 * The title of a quickfix/location list is set, by default, to the command
1820 * that created the quickfix list with the ":" prefix.
1821 * Create a quickfix list title string by prepending ":" to a user command.
1822 * Returns a pointer to a static buffer with the title.
1823 */
1824 static char_u *
1825qf_cmdtitle(char_u *cmd)
1826{
1827 static char_u qftitle_str[IOSIZE];
1828
1829 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1830 return qftitle_str;
1831}
1832
1833/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001834 * Prepare for adding a new quickfix list. If the current list is in the
1835 * middle of the stack, then all the following lists are freed and then
1836 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 */
1838 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001839qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 int i;
1842
1843 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001844 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 * the current entry. This makes it possible to browse in a tree-like
1846 * way with ":grep'.
1847 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001848 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001849 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 /*
1852 * When the stack is full, remove to oldest entry
1853 * Otherwise, add a new entry.
1854 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001855 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001857 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001859 qi->qf_lists[i - 1] = qi->qf_lists[i];
1860 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
1862 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001863 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001864 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001865 qf_store_title(&qi->qf_lists[qi->qf_curlist], qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001866 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867}
1868
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001869/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001870 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001871 */
1872 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001873ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001874{
1875 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001876 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001877
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001878 qi = *pqi;
1879 if (qi == NULL)
1880 return;
1881 *pqi = NULL; /* Remove reference to this list */
1882
1883 qi->qf_refcount--;
1884 if (qi->qf_refcount < 1)
1885 {
1886 /* No references to this location list */
1887 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001888 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001889 vim_free(qi);
1890 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001891}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001892
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001893/*
1894 * Free all the quickfix/location lists in the stack.
1895 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001896 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001897qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001898{
1899 int i;
1900 qf_info_T *qi = &ql_info;
1901
1902 if (wp != NULL)
1903 {
1904 /* location list */
1905 ll_free_all(&wp->w_llist);
1906 ll_free_all(&wp->w_llist_ref);
1907 }
1908 else
1909 /* quickfix list */
1910 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001911 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001912}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001913
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914/*
1915 * Add an entry to the end of the list of errors.
1916 * Returns OK or FAIL.
1917 */
1918 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001919qf_add_entry(
1920 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001921 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001922 char_u *dir, /* optional directory name */
1923 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001924 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001925 int bufnum, /* buffer number or zero */
1926 char_u *mesg, /* message */
1927 long lnum, /* line number */
1928 int col, /* column */
1929 int vis_col, /* using visual column */
1930 char_u *pattern, /* search pattern */
1931 int nr, /* error number */
1932 int type, /* type character */
1933 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001935 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001936 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001937 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001939 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001941 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001942 {
1943 buf_T *buf = buflist_findnr(bufnum);
1944
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001945 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001946 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001947 buf->b_has_qf_entry |=
Bram Moolenaar4d77c652018-08-18 19:59:54 +02001948 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001949 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001950 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001951 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1953 {
1954 vim_free(qfp);
1955 return FAIL;
1956 }
1957 qfp->qf_lnum = lnum;
1958 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001959 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001960 if (pattern == NULL || *pattern == NUL)
1961 qfp->qf_pattern = NULL;
1962 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1963 {
1964 vim_free(qfp->qf_text);
1965 vim_free(qfp);
1966 return FAIL;
1967 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001968 if (module == NULL || *module == NUL)
1969 qfp->qf_module = NULL;
1970 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1971 {
1972 vim_free(qfp->qf_text);
1973 vim_free(qfp->qf_pattern);
1974 vim_free(qfp);
1975 return FAIL;
1976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 qfp->qf_nr = nr;
1978 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1979 type = 0;
1980 qfp->qf_type = type;
1981 qfp->qf_valid = valid;
1982
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001983 lastp = &qfl->qf_last;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001984 if (qf_list_empty(qi, qf_idx)) /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001986 qfl->qf_start = qfp;
1987 qfl->qf_ptr = qfp;
1988 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001989 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
1991 else
1992 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001993 qfp->qf_prev = *lastp;
1994 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001996 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001998 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001999 ++qfl->qf_count;
2000 if (qfl->qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002001 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002003 qfl->qf_index = qfl->qf_count;
2004 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
2006
2007 return OK;
2008}
2009
2010/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002011 * Allocate a new location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002012 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002013 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002014ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002015{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002016 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002018 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002019 if (qi != NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002020 qi->qf_refcount++;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002021 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002022}
2023
2024/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002025 * Return the location list stack for window 'wp'.
2026 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002027 */
2028 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002029ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002030{
2031 if (IS_LL_WINDOW(wp))
2032 /* For a location list window, use the referenced location list */
2033 return wp->w_llist_ref;
2034
2035 /*
2036 * For a non-location list window, w_llist_ref should not point to a
2037 * location list.
2038 */
2039 ll_free_all(&wp->w_llist_ref);
2040
2041 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002042 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002043 return wp->w_llist;
2044}
2045
2046/*
2047 * Copy the location list from window "from" to window "to".
2048 */
2049 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002050copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002051{
2052 qf_info_T *qi;
2053 int idx;
2054 int i;
2055
2056 /*
2057 * When copying from a location list window, copy the referenced
2058 * location list. For other windows, copy the location list for
2059 * that window.
2060 */
2061 if (IS_LL_WINDOW(from))
2062 qi = from->w_llist_ref;
2063 else
2064 qi = from->w_llist;
2065
2066 if (qi == NULL) /* no location list to copy */
2067 return;
2068
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002069 /* allocate a new location list */
2070 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002071 return;
2072
2073 to->w_llist->qf_listcount = qi->qf_listcount;
2074
2075 /* Copy the location lists one at a time */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002076 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002077 {
2078 qf_list_T *from_qfl;
2079 qf_list_T *to_qfl;
2080
2081 to->w_llist->qf_curlist = idx;
2082
2083 from_qfl = &qi->qf_lists[idx];
2084 to_qfl = &to->w_llist->qf_lists[idx];
2085
2086 /* Some of the fields are populated by qf_add_entry() */
2087 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2088 to_qfl->qf_count = 0;
2089 to_qfl->qf_index = 0;
2090 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002091 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002092 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002093 if (from_qfl->qf_title != NULL)
2094 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2095 else
2096 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002097 if (from_qfl->qf_ctx != NULL)
2098 {
2099 to_qfl->qf_ctx = alloc_tv();
2100 if (to_qfl->qf_ctx != NULL)
2101 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2102 }
2103 else
2104 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002105
2106 if (from_qfl->qf_count)
2107 {
2108 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002109 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002110
2111 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002112 for (i = 0, from_qfp = from_qfl->qf_start;
2113 i < from_qfl->qf_count && from_qfp != NULL;
2114 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002115 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002116 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02002117 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002118 NULL,
2119 NULL,
Bram Moolenaard76ce852018-05-01 15:02:04 +02002120 from_qfp->qf_module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002121 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002122 from_qfp->qf_text,
2123 from_qfp->qf_lnum,
2124 from_qfp->qf_col,
2125 from_qfp->qf_viscol,
2126 from_qfp->qf_pattern,
2127 from_qfp->qf_nr,
2128 0,
2129 from_qfp->qf_valid) == FAIL)
2130 {
2131 qf_free_all(to);
2132 return;
2133 }
2134 /*
2135 * qf_add_entry() will not set the qf_num field, as the
2136 * directory and file names are not supplied. So the qf_fnum
2137 * field is copied here.
2138 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002139 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002140 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
2141 prevp->qf_type = from_qfp->qf_type; /* error type */
2142 if (from_qfl->qf_ptr == from_qfp)
2143 to_qfl->qf_ptr = prevp; /* current location */
2144 }
2145 }
2146
2147 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
2148
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002149 /* Assign a new ID for the location list */
2150 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01002151 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002152
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002153 /* When no valid entries are present in the list, qf_ptr points to
2154 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02002155 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002156 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002157 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002158 to_qfl->qf_index = 1;
2159 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002160 }
2161
2162 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
2163}
2164
2165/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002166 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002167 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 */
2169 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002170qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
Bram Moolenaar82404332016-07-10 17:00:38 +02002172 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002173 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002174 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002175
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (fname == NULL || *fname == NUL) /* no file name */
2177 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
Bram Moolenaare60acc12011-05-10 16:41:25 +02002179#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002180 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002181#endif
2182#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002183 if (directory != NULL)
2184 slash_adjust(directory);
2185 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002186#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002187 if (directory != NULL && !vim_isAbsName(fname)
2188 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2189 {
2190 /*
2191 * Here we check if the file really exists.
2192 * This should normally be true, but if make works without
2193 * "leaving directory"-messages we might have missed a
2194 * directory change.
2195 */
2196 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 vim_free(ptr);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002199 directory = qf_guess_filepath(&qi->qf_lists[qf_idx], fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002200 if (directory)
2201 ptr = concat_fnames(directory, fname, TRUE);
2202 else
2203 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002205 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02002206 bufname = ptr;
2207 }
2208 else
2209 bufname = fname;
2210
2211 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002212 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002213 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002214 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002215 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002217 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002218 {
2219 vim_free(qf_last_bufname);
2220 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2221 if (bufname == ptr)
2222 qf_last_bufname = bufname;
2223 else
2224 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002225 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002226 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002227 if (buf == NULL)
2228 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002229
Bram Moolenaarc1542742016-07-20 21:44:37 +02002230 buf->b_has_qf_entry =
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002231 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002232 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233}
2234
2235/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002236 * Push dirbuf onto the directory stack and return pointer to actual dir or
2237 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 */
2239 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002240qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
2242 struct dir_stack_T *ds_new;
2243 struct dir_stack_T *ds_ptr;
2244
2245 /* allocate new stack element and hook it in */
2246 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2247 if (ds_new == NULL)
2248 return NULL;
2249
2250 ds_new->next = *stackptr;
2251 *stackptr = ds_new;
2252
2253 /* store directory on the stack */
2254 if (vim_isAbsName(dirbuf)
2255 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002256 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 (*stackptr)->dirname = vim_strsave(dirbuf);
2258 else
2259 {
2260 /* Okay we don't have an absolute path.
2261 * dirbuf must be a subdir of one of the directories on the stack.
2262 * Let's search...
2263 */
2264 ds_new = (*stackptr)->next;
2265 (*stackptr)->dirname = NULL;
2266 while (ds_new)
2267 {
2268 vim_free((*stackptr)->dirname);
2269 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2270 TRUE);
2271 if (mch_isdir((*stackptr)->dirname) == TRUE)
2272 break;
2273
2274 ds_new = ds_new->next;
2275 }
2276
2277 /* clean up all dirs we already left */
2278 while ((*stackptr)->next != ds_new)
2279 {
2280 ds_ptr = (*stackptr)->next;
2281 (*stackptr)->next = (*stackptr)->next->next;
2282 vim_free(ds_ptr->dirname);
2283 vim_free(ds_ptr);
2284 }
2285
2286 /* Nothing found -> it must be on top level */
2287 if (ds_new == NULL)
2288 {
2289 vim_free((*stackptr)->dirname);
2290 (*stackptr)->dirname = vim_strsave(dirbuf);
2291 }
2292 }
2293
2294 if ((*stackptr)->dirname != NULL)
2295 return (*stackptr)->dirname;
2296 else
2297 {
2298 ds_ptr = *stackptr;
2299 *stackptr = (*stackptr)->next;
2300 vim_free(ds_ptr);
2301 return NULL;
2302 }
2303}
2304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305/*
2306 * pop dirbuf from the directory stack and return previous directory or NULL if
2307 * stack is empty
2308 */
2309 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002310qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
2312 struct dir_stack_T *ds_ptr;
2313
2314 /* TODO: Should we check if dirbuf is the directory on top of the stack?
2315 * What to do if it isn't? */
2316
2317 /* pop top element and free it */
2318 if (*stackptr != NULL)
2319 {
2320 ds_ptr = *stackptr;
2321 *stackptr = (*stackptr)->next;
2322 vim_free(ds_ptr->dirname);
2323 vim_free(ds_ptr);
2324 }
2325
2326 /* return NEW top element as current dir or NULL if stack is empty*/
2327 return *stackptr ? (*stackptr)->dirname : NULL;
2328}
2329
2330/*
2331 * clean up directory stack
2332 */
2333 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002334qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336 struct dir_stack_T *ds_ptr;
2337
2338 while ((ds_ptr = *stackptr) != NULL)
2339 {
2340 *stackptr = (*stackptr)->next;
2341 vim_free(ds_ptr->dirname);
2342 vim_free(ds_ptr);
2343 }
2344}
2345
2346/*
2347 * Check in which directory of the directory stack the given file can be
2348 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002349 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 * Cleans up intermediate directory entries.
2351 *
2352 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002353 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 * ./
2355 * ./aa
2356 * ./aa/bb
2357 * ./bb
2358 * ./bb/x.c
2359 * and make says:
2360 * making all in aa
2361 * making all in bb
2362 * x.c:9: Error
2363 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2364 * qf_guess_filepath will return NULL.
2365 */
2366 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002367qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368{
2369 struct dir_stack_T *ds_ptr;
2370 struct dir_stack_T *ds_tmp;
2371 char_u *fullname;
2372
2373 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002374 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 return NULL;
2376
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002377 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 fullname = NULL;
2379 while (ds_ptr)
2380 {
2381 vim_free(fullname);
2382 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2383
2384 /* If concat_fnames failed, just go on. The worst thing that can happen
2385 * is that we delete the entire stack.
2386 */
2387 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2388 break;
2389
2390 ds_ptr = ds_ptr->next;
2391 }
2392
2393 vim_free(fullname);
2394
2395 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002396 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002398 ds_tmp = qfl->qf_dir_stack->next;
2399 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 vim_free(ds_tmp->dirname);
2401 vim_free(ds_tmp);
2402 }
2403
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002404 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405}
2406
2407/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002408 * Returns TRUE if a quickfix/location list with the given identifier exists.
2409 */
2410 static int
2411qflist_valid (win_T *wp, int_u qf_id)
2412{
2413 qf_info_T *qi = &ql_info;
2414 int i;
2415
2416 if (wp != NULL)
2417 {
2418 qi = GET_LOC_LIST(wp); /* Location list */
2419 if (qi == NULL)
2420 return FALSE;
2421 }
2422
2423 for (i = 0; i < qi->qf_listcount; ++i)
2424 if (qi->qf_lists[i].qf_id == qf_id)
2425 return TRUE;
2426
2427 return FALSE;
2428}
2429
2430/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002431 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002432 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002433 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002434 * Similar to location list.
2435 */
2436 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002437is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002438{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002439 qfline_T *qfp;
2440 int i;
2441
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002442 /* Search for the entry in the current list */
2443 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2444 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002445 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002446 break;
2447
2448 if (i == qfl->qf_count) /* Entry is not found */
2449 return FALSE;
2450
2451 return TRUE;
2452}
2453
2454/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002455 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002456 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002457 */
2458 static qfline_T *
2459get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002460 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002461 qfline_T *qf_ptr,
2462 int *qf_index,
2463 int dir)
2464{
2465 int idx;
2466 int old_qf_fnum;
2467
2468 idx = *qf_index;
2469 old_qf_fnum = qf_ptr->qf_fnum;
2470
2471 do
2472 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002473 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002474 return NULL;
2475 ++idx;
2476 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002477 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002478 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2479
2480 *qf_index = idx;
2481 return qf_ptr;
2482}
2483
2484/*
2485 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002486 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002487 */
2488 static qfline_T *
2489get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002490 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002491 qfline_T *qf_ptr,
2492 int *qf_index,
2493 int dir)
2494{
2495 int idx;
2496 int old_qf_fnum;
2497
2498 idx = *qf_index;
2499 old_qf_fnum = qf_ptr->qf_fnum;
2500
2501 do
2502 {
2503 if (idx == 1 || qf_ptr->qf_prev == NULL)
2504 return NULL;
2505 --idx;
2506 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002507 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002508 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2509
2510 *qf_index = idx;
2511 return qf_ptr;
2512}
2513
2514/*
2515 * Get the n'th (errornr) previous/next valid entry from the current entry in
2516 * the quickfix list.
2517 * dir == FORWARD or FORWARD_FILE: next valid entry
2518 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2519 */
2520 static qfline_T *
2521get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002522 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002523 int errornr,
2524 qfline_T *qf_ptr,
2525 int *qf_index,
2526 int dir)
2527{
2528 qfline_T *prev_qf_ptr;
2529 int prev_index;
2530 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2531 char_u *err = e_no_more_items;
2532
2533 while (errornr--)
2534 {
2535 prev_qf_ptr = qf_ptr;
2536 prev_index = *qf_index;
2537
2538 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002539 qf_ptr = get_next_valid_entry(qfl, qf_ptr, qf_index, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002540 else
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002541 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, qf_index, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002542 if (qf_ptr == NULL)
2543 {
2544 qf_ptr = prev_qf_ptr;
2545 *qf_index = prev_index;
2546 if (err != NULL)
2547 {
2548 EMSG(_(err));
2549 return NULL;
2550 }
2551 break;
2552 }
2553
2554 err = NULL;
2555 }
2556
2557 return qf_ptr;
2558}
2559
2560/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002561 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002562 */
2563 static qfline_T *
2564get_nth_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002565 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002566 int errornr,
2567 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002568 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002569{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002570 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002571
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002572 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002573 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2574 {
2575 --qf_idx;
2576 qf_ptr = qf_ptr->qf_prev;
2577 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002578 /* New error number is greater than the current error number */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002579 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2580 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002581 {
2582 ++qf_idx;
2583 qf_ptr = qf_ptr->qf_next;
2584 }
2585
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002586 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002587 return qf_ptr;
2588}
2589
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002590/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002591 * Find a window displaying a Vim help file.
2592 */
2593 static win_T *
2594qf_find_help_win(void)
2595{
2596 win_T *wp;
2597
2598 FOR_ALL_WINDOWS(wp)
2599 if (bt_help(wp->w_buffer))
2600 return wp;
2601
2602 return NULL;
2603}
2604
2605/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002606 * Find a help window or open one.
2607 */
2608 static int
2609jump_to_help_window(qf_info_T *qi, int *opened_window)
2610{
2611 win_T *wp;
2612 int flags;
2613
2614 if (cmdmod.tab != 0)
2615 wp = NULL;
2616 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002617 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002618 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2619 win_enter(wp, TRUE);
2620 else
2621 {
2622 /*
2623 * Split off help window; put it at far top if no position
2624 * specified, the current window is vertically split and narrow.
2625 */
2626 flags = WSP_HELP;
2627 if (cmdmod.split == 0 && curwin->w_width != Columns
2628 && curwin->w_width < 80)
2629 flags |= WSP_TOP;
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002630 if (IS_LL_STACK(qi))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002631 flags |= WSP_NEWLOC; /* don't copy the location list */
2632
2633 if (win_split(0, flags) == FAIL)
2634 return FAIL;
2635
2636 *opened_window = TRUE;
2637
2638 if (curwin->w_height < p_hh)
2639 win_setheight((int)p_hh);
2640
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002641 if (IS_LL_STACK(qi)) // not a quickfix list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002642 {
2643 /* The new window should use the supplied location list */
2644 curwin->w_llist = qi;
2645 qi->qf_refcount++;
2646 }
2647 }
2648
2649 if (!p_im)
2650 restart_edit = 0; /* don't want insert mode in help file */
2651
2652 return OK;
2653}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002654
2655/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002656 * Find a non-quickfix window using the given location list.
2657 * Returns NULL if a matching window is not found.
2658 */
2659 static win_T *
2660qf_find_win_with_loclist(qf_info_T *ll)
2661{
2662 win_T *wp;
2663
2664 FOR_ALL_WINDOWS(wp)
2665 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2666 return wp;
2667
2668 return NULL;
2669}
2670
2671/*
2672 * Find a window containing a normal buffer
2673 */
2674 static win_T *
2675qf_find_win_with_normal_buf(void)
2676{
2677 win_T *wp;
2678
2679 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002680 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002681 return wp;
2682
2683 return NULL;
2684}
2685
2686/*
2687 * Go to a window in any tabpage containing the specified file. Returns TRUE
2688 * if successfully jumped to the window. Otherwise returns FALSE.
2689 */
2690 static int
2691qf_goto_tabwin_with_file(int fnum)
2692{
2693 tabpage_T *tp;
2694 win_T *wp;
2695
2696 FOR_ALL_TAB_WINDOWS(tp, wp)
2697 if (wp->w_buffer->b_fnum == fnum)
2698 {
2699 goto_tabpage_win(tp, wp);
2700 return TRUE;
2701 }
2702
2703 return FALSE;
2704}
2705
2706/*
2707 * Create a new window to show a file above the quickfix window. Called when
2708 * only the quickfix window is present.
2709 */
2710 static int
2711qf_open_new_file_win(qf_info_T *ll_ref)
2712{
2713 int flags;
2714
2715 flags = WSP_ABOVE;
2716 if (ll_ref != NULL)
2717 flags |= WSP_NEWLOC;
2718 if (win_split(0, flags) == FAIL)
2719 return FAIL; /* not enough room for window */
2720 p_swb = empty_option; /* don't split again */
2721 swb_flags = 0;
2722 RESET_BINDING(curwin);
2723 if (ll_ref != NULL)
2724 {
2725 /* The new window should use the location list from the
2726 * location list window */
2727 curwin->w_llist = ll_ref;
2728 ll_ref->qf_refcount++;
2729 }
2730 return OK;
2731}
2732
2733/*
2734 * Go to a window that shows the right buffer. If the window is not found, go
2735 * to the window just above the location list window. This is used for opening
2736 * a file from a location window and not from a quickfix window. If some usable
2737 * window is previously found, then it is supplied in 'use_win'.
2738 */
2739 static void
2740qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2741{
2742 win_T *win = use_win;
2743
2744 if (win == NULL)
2745 {
2746 /* Find the window showing the selected file */
2747 FOR_ALL_WINDOWS(win)
2748 if (win->w_buffer->b_fnum == qf_fnum)
2749 break;
2750 if (win == NULL)
2751 {
2752 /* Find a previous usable window */
2753 win = curwin;
2754 do
2755 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002756 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002757 break;
2758 if (win->w_prev == NULL)
2759 win = lastwin; /* wrap around the top */
2760 else
2761 win = win->w_prev; /* go to previous window */
2762 } while (win != curwin);
2763 }
2764 }
2765 win_goto(win);
2766
2767 /* If the location list for the window is not set, then set it
2768 * to the location list from the location window */
2769 if (win->w_llist == NULL)
2770 {
2771 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002772 if (ll_ref != NULL)
2773 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002774 }
2775}
2776
2777/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002778 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2779 * not found, then go to the window just above the quickfix window. This is
2780 * used for opening a file from a quickfix window and not from a location
2781 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002782 */
2783 static void
2784qf_goto_win_with_qfl_file(int qf_fnum)
2785{
2786 win_T *win;
2787 win_T *altwin;
2788
2789 win = curwin;
2790 altwin = NULL;
2791 for (;;)
2792 {
2793 if (win->w_buffer->b_fnum == qf_fnum)
2794 break;
2795 if (win->w_prev == NULL)
2796 win = lastwin; /* wrap around the top */
2797 else
2798 win = win->w_prev; /* go to previous window */
2799
2800 if (IS_QF_WINDOW(win))
2801 {
2802 /* Didn't find it, go to the window before the quickfix
2803 * window. */
2804 if (altwin != NULL)
2805 win = altwin;
2806 else if (curwin->w_prev != NULL)
2807 win = curwin->w_prev;
2808 else
2809 win = curwin->w_next;
2810 break;
2811 }
2812
2813 /* Remember a usable window. */
Bram Moolenaar91335e52018-08-01 17:53:12 +02002814 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002815 altwin = win;
2816 }
2817
2818 win_goto(win);
2819}
2820
2821/*
2822 * Find a suitable window for opening a file (qf_fnum) from the
2823 * quickfix/location list and jump to it. If the file is already opened in a
2824 * window, jump to it. Otherwise open a new window to display the file. This is
2825 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002826 */
2827 static int
2828qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2829{
2830 win_T *usable_win_ptr = NULL;
2831 int usable_win;
2832 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002833 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002834
2835 usable_win = 0;
2836
2837 ll_ref = curwin->w_llist_ref;
2838 if (ll_ref != NULL)
2839 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002840 /* Find a non-quickfix window with this location list */
2841 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2842 if (usable_win_ptr != NULL)
2843 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002844 }
2845
2846 if (!usable_win)
2847 {
2848 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002849 win = qf_find_win_with_normal_buf();
2850 if (win != NULL)
2851 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002852 }
2853
2854 /*
2855 * If no usable window is found and 'switchbuf' contains "usetab"
2856 * then search in other tabs.
2857 */
2858 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002859 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002860
2861 /*
2862 * If there is only one window and it is the quickfix window, create a
2863 * new one above the quickfix window.
2864 */
2865 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2866 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002867 if (qf_open_new_file_win(ll_ref) != OK)
2868 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002869 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002870 }
2871 else
2872 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002873 if (curwin->w_llist_ref != NULL) /* In a location window */
2874 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2875 else /* In a quickfix window */
2876 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002877 }
2878
2879 return OK;
2880}
2881
2882/*
2883 * Edit the selected file or help file.
2884 */
2885 static int
2886qf_jump_edit_buffer(
2887 qf_info_T *qi,
2888 qfline_T *qf_ptr,
2889 int forceit,
2890 win_T *oldwin,
2891 int *opened_window,
2892 int *abort)
2893{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002894 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002895 int retval = OK;
2896
2897 if (qf_ptr->qf_type == 1)
2898 {
2899 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2900 * set b_p_ro flag). */
2901 if (!can_abandon(curbuf, forceit))
2902 {
2903 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002904 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002905 }
2906 else
2907 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2908 ECMD_HIDE + ECMD_SET_HELP,
2909 oldwin == curwin ? curwin : NULL);
2910 }
2911 else
2912 {
2913 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002914 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002915
2916 retval = buflist_getfile(qf_ptr->qf_fnum,
2917 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002918
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002919 if (IS_LL_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002920 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002921 /*
2922 * Location list. Check whether the associated window is still
2923 * present and the list is still valid.
2924 */
2925 if (!win_valid_any_tab(oldwin))
2926 {
2927 EMSG(_("E924: Current window was closed"));
2928 *abort = TRUE;
2929 *opened_window = FALSE;
2930 }
2931 else if (!qflist_valid(oldwin, save_qfid))
2932 {
2933 EMSG(_(e_loc_list_changed));
2934 *abort = TRUE;
2935 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002936 }
2937 else if (old_qf_curlist != qi->qf_curlist
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002938 || !is_qf_entry_present(qfl, qf_ptr))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002939 {
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002940 if (IS_QF_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002941 EMSG(_("E925: Current quickfix was changed"));
2942 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002943 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002944 *abort = TRUE;
2945 }
2946
2947 if (*abort)
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002948 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002949 }
2950
2951 return retval;
2952}
2953
2954/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002955 * Go to the error line in the current file using either line/column number or
2956 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002957 */
2958 static void
2959qf_jump_goto_line(
2960 linenr_T qf_lnum,
2961 int qf_col,
2962 char_u qf_viscol,
2963 char_u *qf_pattern)
2964{
2965 linenr_T i;
2966 char_u *line;
2967 colnr_T screen_col;
2968 colnr_T char_col;
2969
2970 if (qf_pattern == NULL)
2971 {
2972 /*
2973 * Go to line with error, unless qf_lnum is 0.
2974 */
2975 i = qf_lnum;
2976 if (i > 0)
2977 {
2978 if (i > curbuf->b_ml.ml_line_count)
2979 i = curbuf->b_ml.ml_line_count;
2980 curwin->w_cursor.lnum = i;
2981 }
2982 if (qf_col > 0)
2983 {
2984 curwin->w_cursor.col = qf_col - 1;
2985#ifdef FEAT_VIRTUALEDIT
2986 curwin->w_cursor.coladd = 0;
2987#endif
2988 if (qf_viscol == TRUE)
2989 {
2990 /*
2991 * Check each character from the beginning of the error
2992 * line up to the error column. For each tab character
2993 * found, reduce the error column value by the length of
2994 * a tab character.
2995 */
2996 line = ml_get_curline();
2997 screen_col = 0;
2998 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2999 {
3000 if (*line == NUL)
3001 break;
3002 if (*line++ == '\t')
3003 {
3004 curwin->w_cursor.col -= 7 - (screen_col % 8);
3005 screen_col += 8 - (screen_col % 8);
3006 }
3007 else
3008 ++screen_col;
3009 }
3010 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003011 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003012 check_cursor();
3013 }
3014 else
3015 beginline(BL_WHITE | BL_FIX);
3016 }
3017 else
3018 {
3019 pos_T save_cursor;
3020
3021 /* Move the cursor to the first line in the buffer */
3022 save_cursor = curwin->w_cursor;
3023 curwin->w_cursor.lnum = 0;
3024 if (!do_search(NULL, '/', qf_pattern, (long)1,
3025 SEARCH_KEEP, NULL, NULL))
3026 curwin->w_cursor = save_cursor;
3027 }
3028}
3029
3030/*
3031 * Display quickfix list index and size message
3032 */
3033 static void
3034qf_jump_print_msg(
3035 qf_info_T *qi,
3036 int qf_index,
3037 qfline_T *qf_ptr,
3038 buf_T *old_curbuf,
3039 linenr_T old_lnum)
3040{
3041 linenr_T i;
3042 int len;
3043
3044 /* Update the screen before showing the message, unless the screen
3045 * scrolled up. */
3046 if (!msg_scrolled)
3047 update_topline_redraw();
3048 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3049 qi->qf_lists[qi->qf_curlist].qf_count,
3050 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3051 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
3052 /* Add the message, skipping leading whitespace and newlines. */
3053 len = (int)STRLEN(IObuff);
3054 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3055
3056 /* Output the message. Overwrite to avoid scrolling when the 'O'
3057 * flag is present in 'shortmess'; But when not jumping, print the
3058 * whole message. */
3059 i = msg_scroll;
3060 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3061 msg_scroll = TRUE;
3062 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3063 msg_scroll = FALSE;
3064 msg_attr_keep(IObuff, 0, TRUE);
3065 msg_scroll = i;
3066}
3067
3068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 * jump to a quickfix line
3070 * if dir == FORWARD go "errornr" valid entries forward
3071 * if dir == BACKWARD go "errornr" valid entries backward
3072 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3073 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3074 * else if "errornr" is zero, redisplay the same line
3075 * else go to entry "errornr"
3076 */
3077 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003078qf_jump(qf_info_T *qi,
3079 int dir,
3080 int errornr,
3081 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003083 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003084 qfline_T *qf_ptr;
3085 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 buf_T *old_curbuf;
3089 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003090 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003091 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003093 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#ifdef FEAT_FOLDING
3096 int old_KeyTyped = KeyTyped; /* getting file may reset it */
3097#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003098 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003100 if (qi == NULL)
3101 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003102
3103 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003104 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
3106 EMSG(_(e_quickfix));
3107 return;
3108 }
3109
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003110 qfl = &qi->qf_lists[qi->qf_curlist];
3111
3112 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003114 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02003116 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003118 qf_ptr = get_nth_valid_entry(qfl, errornr, qf_ptr, &qf_index, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003119 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003121 qf_ptr = old_qf_ptr;
3122 qf_index = old_qf_index;
3123 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 }
3125 }
3126 else if (errornr != 0) /* go to specified number */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003127 qf_ptr = get_nth_entry(qfl, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003129 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003130 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 /* No need to print the error message if it's visible in the error
3132 * window */
3133 print_message = FALSE;
3134
3135 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003136 * For ":helpgrep" find a help window or open one.
3137 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003138 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003139 if (jump_to_help_window(qi, &opened_window) == FAIL)
3140 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003141
3142 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 * If currently in the quickfix window, find another window to show the
3144 * file in.
3145 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003146 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 /*
3149 * If there is no file specified, we don't know where to go.
3150 * But do advance, otherwise ":cn" gets stuck.
3151 */
3152 if (qf_ptr->qf_fnum == 0)
3153 goto theend;
3154
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003155 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
3156 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
3159 /*
3160 * If there is a file name,
3161 * read the wanted file if needed, and check autowrite etc.
3162 */
3163 old_curbuf = curbuf;
3164 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003165
3166 if (qf_ptr->qf_fnum != 0)
3167 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003168 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003169
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003170 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3171 &opened_window, &abort);
3172 if (abort)
3173 {
3174 qi = NULL;
3175 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003176 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003177 }
3178
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003179 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 {
3181 /* When not switched to another buffer, still need to set pc mark */
3182 if (curbuf == old_curbuf)
3183 setpcmark();
3184
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003185 if (qf_ptr != NULL)
3186 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col,
3187 qf_ptr->qf_viscol, qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189#ifdef FEAT_FOLDING
3190 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
3191 foldOpenCursor();
3192#endif
3193 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003194 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196 else
3197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 if (opened_window)
3199 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01003200 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
3202 /*
3203 * Couldn't open file, so put index back where it was. This could
3204 * happen if the file was readonly and we changed something.
3205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 qf_ptr = old_qf_ptr;
3208 qf_index = old_qf_index;
3209 }
3210 }
3211theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003212 if (qi != NULL)
3213 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003214 qfl->qf_ptr = qf_ptr;
3215 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 if (p_swb != old_swb && opened_window)
3218 {
3219 /* Restore old 'switchbuf' value, but not when an autocommand or
3220 * modeline has changed the value. */
3221 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003224 swb_flags = old_swb_flags;
3225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 else
3227 free_string_option(old_swb);
3228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229}
3230
3231/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003232 * Highlight attributes used for displaying entries from the quickfix list.
3233 */
3234static int qfFileAttr;
3235static int qfSepAttr;
3236static int qfLineAttr;
3237
3238/*
3239 * Display information about a single entry from the quickfix/location list.
3240 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003241 * 'cursel' will be set to TRUE for the currently selected entry in the
3242 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003243 */
3244 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003245qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003246{
3247 char_u *fname;
3248 buf_T *buf;
3249 int filter_entry;
3250
3251 fname = NULL;
3252 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3253 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3254 (char *)qfp->qf_module);
3255 else {
3256 if (qfp->qf_fnum != 0
3257 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3258 {
3259 fname = buf->b_fname;
3260 if (qfp->qf_type == 1) /* :helpgrep */
3261 fname = gettail(fname);
3262 }
3263 if (fname == NULL)
3264 sprintf((char *)IObuff, "%2d", qf_idx);
3265 else
3266 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3267 qf_idx, (char *)fname);
3268 }
3269
3270 // Support for filtering entries using :filter /pat/ clist
3271 // Match against the module name, file name, search pattern and
3272 // text of the entry.
3273 filter_entry = TRUE;
3274 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3275 filter_entry &= message_filtered(qfp->qf_module);
3276 if (filter_entry && fname != NULL)
3277 filter_entry &= message_filtered(fname);
3278 if (filter_entry && qfp->qf_pattern != NULL)
3279 filter_entry &= message_filtered(qfp->qf_pattern);
3280 if (filter_entry)
3281 filter_entry &= message_filtered(qfp->qf_text);
3282 if (filter_entry)
3283 return;
3284
3285 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003286 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003287
3288 if (qfp->qf_lnum != 0)
3289 msg_puts_attr((char_u *)":", qfSepAttr);
3290 if (qfp->qf_lnum == 0)
3291 IObuff[0] = NUL;
3292 else if (qfp->qf_col == 0)
3293 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3294 else
3295 sprintf((char *)IObuff, "%ld col %d",
3296 qfp->qf_lnum, qfp->qf_col);
3297 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3298 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3299 msg_puts_attr(IObuff, qfLineAttr);
3300 msg_puts_attr((char_u *)":", qfSepAttr);
3301 if (qfp->qf_pattern != NULL)
3302 {
3303 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3304 msg_puts(IObuff);
3305 msg_puts_attr((char_u *)":", qfSepAttr);
3306 }
3307 msg_puts((char_u *)" ");
3308
3309 /* Remove newlines and leading whitespace from the text. For an
3310 * unrecognized line keep the indent, the compiler may mark a word
3311 * with ^^^^. */
3312 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3313 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3314 IObuff, IOSIZE);
3315 msg_prt_line(IObuff, FALSE);
3316 out_flush(); /* show one line at a time */
3317}
3318
3319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003321 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 */
3323 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003324qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003326 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003327 qfline_T *qfp;
3328 int i;
3329 int idx1 = 1;
3330 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003331 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003332 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003333 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003335 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
Bram Moolenaar39665952018-08-15 20:59:48 +02003337 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003338 {
3339 qi = GET_LOC_LIST(curwin);
3340 if (qi == NULL)
3341 {
3342 EMSG(_(e_loclist));
3343 return;
3344 }
3345 }
3346
3347 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003348 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
3350 EMSG(_(e_quickfix));
3351 return;
3352 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003353 if (*arg == '+')
3354 {
3355 ++arg;
3356 plus = TRUE;
3357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3359 {
3360 EMSG(_(e_trailing));
3361 return;
3362 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003363 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003364 if (plus)
3365 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003366 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003367 idx2 = i + idx1;
3368 idx1 = i;
3369 }
3370 else
3371 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003372 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003373 if (idx1 < 0)
3374 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3375 if (idx2 < 0)
3376 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
Bram Moolenaara796d462018-05-01 14:30:36 +02003379 /* Shorten all the file names, so that it is easy to read */
3380 shorten_fnames(FALSE);
3381
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003382 /*
3383 * Get the attributes for the different quickfix highlight items. Note
3384 * that this depends on syntax items defined in the qf.vim syntax file
3385 */
3386 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3387 if (qfFileAttr == 0)
3388 qfFileAttr = HL_ATTR(HLF_D);
3389 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3390 if (qfSepAttr == 0)
3391 qfSepAttr = HL_ATTR(HLF_D);
3392 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3393 if (qfLineAttr == 0)
3394 qfLineAttr = HL_ATTR(HLF_N);
3395
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003396 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003398 qfp = qfl->qf_start;
3399 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
3401 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3402 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003403 if (got_int)
3404 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003405
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003406 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003408
3409 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003410 if (qfp == NULL)
3411 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003412 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 ui_breakcheck();
3414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415}
3416
3417/*
3418 * Remove newlines and leading whitespace from an error message.
3419 * Put the result in "buf[bufsize]".
3420 */
3421 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003422qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
3424 int i;
3425 char_u *p = text;
3426
3427 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3428 {
3429 if (*p == '\n')
3430 {
3431 buf[i] = ' ';
3432 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003433 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 break;
3435 }
3436 else
3437 buf[i] = *p++;
3438 }
3439 buf[i] = NUL;
3440}
3441
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003442/*
3443 * Display information (list number, list size and the title) about a
3444 * quickfix/location list.
3445 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003446 static void
3447qf_msg(qf_info_T *qi, int which, char *lead)
3448{
3449 char *title = (char *)qi->qf_lists[which].qf_title;
3450 int count = qi->qf_lists[which].qf_count;
3451 char_u buf[IOSIZE];
3452
3453 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3454 lead,
3455 which + 1,
3456 qi->qf_listcount,
3457 count);
3458
3459 if (title != NULL)
3460 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003461 size_t len = STRLEN(buf);
3462
3463 if (len < 34)
3464 {
3465 vim_memset(buf + len, ' ', 34 - len);
3466 buf[34] = NUL;
3467 }
3468 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003469 }
3470 trunc_string(buf, buf, Columns - 1, IOSIZE);
3471 msg(buf);
3472}
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474/*
3475 * ":colder [count]": Up in the quickfix stack.
3476 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003477 * ":lolder [count]": Up in the location list stack.
3478 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 */
3480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003481qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003483 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 int count;
3485
Bram Moolenaar39665952018-08-15 20:59:48 +02003486 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003487 {
3488 qi = GET_LOC_LIST(curwin);
3489 if (qi == NULL)
3490 {
3491 EMSG(_(e_loclist));
3492 return;
3493 }
3494 }
3495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (eap->addr_count != 0)
3497 count = eap->line2;
3498 else
3499 count = 1;
3500 while (count--)
3501 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003502 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003504 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003507 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003509 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511 else
3512 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003513 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
3515 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003516 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003518 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
3520 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003521 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003522 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523}
3524
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003525/*
3526 * Display the information about all the quickfix/location lists in the stack
3527 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003528 void
3529qf_history(exarg_T *eap)
3530{
3531 qf_info_T *qi = &ql_info;
3532 int i;
3533
Bram Moolenaar39665952018-08-15 20:59:48 +02003534 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003535 qi = GET_LOC_LIST(curwin);
3536 if (qi == NULL || (qi->qf_listcount == 0
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003537 && qf_list_empty(qi, qi->qf_curlist)))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003538 MSG(_("No entries"));
3539 else
3540 for (i = 0; i < qi->qf_listcount; ++i)
3541 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3542}
3543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003545 * Free all the entries in the error list "idx". Note that other information
3546 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 */
3548 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003549qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003551 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003552 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003553 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003555 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003557 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003558 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003559 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003560 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003561 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003562 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003563 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003564 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003565 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003566 if (stop)
3567 /* Somehow qf_count may have an incorrect value, set it to 1
3568 * to avoid crashing when it's wrong.
3569 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003570 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003571 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003572 qfl->qf_start = qfpnext;
3573 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003575
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003576 qfl->qf_index = 0;
3577 qfl->qf_start = NULL;
3578 qfl->qf_last = NULL;
3579 qfl->qf_ptr = NULL;
3580 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003581
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003582 qf_clean_dir_stack(&qfl->qf_dir_stack);
3583 qfl->qf_directory = NULL;
3584 qf_clean_dir_stack(&qfl->qf_file_stack);
3585 qfl->qf_currfile = NULL;
3586 qfl->qf_multiline = FALSE;
3587 qfl->qf_multiignore = FALSE;
3588 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589}
3590
3591/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003592 * Free error list "idx". Frees all the entries in the quickfix list,
3593 * associated context information and the title.
3594 */
3595 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003596qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003597{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003598 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003599
Bram Moolenaard23a8232018-02-10 18:45:26 +01003600 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003601 free_tv(qfl->qf_ctx);
3602 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003603 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003604 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003605}
3606
3607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 * qf_mark_adjust: adjust marks
3609 */
3610 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003611qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003612 win_T *wp,
3613 linenr_T line1,
3614 linenr_T line2,
3615 long amount,
3616 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003618 int i;
3619 qfline_T *qfp;
3620 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003621 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003622 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003623 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
Bram Moolenaarc1542742016-07-20 21:44:37 +02003625 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003626 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003627 if (wp != NULL)
3628 {
3629 if (wp->w_llist == NULL)
3630 return;
3631 qi = wp->w_llist;
3632 }
3633
3634 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003635 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003636 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003637 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3638 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 if (qfp->qf_fnum == curbuf->b_fnum)
3640 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003641 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3643 {
3644 if (amount == MAXLNUM)
3645 qfp->qf_cleared = TRUE;
3646 else
3647 qfp->qf_lnum += amount;
3648 }
3649 else if (amount_after && qfp->qf_lnum > line2)
3650 qfp->qf_lnum += amount_after;
3651 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003652
3653 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003654 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655}
3656
3657/*
3658 * Make a nice message out of the error character and the error number:
3659 * char number message
3660 * e or E 0 " error"
3661 * w or W 0 " warning"
3662 * i or I 0 " info"
3663 * 0 0 ""
3664 * other 0 " c"
3665 * e or E n " error n"
3666 * w or W n " warning n"
3667 * i or I n " info n"
3668 * 0 n " error n"
3669 * other n " c n"
3670 * 1 x "" :helpgrep
3671 */
3672 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003673qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
3675 static char_u buf[20];
3676 static char_u cc[3];
3677 char_u *p;
3678
3679 if (c == 'W' || c == 'w')
3680 p = (char_u *)" warning";
3681 else if (c == 'I' || c == 'i')
3682 p = (char_u *)" info";
3683 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3684 p = (char_u *)" error";
3685 else if (c == 0 || c == 1)
3686 p = (char_u *)"";
3687 else
3688 {
3689 cc[0] = ' ';
3690 cc[1] = c;
3691 cc[2] = NUL;
3692 p = cc;
3693 }
3694
3695 if (nr <= 0)
3696 return p;
3697
3698 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3699 return buf;
3700}
3701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003703 * When "split" is FALSE: Open the entry/result under the cursor.
3704 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3705 */
3706 void
3707qf_view_result(int split)
3708{
3709 qf_info_T *qi = &ql_info;
3710
3711 if (!bt_quickfix(curbuf))
3712 return;
3713
3714 if (IS_LL_WINDOW(curwin))
3715 qi = GET_LOC_LIST(curwin);
3716
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003717 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003718 {
3719 EMSG(_(e_quickfix));
3720 return;
3721 }
3722
3723 if (split)
3724 {
3725 char_u cmd[32];
3726
3727 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3728 (long)curwin->w_cursor.lnum,
3729 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3730 if (do_cmdline_cmd(cmd) == OK)
3731 do_cmdline_cmd((char_u *) "clearjumps");
3732 return;
3733 }
3734
3735 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3736}
3737
3738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 * ":cwindow": open the quickfix window if we have errors to display,
3740 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003741 * ":lwindow": open the location list window if we have locations to display,
3742 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 */
3744 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003745ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003747 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 win_T *win;
3749
Bram Moolenaar39665952018-08-15 20:59:48 +02003750 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003751 {
3752 qi = GET_LOC_LIST(curwin);
3753 if (qi == NULL)
3754 return;
3755 }
3756
3757 /* Look for an existing quickfix window. */
3758 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
3760 /*
3761 * If a quickfix window is open but we have no errors to display,
3762 * close the window. If a quickfix window is not open, then open
3763 * it if we have errors; otherwise, leave it closed.
3764 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003765 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003766 || qf_list_empty(qi, qi->qf_curlist)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003767 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
3769 if (win != NULL)
3770 ex_cclose(eap);
3771 }
3772 else if (win == NULL)
3773 ex_copen(eap);
3774}
3775
3776/*
3777 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003778 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003781ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003783 win_T *win = NULL;
3784 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
Bram Moolenaar39665952018-08-15 20:59:48 +02003786 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003787 {
3788 qi = GET_LOC_LIST(curwin);
3789 if (qi == NULL)
3790 return;
3791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003793 /* Find existing quickfix window and close it. */
3794 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (win != NULL)
3796 win_close(win, FALSE);
3797}
3798
3799/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003800 * Set "w:quickfix_title" if "qi" has a title.
3801 */
3802 static void
3803qf_set_title_var(qf_list_T *qfl)
3804{
3805 if (qfl->qf_title != NULL)
3806 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3807}
3808
3809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003811 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 */
3813 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003814ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003816 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003819 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003820 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003821 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
Bram Moolenaar39665952018-08-15 20:59:48 +02003823 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003824 {
3825 qi = GET_LOC_LIST(curwin);
3826 if (qi == NULL)
3827 {
3828 EMSG(_(e_loclist));
3829 return;
3830 }
3831 }
3832
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (eap->addr_count != 0)
3834 height = eap->line2;
3835 else
3836 height = QF_WINHEIGHT;
3837
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839#ifdef FEAT_GUI
3840 need_mouse_correct = TRUE;
3841#endif
3842
3843 /*
3844 * Find existing quickfix window, or open a new one.
3845 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003846 win = qf_find_win(qi);
3847
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003848 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003849 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003851 if (eap->addr_count != 0)
3852 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003853 if (cmdmod.split & WSP_VERT)
3854 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003855 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003856 win_setwidth(height);
3857 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003858 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003859 win_setheight(height);
3860 }
3861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 else
3863 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003864 int flags = 0;
3865
Bram Moolenaar9c102382006-05-03 21:26:49 +00003866 qf_buf = qf_find_buf(qi);
3867
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 /* The current window becomes the previous window afterwards. */
3869 win = curwin;
3870
Bram Moolenaar77642c02012-11-20 17:55:10 +01003871 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3872 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003873 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003874 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003875 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003876 /* Default is to open the window below the current window */
3877 if (cmdmod.split == 0)
3878 flags = WSP_BELOW;
3879 flags |= WSP_NEWLOC;
3880 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003882 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003884 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003886 /*
3887 * For the location list window, create a reference to the
3888 * location list from the window 'win'.
3889 */
3890 curwin->w_llist_ref = win->w_llist;
3891 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003893
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003894 if (oldwin != curwin)
3895 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003896 if (qf_buf != NULL)
3897 /* Use the existing quickfix buffer */
3898 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003899 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003900 else
3901 {
3902 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003903 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003904 /* switch off 'swapfile' */
3905 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3906 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003907 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003908 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003909 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003910#ifdef FEAT_DIFF
3911 curwin->w_p_diff = FALSE;
3912#endif
3913#ifdef FEAT_FOLDING
3914 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3915 OPT_LOCAL);
3916#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003919 /* Only set the height when still in the same tab page and there is no
3920 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003921 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 win_setheight(height);
3923 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3924 if (win_valid(win))
3925 prevwin = win;
3926 }
3927
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003928 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 /*
3931 * Fill the buffer with the quickfix list.
3932 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003933 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003935 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 curwin->w_cursor.col = 0;
3937 check_cursor();
3938 update_topline(); /* scroll to show the line */
3939}
3940
3941/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003942 * Move the cursor in the quickfix window to "lnum".
3943 */
3944 static void
3945qf_win_goto(win_T *win, linenr_T lnum)
3946{
3947 win_T *old_curwin = curwin;
3948
3949 curwin = win;
3950 curbuf = win->w_buffer;
3951 curwin->w_cursor.lnum = lnum;
3952 curwin->w_cursor.col = 0;
3953#ifdef FEAT_VIRTUALEDIT
3954 curwin->w_cursor.coladd = 0;
3955#endif
3956 curwin->w_curswant = 0;
3957 update_topline(); /* scroll to show the line */
3958 redraw_later(VALID);
3959 curwin->w_redr_status = TRUE; /* update ruler */
3960 curwin = old_curwin;
3961 curbuf = curwin->w_buffer;
3962}
3963
3964/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003965 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003966 */
3967 void
Bram Moolenaar39665952018-08-15 20:59:48 +02003968ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02003969{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003970 qf_info_T *qi = &ql_info;
3971 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003972
Bram Moolenaar39665952018-08-15 20:59:48 +02003973 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02003974 {
3975 qi = GET_LOC_LIST(curwin);
3976 if (qi == NULL)
3977 {
3978 EMSG(_(e_loclist));
3979 return;
3980 }
3981 }
3982
3983 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003984 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3985 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3986}
3987
3988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 * Return the number of the current entry (line number in the quickfix
3990 * window).
3991 */
3992 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003993qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003995 qf_info_T *qi = &ql_info;
3996
3997 if (IS_LL_WINDOW(wp))
3998 /* In the location list window, use the referenced location list */
3999 qi = wp->w_llist_ref;
4000
4001 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002}
4003
4004/*
4005 * Update the cursor position in the quickfix window to the current error.
4006 * Return TRUE if there is a quickfix window.
4007 */
4008 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004009qf_win_pos_update(
4010 qf_info_T *qi,
4011 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012{
4013 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004014 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
4016 /*
4017 * Put the cursor on the current error in the quickfix window, so that
4018 * it's viewable.
4019 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004020 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 if (win != NULL
4022 && qf_index <= win->w_buffer->b_ml.ml_line_count
4023 && old_qf_index != qf_index)
4024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 if (qf_index > old_qf_index)
4026 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004027 win->w_redraw_top = old_qf_index;
4028 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
4030 else
4031 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004032 win->w_redraw_top = qf_index;
4033 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004035 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 }
4037 return win != NULL;
4038}
4039
4040/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004041 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004042 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004043 */
4044 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004045is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004046{
4047 /*
4048 * A window displaying the quickfix buffer will have the w_llist_ref field
4049 * set to NULL.
4050 * A window displaying a location list buffer will have the w_llist_ref
4051 * pointing to the location list.
4052 */
4053 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004054 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4055 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004056 return TRUE;
4057
4058 return FALSE;
4059}
4060
4061/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004062 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004063 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004064 */
4065 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004066qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004067{
4068 win_T *win;
4069
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004070 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004071 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004072 return win;
4073 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004074}
4075
4076/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004077 * Find a quickfix buffer.
4078 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 */
4080 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004081qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004083 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004084 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
Bram Moolenaar9c102382006-05-03 21:26:49 +00004086 FOR_ALL_TAB_WINDOWS(tp, win)
4087 if (is_qf_win(win, qi))
4088 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004089
Bram Moolenaar9c102382006-05-03 21:26:49 +00004090 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091}
4092
4093/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004094 * Update the w:quickfix_title variable in the quickfix/location list window
4095 */
4096 static void
4097qf_update_win_titlevar(qf_info_T *qi)
4098{
4099 win_T *win;
4100 win_T *curwin_save;
4101
4102 if ((win = qf_find_win(qi)) != NULL)
4103 {
4104 curwin_save = curwin;
4105 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004106 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004107 curwin = curwin_save;
4108 }
4109}
4110
4111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 * Find the quickfix buffer. If it exists, update the contents.
4113 */
4114 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004115qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116{
4117 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004118 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
4121 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004122 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (buf != NULL)
4124 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004125 linenr_T old_line_count = buf->b_ml.ml_line_count;
4126
4127 if (old_last == NULL)
4128 /* set curwin/curbuf to buf and save a few things */
4129 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
Bram Moolenaard823fa92016-08-12 16:29:27 +02004131 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004132
Bram Moolenaar864293a2016-06-02 13:40:04 +02004133 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004134 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004135
Bram Moolenaar864293a2016-06-02 13:40:04 +02004136 if (old_last == NULL)
4137 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004138 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004139
4140 /* restore curwin/curbuf and a few other things */
4141 aucmd_restbuf(&aco);
4142 }
4143
4144 /* Only redraw when added lines are visible. This avoids flickering
4145 * when the added lines are not visible. */
4146 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4147 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
4149}
4150
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004151/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004152 * Add an error line to the quickfix buffer.
4153 */
4154 static int
4155qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4156{
4157 int len;
4158 buf_T *errbuf;
4159
4160 if (qfp->qf_module != NULL)
4161 {
4162 STRCPY(IObuff, qfp->qf_module);
4163 len = (int)STRLEN(IObuff);
4164 }
4165 else if (qfp->qf_fnum != 0
4166 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4167 && errbuf->b_fname != NULL)
4168 {
4169 if (qfp->qf_type == 1) /* :helpgrep */
4170 STRCPY(IObuff, gettail(errbuf->b_fname));
4171 else
4172 {
4173 /* shorten the file name if not done already */
4174 if (errbuf->b_sfname == NULL
4175 || mch_isFullName(errbuf->b_sfname))
4176 {
4177 if (*dirname == NUL)
4178 mch_dirname(dirname, MAXPATHL);
4179 shorten_buf_fname(errbuf, dirname, FALSE);
4180 }
4181 STRCPY(IObuff, errbuf->b_fname);
4182 }
4183 len = (int)STRLEN(IObuff);
4184 }
4185 else
4186 len = 0;
4187 IObuff[len++] = '|';
4188
4189 if (qfp->qf_lnum > 0)
4190 {
4191 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4192 len += (int)STRLEN(IObuff + len);
4193
4194 if (qfp->qf_col > 0)
4195 {
4196 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4197 len += (int)STRLEN(IObuff + len);
4198 }
4199
4200 sprintf((char *)IObuff + len, "%s",
4201 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4202 len += (int)STRLEN(IObuff + len);
4203 }
4204 else if (qfp->qf_pattern != NULL)
4205 {
4206 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4207 len += (int)STRLEN(IObuff + len);
4208 }
4209 IObuff[len++] = '|';
4210 IObuff[len++] = ' ';
4211
4212 /* Remove newlines and leading whitespace from the text.
4213 * For an unrecognized line keep the indent, the compiler may
4214 * mark a word with ^^^^. */
4215 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4216 IObuff + len, IOSIZE - len);
4217
4218 if (ml_append_buf(buf, lnum, IObuff,
4219 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4220 return FAIL;
4221
4222 return OK;
4223}
4224
4225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 * Fill current buffer with quickfix errors, replacing any previous contents.
4227 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004228 * If "old_last" is not NULL append the items after this one.
4229 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4230 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 */
4232 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004233qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004235 linenr_T lnum;
4236 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004237 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaar864293a2016-06-02 13:40:04 +02004239 if (old_last == NULL)
4240 {
4241 if (buf != curbuf)
4242 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004243 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004244 return;
4245 }
4246
4247 /* delete all existing lines */
4248 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4249 (void)ml_delete((linenr_T)1, FALSE);
4250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004253 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
Bram Moolenaara796d462018-05-01 14:30:36 +02004255 char_u dirname[MAXPATHL];
4256
4257 *dirname = NUL;
4258
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004260 if (old_last == NULL)
4261 {
4262 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4263 lnum = 0;
4264 }
4265 else
4266 {
4267 qfp = old_last->qf_next;
4268 lnum = buf->b_ml.ml_line_count;
4269 }
4270 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004272 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004274
Bram Moolenaar864293a2016-06-02 13:40:04 +02004275 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004277 if (qfp == NULL)
4278 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004280
4281 if (old_last == NULL)
4282 /* Delete the empty line which is now at the end */
4283 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285
4286 /* correct cursor position */
4287 check_lnums(TRUE);
4288
Bram Moolenaar864293a2016-06-02 13:40:04 +02004289 if (old_last == NULL)
4290 {
4291 /* Set the 'filetype' to "qf" each time after filling the buffer.
4292 * This resembles reading a file into a buffer, it's more logical when
4293 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02004294 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004295 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4296 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297
Bram Moolenaar864293a2016-06-02 13:40:04 +02004298 keep_filetype = TRUE; /* don't detect 'filetype' */
4299 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004301 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004303 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004304 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004305
Bram Moolenaar864293a2016-06-02 13:40:04 +02004306 /* make sure it will be redrawn */
4307 redraw_curbuf_later(NOT_VALID);
4308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 /* Restore KeyTyped, setting 'filetype' may reset it. */
4311 KeyTyped = old_KeyTyped;
4312}
4313
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004314/*
4315 * For every change made to the quickfix list, update the changed tick.
4316 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004317 static void
4318qf_list_changed(qf_info_T *qi, int qf_idx)
4319{
4320 qi->qf_lists[qf_idx].qf_changedtick++;
4321}
4322
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004324 * Return the quickfix/location list number with the given identifier.
4325 * Returns -1 if list is not found.
4326 */
4327 static int
4328qf_id2nr(qf_info_T *qi, int_u qfid)
4329{
4330 int qf_idx;
4331
4332 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4333 if (qi->qf_lists[qf_idx].qf_id == qfid)
4334 return qf_idx;
4335 return INVALID_QFIDX;
4336}
4337
4338/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004339 * If the current list is not "save_qfid" and we can find the list with that ID
4340 * then make it the current list.
4341 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004342 * Returns OK if successfully restored the list. Returns FAIL if the list with
4343 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004344 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004345 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004346qf_restore_list(qf_info_T *qi, int_u save_qfid)
4347{
4348 int curlist;
4349
4350 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4351 {
4352 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004353 if (curlist < 0)
4354 // list is not present
4355 return FAIL;
4356 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004357 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004358 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004359}
4360
4361/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004362 * Jump to the first entry if there is one.
4363 */
4364 static void
4365qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4366{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004367 if (qf_restore_list(qi, save_qfid) == FAIL)
4368 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004369
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004370 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004371 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004372 qf_jump(qi, 0, 0, forceit);
4373}
4374
4375/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004376 * Return TRUE when using ":vimgrep" for ":grep".
4377 */
4378 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004379grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004380{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004381 return ((cmdidx == CMD_grep
4382 || cmdidx == CMD_lgrep
4383 || cmdidx == CMD_grepadd
4384 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004385 && STRCMP("internal",
4386 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4387}
4388
4389/*
Bram Moolenaara6557602006-02-04 22:43:20 +00004390 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 */
4392 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004393ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
Bram Moolenaar7c626922005-02-07 22:01:03 +00004395 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004397 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00004399 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004400 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004401 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004402 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004403 int_u save_qfid;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004404
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004405 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
4406 if (grep_internal(eap->cmdidx))
4407 {
4408 ex_vimgrep(eap);
4409 return;
4410 }
4411
Bram Moolenaar7c626922005-02-07 22:01:03 +00004412 switch (eap->cmdidx)
4413 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00004414 case CMD_make: au_name = (char_u *)"make"; break;
4415 case CMD_lmake: au_name = (char_u *)"lmake"; break;
4416 case CMD_grep: au_name = (char_u *)"grep"; break;
4417 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4418 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4419 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004420 default: break;
4421 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004422 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4423 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004424 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004425#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004426 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004427 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004428#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004429 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004430#ifdef FEAT_MBYTE
4431 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
Bram Moolenaar39665952018-08-15 20:59:48 +02004434 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00004435 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004436
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00004438 fname = get_mef_name();
4439 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004441 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
4443 /*
4444 * If 'shellpipe' empty: don't redirect to 'errorfile'.
4445 */
4446 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
4447 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00004448 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 cmd = alloc(len);
4450 if (cmd == NULL)
4451 return;
4452 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
4453 (char *)p_shq);
4454 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004455 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 /*
4457 * Output a newline if there's something else than the :make command that
4458 * was typed (in which case the cursor is in column 0).
4459 */
4460 if (msg_col == 0)
4461 msg_didout = FALSE;
4462 msg_start();
4463 MSG_PUTS(":!");
4464 msg_outtrans(cmd); /* show what we are doing */
4465
4466 /* let the shell know if we are redirecting output or not */
4467 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4468
4469#ifdef AMIGA
4470 out_flush();
4471 /* read window status report and redraw before message */
4472 (void)char_avail();
4473#endif
4474
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004475 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00004476 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4477 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004478 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004479 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004480 if (wp != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004481 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004482 qi = GET_LOC_LIST(wp);
4483 if (qi == NULL)
4484 goto cleanup;
4485 }
4486 if (res >= 0)
4487 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004488
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004489 // Remember the current quickfix list identifier, so that we can
4490 // check for autocommands changing the current quickfix list.
4491 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4492 if (au_name != NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004493 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4494 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004495 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004496 // display the first error
4497 qf_jump_first(qi, save_qfid, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004499cleanup:
Bram Moolenaar7c626922005-02-07 22:01:03 +00004500 mch_remove(fname);
4501 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 vim_free(cmd);
4503}
4504
4505/*
4506 * Return the name for the errorfile, in allocated memory.
4507 * Find a new unique name when 'makeef' contains "##".
4508 * Returns NULL for error.
4509 */
4510 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004511get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512{
4513 char_u *p;
4514 char_u *name;
4515 static int start = -1;
4516 static int off = 0;
4517#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004518 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519#endif
4520
4521 if (*p_mef == NUL)
4522 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004523 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 if (name == NULL)
4525 EMSG(_(e_notmp));
4526 return name;
4527 }
4528
4529 for (p = p_mef; *p; ++p)
4530 if (p[0] == '#' && p[1] == '#')
4531 break;
4532
4533 if (*p == NUL)
4534 return vim_strsave(p_mef);
4535
4536 /* Keep trying until the name doesn't exist yet. */
4537 for (;;)
4538 {
4539 if (start == -1)
4540 start = mch_get_pid();
4541 else
4542 off += 19;
4543
4544 name = alloc((unsigned)STRLEN(p_mef) + 30);
4545 if (name == NULL)
4546 break;
4547 STRCPY(name, p_mef);
4548 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4549 STRCAT(name, p + 2);
4550 if (mch_getperm(name) < 0
4551#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02004552 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 && mch_lstat((char *)name, &sb) < 0
4554#endif
4555 )
4556 break;
4557 vim_free(name);
4558 }
4559 return name;
4560}
4561
4562/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004563 * Returns the number of valid entries in the current quickfix/location list.
4564 */
4565 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004566qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004567{
4568 qf_info_T *qi = &ql_info;
4569 qfline_T *qfp;
4570 int i, sz = 0;
4571 int prev_fnum = 0;
4572
Bram Moolenaar39665952018-08-15 20:59:48 +02004573 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004574 {
4575 /* Location list */
4576 qi = GET_LOC_LIST(curwin);
4577 if (qi == NULL)
4578 return 0;
4579 }
4580
4581 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004582 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004583 ++i, qfp = qfp->qf_next)
4584 {
4585 if (qfp->qf_valid)
4586 {
4587 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4588 sz++; /* Count all valid entries */
4589 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4590 {
4591 /* Count the number of files */
4592 sz++;
4593 prev_fnum = qfp->qf_fnum;
4594 }
4595 }
4596 }
4597
4598 return sz;
4599}
4600
4601/*
4602 * Returns the current index of the quickfix/location list.
4603 * Returns 0 if there is an error.
4604 */
4605 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004606qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004607{
4608 qf_info_T *qi = &ql_info;
4609
Bram Moolenaar39665952018-08-15 20:59:48 +02004610 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004611 {
4612 /* Location list */
4613 qi = GET_LOC_LIST(curwin);
4614 if (qi == NULL)
4615 return 0;
4616 }
4617
4618 return qi->qf_lists[qi->qf_curlist].qf_index;
4619}
4620
4621/*
4622 * Returns the current index in the quickfix/location list (counting only valid
4623 * entries). If no valid entries are in the list, then returns 1.
4624 */
4625 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004626qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004627{
4628 qf_info_T *qi = &ql_info;
4629 qf_list_T *qfl;
4630 qfline_T *qfp;
4631 int i, eidx = 0;
4632 int prev_fnum = 0;
4633
Bram Moolenaar39665952018-08-15 20:59:48 +02004634 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004635 {
4636 /* Location list */
4637 qi = GET_LOC_LIST(curwin);
4638 if (qi == NULL)
4639 return 1;
4640 }
4641
4642 qfl = &qi->qf_lists[qi->qf_curlist];
4643 qfp = qfl->qf_start;
4644
4645 /* check if the list has valid errors */
4646 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4647 return 1;
4648
4649 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4650 {
4651 if (qfp->qf_valid)
4652 {
4653 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4654 {
4655 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4656 {
4657 /* Count the number of files */
4658 eidx++;
4659 prev_fnum = qfp->qf_fnum;
4660 }
4661 }
4662 else
4663 eidx++;
4664 }
4665 }
4666
4667 return eidx ? eidx : 1;
4668}
4669
4670/*
4671 * Get the 'n'th valid error entry in the quickfix or location list.
4672 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4673 * For :cdo and :ldo returns the 'n'th valid error entry.
4674 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4675 */
4676 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004677qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004678{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004679 qfline_T *qfp = qfl->qf_start;
4680 int i, eidx;
4681 int prev_fnum = 0;
4682
4683 /* check if the list has valid errors */
4684 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4685 return 1;
4686
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004687 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004688 i++, qfp = qfp->qf_next)
4689 {
4690 if (qfp->qf_valid)
4691 {
4692 if (fdo)
4693 {
4694 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4695 {
4696 /* Count the number of files */
4697 eidx++;
4698 prev_fnum = qfp->qf_fnum;
4699 }
4700 }
4701 else
4702 eidx++;
4703 }
4704
4705 if (eidx == n)
4706 break;
4707 }
4708
4709 if (i <= qfl->qf_count)
4710 return i;
4711 else
4712 return 1;
4713}
4714
4715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004717 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004718 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 */
4720 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004721ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004723 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004724 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004725
Bram Moolenaar39665952018-08-15 20:59:48 +02004726 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004727 {
4728 qi = GET_LOC_LIST(curwin);
4729 if (qi == NULL)
4730 {
4731 EMSG(_(e_loclist));
4732 return;
4733 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004734 }
4735
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004736 if (eap->addr_count > 0)
4737 errornr = (int)eap->line2;
4738 else
4739 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004740 switch (eap->cmdidx)
4741 {
4742 case CMD_cc: case CMD_ll:
4743 errornr = 0;
4744 break;
4745 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4746 case CMD_lfirst:
4747 errornr = 1;
4748 break;
4749 default:
4750 errornr = 32767;
4751 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004752 }
4753
4754 /* For cdo and ldo commands, jump to the nth valid error.
4755 * For cfdo and lfdo commands, jump to the nth valid file entry.
4756 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004757 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4758 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004759 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004760 eap->addr_count > 0 ? (int)eap->line1 : 1,
4761 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4762
4763 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764}
4765
4766/*
4767 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004768 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004769 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 */
4771 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004772ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004774 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004775 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004776 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004777
Bram Moolenaar39665952018-08-15 20:59:48 +02004778 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004779 {
4780 qi = GET_LOC_LIST(curwin);
4781 if (qi == NULL)
4782 {
4783 EMSG(_(e_loclist));
4784 return;
4785 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004786 }
4787
Bram Moolenaar55b69262017-08-13 13:42:01 +02004788 if (eap->addr_count > 0
4789 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4790 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004791 errornr = (int)eap->line2;
4792 else
4793 errornr = 1;
4794
Bram Moolenaar39665952018-08-15 20:59:48 +02004795 // Depending on the command jump to either next or previous entry/file.
4796 switch (eap->cmdidx)
4797 {
4798 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4799 dir = FORWARD;
4800 break;
4801 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4802 case CMD_lNext:
4803 dir = BACKWARD;
4804 break;
4805 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4806 dir = FORWARD_FILE;
4807 break;
4808 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4809 dir = BACKWARD_FILE;
4810 break;
4811 default:
4812 dir = FORWARD;
4813 break;
4814 }
4815
4816 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817}
4818
4819/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004820 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004821 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 */
4823 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004824ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004826 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004827 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004828 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004829 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004830 int_u save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004831 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004832
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004833 switch (eap->cmdidx)
4834 {
4835 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4836 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4837 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4838 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4839 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4840 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4841 default: break;
4842 }
4843 if (au_name != NULL)
4844 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004845#ifdef FEAT_MBYTE
4846 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4847#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004848#ifdef FEAT_BROWSE
4849 if (cmdmod.browse)
4850 {
4851 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004852 NULL, NULL,
4853 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004854 if (browse_file == NULL)
4855 return;
4856 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4857 vim_free(browse_file);
4858 }
4859 else
4860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004862 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004863
Bram Moolenaar39665952018-08-15 20:59:48 +02004864 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004865 wp = curwin;
4866
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004867 /*
4868 * This function is used by the :cfile, :cgetfile and :caddfile
4869 * commands.
4870 * :cfile always creates a new quickfix list and jumps to the
4871 * first error.
4872 * :cgetfile creates a new quickfix list but doesn't jump to the
4873 * first error.
4874 * :caddfile adds to an existing quickfix list. If there is no
4875 * quickfix list then a new list is created.
4876 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004877 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004878 && eap->cmdidx != CMD_laddfile),
4879 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004880 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004881 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01004882 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004883 if (qi == NULL)
4884 return;
4885 }
4886 if (res >= 0)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004887 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004888 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004889 if (au_name != NULL)
4890 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004891
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004892 // Jump to the first error for a new list and if autocmds didn't
4893 // free the list.
4894 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
4895 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004896 // display the first error
4897 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004898}
4899
4900/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004901 * Return the vimgrep autocmd name.
4902 */
4903 static char_u *
4904vgr_get_auname(cmdidx_T cmdidx)
4905{
4906 switch (cmdidx)
4907 {
4908 case CMD_vimgrep: return (char_u *)"vimgrep";
4909 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4910 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4911 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4912 case CMD_grep: return (char_u *)"grep";
4913 case CMD_lgrep: return (char_u *)"lgrep";
4914 case CMD_grepadd: return (char_u *)"grepadd";
4915 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4916 default: return NULL;
4917 }
4918}
4919
4920/*
4921 * Initialize the regmatch used by vimgrep for pattern "s".
4922 */
4923 static void
4924vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4925{
4926 /* Get the search pattern: either white-separated or enclosed in // */
4927 regmatch->regprog = NULL;
4928
4929 if (s == NULL || *s == NUL)
4930 {
4931 /* Pattern is empty, use last search pattern. */
4932 if (last_search_pat() == NULL)
4933 {
4934 EMSG(_(e_noprevre));
4935 return;
4936 }
4937 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4938 }
4939 else
4940 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4941
4942 regmatch->rmm_ic = p_ic;
4943 regmatch->rmm_maxcol = 0;
4944}
4945
4946/*
4947 * Display a file name when vimgrep is running.
4948 */
4949 static void
4950vgr_display_fname(char_u *fname)
4951{
4952 char_u *p;
4953
4954 msg_start();
4955 p = msg_strtrunc(fname, TRUE);
4956 if (p == NULL)
4957 msg_outtrans(fname);
4958 else
4959 {
4960 msg_outtrans(p);
4961 vim_free(p);
4962 }
4963 msg_clr_eos();
4964 msg_didout = FALSE; /* overwrite this message */
4965 msg_nowait = TRUE; /* don't wait for this message */
4966 msg_col = 0;
4967 out_flush();
4968}
4969
4970/*
4971 * Load a dummy buffer to search for a pattern using vimgrep.
4972 */
4973 static buf_T *
4974vgr_load_dummy_buf(
4975 char_u *fname,
4976 char_u *dirname_start,
4977 char_u *dirname_now)
4978{
4979 int save_mls;
4980#if defined(FEAT_SYN_HL)
4981 char_u *save_ei = NULL;
4982#endif
4983 buf_T *buf;
4984
4985#if defined(FEAT_SYN_HL)
4986 /* Don't do Filetype autocommands to avoid loading syntax and
4987 * indent scripts, a great speed improvement. */
4988 save_ei = au_event_disable(",Filetype");
4989#endif
4990 /* Don't use modelines here, it's useless. */
4991 save_mls = p_mls;
4992 p_mls = 0;
4993
4994 /* Load file into a buffer, so that 'fileencoding' is detected,
4995 * autocommands applied, etc. */
4996 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4997
4998 p_mls = save_mls;
4999#if defined(FEAT_SYN_HL)
5000 au_event_restore(save_ei);
5001#endif
5002
5003 return buf;
5004}
5005
5006/*
5007 * Check whether a quickfix/location list valid. Autocmds may remove or change
5008 * a quickfix list when vimgrep is running. If the list is not found, create a
5009 * new list.
5010 */
5011 static int
5012vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005013 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005014 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005015 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005016 char_u *title)
5017{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005018 /* Verify that the quickfix/location list was not freed by an autocmd */
5019 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005020 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005021 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005022 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005023 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005024 EMSG(_(e_loc_list_changed));
5025 return FALSE;
5026 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005027 else
5028 {
5029 /* Quickfix list is not found, create a new one. */
5030 qf_new_list(qi, title);
5031 return TRUE;
5032 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005033 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005034
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005035 if (qf_restore_list(qi, qfid) == FAIL)
5036 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005037
5038 return TRUE;
5039}
5040
5041/*
5042 * Search for a pattern in all the lines in a buffer and add the matching lines
5043 * to a quickfix list.
5044 */
5045 static int
5046vgr_match_buflines(
5047 qf_info_T *qi,
5048 char_u *fname,
5049 buf_T *buf,
5050 regmmatch_T *regmatch,
5051 long tomatch,
5052 int duplicate_name,
5053 int flags)
5054{
5055 int found_match = FALSE;
5056 long lnum;
5057 colnr_T col;
5058
5059 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5060 {
5061 col = 0;
5062 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5063 col, NULL, NULL) > 0)
5064 {
5065 /* Pass the buffer number so that it gets used even for a
5066 * dummy buffer, unless duplicate_name is set, then the
5067 * buffer will be wiped out below. */
5068 if (qf_add_entry(qi,
5069 qi->qf_curlist,
5070 NULL, /* dir */
5071 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005072 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005073 duplicate_name ? 0 : buf->b_fnum,
5074 ml_get_buf(buf,
5075 regmatch->startpos[0].lnum + lnum, FALSE),
5076 regmatch->startpos[0].lnum + lnum,
5077 regmatch->startpos[0].col + 1,
5078 FALSE, /* vis_col */
5079 NULL, /* search pattern */
5080 0, /* nr */
5081 0, /* type */
5082 TRUE /* valid */
5083 ) == FAIL)
5084 {
5085 got_int = TRUE;
5086 break;
5087 }
5088 found_match = TRUE;
5089 if (--tomatch == 0)
5090 break;
5091 if ((flags & VGR_GLOBAL) == 0
5092 || regmatch->endpos[0].lnum > 0)
5093 break;
5094 col = regmatch->endpos[0].col
5095 + (col == regmatch->endpos[0].col);
5096 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5097 break;
5098 }
5099 line_breakcheck();
5100 if (got_int)
5101 break;
5102 }
5103
5104 return found_match;
5105}
5106
5107/*
5108 * Jump to the first match and update the directory.
5109 */
5110 static void
5111vgr_jump_to_match(
5112 qf_info_T *qi,
5113 int forceit,
5114 int *redraw_for_dummy,
5115 buf_T *first_match_buf,
5116 char_u *target_dir)
5117{
5118 buf_T *buf;
5119
5120 buf = curbuf;
5121 qf_jump(qi, 0, 0, forceit);
5122 if (buf != curbuf)
5123 /* If we jumped to another buffer redrawing will already be
5124 * taken care of. */
5125 *redraw_for_dummy = FALSE;
5126
5127 /* Jump to the directory used after loading the buffer. */
5128 if (curbuf == first_match_buf && target_dir != NULL)
5129 {
5130 exarg_T ea;
5131
5132 ea.arg = target_dir;
5133 ea.cmdidx = CMD_lcd;
5134 ex_cd(&ea);
5135 }
5136}
5137
5138/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005139 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005140 * ":vimgrepadd {pattern} file(s)"
5141 * ":lvimgrep {pattern} file(s)"
5142 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005143 */
5144 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005145ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005146{
Bram Moolenaar81695252004-12-29 20:58:21 +00005147 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005148 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005149 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005150 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005151 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005152 char_u *s;
5153 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005154 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005155 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005156 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005157 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005158 buf_T *buf;
5159 int duplicate_name = FALSE;
5160 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005161 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005162 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005163 buf_T *first_match_buf = NULL;
5164 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005165 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005166 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005167 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005168 char_u *dirname_start = NULL;
5169 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005170 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005171 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005172
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005173 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005174 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5175 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005176 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005177#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005178 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005179 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005180#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005181 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005182
Bram Moolenaar39665952018-08-15 20:59:48 +02005183 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005184 {
5185 qi = ll_get_or_alloc_list(curwin);
5186 if (qi == NULL)
5187 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005188 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005189 }
5190
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005191 if (eap->addr_count > 0)
5192 tomatch = eap->line2;
5193 else
5194 tomatch = MAXLNUM;
5195
Bram Moolenaar81695252004-12-29 20:58:21 +00005196 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005197 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005198 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005199 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005200 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005201 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005202 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005203 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005204 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005205
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005206 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005207 if (regmatch.regprog == NULL)
5208 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005209
5210 p = skipwhite(p);
5211 if (*p == NUL)
5212 {
5213 EMSG(_("E683: File name missing or invalid pattern"));
5214 goto theend;
5215 }
5216
Bram Moolenaar55b69262017-08-13 13:42:01 +02005217 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005218 && eap->cmdidx != CMD_vimgrepadd
5219 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005220 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005221 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005222 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005223
5224 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005225 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005226 goto theend;
5227 if (fcount == 0)
5228 {
5229 EMSG(_(e_nomatch));
5230 goto theend;
5231 }
5232
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005233 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5234 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005235 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005236 {
5237 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005238 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005239 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005240
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005241 /* Remember the current directory, because a BufRead autocommand that does
5242 * ":lcd %:p:h" changes the meaning of short path names. */
5243 mch_dirname(dirname_start, MAXPATHL);
5244
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005245 /* Remember the current quickfix list identifier, so that we can check for
5246 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005247 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005248
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005249 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005250 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005251 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005252 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005253 if (time(NULL) > seconds)
5254 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005255 /* Display the file name every second or so, show the user we are
5256 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005257 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005258 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005259 }
5260
Bram Moolenaar81695252004-12-29 20:58:21 +00005261 buf = buflist_findname_exp(fnames[fi]);
5262 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5263 {
5264 /* Remember that a buffer with this name already exists. */
5265 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005266 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005267 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005268
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005269 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005270 }
5271 else
5272 /* Use existing, loaded buffer. */
5273 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005274
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005275 /* Check whether the quickfix list is still valid. When loading a
5276 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005277 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005278 {
5279 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005280 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005281 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005282 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005283
Bram Moolenaar81695252004-12-29 20:58:21 +00005284 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005285 {
5286 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005287 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005288 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005289 else
5290 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005291 /* Try for a match in all lines of the buffer.
5292 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005293 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5294 tomatch, duplicate_name, flags);
5295
Bram Moolenaar81695252004-12-29 20:58:21 +00005296 if (using_dummy)
5297 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005298 if (found_match && first_match_buf == NULL)
5299 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005300 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005301 {
Bram Moolenaar81695252004-12-29 20:58:21 +00005302 /* Never keep a dummy buffer if there is another buffer
5303 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005304 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005305 buf = NULL;
5306 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005307 else if (!cmdmod.hide
5308 || buf->b_p_bh[0] == 'u' /* "unload" */
5309 || buf->b_p_bh[0] == 'w' /* "wipe" */
5310 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00005311 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005312 /* When no match was found we don't need to remember the
5313 * buffer, wipe it out. If there was a match and it
5314 * wasn't the first one or we won't jump there: only
5315 * unload the buffer.
5316 * Ignore 'hidden' here, because it may lead to having too
5317 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00005318 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005319 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005320 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005321 buf = NULL;
5322 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005323 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005324 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005325 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02005326 /* Keeping the buffer, remove the dummy flag. */
5327 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005328 buf = NULL;
5329 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005330 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005331
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005332 if (buf != NULL)
5333 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02005334 /* Keeping the buffer, remove the dummy flag. */
5335 buf->b_flags &= ~BF_DUMMY;
5336
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005337 /* If the buffer is still loaded we need to use the
5338 * directory we jumped to below. */
5339 if (buf == first_match_buf
5340 && target_dir == NULL
5341 && STRCMP(dirname_start, dirname_now) != 0)
5342 target_dir = vim_strsave(dirname_now);
5343
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005344 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005345 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00005346 * need to be done (again). But not the window-local
5347 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005348 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005349#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005350 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5351 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005352#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005353 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005354 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005355 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005356 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005357 }
5358 }
5359
5360 FreeWild(fcount, fnames);
5361
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005362 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5363 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5364 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005365 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005366
Bram Moolenaar864293a2016-06-02 13:40:04 +02005367 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005368
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005369 if (au_name != NULL)
5370 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5371 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005372 /*
5373 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5374 * is still valid.
5375 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005376 if (!qflist_valid(wp, save_qfid))
5377 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005378
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005379 if (qf_restore_list(qi, save_qfid) == FAIL)
5380 goto theend;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005381
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005382 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005383 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005384 {
5385 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005386 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5387 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005388 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005389 else
5390 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005391
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005392 /* If we loaded a dummy buffer into the current window, the autocommands
5393 * may have messed up things, need to redraw and recompute folds. */
5394 if (redraw_for_dummy)
5395 {
5396#ifdef FEAT_FOLDING
5397 foldUpdateAll(curwin);
5398#else
5399 redraw_later(NOT_VALID);
5400#endif
5401 }
5402
Bram Moolenaar86b68352004-12-27 21:59:20 +00005403theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005404 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005405 vim_free(dirname_now);
5406 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005407 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005408 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005409}
5410
5411/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005412 * Restore current working directory to "dirname_start" if they differ, taking
5413 * into account whether it is set locally or globally.
5414 */
5415 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005416restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005417{
5418 char_u *dirname_now = alloc(MAXPATHL);
5419
5420 if (NULL != dirname_now)
5421 {
5422 mch_dirname(dirname_now, MAXPATHL);
5423 if (STRCMP(dirname_start, dirname_now) != 0)
5424 {
5425 /* If the directory has changed, change it back by building up an
5426 * appropriate ex command and executing it. */
5427 exarg_T ea;
5428
5429 ea.arg = dirname_start;
5430 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5431 ex_cd(&ea);
5432 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005433 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005434 }
5435}
5436
5437/*
5438 * Load file "fname" into a dummy buffer and return the buffer pointer,
5439 * placing the directory resulting from the buffer load into the
5440 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5441 * prior to calling this function. Restores directory to "dirname_start" prior
5442 * to returning, if autocmds or the 'autochdir' option have changed it.
5443 *
5444 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5445 * or wipe_dummy_buffer() later!
5446 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005447 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005448 */
5449 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005450load_dummy_buffer(
5451 char_u *fname,
5452 char_u *dirname_start, /* in: old directory */
5453 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005454{
5455 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005456 bufref_T newbufref;
5457 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005458 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005459 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005460 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005461
5462 /* Allocate a buffer without putting it in the buffer list. */
5463 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5464 if (newbuf == NULL)
5465 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005466 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005467
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005468 /* Init the options. */
5469 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5470
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005471 /* need to open the memfile before putting the buffer in a window */
5472 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005473 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005474 /* Make sure this buffer isn't wiped out by autocommands. */
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005475 ++newbuf->b_locked;
5476
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005477 /* set curwin/curbuf to buf and save a few things */
5478 aucmd_prepbuf(&aco, newbuf);
5479
5480 /* Need to set the filename for autocommands. */
5481 (void)setfname(curbuf, fname, NULL, FALSE);
5482
Bram Moolenaar81695252004-12-29 20:58:21 +00005483 /* Create swap file now to avoid the ATTENTION message. */
5484 check_need_swap(TRUE);
5485
5486 /* Remove the "dummy" flag, otherwise autocommands may not
5487 * work. */
5488 curbuf->b_flags &= ~BF_DUMMY;
5489
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005490 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005491 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005492 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005493 NULL, READ_NEW | READ_DUMMY);
5494 --newbuf->b_locked;
5495 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005496 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005497 && !(curbuf->b_flags & BF_NEW))
5498 {
5499 failed = FALSE;
5500 if (curbuf != newbuf)
5501 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005502 /* Bloody autocommands changed the buffer! Can happen when
5503 * using netrw and editing a remote file. Use the current
5504 * buffer instead, delete the dummy one after restoring the
5505 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005506 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005507 newbuf = curbuf;
5508 }
5509 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005510
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005511 /* restore curwin/curbuf and a few other things */
5512 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005513 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5514 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005515
5516 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5517 * skip it. */
5518 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005519 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005520
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005521 /*
5522 * When autocommands/'autochdir' option changed directory: go back.
5523 * Let the caller know what the resulting dir was first, in case it is
5524 * important.
5525 */
5526 mch_dirname(resulting_dir, MAXPATHL);
5527 restore_start_dir(dirname_start);
5528
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005529 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005530 return NULL;
5531 if (failed)
5532 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005533 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005534 return NULL;
5535 }
5536 return newbuf;
5537}
5538
5539/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005540 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5541 * directory to "dirname_start" prior to returning, if autocmds or the
5542 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005543 */
5544 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005545wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005546{
5547 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005548 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005549#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005550 cleanup_T cs;
5551
5552 /* Reset the error/interrupt/exception state here so that aborting()
5553 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5554 * work when got_int is set. */
5555 enter_cleanup(&cs);
5556#endif
5557
Bram Moolenaar81695252004-12-29 20:58:21 +00005558 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005559
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005560#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005561 /* Restore the error/interrupt/exception state if not discarded by a
5562 * new aborting error, interrupt, or uncaught exception. */
5563 leave_cleanup(&cs);
5564#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005565 /* When autocommands/'autochdir' option changed directory: go back. */
5566 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005567 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005568}
5569
5570/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005571 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5572 * directory to "dirname_start" prior to returning, if autocmds or the
5573 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005574 */
5575 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005576unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005577{
5578 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005579 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005580 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005581
5582 /* When autocommands/'autochdir' option changed directory: go back. */
5583 restore_start_dir(dirname_start);
5584 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005585}
5586
Bram Moolenaar05159a02005-02-26 23:04:13 +00005587#if defined(FEAT_EVAL) || defined(PROTO)
5588/*
5589 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005590 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005591 */
5592 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005593get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005594{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005595 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005596 dict_T *dict;
5597 char_u buf[2];
5598 qfline_T *qfp;
5599 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005600 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005601
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005602 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005603 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005604 qi = &ql_info;
5605 if (wp != NULL)
5606 {
5607 qi = GET_LOC_LIST(wp);
5608 if (qi == NULL)
5609 return FAIL;
5610 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005611 }
5612
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005613 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005614 qf_idx = qi->qf_curlist;
5615
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005616 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005617 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005618
Bram Moolenaard823fa92016-08-12 16:29:27 +02005619 qfp = qi->qf_lists[qf_idx].qf_start;
5620 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005621 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005622 /* Handle entries with a non-existing buffer number. */
5623 bufnum = qfp->qf_fnum;
5624 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5625 bufnum = 0;
5626
Bram Moolenaar05159a02005-02-26 23:04:13 +00005627 if ((dict = dict_alloc()) == NULL)
5628 return FAIL;
5629 if (list_append_dict(list, dict) == FAIL)
5630 return FAIL;
5631
5632 buf[0] = qfp->qf_type;
5633 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005634 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5635 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5636 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5637 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5638 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5639 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5640 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5641 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5642 || dict_add_string(dict, "type", buf) == FAIL
5643 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005644 return FAIL;
5645
5646 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005647 if (qfp == NULL)
5648 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005649 }
5650 return OK;
5651}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005652
5653/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005654 * Flags used by getqflist()/getloclist() to determine which fields to return.
5655 */
5656enum {
5657 QF_GETLIST_NONE = 0x0,
5658 QF_GETLIST_TITLE = 0x1,
5659 QF_GETLIST_ITEMS = 0x2,
5660 QF_GETLIST_NR = 0x4,
5661 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005662 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005663 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005664 QF_GETLIST_IDX = 0x40,
5665 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005666 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005667 QF_GETLIST_FILEWINID = 0x200,
5668 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005669};
5670
5671/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005672 * Parse text from 'di' and return the quickfix list items.
5673 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005674 */
5675 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005676qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005677{
5678 int status = FAIL;
5679 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005680 char_u *errorformat = p_efm;
5681 dictitem_T *efm_di;
5682 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005683
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005684 /* Only a List value is supported */
5685 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005686 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005687 /* If errorformat is supplied then use it, otherwise use the 'efm'
5688 * option setting
5689 */
5690 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5691 {
5692 if (efm_di->di_tv.v_type != VAR_STRING ||
5693 efm_di->di_tv.vval.v_string == NULL)
5694 return FAIL;
5695 errorformat = efm_di->di_tv.vval.v_string;
5696 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005697
Bram Moolenaar36538222017-09-02 19:51:44 +02005698 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005699 if (l == NULL)
5700 return FAIL;
5701
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005702 qi = ll_new_list();
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005703 if (qi != NULL)
5704 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005705 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005706 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5707 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005708 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005709 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005710 }
5711 free(qi);
5712 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005713 dict_add_list(retdict, "items", l);
5714 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005715 }
5716
5717 return status;
5718}
5719
5720/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005721 * Return the quickfix/location list window identifier in the current tabpage.
5722 */
5723 static int
5724qf_winid(qf_info_T *qi)
5725{
5726 win_T *win;
5727
5728 /* The quickfix window can be opened even if the quickfix list is not set
5729 * using ":copen". This is not true for location lists. */
5730 if (qi == NULL)
5731 return 0;
5732 win = qf_find_win(qi);
5733 if (win != NULL)
5734 return win->w_id;
5735 return 0;
5736}
5737
5738/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005739 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005740 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005741 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005742qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005743{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005744 int flags = QF_GETLIST_NONE;
5745
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005746 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005747 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005748 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005749 if (!loclist)
5750 // File window ID is applicable only to location list windows
5751 flags &= ~ QF_GETLIST_FILEWINID;
5752 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005753
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005754 if (dict_find(what, (char_u *)"title", -1) != NULL)
5755 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005756
Bram Moolenaara6d48492017-12-12 22:45:31 +01005757 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5758 flags |= QF_GETLIST_NR;
5759
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005760 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5761 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005762
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005763 if (dict_find(what, (char_u *)"context", -1) != NULL)
5764 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005765
Bram Moolenaara6d48492017-12-12 22:45:31 +01005766 if (dict_find(what, (char_u *)"id", -1) != NULL)
5767 flags |= QF_GETLIST_ID;
5768
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005769 if (dict_find(what, (char_u *)"items", -1) != NULL)
5770 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005771
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005772 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5773 flags |= QF_GETLIST_IDX;
5774
5775 if (dict_find(what, (char_u *)"size", -1) != NULL)
5776 flags |= QF_GETLIST_SIZE;
5777
Bram Moolenaarb254af32017-12-18 19:48:58 +01005778 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5779 flags |= QF_GETLIST_TICK;
5780
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005781 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5782 flags |= QF_GETLIST_FILEWINID;
5783
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005784 return flags;
5785}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005786
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005787/*
5788 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5789 * If 'nr' and 'id' are not present in 'what' then return the current
5790 * quickfix list index.
5791 * If 'nr' is zero then return the current quickfix list index.
5792 * If 'nr' is '$' then return the last quickfix list index.
5793 * If 'id' is present then return the index of the quickfix list with that id.
5794 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5795 * Return -1, if quickfix list is not present or if the stack is empty.
5796 */
5797 static int
5798qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5799{
5800 int qf_idx;
5801 dictitem_T *di;
5802
5803 qf_idx = qi->qf_curlist; /* default is the current list */
5804 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5805 {
5806 /* Use the specified quickfix/location list */
5807 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005808 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005809 /* for zero use the current list */
5810 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005811 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005812 qf_idx = di->di_tv.vval.v_number - 1;
5813 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005814 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005815 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005816 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005817 else if (di->di_tv.v_type == VAR_STRING
5818 && di->di_tv.vval.v_string != NULL
5819 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5820 /* Get the last quickfix list number */
5821 qf_idx = qi->qf_listcount - 1;
5822 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005823 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005824 }
5825
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005826 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5827 {
5828 /* Look for a list with the specified id */
5829 if (di->di_tv.v_type == VAR_NUMBER)
5830 {
5831 /*
5832 * For zero, use the current list or the list specified by 'nr'
5833 */
5834 if (di->di_tv.vval.v_number != 0)
5835 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5836 }
5837 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005838 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005839 }
5840
5841 return qf_idx;
5842}
5843
5844/*
5845 * Return default values for quickfix list properties in retdict.
5846 */
5847 static int
5848qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5849{
5850 int status = OK;
5851
5852 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005853 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005854 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5855 {
5856 list_T *l = list_alloc();
5857 if (l != NULL)
5858 status = dict_add_list(retdict, "items", l);
5859 else
5860 status = FAIL;
5861 }
5862 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005863 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005864 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005865 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005866 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005867 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005868 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005869 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005870 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005871 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005872 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005873 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005874 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005875 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005876 if ((status == OK) && (qi != &ql_info) && (flags & QF_GETLIST_FILEWINID))
5877 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005878
5879 return status;
5880}
5881
5882/*
5883 * Return the quickfix list title as 'title' in retdict
5884 */
5885 static int
5886qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5887{
Bram Moolenaare0be1672018-07-08 16:50:37 +02005888 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005889}
5890
5891/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005892 * Returns the identifier of the window used to display files from a location
5893 * list. If there is no associated window, then returns 0. Useful only when
5894 * called from a location list window.
5895 */
5896 static int
5897qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
5898{
5899 int winid = 0;
5900
5901 if (wp != NULL && IS_LL_WINDOW(wp))
5902 {
5903 win_T *ll_wp = qf_find_win_with_loclist(qi);
5904 if (ll_wp != NULL)
5905 winid = ll_wp->w_id;
5906 }
5907
5908 return dict_add_number(retdict, "filewinid", winid);
5909}
5910
5911/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005912 * Return the quickfix list items/entries as 'items' in retdict
5913 */
5914 static int
5915qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5916{
5917 int status = OK;
5918 list_T *l = list_alloc();
5919 if (l != NULL)
5920 {
5921 (void)get_errorlist(qi, NULL, qf_idx, l);
5922 dict_add_list(retdict, "items", l);
5923 }
5924 else
5925 status = FAIL;
5926
5927 return status;
5928}
5929
5930/*
5931 * Return the quickfix list context (if any) as 'context' in retdict.
5932 */
5933 static int
5934qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5935{
5936 int status;
5937 dictitem_T *di;
5938
5939 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5940 {
5941 di = dictitem_alloc((char_u *)"context");
5942 if (di != NULL)
5943 {
5944 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5945 status = dict_add(retdict, di);
5946 if (status == FAIL)
5947 dictitem_free(di);
5948 }
5949 else
5950 status = FAIL;
5951 }
5952 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02005953 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005954
5955 return status;
5956}
5957
5958/*
5959 * Return the quickfix list index as 'idx' in retdict
5960 */
5961 static int
5962qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5963{
5964 int idx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005965 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005966 /* For empty lists, qf_index is set to 1 */
5967 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005968 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005969}
5970
5971/*
5972 * Return quickfix/location list details (title) as a
5973 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5974 * then current list is used. Otherwise the specified list is used.
5975 */
5976 int
5977qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5978{
5979 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005980 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005981 int status = OK;
5982 int qf_idx;
5983 dictitem_T *di;
5984 int flags = QF_GETLIST_NONE;
5985
5986 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5987 return qf_get_list_from_lines(what, di, retdict);
5988
5989 if (wp != NULL)
5990 qi = GET_LOC_LIST(wp);
5991
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005992 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005993
5994 if (qi != NULL && qi->qf_listcount != 0)
5995 qf_idx = qf_getprop_qfidx(qi, what);
5996
Bram Moolenaara6d48492017-12-12 22:45:31 +01005997 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005998 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005999 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006000
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006001 qfl = &qi->qf_lists[qf_idx];
6002
Bram Moolenaard823fa92016-08-12 16:29:27 +02006003 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006004 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006005 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006006 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006007 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006008 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006009 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006010 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006011 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006012 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006013 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006014 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006015 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006016 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006017 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006018 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006019 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006020 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006021 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6022 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006023
Bram Moolenaard823fa92016-08-12 16:29:27 +02006024 return status;
6025}
6026
6027/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006028 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
6029 * items in the dict 'd'.
6030 */
6031 static int
6032qf_add_entry_from_dict(
6033 qf_info_T *qi,
6034 int qf_idx,
6035 dict_T *d,
6036 int first_entry)
6037{
6038 static int did_bufnr_emsg;
6039 char_u *filename, *module, *pattern, *text, *type;
6040 int bufnum, valid, status, col, vcol, nr;
6041 long lnum;
6042
6043 if (first_entry)
6044 did_bufnr_emsg = FALSE;
6045
6046 filename = get_dict_string(d, (char_u *)"filename", TRUE);
6047 module = get_dict_string(d, (char_u *)"module", TRUE);
6048 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6049 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6050 col = (int)get_dict_number(d, (char_u *)"col");
6051 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6052 nr = (int)get_dict_number(d, (char_u *)"nr");
6053 type = get_dict_string(d, (char_u *)"type", TRUE);
6054 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6055 text = get_dict_string(d, (char_u *)"text", TRUE);
6056 if (text == NULL)
6057 text = vim_strsave((char_u *)"");
6058
6059 valid = TRUE;
6060 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6061 valid = FALSE;
6062
6063 // Mark entries with non-existing buffer number as not valid. Give the
6064 // error message only once.
6065 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6066 {
6067 if (!did_bufnr_emsg)
6068 {
6069 did_bufnr_emsg = TRUE;
6070 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6071 }
6072 valid = FALSE;
6073 bufnum = 0;
6074 }
6075
6076 // If the 'valid' field is present it overrules the detected value.
6077 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6078 valid = (int)get_dict_number(d, (char_u *)"valid");
6079
6080 status = qf_add_entry(qi,
6081 qf_idx,
6082 NULL, // dir
6083 filename,
6084 module,
6085 bufnum,
6086 text,
6087 lnum,
6088 col,
6089 vcol, // vis_col
6090 pattern, // search pattern
6091 nr,
6092 type == NULL ? NUL : *type,
6093 valid);
6094
6095 vim_free(filename);
6096 vim_free(module);
6097 vim_free(pattern);
6098 vim_free(text);
6099 vim_free(type);
6100
6101 return status;
6102}
6103
6104/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006105 * Add list of entries to quickfix/location list. Each list entry is
6106 * a dictionary with item information.
6107 */
6108 static int
6109qf_add_entries(
6110 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006111 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006112 list_T *list,
6113 char_u *title,
6114 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006115{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006116 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006117 listitem_T *li;
6118 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006119 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006120 int retval = OK;
6121
Bram Moolenaara3921f42017-06-04 15:30:34 +02006122 if (action == ' ' || qf_idx == qi->qf_listcount)
6123 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006124 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006125 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006126 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006127 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006128 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006129 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006130 /* Adding to existing list, use last entry. */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006131 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006132 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006133 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006134 qf_free_items(qfl);
6135 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006136 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006137
6138 for (li = list->lv_first; li != NULL; li = li->li_next)
6139 {
6140 if (li->li_tv.v_type != VAR_DICT)
6141 continue; /* Skip non-dict items */
6142
6143 d = li->li_tv.vval.v_dict;
6144 if (d == NULL)
6145 continue;
6146
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006147 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first);
6148 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006149 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006150 }
6151
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006152 if (qfl->qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02006153 /* no valid entry */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006154 qfl->qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006155 else
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006156 qfl->qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006157 if (action != 'a')
6158 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006159 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006160 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006161 qfl->qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006162 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006163
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006164 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02006165 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006166
6167 return retval;
6168}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006169
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006170/*
6171 * Get the quickfix list index from 'nr' or 'id'
6172 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006173 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006174qf_setprop_get_qfidx(
6175 qf_info_T *qi,
6176 dict_T *what,
6177 int action,
6178 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006179{
6180 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006181 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006182
Bram Moolenaard823fa92016-08-12 16:29:27 +02006183 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6184 {
6185 /* Use the specified quickfix/location list */
6186 if (di->di_tv.v_type == VAR_NUMBER)
6187 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006188 /* for zero use the current list */
6189 if (di->di_tv.vval.v_number != 0)
6190 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006191
Bram Moolenaar55b69262017-08-13 13:42:01 +02006192 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6193 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006194 /*
6195 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02006196 * non-available list and add the new list at the end of the
6197 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006198 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006199 *newlist = TRUE;
6200 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006201 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006202 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006203 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006204 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006205 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02006206 }
6207 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006208 && di->di_tv.vval.v_string != NULL
6209 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006210 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006211 if (qi->qf_listcount > 0)
6212 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006213 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006214 qf_idx = 0;
6215 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006216 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006217 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006218 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006219 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006220 }
6221
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006222 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006223 {
6224 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006225 if (di->di_tv.v_type != VAR_NUMBER)
6226 return INVALID_QFIDX;
6227
6228 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006229 }
6230
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006231 return qf_idx;
6232}
6233
6234/*
6235 * Set the quickfix list title.
6236 */
6237 static int
6238qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6239{
6240 if (di->di_tv.v_type != VAR_STRING)
6241 return FAIL;
6242
6243 vim_free(qi->qf_lists[qf_idx].qf_title);
6244 qi->qf_lists[qf_idx].qf_title =
6245 get_dict_string(what, (char_u *)"title", TRUE);
6246 if (qf_idx == qi->qf_curlist)
6247 qf_update_win_titlevar(qi);
6248
6249 return OK;
6250}
6251
6252/*
6253 * Set quickfix list items/entries.
6254 */
6255 static int
6256qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6257{
6258 int retval = FAIL;
6259 char_u *title_save;
6260
6261 if (di->di_tv.v_type != VAR_LIST)
6262 return FAIL;
6263
6264 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6265 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6266 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006267 vim_free(title_save);
6268
6269 return retval;
6270}
6271
6272/*
6273 * Set quickfix list items/entries from a list of lines.
6274 */
6275 static int
6276qf_setprop_items_from_lines(
6277 qf_info_T *qi,
6278 int qf_idx,
6279 dict_T *what,
6280 dictitem_T *di,
6281 int action)
6282{
6283 char_u *errorformat = p_efm;
6284 dictitem_T *efm_di;
6285 int retval = FAIL;
6286
6287 /* Use the user supplied errorformat settings (if present) */
6288 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6289 {
6290 if (efm_di->di_tv.v_type != VAR_STRING ||
6291 efm_di->di_tv.vval.v_string == NULL)
6292 return FAIL;
6293 errorformat = efm_di->di_tv.vval.v_string;
6294 }
6295
6296 /* Only a List value is supported */
6297 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6298 return FAIL;
6299
6300 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006301 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006302 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6303 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6304 retval = OK;
6305
6306 return retval;
6307}
6308
6309/*
6310 * Set quickfix list context.
6311 */
6312 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006313qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006314{
6315 typval_T *ctx;
6316
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006317 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006318 ctx = alloc_tv();
6319 if (ctx != NULL)
6320 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006321 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006322
6323 return OK;
6324}
6325
6326/*
6327 * Set quickfix/location list properties (title, items, context).
6328 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006329 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006330 */
6331 static int
6332qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6333{
6334 dictitem_T *di;
6335 int retval = FAIL;
6336 int qf_idx;
6337 int newlist = FALSE;
6338
6339 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
6340 newlist = TRUE;
6341
6342 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6343 if (qf_idx == INVALID_QFIDX) /* List not found */
6344 return FAIL;
6345
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006346 if (newlist)
6347 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006348 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006349 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006350 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006351 }
6352
6353 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006354 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006355 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006356 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006357 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006358 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006359 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006360 retval = qf_setprop_context(&qi->qf_lists[qf_idx], di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006361
Bram Moolenaarb254af32017-12-18 19:48:58 +01006362 if (retval == OK)
6363 qf_list_changed(qi, qf_idx);
6364
Bram Moolenaard823fa92016-08-12 16:29:27 +02006365 return retval;
6366}
6367
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006368/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006369 * Find the non-location list window with the specified location list in the
6370 * current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006371 */
6372 static win_T *
6373find_win_with_ll(qf_info_T *qi)
6374{
6375 win_T *wp = NULL;
6376
6377 FOR_ALL_WINDOWS(wp)
6378 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6379 return wp;
6380
6381 return NULL;
6382}
6383
6384/*
6385 * Free the entire quickfix/location list stack.
6386 * If the quickfix/location list window is open, then clear it.
6387 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006388 static void
6389qf_free_stack(win_T *wp, qf_info_T *qi)
6390{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006391 win_T *qfwin = qf_find_win(qi);
6392 win_T *llwin = NULL;
6393 win_T *orig_wp = wp;
6394
6395 if (qfwin != NULL)
6396 {
6397 /* If the quickfix/location list window is open, then clear it */
6398 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006399 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006400 qf_update_buffer(qi, NULL);
6401 }
6402
6403 if (wp != NULL && IS_LL_WINDOW(wp))
6404 {
6405 /* If in the location list window, then use the non-location list
6406 * window with this location list (if present)
6407 */
6408 llwin = find_win_with_ll(qi);
6409 if (llwin != NULL)
6410 wp = llwin;
6411 }
6412
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006413 qf_free_all(wp);
6414 if (wp == NULL)
6415 {
6416 /* quickfix list */
6417 qi->qf_curlist = 0;
6418 qi->qf_listcount = 0;
6419 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006420 else if (IS_LL_WINDOW(orig_wp))
6421 {
6422 /* If the location list window is open, then create a new empty
6423 * location list */
6424 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006425
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006426 /* first free the list reference in the location list window */
6427 ll_free_all(&orig_wp->w_llist_ref);
6428
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006429 orig_wp->w_llist_ref = new_ll;
6430 if (llwin != NULL)
6431 {
6432 llwin->w_llist = new_ll;
6433 new_ll->qf_refcount++;
6434 }
6435 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006436}
6437
Bram Moolenaard823fa92016-08-12 16:29:27 +02006438/*
6439 * Populate the quickfix list with the items supplied in the list
6440 * of dictionaries. "title" will be copied to w:quickfix_title.
6441 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6442 */
6443 int
6444set_errorlist(
6445 win_T *wp,
6446 list_T *list,
6447 int action,
6448 char_u *title,
6449 dict_T *what)
6450{
6451 qf_info_T *qi = &ql_info;
6452 int retval = OK;
6453
6454 if (wp != NULL)
6455 {
6456 qi = ll_get_or_alloc_list(wp);
6457 if (qi == NULL)
6458 return FAIL;
6459 }
6460
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006461 if (action == 'f')
6462 {
6463 /* Free the entire quickfix or location list stack */
6464 qf_free_stack(wp, qi);
6465 }
6466 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006467 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006468 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006469 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006470 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006471 if (retval == OK)
6472 qf_list_changed(qi, qi->qf_curlist);
6473 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006474
6475 return retval;
6476}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006477
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006478/*
6479 * Mark the context as in use for all the lists in a quickfix stack.
6480 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006481 static int
6482mark_quickfix_ctx(qf_info_T *qi, int copyID)
6483{
6484 int i;
6485 int abort = FALSE;
6486 typval_T *ctx;
6487
6488 for (i = 0; i < LISTCOUNT && !abort; ++i)
6489 {
6490 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006491 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6492 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006493 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6494 }
6495
6496 return abort;
6497}
6498
6499/*
6500 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006501 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006502 */
6503 int
6504set_ref_in_quickfix(int copyID)
6505{
6506 int abort = FALSE;
6507 tabpage_T *tp;
6508 win_T *win;
6509
6510 abort = mark_quickfix_ctx(&ql_info, copyID);
6511 if (abort)
6512 return abort;
6513
6514 FOR_ALL_TAB_WINDOWS(tp, win)
6515 {
6516 if (win->w_llist != NULL)
6517 {
6518 abort = mark_quickfix_ctx(win->w_llist, copyID);
6519 if (abort)
6520 return abort;
6521 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006522 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6523 {
6524 /* In a location list window and none of the other windows is
6525 * referring to this location list. Mark the location list
6526 * context as still in use.
6527 */
6528 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6529 if (abort)
6530 return abort;
6531 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006532 }
6533
6534 return abort;
6535}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006536#endif
6537
Bram Moolenaar81695252004-12-29 20:58:21 +00006538/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006539 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006540 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006541 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006542 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006543 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006544 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006545 */
6546 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006547ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006548{
6549 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006550 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006551 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006552 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006553 int_u save_qfid;
6554 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006555
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006556 switch (eap->cmdidx)
6557 {
6558 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6559 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6560 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6561 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6562 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6563 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6564 default: break;
6565 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006566 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6567 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006568 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006569#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006570 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006571 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006572#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006573 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006574
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006575 /* Must come after autocommands. */
Bram Moolenaar39665952018-08-15 20:59:48 +02006576 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006577 {
6578 qi = ll_get_or_alloc_list(curwin);
6579 if (qi == NULL)
6580 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006581 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006582 }
6583
Bram Moolenaar86b68352004-12-27 21:59:20 +00006584 if (*eap->arg == NUL)
6585 buf = curbuf;
6586 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6587 buf = buflist_findnr(atoi((char *)eap->arg));
6588 if (buf == NULL)
6589 EMSG(_(e_invarg));
6590 else if (buf->b_ml.ml_mfp == NULL)
6591 EMSG(_("E681: Buffer is not loaded"));
6592 else
6593 {
6594 if (eap->addr_count == 0)
6595 {
6596 eap->line1 = 1;
6597 eap->line2 = buf->b_ml.ml_line_count;
6598 }
6599 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6600 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6601 EMSG(_(e_invrange));
6602 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006603 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006604 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006605
6606 if (buf->b_sfname)
6607 {
6608 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6609 (char *)qf_title, (char *)buf->b_sfname);
6610 qf_title = IObuff;
6611 }
6612
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006613 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006614 (eap->cmdidx != CMD_caddbuffer
6615 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006616 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006617 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006618 if (res >= 0)
6619 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006620
6621 // Remember the current quickfix list identifier, so that we can
6622 // check for autocommands changing the current quickfix list.
6623 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006624 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006625 {
6626 buf_T *curbuf_old = curbuf;
6627
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006628 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6629 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006630 if (curbuf != curbuf_old)
6631 // Autocommands changed buffer, don't jump now, "qi" may
6632 // be invalid.
6633 res = 0;
6634 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006635 // Jump to the first error for a new list and if autocmds didn't
6636 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006637 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006638 eap->cmdidx == CMD_lbuffer)
6639 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006640 // display the first error
6641 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar754b5602006-02-09 23:53:20 +00006642 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006643 }
6644}
6645
Bram Moolenaar1e015462005-09-25 22:16:38 +00006646#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006647/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006648 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6649 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006650 */
6651 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006652ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006653{
6654 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006655 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006656 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006657 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006658 int_u save_qfid;
6659 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006660
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006661 switch (eap->cmdidx)
6662 {
6663 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6664 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6665 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6666 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6667 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6668 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6669 default: break;
6670 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006671 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6672 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006673 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006674#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006675 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006676 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006677#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006678 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006679
Bram Moolenaar39665952018-08-15 20:59:48 +02006680 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006681 {
6682 qi = ll_get_or_alloc_list(curwin);
6683 if (qi == NULL)
6684 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006685 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006686 }
6687
Bram Moolenaar4770d092006-01-12 23:22:24 +00006688 /* Evaluate the expression. When the result is a string or a list we can
6689 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006690 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006691 if (tv != NULL)
6692 {
6693 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6694 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6695 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006696 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006697 (eap->cmdidx != CMD_caddexpr
6698 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006699 (linenr_T)0, (linenr_T)0,
6700 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006701 if (res >= 0)
6702 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006703
6704 // Remember the current quickfix list identifier, so that we can
6705 // check for autocommands changing the current quickfix list.
6706 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006707 if (au_name != NULL)
6708 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6709 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006710
6711 // Jump to the first error for a new list and if autocmds didn't
6712 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006713 if (res > 0 && (eap->cmdidx == CMD_cexpr
6714 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006715 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006716 // display the first error
6717 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006718 }
6719 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006720 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006721 free_tv(tv);
6722 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006723}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006724#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006725
6726/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006727 * Get the location list for ":lhelpgrep"
6728 */
6729 static qf_info_T *
6730hgr_get_ll(int *new_ll)
6731{
6732 win_T *wp;
6733 qf_info_T *qi;
6734
6735 /* If the current window is a help window, then use it */
6736 if (bt_help(curwin->w_buffer))
6737 wp = curwin;
6738 else
6739 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006740 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006741
6742 if (wp == NULL) /* Help window not found */
6743 qi = NULL;
6744 else
6745 qi = wp->w_llist;
6746
6747 if (qi == NULL)
6748 {
6749 /* Allocate a new location list for help text matches */
6750 if ((qi = ll_new_list()) == NULL)
6751 return NULL;
6752 *new_ll = TRUE;
6753 }
6754
6755 return qi;
6756}
6757
6758/*
6759 * Search for a pattern in a help file.
6760 */
6761 static void
6762hgr_search_file(
6763 qf_info_T *qi,
6764 char_u *fname,
6765#ifdef FEAT_MBYTE
6766 vimconv_T *p_vc,
6767#endif
6768 regmatch_T *p_regmatch)
6769{
6770 FILE *fd;
6771 long lnum;
6772
6773 fd = mch_fopen((char *)fname, "r");
6774 if (fd == NULL)
6775 return;
6776
6777 lnum = 1;
6778 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6779 {
6780 char_u *line = IObuff;
6781#ifdef FEAT_MBYTE
6782 /* Convert a line if 'encoding' is not utf-8 and
6783 * the line contains a non-ASCII character. */
6784 if (p_vc->vc_type != CONV_NONE
6785 && has_non_ascii(IObuff))
6786 {
6787 line = string_convert(p_vc, IObuff, NULL);
6788 if (line == NULL)
6789 line = IObuff;
6790 }
6791#endif
6792
6793 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6794 {
6795 int l = (int)STRLEN(line);
6796
6797 /* remove trailing CR, LF, spaces, etc. */
6798 while (l > 0 && line[l - 1] <= ' ')
6799 line[--l] = NUL;
6800
6801 if (qf_add_entry(qi,
6802 qi->qf_curlist,
6803 NULL, /* dir */
6804 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006805 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006806 0,
6807 line,
6808 lnum,
6809 (int)(p_regmatch->startp[0] - line)
6810 + 1, /* col */
6811 FALSE, /* vis_col */
6812 NULL, /* search pattern */
6813 0, /* nr */
6814 1, /* type */
6815 TRUE /* valid */
6816 ) == FAIL)
6817 {
6818 got_int = TRUE;
6819#ifdef FEAT_MBYTE
6820 if (line != IObuff)
6821 vim_free(line);
6822#endif
6823 break;
6824 }
6825 }
6826#ifdef FEAT_MBYTE
6827 if (line != IObuff)
6828 vim_free(line);
6829#endif
6830 ++lnum;
6831 line_breakcheck();
6832 }
6833 fclose(fd);
6834}
6835
6836/*
6837 * Search for a pattern in all the help files in the doc directory under
6838 * the given directory.
6839 */
6840 static void
6841hgr_search_files_in_dir(
6842 qf_info_T *qi,
6843 char_u *dirname,
6844 regmatch_T *p_regmatch
6845#ifdef FEAT_MBYTE
6846 , vimconv_T *p_vc
6847#endif
6848#ifdef FEAT_MULTI_LANG
6849 , char_u *lang
6850#endif
6851 )
6852{
6853 int fcount;
6854 char_u **fnames;
6855 int fi;
6856
6857 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6858 add_pathsep(dirname);
6859 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6860 if (gen_expand_wildcards(1, &dirname, &fcount,
6861 &fnames, EW_FILE|EW_SILENT) == OK
6862 && fcount > 0)
6863 {
6864 for (fi = 0; fi < fcount && !got_int; ++fi)
6865 {
6866#ifdef FEAT_MULTI_LANG
6867 /* Skip files for a different language. */
6868 if (lang != NULL
6869 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006870 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006871 && !(STRNICMP(lang, "en", 2) == 0
6872 && STRNICMP("txt", fnames[fi]
6873 + STRLEN(fnames[fi]) - 3, 3) == 0))
6874 continue;
6875#endif
6876
6877 hgr_search_file(qi, fnames[fi],
6878#ifdef FEAT_MBYTE
6879 p_vc,
6880#endif
6881 p_regmatch);
6882 }
6883 FreeWild(fcount, fnames);
6884 }
6885}
6886
6887/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006888 * Search for a pattern in all the help files in the 'runtimepath'
6889 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006890 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006891 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006892 */
6893 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006894hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006895{
6896 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006897
6898#ifdef FEAT_MBYTE
6899 vimconv_T vc;
6900
6901 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6902 * differs. */
6903 vc.vc_type = CONV_NONE;
6904 if (!enc_utf8)
6905 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6906#endif
6907
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006908
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006909 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006910 p = p_rtp;
6911 while (*p != NUL && !got_int)
6912 {
6913 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6914
6915 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6916#ifdef FEAT_MBYTE
6917 , &vc
6918#endif
6919#ifdef FEAT_MULTI_LANG
6920 , lang
6921#endif
6922 );
6923 }
6924
6925#ifdef FEAT_MBYTE
6926 if (vc.vc_type != CONV_NONE)
6927 convert_setup(&vc, NULL, NULL);
6928#endif
6929}
6930
6931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 * ":helpgrep {pattern}"
6933 */
6934 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006935ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936{
6937 regmatch_T regmatch;
6938 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006939 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006940 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006941 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006942 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943
Bram Moolenaar73633f82012-01-20 13:39:07 +01006944 switch (eap->cmdidx)
6945 {
6946 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6947 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6948 default: break;
6949 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006950 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6951 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006952 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006953#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006954 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006955 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006956#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006957 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006958
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006959 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01006960 save_cpo = p_cpo;
6961 p_cpo = empty_option;
6962
Bram Moolenaar39665952018-08-15 20:59:48 +02006963 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006964 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006965 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006966 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006967 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006968 }
6969
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006970#ifdef FEAT_MULTI_LANG
6971 // Check for a specified language
6972 lang = check_help_lang(eap->arg);
6973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6975 regmatch.rm_ic = FALSE;
6976 if (regmatch.regprog != NULL)
6977 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006978 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006979 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006981 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006982
Bram Moolenaar473de612013-06-08 18:19:48 +02006983 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006985 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6986 qi->qf_lists[qi->qf_curlist].qf_ptr =
6987 qi->qf_lists[qi->qf_curlist].qf_start;
6988 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 }
6990
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006991 if (p_cpo == empty_option)
6992 p_cpo = save_cpo;
6993 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006994 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006995 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
Bram Moolenaarb254af32017-12-18 19:48:58 +01006997 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006998 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
Bram Moolenaar73633f82012-01-20 13:39:07 +01007000 if (au_name != NULL)
7001 {
7002 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7003 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007004 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01007005 /* autocommands made "qi" invalid */
7006 return;
7007 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007008
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007010 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007011 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007012 else
7013 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007014
7015 if (eap->cmdidx == CMD_lhelpgrep)
7016 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007017 // If the help window is not opened or if it already points to the
7018 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007019 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007020 {
7021 if (new_qi)
7022 ll_free_all(&qi);
7023 }
7024 else if (curwin->w_llist == NULL)
7025 curwin->w_llist = qi;
7026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027}
7028
7029#endif /* FEAT_QUICKFIX */