blob: 1c3343f344d48084548c1e834fc07b7d82414c0b [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/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002047 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2048 */
2049 static int
2050copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2051{
2052 int i;
2053 qfline_T *from_qfp;
2054 qfline_T *prevp;
2055
2056 // copy all the location entries in this list
2057 for (i = 0, from_qfp = from_qfl->qf_start;
2058 i < from_qfl->qf_count && from_qfp != NULL;
2059 ++i, from_qfp = from_qfp->qf_next)
2060 {
2061 if (qf_add_entry(to_qi,
2062 to_qi->qf_curlist,
2063 NULL,
2064 NULL,
2065 from_qfp->qf_module,
2066 0,
2067 from_qfp->qf_text,
2068 from_qfp->qf_lnum,
2069 from_qfp->qf_col,
2070 from_qfp->qf_viscol,
2071 from_qfp->qf_pattern,
2072 from_qfp->qf_nr,
2073 0,
2074 from_qfp->qf_valid) == FAIL)
2075 return FAIL;
2076
2077 // qf_add_entry() will not set the qf_num field, as the
2078 // directory and file names are not supplied. So the qf_fnum
2079 // field is copied here.
2080 prevp = to_qfl->qf_last;
2081 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2082 prevp->qf_type = from_qfp->qf_type; // error type
2083 if (from_qfl->qf_ptr == from_qfp)
2084 to_qfl->qf_ptr = prevp; // current location
2085 }
2086
2087 return OK;
2088}
2089
2090/*
2091 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2092 */
2093 static int
2094copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2095{
2096 // Some of the fields are populated by qf_add_entry()
2097 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2098 to_qfl->qf_count = 0;
2099 to_qfl->qf_index = 0;
2100 to_qfl->qf_start = NULL;
2101 to_qfl->qf_last = NULL;
2102 to_qfl->qf_ptr = NULL;
2103 if (from_qfl->qf_title != NULL)
2104 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2105 else
2106 to_qfl->qf_title = NULL;
2107 if (from_qfl->qf_ctx != NULL)
2108 {
2109 to_qfl->qf_ctx = alloc_tv();
2110 if (to_qfl->qf_ctx != NULL)
2111 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2112 }
2113 else
2114 to_qfl->qf_ctx = NULL;
2115
2116 if (from_qfl->qf_count)
2117 if (copy_loclist_entries(from_qfl, to_qfl, to_qi) == FAIL)
2118 return FAIL;
2119
2120 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2121
2122 // Assign a new ID for the location list
2123 to_qfl->qf_id = ++last_qf_id;
2124 to_qfl->qf_changedtick = 0L;
2125
2126 // When no valid entries are present in the list, qf_ptr points to
2127 // the first item in the list
2128 if (to_qfl->qf_nonevalid)
2129 {
2130 to_qfl->qf_ptr = to_qfl->qf_start;
2131 to_qfl->qf_index = 1;
2132 }
2133
2134 return OK;
2135}
2136
2137/*
2138 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002139 */
2140 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002141copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002142{
2143 qf_info_T *qi;
2144 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002145
Bram Moolenaar09037502018-09-25 22:08:14 +02002146 // When copying from a location list window, copy the referenced
2147 // location list. For other windows, copy the location list for
2148 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002149 if (IS_LL_WINDOW(from))
2150 qi = from->w_llist_ref;
2151 else
2152 qi = from->w_llist;
2153
Bram Moolenaar09037502018-09-25 22:08:14 +02002154 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002155 return;
2156
Bram Moolenaar09037502018-09-25 22:08:14 +02002157 // allocate a new location list
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002158 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002159 return;
2160
2161 to->w_llist->qf_listcount = qi->qf_listcount;
2162
Bram Moolenaar09037502018-09-25 22:08:14 +02002163 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002164 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002165 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002166 to->w_llist->qf_curlist = idx;
2167
Bram Moolenaar09037502018-09-25 22:08:14 +02002168 if (copy_loclist(&qi->qf_lists[idx],
2169 &to->w_llist->qf_lists[idx], to->w_llist) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002170 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002171 qf_free_all(to);
2172 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002173 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002174 }
2175
Bram Moolenaar09037502018-09-25 22:08:14 +02002176 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002177}
2178
2179/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002180 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002181 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 */
2183 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002184qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
Bram Moolenaar82404332016-07-10 17:00:38 +02002186 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002187 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002188 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002189
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 if (fname == NULL || *fname == NUL) /* no file name */
2191 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
Bram Moolenaare60acc12011-05-10 16:41:25 +02002193#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002194 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002195#endif
2196#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002197 if (directory != NULL)
2198 slash_adjust(directory);
2199 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002200#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002201 if (directory != NULL && !vim_isAbsName(fname)
2202 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2203 {
2204 /*
2205 * Here we check if the file really exists.
2206 * This should normally be true, but if make works without
2207 * "leaving directory"-messages we might have missed a
2208 * directory change.
2209 */
2210 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 vim_free(ptr);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002213 directory = qf_guess_filepath(&qi->qf_lists[qf_idx], fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002214 if (directory)
2215 ptr = concat_fnames(directory, fname, TRUE);
2216 else
2217 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002219 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02002220 bufname = ptr;
2221 }
2222 else
2223 bufname = fname;
2224
2225 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002226 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002227 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002228 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002229 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002231 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002232 {
2233 vim_free(qf_last_bufname);
2234 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2235 if (bufname == ptr)
2236 qf_last_bufname = bufname;
2237 else
2238 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002239 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002240 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002241 if (buf == NULL)
2242 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002243
Bram Moolenaarc1542742016-07-20 21:44:37 +02002244 buf->b_has_qf_entry =
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002245 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002246 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247}
2248
2249/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002250 * Push dirbuf onto the directory stack and return pointer to actual dir or
2251 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 */
2253 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002254qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
2256 struct dir_stack_T *ds_new;
2257 struct dir_stack_T *ds_ptr;
2258
2259 /* allocate new stack element and hook it in */
2260 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2261 if (ds_new == NULL)
2262 return NULL;
2263
2264 ds_new->next = *stackptr;
2265 *stackptr = ds_new;
2266
2267 /* store directory on the stack */
2268 if (vim_isAbsName(dirbuf)
2269 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002270 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 (*stackptr)->dirname = vim_strsave(dirbuf);
2272 else
2273 {
2274 /* Okay we don't have an absolute path.
2275 * dirbuf must be a subdir of one of the directories on the stack.
2276 * Let's search...
2277 */
2278 ds_new = (*stackptr)->next;
2279 (*stackptr)->dirname = NULL;
2280 while (ds_new)
2281 {
2282 vim_free((*stackptr)->dirname);
2283 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2284 TRUE);
2285 if (mch_isdir((*stackptr)->dirname) == TRUE)
2286 break;
2287
2288 ds_new = ds_new->next;
2289 }
2290
2291 /* clean up all dirs we already left */
2292 while ((*stackptr)->next != ds_new)
2293 {
2294 ds_ptr = (*stackptr)->next;
2295 (*stackptr)->next = (*stackptr)->next->next;
2296 vim_free(ds_ptr->dirname);
2297 vim_free(ds_ptr);
2298 }
2299
2300 /* Nothing found -> it must be on top level */
2301 if (ds_new == NULL)
2302 {
2303 vim_free((*stackptr)->dirname);
2304 (*stackptr)->dirname = vim_strsave(dirbuf);
2305 }
2306 }
2307
2308 if ((*stackptr)->dirname != NULL)
2309 return (*stackptr)->dirname;
2310 else
2311 {
2312 ds_ptr = *stackptr;
2313 *stackptr = (*stackptr)->next;
2314 vim_free(ds_ptr);
2315 return NULL;
2316 }
2317}
2318
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319/*
2320 * pop dirbuf from the directory stack and return previous directory or NULL if
2321 * stack is empty
2322 */
2323 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002324qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
2326 struct dir_stack_T *ds_ptr;
2327
2328 /* TODO: Should we check if dirbuf is the directory on top of the stack?
2329 * What to do if it isn't? */
2330
2331 /* pop top element and free it */
2332 if (*stackptr != NULL)
2333 {
2334 ds_ptr = *stackptr;
2335 *stackptr = (*stackptr)->next;
2336 vim_free(ds_ptr->dirname);
2337 vim_free(ds_ptr);
2338 }
2339
2340 /* return NEW top element as current dir or NULL if stack is empty*/
2341 return *stackptr ? (*stackptr)->dirname : NULL;
2342}
2343
2344/*
2345 * clean up directory stack
2346 */
2347 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002348qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349{
2350 struct dir_stack_T *ds_ptr;
2351
2352 while ((ds_ptr = *stackptr) != NULL)
2353 {
2354 *stackptr = (*stackptr)->next;
2355 vim_free(ds_ptr->dirname);
2356 vim_free(ds_ptr);
2357 }
2358}
2359
2360/*
2361 * Check in which directory of the directory stack the given file can be
2362 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002363 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 * Cleans up intermediate directory entries.
2365 *
2366 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002367 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 * ./
2369 * ./aa
2370 * ./aa/bb
2371 * ./bb
2372 * ./bb/x.c
2373 * and make says:
2374 * making all in aa
2375 * making all in bb
2376 * x.c:9: Error
2377 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2378 * qf_guess_filepath will return NULL.
2379 */
2380 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002381qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382{
2383 struct dir_stack_T *ds_ptr;
2384 struct dir_stack_T *ds_tmp;
2385 char_u *fullname;
2386
2387 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002388 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 return NULL;
2390
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002391 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 fullname = NULL;
2393 while (ds_ptr)
2394 {
2395 vim_free(fullname);
2396 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2397
2398 /* If concat_fnames failed, just go on. The worst thing that can happen
2399 * is that we delete the entire stack.
2400 */
2401 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2402 break;
2403
2404 ds_ptr = ds_ptr->next;
2405 }
2406
2407 vim_free(fullname);
2408
2409 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002410 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002412 ds_tmp = qfl->qf_dir_stack->next;
2413 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 vim_free(ds_tmp->dirname);
2415 vim_free(ds_tmp);
2416 }
2417
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002418 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419}
2420
2421/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002422 * Returns TRUE if a quickfix/location list with the given identifier exists.
2423 */
2424 static int
2425qflist_valid (win_T *wp, int_u qf_id)
2426{
2427 qf_info_T *qi = &ql_info;
2428 int i;
2429
2430 if (wp != NULL)
2431 {
2432 qi = GET_LOC_LIST(wp); /* Location list */
2433 if (qi == NULL)
2434 return FALSE;
2435 }
2436
2437 for (i = 0; i < qi->qf_listcount; ++i)
2438 if (qi->qf_lists[i].qf_id == qf_id)
2439 return TRUE;
2440
2441 return FALSE;
2442}
2443
2444/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002445 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002446 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002447 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002448 * Similar to location list.
2449 */
2450 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002451is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002452{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002453 qfline_T *qfp;
2454 int i;
2455
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002456 /* Search for the entry in the current list */
2457 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2458 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002459 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002460 break;
2461
2462 if (i == qfl->qf_count) /* Entry is not found */
2463 return FALSE;
2464
2465 return TRUE;
2466}
2467
2468/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002469 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002470 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002471 */
2472 static qfline_T *
2473get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002474 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002475 qfline_T *qf_ptr,
2476 int *qf_index,
2477 int dir)
2478{
2479 int idx;
2480 int old_qf_fnum;
2481
2482 idx = *qf_index;
2483 old_qf_fnum = qf_ptr->qf_fnum;
2484
2485 do
2486 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002487 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002488 return NULL;
2489 ++idx;
2490 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002491 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002492 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2493
2494 *qf_index = idx;
2495 return qf_ptr;
2496}
2497
2498/*
2499 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002500 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002501 */
2502 static qfline_T *
2503get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002504 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002505 qfline_T *qf_ptr,
2506 int *qf_index,
2507 int dir)
2508{
2509 int idx;
2510 int old_qf_fnum;
2511
2512 idx = *qf_index;
2513 old_qf_fnum = qf_ptr->qf_fnum;
2514
2515 do
2516 {
2517 if (idx == 1 || qf_ptr->qf_prev == NULL)
2518 return NULL;
2519 --idx;
2520 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002521 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002522 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2523
2524 *qf_index = idx;
2525 return qf_ptr;
2526}
2527
2528/*
2529 * Get the n'th (errornr) previous/next valid entry from the current entry in
2530 * the quickfix list.
2531 * dir == FORWARD or FORWARD_FILE: next valid entry
2532 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2533 */
2534 static qfline_T *
2535get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002536 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002537 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002538 int dir,
2539 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002540{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002541 qfline_T *qf_ptr = qfl->qf_ptr;
2542 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002543 qfline_T *prev_qf_ptr;
2544 int prev_index;
2545 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2546 char_u *err = e_no_more_items;
2547
2548 while (errornr--)
2549 {
2550 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002551 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002552
2553 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002554 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002555 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002556 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002557 if (qf_ptr == NULL)
2558 {
2559 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002560 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002561 if (err != NULL)
2562 {
2563 EMSG(_(err));
2564 return NULL;
2565 }
2566 break;
2567 }
2568
2569 err = NULL;
2570 }
2571
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002572 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002573 return qf_ptr;
2574}
2575
2576/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002577 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2578 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002579 */
2580 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002581get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002582{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002583 qfline_T *qf_ptr = qfl->qf_ptr;
2584 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002585
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002586 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002587 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2588 {
2589 --qf_idx;
2590 qf_ptr = qf_ptr->qf_prev;
2591 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002592 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002593 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2594 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002595 {
2596 ++qf_idx;
2597 qf_ptr = qf_ptr->qf_next;
2598 }
2599
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002600 *new_qfidx = qf_idx;
2601 return qf_ptr;
2602}
2603
2604/*
2605 * Get a entry specied by 'errornr' and 'dir' from the current
2606 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2607 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2608 * Returns a pointer to the entry and the index of the new entry is stored in
2609 * 'new_qfidx'.
2610 */
2611 static qfline_T *
2612qf_get_entry(
2613 qf_list_T *qfl,
2614 int errornr,
2615 int dir,
2616 int *new_qfidx)
2617{
2618 qfline_T *qf_ptr = qfl->qf_ptr;
2619 int qfidx = qfl->qf_index;
2620
2621 if (dir != 0) // next/prev valid entry
2622 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2623 else if (errornr != 0) // go to specified number
2624 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2625
2626 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002627 return qf_ptr;
2628}
2629
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002630/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002631 * Find a window displaying a Vim help file.
2632 */
2633 static win_T *
2634qf_find_help_win(void)
2635{
2636 win_T *wp;
2637
2638 FOR_ALL_WINDOWS(wp)
2639 if (bt_help(wp->w_buffer))
2640 return wp;
2641
2642 return NULL;
2643}
2644
2645/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002646 * Find a help window or open one.
2647 */
2648 static int
2649jump_to_help_window(qf_info_T *qi, int *opened_window)
2650{
2651 win_T *wp;
2652 int flags;
2653
2654 if (cmdmod.tab != 0)
2655 wp = NULL;
2656 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002657 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002658 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2659 win_enter(wp, TRUE);
2660 else
2661 {
2662 /*
2663 * Split off help window; put it at far top if no position
2664 * specified, the current window is vertically split and narrow.
2665 */
2666 flags = WSP_HELP;
2667 if (cmdmod.split == 0 && curwin->w_width != Columns
2668 && curwin->w_width < 80)
2669 flags |= WSP_TOP;
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002670 if (IS_LL_STACK(qi))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002671 flags |= WSP_NEWLOC; /* don't copy the location list */
2672
2673 if (win_split(0, flags) == FAIL)
2674 return FAIL;
2675
2676 *opened_window = TRUE;
2677
2678 if (curwin->w_height < p_hh)
2679 win_setheight((int)p_hh);
2680
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002681 if (IS_LL_STACK(qi)) // not a quickfix list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002682 {
2683 /* The new window should use the supplied location list */
2684 curwin->w_llist = qi;
2685 qi->qf_refcount++;
2686 }
2687 }
2688
2689 if (!p_im)
2690 restart_edit = 0; /* don't want insert mode in help file */
2691
2692 return OK;
2693}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002694
2695/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002696 * Find a non-quickfix window using the given location list.
2697 * Returns NULL if a matching window is not found.
2698 */
2699 static win_T *
2700qf_find_win_with_loclist(qf_info_T *ll)
2701{
2702 win_T *wp;
2703
2704 FOR_ALL_WINDOWS(wp)
2705 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2706 return wp;
2707
2708 return NULL;
2709}
2710
2711/*
2712 * Find a window containing a normal buffer
2713 */
2714 static win_T *
2715qf_find_win_with_normal_buf(void)
2716{
2717 win_T *wp;
2718
2719 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002720 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002721 return wp;
2722
2723 return NULL;
2724}
2725
2726/*
2727 * Go to a window in any tabpage containing the specified file. Returns TRUE
2728 * if successfully jumped to the window. Otherwise returns FALSE.
2729 */
2730 static int
2731qf_goto_tabwin_with_file(int fnum)
2732{
2733 tabpage_T *tp;
2734 win_T *wp;
2735
2736 FOR_ALL_TAB_WINDOWS(tp, wp)
2737 if (wp->w_buffer->b_fnum == fnum)
2738 {
2739 goto_tabpage_win(tp, wp);
2740 return TRUE;
2741 }
2742
2743 return FALSE;
2744}
2745
2746/*
2747 * Create a new window to show a file above the quickfix window. Called when
2748 * only the quickfix window is present.
2749 */
2750 static int
2751qf_open_new_file_win(qf_info_T *ll_ref)
2752{
2753 int flags;
2754
2755 flags = WSP_ABOVE;
2756 if (ll_ref != NULL)
2757 flags |= WSP_NEWLOC;
2758 if (win_split(0, flags) == FAIL)
2759 return FAIL; /* not enough room for window */
2760 p_swb = empty_option; /* don't split again */
2761 swb_flags = 0;
2762 RESET_BINDING(curwin);
2763 if (ll_ref != NULL)
2764 {
2765 /* The new window should use the location list from the
2766 * location list window */
2767 curwin->w_llist = ll_ref;
2768 ll_ref->qf_refcount++;
2769 }
2770 return OK;
2771}
2772
2773/*
2774 * Go to a window that shows the right buffer. If the window is not found, go
2775 * to the window just above the location list window. This is used for opening
2776 * a file from a location window and not from a quickfix window. If some usable
2777 * window is previously found, then it is supplied in 'use_win'.
2778 */
2779 static void
2780qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2781{
2782 win_T *win = use_win;
2783
2784 if (win == NULL)
2785 {
2786 /* Find the window showing the selected file */
2787 FOR_ALL_WINDOWS(win)
2788 if (win->w_buffer->b_fnum == qf_fnum)
2789 break;
2790 if (win == NULL)
2791 {
2792 /* Find a previous usable window */
2793 win = curwin;
2794 do
2795 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002796 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002797 break;
2798 if (win->w_prev == NULL)
2799 win = lastwin; /* wrap around the top */
2800 else
2801 win = win->w_prev; /* go to previous window */
2802 } while (win != curwin);
2803 }
2804 }
2805 win_goto(win);
2806
2807 /* If the location list for the window is not set, then set it
2808 * to the location list from the location window */
2809 if (win->w_llist == NULL)
2810 {
2811 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002812 if (ll_ref != NULL)
2813 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002814 }
2815}
2816
2817/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002818 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2819 * not found, then go to the window just above the quickfix window. This is
2820 * used for opening a file from a quickfix window and not from a location
2821 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002822 */
2823 static void
2824qf_goto_win_with_qfl_file(int qf_fnum)
2825{
2826 win_T *win;
2827 win_T *altwin;
2828
2829 win = curwin;
2830 altwin = NULL;
2831 for (;;)
2832 {
2833 if (win->w_buffer->b_fnum == qf_fnum)
2834 break;
2835 if (win->w_prev == NULL)
2836 win = lastwin; /* wrap around the top */
2837 else
2838 win = win->w_prev; /* go to previous window */
2839
2840 if (IS_QF_WINDOW(win))
2841 {
2842 /* Didn't find it, go to the window before the quickfix
2843 * window. */
2844 if (altwin != NULL)
2845 win = altwin;
2846 else if (curwin->w_prev != NULL)
2847 win = curwin->w_prev;
2848 else
2849 win = curwin->w_next;
2850 break;
2851 }
2852
2853 /* Remember a usable window. */
Bram Moolenaar91335e52018-08-01 17:53:12 +02002854 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002855 altwin = win;
2856 }
2857
2858 win_goto(win);
2859}
2860
2861/*
2862 * Find a suitable window for opening a file (qf_fnum) from the
2863 * quickfix/location list and jump to it. If the file is already opened in a
2864 * window, jump to it. Otherwise open a new window to display the file. This is
2865 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002866 */
2867 static int
2868qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2869{
2870 win_T *usable_win_ptr = NULL;
2871 int usable_win;
2872 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002873 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002874
2875 usable_win = 0;
2876
2877 ll_ref = curwin->w_llist_ref;
2878 if (ll_ref != NULL)
2879 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002880 /* Find a non-quickfix window with this location list */
2881 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2882 if (usable_win_ptr != NULL)
2883 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002884 }
2885
2886 if (!usable_win)
2887 {
2888 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002889 win = qf_find_win_with_normal_buf();
2890 if (win != NULL)
2891 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002892 }
2893
2894 /*
2895 * If no usable window is found and 'switchbuf' contains "usetab"
2896 * then search in other tabs.
2897 */
2898 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002899 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002900
2901 /*
2902 * If there is only one window and it is the quickfix window, create a
2903 * new one above the quickfix window.
2904 */
2905 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2906 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002907 if (qf_open_new_file_win(ll_ref) != OK)
2908 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002909 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002910 }
2911 else
2912 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002913 if (curwin->w_llist_ref != NULL) /* In a location window */
2914 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2915 else /* In a quickfix window */
2916 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002917 }
2918
2919 return OK;
2920}
2921
2922/*
2923 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002924 * Returns OK if successfully edited the file, FAIL on failing to open the
2925 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
2926 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002927 */
2928 static int
2929qf_jump_edit_buffer(
2930 qf_info_T *qi,
2931 qfline_T *qf_ptr,
2932 int forceit,
2933 win_T *oldwin,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002934 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002935{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002936 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002937 int retval = OK;
2938
2939 if (qf_ptr->qf_type == 1)
2940 {
2941 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2942 * set b_p_ro flag). */
2943 if (!can_abandon(curbuf, forceit))
2944 {
2945 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002946 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002947 }
2948 else
2949 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2950 ECMD_HIDE + ECMD_SET_HELP,
2951 oldwin == curwin ? curwin : NULL);
2952 }
2953 else
2954 {
2955 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002956 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002957
2958 retval = buflist_getfile(qf_ptr->qf_fnum,
2959 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002960
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002961 if (IS_LL_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002962 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002963 /*
2964 * Location list. Check whether the associated window is still
2965 * present and the list is still valid.
2966 */
2967 if (!win_valid_any_tab(oldwin))
2968 {
2969 EMSG(_("E924: Current window was closed"));
Bram Moolenaar3c097222017-12-21 20:54:49 +01002970 *opened_window = FALSE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002971 return NOTDONE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002972 }
2973 else if (!qflist_valid(oldwin, save_qfid))
2974 {
2975 EMSG(_(e_loc_list_changed));
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002976 return NOTDONE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002977 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002978 }
2979 else if (old_qf_curlist != qi->qf_curlist
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002980 || !is_qf_entry_present(qfl, qf_ptr))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002981 {
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002982 if (IS_QF_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002983 EMSG(_("E925: Current quickfix was changed"));
2984 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002985 EMSG(_(e_loc_list_changed));
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002986 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002987 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002988 }
2989
2990 return retval;
2991}
2992
2993/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002994 * Go to the error line in the current file using either line/column number or
2995 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002996 */
2997 static void
2998qf_jump_goto_line(
2999 linenr_T qf_lnum,
3000 int qf_col,
3001 char_u qf_viscol,
3002 char_u *qf_pattern)
3003{
3004 linenr_T i;
3005 char_u *line;
3006 colnr_T screen_col;
3007 colnr_T char_col;
3008
3009 if (qf_pattern == NULL)
3010 {
3011 /*
3012 * Go to line with error, unless qf_lnum is 0.
3013 */
3014 i = qf_lnum;
3015 if (i > 0)
3016 {
3017 if (i > curbuf->b_ml.ml_line_count)
3018 i = curbuf->b_ml.ml_line_count;
3019 curwin->w_cursor.lnum = i;
3020 }
3021 if (qf_col > 0)
3022 {
3023 curwin->w_cursor.col = qf_col - 1;
3024#ifdef FEAT_VIRTUALEDIT
3025 curwin->w_cursor.coladd = 0;
3026#endif
3027 if (qf_viscol == TRUE)
3028 {
3029 /*
3030 * Check each character from the beginning of the error
3031 * line up to the error column. For each tab character
3032 * found, reduce the error column value by the length of
3033 * a tab character.
3034 */
3035 line = ml_get_curline();
3036 screen_col = 0;
3037 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3038 {
3039 if (*line == NUL)
3040 break;
3041 if (*line++ == '\t')
3042 {
3043 curwin->w_cursor.col -= 7 - (screen_col % 8);
3044 screen_col += 8 - (screen_col % 8);
3045 }
3046 else
3047 ++screen_col;
3048 }
3049 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003050 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003051 check_cursor();
3052 }
3053 else
3054 beginline(BL_WHITE | BL_FIX);
3055 }
3056 else
3057 {
3058 pos_T save_cursor;
3059
3060 /* Move the cursor to the first line in the buffer */
3061 save_cursor = curwin->w_cursor;
3062 curwin->w_cursor.lnum = 0;
3063 if (!do_search(NULL, '/', qf_pattern, (long)1,
3064 SEARCH_KEEP, NULL, NULL))
3065 curwin->w_cursor = save_cursor;
3066 }
3067}
3068
3069/*
3070 * Display quickfix list index and size message
3071 */
3072 static void
3073qf_jump_print_msg(
3074 qf_info_T *qi,
3075 int qf_index,
3076 qfline_T *qf_ptr,
3077 buf_T *old_curbuf,
3078 linenr_T old_lnum)
3079{
3080 linenr_T i;
3081 int len;
3082
3083 /* Update the screen before showing the message, unless the screen
3084 * scrolled up. */
3085 if (!msg_scrolled)
3086 update_topline_redraw();
3087 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3088 qi->qf_lists[qi->qf_curlist].qf_count,
3089 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3090 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
3091 /* Add the message, skipping leading whitespace and newlines. */
3092 len = (int)STRLEN(IObuff);
3093 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3094
3095 /* Output the message. Overwrite to avoid scrolling when the 'O'
3096 * flag is present in 'shortmess'; But when not jumping, print the
3097 * whole message. */
3098 i = msg_scroll;
3099 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3100 msg_scroll = TRUE;
3101 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3102 msg_scroll = FALSE;
3103 msg_attr_keep(IObuff, 0, TRUE);
3104 msg_scroll = i;
3105}
3106
3107/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003108 * Find a usable window for opening a file from the quickfix/location list. If
3109 * a window is not found then open a new window.
3110 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3111 * able to jump/open a window. Returns NOTDONE if a file is not associated
3112 * with the entry.
3113 */
3114 static int
3115qf_jump_open_window(qf_info_T *qi, qfline_T *qf_ptr, int *opened_window)
3116{
3117 // For ":helpgrep" find a help window or open one.
3118 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
3119 if (jump_to_help_window(qi, opened_window) == FAIL)
3120 return FAIL;
3121
3122 // If currently in the quickfix window, find another window to show the
3123 // file in.
3124 if (bt_quickfix(curbuf) && !*opened_window)
3125 {
3126 // If there is no file specified, we don't know where to go.
3127 // But do advance, otherwise ":cn" gets stuck.
3128 if (qf_ptr->qf_fnum == 0)
3129 return NOTDONE;
3130
3131 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, opened_window) == FAIL)
3132 return FAIL;
3133 }
3134
3135 return OK;
3136}
3137
3138/*
3139 * Edit a selected file from the quickfix/location list and jump to a
3140 * particular line/column, adjust the folds and display a message about the
3141 * jump.
3142 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3143 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3144 * the file.
3145 */
3146 static int
3147qf_jump_to_buffer(
3148 qf_info_T *qi,
3149 int qf_index,
3150 qfline_T *qf_ptr,
3151 int forceit,
3152 win_T *oldwin,
3153 int *opened_window,
3154 int openfold,
3155 int print_message)
3156{
3157 buf_T *old_curbuf;
3158 linenr_T old_lnum;
3159 int retval = OK;
3160
3161 // If there is a file name, read the wanted file if needed, and check
3162 // autowrite etc.
3163 old_curbuf = curbuf;
3164 old_lnum = curwin->w_cursor.lnum;
3165
3166 if (qf_ptr->qf_fnum != 0)
3167 {
3168 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3169 opened_window);
3170 if (retval != OK)
3171 return retval;
3172 }
3173
3174 // When not switched to another buffer, still need to set pc mark
3175 if (curbuf == old_curbuf)
3176 setpcmark();
3177
3178 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3179 qf_ptr->qf_pattern);
3180
3181#ifdef FEAT_FOLDING
3182 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3183 foldOpenCursor();
3184#endif
3185 if (print_message)
3186 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3187
3188 return retval;
3189}
3190
3191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 * jump to a quickfix line
3193 * if dir == FORWARD go "errornr" valid entries forward
3194 * if dir == BACKWARD go "errornr" valid entries backward
3195 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3196 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3197 * else if "errornr" is zero, redisplay the same line
3198 * else go to entry "errornr"
3199 */
3200 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003201qf_jump(qf_info_T *qi,
3202 int dir,
3203 int errornr,
3204 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003206 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003207 qfline_T *qf_ptr;
3208 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003211 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003212 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003214 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003217 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003219 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003221 if (qi == NULL)
3222 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003223
3224 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003225 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
3227 EMSG(_(e_quickfix));
3228 return;
3229 }
3230
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003231 qfl = &qi->qf_lists[qi->qf_curlist];
3232
3233 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003235 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003237
3238 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3239 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003241 qf_ptr = old_qf_ptr;
3242 qf_index = old_qf_index;
3243 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003246 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003247 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003248 // No need to print the error message if it's visible in the error
3249 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 print_message = FALSE;
3251
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003252 retval = qf_jump_open_window(qi, qf_ptr, &opened_window);
3253 if (retval == FAIL)
3254 goto failed;
3255 if (retval == NOTDONE)
3256 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003257
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003258 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3259 &opened_window, old_KeyTyped, print_message);
3260 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003262 // Quickfix/location list is freed by an autocmd
3263 qi = NULL;
3264 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003267 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003270 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003271 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003273 // Couldn't open file, so put index back where it was. This could
3274 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 qf_ptr = old_qf_ptr;
3277 qf_index = old_qf_index;
3278 }
3279 }
3280theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003281 if (qi != NULL)
3282 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003283 qfl->qf_ptr = qf_ptr;
3284 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (p_swb != old_swb && opened_window)
3287 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003288 // Restore old 'switchbuf' value, but not when an autocommand or
3289 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003293 swb_flags = old_swb_flags;
3294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 else
3296 free_string_option(old_swb);
3297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298}
3299
3300/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003301 * Highlight attributes used for displaying entries from the quickfix list.
3302 */
3303static int qfFileAttr;
3304static int qfSepAttr;
3305static int qfLineAttr;
3306
3307/*
3308 * Display information about a single entry from the quickfix/location list.
3309 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003310 * 'cursel' will be set to TRUE for the currently selected entry in the
3311 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003312 */
3313 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003314qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003315{
3316 char_u *fname;
3317 buf_T *buf;
3318 int filter_entry;
3319
3320 fname = NULL;
3321 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3322 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3323 (char *)qfp->qf_module);
3324 else {
3325 if (qfp->qf_fnum != 0
3326 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3327 {
3328 fname = buf->b_fname;
3329 if (qfp->qf_type == 1) /* :helpgrep */
3330 fname = gettail(fname);
3331 }
3332 if (fname == NULL)
3333 sprintf((char *)IObuff, "%2d", qf_idx);
3334 else
3335 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3336 qf_idx, (char *)fname);
3337 }
3338
3339 // Support for filtering entries using :filter /pat/ clist
3340 // Match against the module name, file name, search pattern and
3341 // text of the entry.
3342 filter_entry = TRUE;
3343 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3344 filter_entry &= message_filtered(qfp->qf_module);
3345 if (filter_entry && fname != NULL)
3346 filter_entry &= message_filtered(fname);
3347 if (filter_entry && qfp->qf_pattern != NULL)
3348 filter_entry &= message_filtered(qfp->qf_pattern);
3349 if (filter_entry)
3350 filter_entry &= message_filtered(qfp->qf_text);
3351 if (filter_entry)
3352 return;
3353
3354 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003355 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003356
3357 if (qfp->qf_lnum != 0)
3358 msg_puts_attr((char_u *)":", qfSepAttr);
3359 if (qfp->qf_lnum == 0)
3360 IObuff[0] = NUL;
3361 else if (qfp->qf_col == 0)
3362 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3363 else
3364 sprintf((char *)IObuff, "%ld col %d",
3365 qfp->qf_lnum, qfp->qf_col);
3366 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3367 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3368 msg_puts_attr(IObuff, qfLineAttr);
3369 msg_puts_attr((char_u *)":", qfSepAttr);
3370 if (qfp->qf_pattern != NULL)
3371 {
3372 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3373 msg_puts(IObuff);
3374 msg_puts_attr((char_u *)":", qfSepAttr);
3375 }
3376 msg_puts((char_u *)" ");
3377
3378 /* Remove newlines and leading whitespace from the text. For an
3379 * unrecognized line keep the indent, the compiler may mark a word
3380 * with ^^^^. */
3381 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3382 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3383 IObuff, IOSIZE);
3384 msg_prt_line(IObuff, FALSE);
3385 out_flush(); /* show one line at a time */
3386}
3387
3388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003390 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 */
3392 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003393qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003395 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003396 qfline_T *qfp;
3397 int i;
3398 int idx1 = 1;
3399 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003400 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003401 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003402 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003404 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaar39665952018-08-15 20:59:48 +02003406 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003407 {
3408 qi = GET_LOC_LIST(curwin);
3409 if (qi == NULL)
3410 {
3411 EMSG(_(e_loclist));
3412 return;
3413 }
3414 }
3415
3416 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003417 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 {
3419 EMSG(_(e_quickfix));
3420 return;
3421 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003422 if (*arg == '+')
3423 {
3424 ++arg;
3425 plus = TRUE;
3426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3428 {
3429 EMSG(_(e_trailing));
3430 return;
3431 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003432 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003433 if (plus)
3434 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003435 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003436 idx2 = i + idx1;
3437 idx1 = i;
3438 }
3439 else
3440 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003441 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003442 if (idx1 < 0)
3443 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3444 if (idx2 < 0)
3445 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
Bram Moolenaara796d462018-05-01 14:30:36 +02003448 /* Shorten all the file names, so that it is easy to read */
3449 shorten_fnames(FALSE);
3450
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003451 /*
3452 * Get the attributes for the different quickfix highlight items. Note
3453 * that this depends on syntax items defined in the qf.vim syntax file
3454 */
3455 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3456 if (qfFileAttr == 0)
3457 qfFileAttr = HL_ATTR(HLF_D);
3458 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3459 if (qfSepAttr == 0)
3460 qfSepAttr = HL_ATTR(HLF_D);
3461 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3462 if (qfLineAttr == 0)
3463 qfLineAttr = HL_ATTR(HLF_N);
3464
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003465 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003467 qfp = qfl->qf_start;
3468 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3471 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003472 if (got_int)
3473 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003474
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003475 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003477
3478 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003479 if (qfp == NULL)
3480 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003481 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 ui_breakcheck();
3483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484}
3485
3486/*
3487 * Remove newlines and leading whitespace from an error message.
3488 * Put the result in "buf[bufsize]".
3489 */
3490 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003491qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492{
3493 int i;
3494 char_u *p = text;
3495
3496 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3497 {
3498 if (*p == '\n')
3499 {
3500 buf[i] = ' ';
3501 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003502 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 break;
3504 }
3505 else
3506 buf[i] = *p++;
3507 }
3508 buf[i] = NUL;
3509}
3510
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003511/*
3512 * Display information (list number, list size and the title) about a
3513 * quickfix/location list.
3514 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003515 static void
3516qf_msg(qf_info_T *qi, int which, char *lead)
3517{
3518 char *title = (char *)qi->qf_lists[which].qf_title;
3519 int count = qi->qf_lists[which].qf_count;
3520 char_u buf[IOSIZE];
3521
3522 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3523 lead,
3524 which + 1,
3525 qi->qf_listcount,
3526 count);
3527
3528 if (title != NULL)
3529 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003530 size_t len = STRLEN(buf);
3531
3532 if (len < 34)
3533 {
3534 vim_memset(buf + len, ' ', 34 - len);
3535 buf[34] = NUL;
3536 }
3537 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003538 }
3539 trunc_string(buf, buf, Columns - 1, IOSIZE);
3540 msg(buf);
3541}
3542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543/*
3544 * ":colder [count]": Up in the quickfix stack.
3545 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003546 * ":lolder [count]": Up in the location list stack.
3547 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 */
3549 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003550qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003552 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 int count;
3554
Bram Moolenaar39665952018-08-15 20:59:48 +02003555 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003556 {
3557 qi = GET_LOC_LIST(curwin);
3558 if (qi == NULL)
3559 {
3560 EMSG(_(e_loclist));
3561 return;
3562 }
3563 }
3564
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 if (eap->addr_count != 0)
3566 count = eap->line2;
3567 else
3568 count = 1;
3569 while (count--)
3570 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003571 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003573 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
3575 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003576 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003578 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
3580 else
3581 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003582 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003585 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003587 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
3589 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003590 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003591 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592}
3593
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003594/*
3595 * Display the information about all the quickfix/location lists in the stack
3596 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003597 void
3598qf_history(exarg_T *eap)
3599{
3600 qf_info_T *qi = &ql_info;
3601 int i;
3602
Bram Moolenaar39665952018-08-15 20:59:48 +02003603 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003604 qi = GET_LOC_LIST(curwin);
3605 if (qi == NULL || (qi->qf_listcount == 0
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003606 && qf_list_empty(qi, qi->qf_curlist)))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003607 MSG(_("No entries"));
3608 else
3609 for (i = 0; i < qi->qf_listcount; ++i)
3610 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3611}
3612
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003614 * Free all the entries in the error list "idx". Note that other information
3615 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 */
3617 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003618qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003620 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003621 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003622 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003624 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003626 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003627 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003628 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003629 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003630 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003631 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003632 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003633 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003634 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003635 if (stop)
3636 /* Somehow qf_count may have an incorrect value, set it to 1
3637 * to avoid crashing when it's wrong.
3638 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003639 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003640 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003641 qfl->qf_start = qfpnext;
3642 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003644
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003645 qfl->qf_index = 0;
3646 qfl->qf_start = NULL;
3647 qfl->qf_last = NULL;
3648 qfl->qf_ptr = NULL;
3649 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003650
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003651 qf_clean_dir_stack(&qfl->qf_dir_stack);
3652 qfl->qf_directory = NULL;
3653 qf_clean_dir_stack(&qfl->qf_file_stack);
3654 qfl->qf_currfile = NULL;
3655 qfl->qf_multiline = FALSE;
3656 qfl->qf_multiignore = FALSE;
3657 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658}
3659
3660/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003661 * Free error list "idx". Frees all the entries in the quickfix list,
3662 * associated context information and the title.
3663 */
3664 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003665qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003666{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003667 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003668
Bram Moolenaard23a8232018-02-10 18:45:26 +01003669 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003670 free_tv(qfl->qf_ctx);
3671 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003672 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003673 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003674}
3675
3676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 * qf_mark_adjust: adjust marks
3678 */
3679 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003680qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003681 win_T *wp,
3682 linenr_T line1,
3683 linenr_T line2,
3684 long amount,
3685 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003687 int i;
3688 qfline_T *qfp;
3689 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003690 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003691 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003692 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
Bram Moolenaarc1542742016-07-20 21:44:37 +02003694 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003695 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003696 if (wp != NULL)
3697 {
3698 if (wp->w_llist == NULL)
3699 return;
3700 qi = wp->w_llist;
3701 }
3702
3703 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003704 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003705 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003706 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3707 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 if (qfp->qf_fnum == curbuf->b_fnum)
3709 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003710 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3712 {
3713 if (amount == MAXLNUM)
3714 qfp->qf_cleared = TRUE;
3715 else
3716 qfp->qf_lnum += amount;
3717 }
3718 else if (amount_after && qfp->qf_lnum > line2)
3719 qfp->qf_lnum += amount_after;
3720 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003721
3722 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003723 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724}
3725
3726/*
3727 * Make a nice message out of the error character and the error number:
3728 * char number message
3729 * e or E 0 " error"
3730 * w or W 0 " warning"
3731 * i or I 0 " info"
3732 * 0 0 ""
3733 * other 0 " c"
3734 * e or E n " error n"
3735 * w or W n " warning n"
3736 * i or I n " info n"
3737 * 0 n " error n"
3738 * other n " c n"
3739 * 1 x "" :helpgrep
3740 */
3741 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003742qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
3744 static char_u buf[20];
3745 static char_u cc[3];
3746 char_u *p;
3747
3748 if (c == 'W' || c == 'w')
3749 p = (char_u *)" warning";
3750 else if (c == 'I' || c == 'i')
3751 p = (char_u *)" info";
3752 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3753 p = (char_u *)" error";
3754 else if (c == 0 || c == 1)
3755 p = (char_u *)"";
3756 else
3757 {
3758 cc[0] = ' ';
3759 cc[1] = c;
3760 cc[2] = NUL;
3761 p = cc;
3762 }
3763
3764 if (nr <= 0)
3765 return p;
3766
3767 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3768 return buf;
3769}
3770
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003772 * When "split" is FALSE: Open the entry/result under the cursor.
3773 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3774 */
3775 void
3776qf_view_result(int split)
3777{
3778 qf_info_T *qi = &ql_info;
3779
3780 if (!bt_quickfix(curbuf))
3781 return;
3782
3783 if (IS_LL_WINDOW(curwin))
3784 qi = GET_LOC_LIST(curwin);
3785
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003786 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003787 {
3788 EMSG(_(e_quickfix));
3789 return;
3790 }
3791
3792 if (split)
3793 {
3794 char_u cmd[32];
3795
3796 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3797 (long)curwin->w_cursor.lnum,
3798 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3799 if (do_cmdline_cmd(cmd) == OK)
3800 do_cmdline_cmd((char_u *) "clearjumps");
3801 return;
3802 }
3803
3804 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3805}
3806
3807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 * ":cwindow": open the quickfix window if we have errors to display,
3809 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003810 * ":lwindow": open the location list window if we have locations to display,
3811 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 */
3813 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003814ex_cwindow(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 win_T *win;
3818
Bram Moolenaar39665952018-08-15 20:59:48 +02003819 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003820 {
3821 qi = GET_LOC_LIST(curwin);
3822 if (qi == NULL)
3823 return;
3824 }
3825
3826 /* Look for an existing quickfix window. */
3827 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
3829 /*
3830 * If a quickfix window is open but we have no errors to display,
3831 * close the window. If a quickfix window is not open, then open
3832 * it if we have errors; otherwise, leave it closed.
3833 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003834 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003835 || qf_list_empty(qi, qi->qf_curlist)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003836 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 {
3838 if (win != NULL)
3839 ex_cclose(eap);
3840 }
3841 else if (win == NULL)
3842 ex_copen(eap);
3843}
3844
3845/*
3846 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003847 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003850ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003852 win_T *win = NULL;
3853 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
Bram Moolenaar39665952018-08-15 20:59:48 +02003855 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003856 {
3857 qi = GET_LOC_LIST(curwin);
3858 if (qi == NULL)
3859 return;
3860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003862 /* Find existing quickfix window and close it. */
3863 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 if (win != NULL)
3865 win_close(win, FALSE);
3866}
3867
3868/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003869 * Set "w:quickfix_title" if "qi" has a title.
3870 */
3871 static void
3872qf_set_title_var(qf_list_T *qfl)
3873{
3874 if (qfl->qf_title != NULL)
3875 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3876}
3877
3878/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003879 * Goto a quickfix or location list window (if present).
3880 * Returns OK if the window is found, FAIL otherwise.
3881 */
3882 static int
3883qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3884{
3885 win_T *win;
3886
3887 win = qf_find_win(qi);
3888 if (win == NULL)
3889 return FAIL;
3890
3891 win_goto(win);
3892 if (resize)
3893 {
3894 if (vertsplit)
3895 {
3896 if (sz != win->w_width)
3897 win_setwidth(sz);
3898 }
3899 else if (sz != win->w_height)
3900 win_setheight(sz);
3901 }
3902
3903 return OK;
3904}
3905
3906/*
3907 * Open a new quickfix or location list window, load the quickfix buffer and
3908 * set the appropriate options for the window.
3909 * Returns FAIL if the window could not be opened.
3910 */
3911 static int
3912qf_open_new_cwindow(qf_info_T *qi, int height)
3913{
3914 buf_T *qf_buf;
3915 win_T *oldwin = curwin;
3916 tabpage_T *prevtab = curtab;
3917 int flags = 0;
3918 win_T *win;
3919
3920 qf_buf = qf_find_buf(qi);
3921
3922 // The current window becomes the previous window afterwards.
3923 win = curwin;
3924
3925 if (IS_QF_STACK(qi) && cmdmod.split == 0)
3926 // Create the new quickfix window at the very bottom, except when
3927 // :belowright or :aboveleft is used.
3928 win_goto(lastwin);
3929 // Default is to open the window below the current window
3930 if (cmdmod.split == 0)
3931 flags = WSP_BELOW;
3932 flags |= WSP_NEWLOC;
3933 if (win_split(height, flags) == FAIL)
3934 return FAIL; // not enough room for window
3935 RESET_BINDING(curwin);
3936
3937 if (IS_LL_STACK(qi))
3938 {
3939 // For the location list window, create a reference to the
3940 // location list from the window 'win'.
3941 curwin->w_llist_ref = win->w_llist;
3942 win->w_llist->qf_refcount++;
3943 }
3944
3945 if (oldwin != curwin)
3946 oldwin = NULL; // don't store info when in another window
3947 if (qf_buf != NULL)
3948 {
3949 // Use the existing quickfix buffer
3950 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
3951 ECMD_HIDE + ECMD_OLDBUF, oldwin);
3952 }
3953 else
3954 {
3955 // Create a new quickfix buffer
3956 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
3957
3958 // switch off 'swapfile'
3959 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3960 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
3961 OPT_LOCAL);
3962 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
3963 RESET_BINDING(curwin);
3964#ifdef FEAT_DIFF
3965 curwin->w_p_diff = FALSE;
3966#endif
3967#ifdef FEAT_FOLDING
3968 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3969 OPT_LOCAL);
3970#endif
3971 }
3972
3973 // Only set the height when still in the same tab page and there is no
3974 // window to the side.
3975 if (curtab == prevtab && curwin->w_width == Columns)
3976 win_setheight(height);
3977 curwin->w_p_wfh = TRUE; // set 'winfixheight'
3978 if (win_valid(win))
3979 prevwin = win;
3980
3981 return OK;
3982}
3983
3984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003986 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 */
3988 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003989ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003991 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003993 int status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
Bram Moolenaar39665952018-08-15 20:59:48 +02003995 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003996 {
3997 qi = GET_LOC_LIST(curwin);
3998 if (qi == NULL)
3999 {
4000 EMSG(_(e_loclist));
4001 return;
4002 }
4003 }
4004
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 if (eap->addr_count != 0)
4006 height = eap->line2;
4007 else
4008 height = QF_WINHEIGHT;
4009
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004010 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#ifdef FEAT_GUI
4012 need_mouse_correct = TRUE;
4013#endif
4014
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004015 // Find an existing quickfix window, or open a new one.
4016 if (cmdmod.tab == 0)
4017 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4018 cmdmod.split & WSP_VERT);
4019 if (status == FAIL)
4020 if (qf_open_new_cwindow(qi, height) == FAIL)
4021 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004023 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004024
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004025 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004026 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004028 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 curwin->w_cursor.col = 0;
4030 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004031 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032}
4033
4034/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004035 * Move the cursor in the quickfix window to "lnum".
4036 */
4037 static void
4038qf_win_goto(win_T *win, linenr_T lnum)
4039{
4040 win_T *old_curwin = curwin;
4041
4042 curwin = win;
4043 curbuf = win->w_buffer;
4044 curwin->w_cursor.lnum = lnum;
4045 curwin->w_cursor.col = 0;
4046#ifdef FEAT_VIRTUALEDIT
4047 curwin->w_cursor.coladd = 0;
4048#endif
4049 curwin->w_curswant = 0;
4050 update_topline(); /* scroll to show the line */
4051 redraw_later(VALID);
4052 curwin->w_redr_status = TRUE; /* update ruler */
4053 curwin = old_curwin;
4054 curbuf = curwin->w_buffer;
4055}
4056
4057/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004058 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004059 */
4060 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004061ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004062{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004063 qf_info_T *qi = &ql_info;
4064 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004065
Bram Moolenaar39665952018-08-15 20:59:48 +02004066 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004067 {
4068 qi = GET_LOC_LIST(curwin);
4069 if (qi == NULL)
4070 {
4071 EMSG(_(e_loclist));
4072 return;
4073 }
4074 }
4075
4076 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004077 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4078 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4079}
4080
4081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 * Return the number of the current entry (line number in the quickfix
4083 * window).
4084 */
4085 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004086qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004088 qf_info_T *qi = &ql_info;
4089
4090 if (IS_LL_WINDOW(wp))
4091 /* In the location list window, use the referenced location list */
4092 qi = wp->w_llist_ref;
4093
4094 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095}
4096
4097/*
4098 * Update the cursor position in the quickfix window to the current error.
4099 * Return TRUE if there is a quickfix window.
4100 */
4101 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004102qf_win_pos_update(
4103 qf_info_T *qi,
4104 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105{
4106 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004107 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108
4109 /*
4110 * Put the cursor on the current error in the quickfix window, so that
4111 * it's viewable.
4112 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004113 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 if (win != NULL
4115 && qf_index <= win->w_buffer->b_ml.ml_line_count
4116 && old_qf_index != qf_index)
4117 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 if (qf_index > old_qf_index)
4119 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004120 win->w_redraw_top = old_qf_index;
4121 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123 else
4124 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004125 win->w_redraw_top = qf_index;
4126 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004128 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130 return win != NULL;
4131}
4132
4133/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004134 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004135 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004136 */
4137 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004138is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004139{
4140 /*
4141 * A window displaying the quickfix buffer will have the w_llist_ref field
4142 * set to NULL.
4143 * A window displaying a location list buffer will have the w_llist_ref
4144 * pointing to the location list.
4145 */
4146 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004147 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4148 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004149 return TRUE;
4150
4151 return FALSE;
4152}
4153
4154/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004155 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004156 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004157 */
4158 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004159qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004160{
4161 win_T *win;
4162
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004163 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004164 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004165 return win;
4166 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004167}
4168
4169/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004170 * Find a quickfix buffer.
4171 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 */
4173 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004174qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004176 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004177 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178
Bram Moolenaar9c102382006-05-03 21:26:49 +00004179 FOR_ALL_TAB_WINDOWS(tp, win)
4180 if (is_qf_win(win, qi))
4181 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004182
Bram Moolenaar9c102382006-05-03 21:26:49 +00004183 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184}
4185
4186/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004187 * Update the w:quickfix_title variable in the quickfix/location list window
4188 */
4189 static void
4190qf_update_win_titlevar(qf_info_T *qi)
4191{
4192 win_T *win;
4193 win_T *curwin_save;
4194
4195 if ((win = qf_find_win(qi)) != NULL)
4196 {
4197 curwin_save = curwin;
4198 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004199 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004200 curwin = curwin_save;
4201 }
4202}
4203
4204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 * Find the quickfix buffer. If it exists, update the contents.
4206 */
4207 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004208qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209{
4210 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004211 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
4214 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004215 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 if (buf != NULL)
4217 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004218 linenr_T old_line_count = buf->b_ml.ml_line_count;
4219
4220 if (old_last == NULL)
4221 /* set curwin/curbuf to buf and save a few things */
4222 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
Bram Moolenaard823fa92016-08-12 16:29:27 +02004224 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004225
Bram Moolenaar864293a2016-06-02 13:40:04 +02004226 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004227 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004228
Bram Moolenaar864293a2016-06-02 13:40:04 +02004229 if (old_last == NULL)
4230 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004231 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004232
4233 /* restore curwin/curbuf and a few other things */
4234 aucmd_restbuf(&aco);
4235 }
4236
4237 /* Only redraw when added lines are visible. This avoids flickering
4238 * when the added lines are not visible. */
4239 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4240 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
4242}
4243
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004244/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004245 * Add an error line to the quickfix buffer.
4246 */
4247 static int
4248qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4249{
4250 int len;
4251 buf_T *errbuf;
4252
4253 if (qfp->qf_module != NULL)
4254 {
4255 STRCPY(IObuff, qfp->qf_module);
4256 len = (int)STRLEN(IObuff);
4257 }
4258 else if (qfp->qf_fnum != 0
4259 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4260 && errbuf->b_fname != NULL)
4261 {
4262 if (qfp->qf_type == 1) /* :helpgrep */
4263 STRCPY(IObuff, gettail(errbuf->b_fname));
4264 else
4265 {
4266 /* shorten the file name if not done already */
4267 if (errbuf->b_sfname == NULL
4268 || mch_isFullName(errbuf->b_sfname))
4269 {
4270 if (*dirname == NUL)
4271 mch_dirname(dirname, MAXPATHL);
4272 shorten_buf_fname(errbuf, dirname, FALSE);
4273 }
4274 STRCPY(IObuff, errbuf->b_fname);
4275 }
4276 len = (int)STRLEN(IObuff);
4277 }
4278 else
4279 len = 0;
4280 IObuff[len++] = '|';
4281
4282 if (qfp->qf_lnum > 0)
4283 {
4284 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4285 len += (int)STRLEN(IObuff + len);
4286
4287 if (qfp->qf_col > 0)
4288 {
4289 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4290 len += (int)STRLEN(IObuff + len);
4291 }
4292
4293 sprintf((char *)IObuff + len, "%s",
4294 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4295 len += (int)STRLEN(IObuff + len);
4296 }
4297 else if (qfp->qf_pattern != NULL)
4298 {
4299 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4300 len += (int)STRLEN(IObuff + len);
4301 }
4302 IObuff[len++] = '|';
4303 IObuff[len++] = ' ';
4304
4305 /* Remove newlines and leading whitespace from the text.
4306 * For an unrecognized line keep the indent, the compiler may
4307 * mark a word with ^^^^. */
4308 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4309 IObuff + len, IOSIZE - len);
4310
4311 if (ml_append_buf(buf, lnum, IObuff,
4312 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4313 return FAIL;
4314
4315 return OK;
4316}
4317
4318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 * Fill current buffer with quickfix errors, replacing any previous contents.
4320 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004321 * If "old_last" is not NULL append the items after this one.
4322 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4323 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 */
4325 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004326qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004328 linenr_T lnum;
4329 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004330 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
Bram Moolenaar864293a2016-06-02 13:40:04 +02004332 if (old_last == NULL)
4333 {
4334 if (buf != curbuf)
4335 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004336 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004337 return;
4338 }
4339
4340 /* delete all existing lines */
4341 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4342 (void)ml_delete((linenr_T)1, FALSE);
4343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
4345 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004346 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 {
Bram Moolenaara796d462018-05-01 14:30:36 +02004348 char_u dirname[MAXPATHL];
4349
4350 *dirname = NUL;
4351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004353 if (old_last == NULL)
4354 {
4355 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4356 lnum = 0;
4357 }
4358 else
4359 {
4360 qfp = old_last->qf_next;
4361 lnum = buf->b_ml.ml_line_count;
4362 }
4363 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004365 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004367
Bram Moolenaar864293a2016-06-02 13:40:04 +02004368 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004370 if (qfp == NULL)
4371 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004373
4374 if (old_last == NULL)
4375 /* Delete the empty line which is now at the end */
4376 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 }
4378
4379 /* correct cursor position */
4380 check_lnums(TRUE);
4381
Bram Moolenaar864293a2016-06-02 13:40:04 +02004382 if (old_last == NULL)
4383 {
4384 /* Set the 'filetype' to "qf" each time after filling the buffer.
4385 * This resembles reading a file into a buffer, it's more logical when
4386 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02004387 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004388 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4389 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
Bram Moolenaar864293a2016-06-02 13:40:04 +02004391 keep_filetype = TRUE; /* don't detect 'filetype' */
4392 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004394 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004396 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004397 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004398
Bram Moolenaar864293a2016-06-02 13:40:04 +02004399 /* make sure it will be redrawn */
4400 redraw_curbuf_later(NOT_VALID);
4401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403 /* Restore KeyTyped, setting 'filetype' may reset it. */
4404 KeyTyped = old_KeyTyped;
4405}
4406
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004407/*
4408 * For every change made to the quickfix list, update the changed tick.
4409 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004410 static void
4411qf_list_changed(qf_info_T *qi, int qf_idx)
4412{
4413 qi->qf_lists[qf_idx].qf_changedtick++;
4414}
4415
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004417 * Return the quickfix/location list number with the given identifier.
4418 * Returns -1 if list is not found.
4419 */
4420 static int
4421qf_id2nr(qf_info_T *qi, int_u qfid)
4422{
4423 int qf_idx;
4424
4425 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4426 if (qi->qf_lists[qf_idx].qf_id == qfid)
4427 return qf_idx;
4428 return INVALID_QFIDX;
4429}
4430
4431/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004432 * If the current list is not "save_qfid" and we can find the list with that ID
4433 * then make it the current list.
4434 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004435 * Returns OK if successfully restored the list. Returns FAIL if the list with
4436 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004437 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004438 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004439qf_restore_list(qf_info_T *qi, int_u save_qfid)
4440{
4441 int curlist;
4442
4443 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4444 {
4445 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004446 if (curlist < 0)
4447 // list is not present
4448 return FAIL;
4449 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004450 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004451 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004452}
4453
4454/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004455 * Jump to the first entry if there is one.
4456 */
4457 static void
4458qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4459{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004460 if (qf_restore_list(qi, save_qfid) == FAIL)
4461 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004462
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004463 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004464 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004465 qf_jump(qi, 0, 0, forceit);
4466}
4467
4468/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004469 * Return TRUE when using ":vimgrep" for ":grep".
4470 */
4471 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004472grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004473{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004474 return ((cmdidx == CMD_grep
4475 || cmdidx == CMD_lgrep
4476 || cmdidx == CMD_grepadd
4477 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004478 && STRCMP("internal",
4479 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4480}
4481
4482/*
Bram Moolenaara6557602006-02-04 22:43:20 +00004483 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 */
4485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004486ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487{
Bram Moolenaar7c626922005-02-07 22:01:03 +00004488 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004490 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00004492 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004493 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004494 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004495 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004496 int_u save_qfid;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004497
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004498 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
4499 if (grep_internal(eap->cmdidx))
4500 {
4501 ex_vimgrep(eap);
4502 return;
4503 }
4504
Bram Moolenaar7c626922005-02-07 22:01:03 +00004505 switch (eap->cmdidx)
4506 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00004507 case CMD_make: au_name = (char_u *)"make"; break;
4508 case CMD_lmake: au_name = (char_u *)"lmake"; break;
4509 case CMD_grep: au_name = (char_u *)"grep"; break;
4510 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4511 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4512 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004513 default: break;
4514 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004515 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4516 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004517 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004518#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004519 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004520 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004521#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004522 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004523#ifdef FEAT_MBYTE
4524 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
Bram Moolenaar39665952018-08-15 20:59:48 +02004527 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00004528 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004529
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00004531 fname = get_mef_name();
4532 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004534 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
4536 /*
4537 * If 'shellpipe' empty: don't redirect to 'errorfile'.
4538 */
4539 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
4540 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00004541 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 cmd = alloc(len);
4543 if (cmd == NULL)
4544 return;
4545 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
4546 (char *)p_shq);
4547 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004548 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 /*
4550 * Output a newline if there's something else than the :make command that
4551 * was typed (in which case the cursor is in column 0).
4552 */
4553 if (msg_col == 0)
4554 msg_didout = FALSE;
4555 msg_start();
4556 MSG_PUTS(":!");
4557 msg_outtrans(cmd); /* show what we are doing */
4558
4559 /* let the shell know if we are redirecting output or not */
4560 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4561
4562#ifdef AMIGA
4563 out_flush();
4564 /* read window status report and redraw before message */
4565 (void)char_avail();
4566#endif
4567
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004568 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00004569 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4570 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004571 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004572 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004573 if (wp != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004574 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004575 qi = GET_LOC_LIST(wp);
4576 if (qi == NULL)
4577 goto cleanup;
4578 }
4579 if (res >= 0)
4580 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004581
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004582 // Remember the current quickfix list identifier, so that we can
4583 // check for autocommands changing the current quickfix list.
4584 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4585 if (au_name != NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004586 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4587 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004588 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004589 // display the first error
4590 qf_jump_first(qi, save_qfid, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004592cleanup:
Bram Moolenaar7c626922005-02-07 22:01:03 +00004593 mch_remove(fname);
4594 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 vim_free(cmd);
4596}
4597
4598/*
4599 * Return the name for the errorfile, in allocated memory.
4600 * Find a new unique name when 'makeef' contains "##".
4601 * Returns NULL for error.
4602 */
4603 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004604get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605{
4606 char_u *p;
4607 char_u *name;
4608 static int start = -1;
4609 static int off = 0;
4610#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004611 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612#endif
4613
4614 if (*p_mef == NUL)
4615 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004616 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (name == NULL)
4618 EMSG(_(e_notmp));
4619 return name;
4620 }
4621
4622 for (p = p_mef; *p; ++p)
4623 if (p[0] == '#' && p[1] == '#')
4624 break;
4625
4626 if (*p == NUL)
4627 return vim_strsave(p_mef);
4628
4629 /* Keep trying until the name doesn't exist yet. */
4630 for (;;)
4631 {
4632 if (start == -1)
4633 start = mch_get_pid();
4634 else
4635 off += 19;
4636
4637 name = alloc((unsigned)STRLEN(p_mef) + 30);
4638 if (name == NULL)
4639 break;
4640 STRCPY(name, p_mef);
4641 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4642 STRCAT(name, p + 2);
4643 if (mch_getperm(name) < 0
4644#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02004645 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 && mch_lstat((char *)name, &sb) < 0
4647#endif
4648 )
4649 break;
4650 vim_free(name);
4651 }
4652 return name;
4653}
4654
4655/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004656 * Returns the number of valid entries in the current quickfix/location list.
4657 */
4658 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004659qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004660{
4661 qf_info_T *qi = &ql_info;
4662 qfline_T *qfp;
4663 int i, sz = 0;
4664 int prev_fnum = 0;
4665
Bram Moolenaar39665952018-08-15 20:59:48 +02004666 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004667 {
4668 /* Location list */
4669 qi = GET_LOC_LIST(curwin);
4670 if (qi == NULL)
4671 return 0;
4672 }
4673
4674 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004675 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004676 ++i, qfp = qfp->qf_next)
4677 {
4678 if (qfp->qf_valid)
4679 {
4680 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4681 sz++; /* Count all valid entries */
4682 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4683 {
4684 /* Count the number of files */
4685 sz++;
4686 prev_fnum = qfp->qf_fnum;
4687 }
4688 }
4689 }
4690
4691 return sz;
4692}
4693
4694/*
4695 * Returns the current index of the quickfix/location list.
4696 * Returns 0 if there is an error.
4697 */
4698 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004699qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004700{
4701 qf_info_T *qi = &ql_info;
4702
Bram Moolenaar39665952018-08-15 20:59:48 +02004703 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004704 {
4705 /* Location list */
4706 qi = GET_LOC_LIST(curwin);
4707 if (qi == NULL)
4708 return 0;
4709 }
4710
4711 return qi->qf_lists[qi->qf_curlist].qf_index;
4712}
4713
4714/*
4715 * Returns the current index in the quickfix/location list (counting only valid
4716 * entries). If no valid entries are in the list, then returns 1.
4717 */
4718 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004719qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004720{
4721 qf_info_T *qi = &ql_info;
4722 qf_list_T *qfl;
4723 qfline_T *qfp;
4724 int i, eidx = 0;
4725 int prev_fnum = 0;
4726
Bram Moolenaar39665952018-08-15 20:59:48 +02004727 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004728 {
4729 /* Location list */
4730 qi = GET_LOC_LIST(curwin);
4731 if (qi == NULL)
4732 return 1;
4733 }
4734
4735 qfl = &qi->qf_lists[qi->qf_curlist];
4736 qfp = qfl->qf_start;
4737
4738 /* check if the list has valid errors */
4739 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4740 return 1;
4741
4742 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4743 {
4744 if (qfp->qf_valid)
4745 {
4746 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4747 {
4748 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4749 {
4750 /* Count the number of files */
4751 eidx++;
4752 prev_fnum = qfp->qf_fnum;
4753 }
4754 }
4755 else
4756 eidx++;
4757 }
4758 }
4759
4760 return eidx ? eidx : 1;
4761}
4762
4763/*
4764 * Get the 'n'th valid error entry in the quickfix or location list.
4765 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4766 * For :cdo and :ldo returns the 'n'th valid error entry.
4767 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4768 */
4769 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004770qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004771{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004772 qfline_T *qfp = qfl->qf_start;
4773 int i, eidx;
4774 int prev_fnum = 0;
4775
4776 /* check if the list has valid errors */
4777 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4778 return 1;
4779
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004780 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004781 i++, qfp = qfp->qf_next)
4782 {
4783 if (qfp->qf_valid)
4784 {
4785 if (fdo)
4786 {
4787 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4788 {
4789 /* Count the number of files */
4790 eidx++;
4791 prev_fnum = qfp->qf_fnum;
4792 }
4793 }
4794 else
4795 eidx++;
4796 }
4797
4798 if (eidx == n)
4799 break;
4800 }
4801
4802 if (i <= qfl->qf_count)
4803 return i;
4804 else
4805 return 1;
4806}
4807
4808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004810 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004811 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 */
4813 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004814ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004816 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004817 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004818
Bram Moolenaar39665952018-08-15 20:59:48 +02004819 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004820 {
4821 qi = GET_LOC_LIST(curwin);
4822 if (qi == NULL)
4823 {
4824 EMSG(_(e_loclist));
4825 return;
4826 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004827 }
4828
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004829 if (eap->addr_count > 0)
4830 errornr = (int)eap->line2;
4831 else
4832 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004833 switch (eap->cmdidx)
4834 {
4835 case CMD_cc: case CMD_ll:
4836 errornr = 0;
4837 break;
4838 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4839 case CMD_lfirst:
4840 errornr = 1;
4841 break;
4842 default:
4843 errornr = 32767;
4844 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004845 }
4846
4847 /* For cdo and ldo commands, jump to the nth valid error.
4848 * For cfdo and lfdo commands, jump to the nth valid file entry.
4849 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004850 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4851 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004852 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004853 eap->addr_count > 0 ? (int)eap->line1 : 1,
4854 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4855
4856 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857}
4858
4859/*
4860 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004861 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004862 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 */
4864 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004865ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004867 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004868 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004869 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004870
Bram Moolenaar39665952018-08-15 20:59:48 +02004871 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004872 {
4873 qi = GET_LOC_LIST(curwin);
4874 if (qi == NULL)
4875 {
4876 EMSG(_(e_loclist));
4877 return;
4878 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004879 }
4880
Bram Moolenaar55b69262017-08-13 13:42:01 +02004881 if (eap->addr_count > 0
4882 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4883 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004884 errornr = (int)eap->line2;
4885 else
4886 errornr = 1;
4887
Bram Moolenaar39665952018-08-15 20:59:48 +02004888 // Depending on the command jump to either next or previous entry/file.
4889 switch (eap->cmdidx)
4890 {
4891 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4892 dir = FORWARD;
4893 break;
4894 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4895 case CMD_lNext:
4896 dir = BACKWARD;
4897 break;
4898 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4899 dir = FORWARD_FILE;
4900 break;
4901 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4902 dir = BACKWARD_FILE;
4903 break;
4904 default:
4905 dir = FORWARD;
4906 break;
4907 }
4908
4909 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910}
4911
4912/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004913 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004914 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 */
4916 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004917ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004919 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004920 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004921 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004922 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004923 int_u save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004924 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004925
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004926 switch (eap->cmdidx)
4927 {
4928 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4929 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4930 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4931 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4932 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4933 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4934 default: break;
4935 }
4936 if (au_name != NULL)
4937 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004938#ifdef FEAT_MBYTE
4939 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4940#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004941#ifdef FEAT_BROWSE
4942 if (cmdmod.browse)
4943 {
4944 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004945 NULL, NULL,
4946 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004947 if (browse_file == NULL)
4948 return;
4949 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4950 vim_free(browse_file);
4951 }
4952 else
4953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004955 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004956
Bram Moolenaar39665952018-08-15 20:59:48 +02004957 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004958 wp = curwin;
4959
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004960 /*
4961 * This function is used by the :cfile, :cgetfile and :caddfile
4962 * commands.
4963 * :cfile always creates a new quickfix list and jumps to the
4964 * first error.
4965 * :cgetfile creates a new quickfix list but doesn't jump to the
4966 * first error.
4967 * :caddfile adds to an existing quickfix list. If there is no
4968 * quickfix list then a new list is created.
4969 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004970 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004971 && eap->cmdidx != CMD_laddfile),
4972 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004973 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004974 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01004975 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004976 if (qi == NULL)
4977 return;
4978 }
4979 if (res >= 0)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004980 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004981 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004982 if (au_name != NULL)
4983 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004984
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004985 // Jump to the first error for a new list and if autocmds didn't
4986 // free the list.
4987 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
4988 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004989 // display the first error
4990 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004991}
4992
4993/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004994 * Return the vimgrep autocmd name.
4995 */
4996 static char_u *
4997vgr_get_auname(cmdidx_T cmdidx)
4998{
4999 switch (cmdidx)
5000 {
5001 case CMD_vimgrep: return (char_u *)"vimgrep";
5002 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5003 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5004 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5005 case CMD_grep: return (char_u *)"grep";
5006 case CMD_lgrep: return (char_u *)"lgrep";
5007 case CMD_grepadd: return (char_u *)"grepadd";
5008 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5009 default: return NULL;
5010 }
5011}
5012
5013/*
5014 * Initialize the regmatch used by vimgrep for pattern "s".
5015 */
5016 static void
5017vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5018{
5019 /* Get the search pattern: either white-separated or enclosed in // */
5020 regmatch->regprog = NULL;
5021
5022 if (s == NULL || *s == NUL)
5023 {
5024 /* Pattern is empty, use last search pattern. */
5025 if (last_search_pat() == NULL)
5026 {
5027 EMSG(_(e_noprevre));
5028 return;
5029 }
5030 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5031 }
5032 else
5033 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5034
5035 regmatch->rmm_ic = p_ic;
5036 regmatch->rmm_maxcol = 0;
5037}
5038
5039/*
5040 * Display a file name when vimgrep is running.
5041 */
5042 static void
5043vgr_display_fname(char_u *fname)
5044{
5045 char_u *p;
5046
5047 msg_start();
5048 p = msg_strtrunc(fname, TRUE);
5049 if (p == NULL)
5050 msg_outtrans(fname);
5051 else
5052 {
5053 msg_outtrans(p);
5054 vim_free(p);
5055 }
5056 msg_clr_eos();
5057 msg_didout = FALSE; /* overwrite this message */
5058 msg_nowait = TRUE; /* don't wait for this message */
5059 msg_col = 0;
5060 out_flush();
5061}
5062
5063/*
5064 * Load a dummy buffer to search for a pattern using vimgrep.
5065 */
5066 static buf_T *
5067vgr_load_dummy_buf(
5068 char_u *fname,
5069 char_u *dirname_start,
5070 char_u *dirname_now)
5071{
5072 int save_mls;
5073#if defined(FEAT_SYN_HL)
5074 char_u *save_ei = NULL;
5075#endif
5076 buf_T *buf;
5077
5078#if defined(FEAT_SYN_HL)
5079 /* Don't do Filetype autocommands to avoid loading syntax and
5080 * indent scripts, a great speed improvement. */
5081 save_ei = au_event_disable(",Filetype");
5082#endif
5083 /* Don't use modelines here, it's useless. */
5084 save_mls = p_mls;
5085 p_mls = 0;
5086
5087 /* Load file into a buffer, so that 'fileencoding' is detected,
5088 * autocommands applied, etc. */
5089 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5090
5091 p_mls = save_mls;
5092#if defined(FEAT_SYN_HL)
5093 au_event_restore(save_ei);
5094#endif
5095
5096 return buf;
5097}
5098
5099/*
5100 * Check whether a quickfix/location list valid. Autocmds may remove or change
5101 * a quickfix list when vimgrep is running. If the list is not found, create a
5102 * new list.
5103 */
5104 static int
5105vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005106 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005107 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005108 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005109 char_u *title)
5110{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005111 /* Verify that the quickfix/location list was not freed by an autocmd */
5112 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005113 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005114 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005115 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005116 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005117 EMSG(_(e_loc_list_changed));
5118 return FALSE;
5119 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005120 else
5121 {
5122 /* Quickfix list is not found, create a new one. */
5123 qf_new_list(qi, title);
5124 return TRUE;
5125 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005126 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005127
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005128 if (qf_restore_list(qi, qfid) == FAIL)
5129 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005130
5131 return TRUE;
5132}
5133
5134/*
5135 * Search for a pattern in all the lines in a buffer and add the matching lines
5136 * to a quickfix list.
5137 */
5138 static int
5139vgr_match_buflines(
5140 qf_info_T *qi,
5141 char_u *fname,
5142 buf_T *buf,
5143 regmmatch_T *regmatch,
5144 long tomatch,
5145 int duplicate_name,
5146 int flags)
5147{
5148 int found_match = FALSE;
5149 long lnum;
5150 colnr_T col;
5151
5152 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5153 {
5154 col = 0;
5155 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5156 col, NULL, NULL) > 0)
5157 {
5158 /* Pass the buffer number so that it gets used even for a
5159 * dummy buffer, unless duplicate_name is set, then the
5160 * buffer will be wiped out below. */
5161 if (qf_add_entry(qi,
5162 qi->qf_curlist,
5163 NULL, /* dir */
5164 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005165 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005166 duplicate_name ? 0 : buf->b_fnum,
5167 ml_get_buf(buf,
5168 regmatch->startpos[0].lnum + lnum, FALSE),
5169 regmatch->startpos[0].lnum + lnum,
5170 regmatch->startpos[0].col + 1,
5171 FALSE, /* vis_col */
5172 NULL, /* search pattern */
5173 0, /* nr */
5174 0, /* type */
5175 TRUE /* valid */
5176 ) == FAIL)
5177 {
5178 got_int = TRUE;
5179 break;
5180 }
5181 found_match = TRUE;
5182 if (--tomatch == 0)
5183 break;
5184 if ((flags & VGR_GLOBAL) == 0
5185 || regmatch->endpos[0].lnum > 0)
5186 break;
5187 col = regmatch->endpos[0].col
5188 + (col == regmatch->endpos[0].col);
5189 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5190 break;
5191 }
5192 line_breakcheck();
5193 if (got_int)
5194 break;
5195 }
5196
5197 return found_match;
5198}
5199
5200/*
5201 * Jump to the first match and update the directory.
5202 */
5203 static void
5204vgr_jump_to_match(
5205 qf_info_T *qi,
5206 int forceit,
5207 int *redraw_for_dummy,
5208 buf_T *first_match_buf,
5209 char_u *target_dir)
5210{
5211 buf_T *buf;
5212
5213 buf = curbuf;
5214 qf_jump(qi, 0, 0, forceit);
5215 if (buf != curbuf)
5216 /* If we jumped to another buffer redrawing will already be
5217 * taken care of. */
5218 *redraw_for_dummy = FALSE;
5219
5220 /* Jump to the directory used after loading the buffer. */
5221 if (curbuf == first_match_buf && target_dir != NULL)
5222 {
5223 exarg_T ea;
5224
5225 ea.arg = target_dir;
5226 ea.cmdidx = CMD_lcd;
5227 ex_cd(&ea);
5228 }
5229}
5230
5231/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005232 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005233 * ":vimgrepadd {pattern} file(s)"
5234 * ":lvimgrep {pattern} file(s)"
5235 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005236 */
5237 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005238ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005239{
Bram Moolenaar81695252004-12-29 20:58:21 +00005240 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005241 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005242 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005243 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005244 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005245 char_u *s;
5246 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005247 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005248 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005249 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005250 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005251 buf_T *buf;
5252 int duplicate_name = FALSE;
5253 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005254 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005255 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005256 buf_T *first_match_buf = NULL;
5257 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005258 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005259 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005260 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005261 char_u *dirname_start = NULL;
5262 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005263 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005264 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005265
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005266 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005267 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5268 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005269 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005270#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005271 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005272 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005273#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005274 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005275
Bram Moolenaar39665952018-08-15 20:59:48 +02005276 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005277 {
5278 qi = ll_get_or_alloc_list(curwin);
5279 if (qi == NULL)
5280 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005281 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005282 }
5283
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005284 if (eap->addr_count > 0)
5285 tomatch = eap->line2;
5286 else
5287 tomatch = MAXLNUM;
5288
Bram Moolenaar81695252004-12-29 20:58:21 +00005289 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005290 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005291 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005292 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005293 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005294 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005295 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005296 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005297 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005298
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005299 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005300 if (regmatch.regprog == NULL)
5301 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005302
5303 p = skipwhite(p);
5304 if (*p == NUL)
5305 {
5306 EMSG(_("E683: File name missing or invalid pattern"));
5307 goto theend;
5308 }
5309
Bram Moolenaar55b69262017-08-13 13:42:01 +02005310 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005311 && eap->cmdidx != CMD_vimgrepadd
5312 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005313 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005314 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005315 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005316
5317 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005318 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005319 goto theend;
5320 if (fcount == 0)
5321 {
5322 EMSG(_(e_nomatch));
5323 goto theend;
5324 }
5325
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005326 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5327 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005328 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005329 {
5330 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005331 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005332 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005333
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005334 /* Remember the current directory, because a BufRead autocommand that does
5335 * ":lcd %:p:h" changes the meaning of short path names. */
5336 mch_dirname(dirname_start, MAXPATHL);
5337
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005338 /* Remember the current quickfix list identifier, so that we can check for
5339 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005340 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005341
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005342 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005343 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005344 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005345 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005346 if (time(NULL) > seconds)
5347 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005348 /* Display the file name every second or so, show the user we are
5349 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005350 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005351 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005352 }
5353
Bram Moolenaar81695252004-12-29 20:58:21 +00005354 buf = buflist_findname_exp(fnames[fi]);
5355 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5356 {
5357 /* Remember that a buffer with this name already exists. */
5358 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005359 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005360 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005361
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005362 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005363 }
5364 else
5365 /* Use existing, loaded buffer. */
5366 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005367
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005368 /* Check whether the quickfix list is still valid. When loading a
5369 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005370 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005371 {
5372 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005373 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005374 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005375 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005376
Bram Moolenaar81695252004-12-29 20:58:21 +00005377 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005378 {
5379 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005380 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005381 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005382 else
5383 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005384 /* Try for a match in all lines of the buffer.
5385 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005386 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5387 tomatch, duplicate_name, flags);
5388
Bram Moolenaar81695252004-12-29 20:58:21 +00005389 if (using_dummy)
5390 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005391 if (found_match && first_match_buf == NULL)
5392 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005393 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005394 {
Bram Moolenaar81695252004-12-29 20:58:21 +00005395 /* Never keep a dummy buffer if there is another buffer
5396 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005397 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005398 buf = NULL;
5399 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005400 else if (!cmdmod.hide
5401 || buf->b_p_bh[0] == 'u' /* "unload" */
5402 || buf->b_p_bh[0] == 'w' /* "wipe" */
5403 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00005404 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005405 /* When no match was found we don't need to remember the
5406 * buffer, wipe it out. If there was a match and it
5407 * wasn't the first one or we won't jump there: only
5408 * unload the buffer.
5409 * Ignore 'hidden' here, because it may lead to having too
5410 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00005411 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005412 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005413 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005414 buf = NULL;
5415 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005416 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005417 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005418 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02005419 /* Keeping the buffer, remove the dummy flag. */
5420 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005421 buf = NULL;
5422 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005423 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005424
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005425 if (buf != NULL)
5426 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02005427 /* Keeping the buffer, remove the dummy flag. */
5428 buf->b_flags &= ~BF_DUMMY;
5429
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005430 /* If the buffer is still loaded we need to use the
5431 * directory we jumped to below. */
5432 if (buf == first_match_buf
5433 && target_dir == NULL
5434 && STRCMP(dirname_start, dirname_now) != 0)
5435 target_dir = vim_strsave(dirname_now);
5436
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005437 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005438 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00005439 * need to be done (again). But not the window-local
5440 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005441 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005442#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005443 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5444 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005445#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005446 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005447 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005448 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005449 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005450 }
5451 }
5452
5453 FreeWild(fcount, fnames);
5454
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005455 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5456 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5457 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005458 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005459
Bram Moolenaar864293a2016-06-02 13:40:04 +02005460 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005461
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005462 if (au_name != NULL)
5463 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5464 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005465 /*
5466 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5467 * is still valid.
5468 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005469 if (!qflist_valid(wp, save_qfid))
5470 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005471
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005472 if (qf_restore_list(qi, save_qfid) == FAIL)
5473 goto theend;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005474
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005475 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005476 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005477 {
5478 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005479 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5480 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005481 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005482 else
5483 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005484
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005485 /* If we loaded a dummy buffer into the current window, the autocommands
5486 * may have messed up things, need to redraw and recompute folds. */
5487 if (redraw_for_dummy)
5488 {
5489#ifdef FEAT_FOLDING
5490 foldUpdateAll(curwin);
5491#else
5492 redraw_later(NOT_VALID);
5493#endif
5494 }
5495
Bram Moolenaar86b68352004-12-27 21:59:20 +00005496theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005497 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005498 vim_free(dirname_now);
5499 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005500 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005501 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005502}
5503
5504/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005505 * Restore current working directory to "dirname_start" if they differ, taking
5506 * into account whether it is set locally or globally.
5507 */
5508 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005509restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005510{
5511 char_u *dirname_now = alloc(MAXPATHL);
5512
5513 if (NULL != dirname_now)
5514 {
5515 mch_dirname(dirname_now, MAXPATHL);
5516 if (STRCMP(dirname_start, dirname_now) != 0)
5517 {
5518 /* If the directory has changed, change it back by building up an
5519 * appropriate ex command and executing it. */
5520 exarg_T ea;
5521
5522 ea.arg = dirname_start;
5523 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5524 ex_cd(&ea);
5525 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005526 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005527 }
5528}
5529
5530/*
5531 * Load file "fname" into a dummy buffer and return the buffer pointer,
5532 * placing the directory resulting from the buffer load into the
5533 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5534 * prior to calling this function. Restores directory to "dirname_start" prior
5535 * to returning, if autocmds or the 'autochdir' option have changed it.
5536 *
5537 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5538 * or wipe_dummy_buffer() later!
5539 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005540 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005541 */
5542 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005543load_dummy_buffer(
5544 char_u *fname,
5545 char_u *dirname_start, /* in: old directory */
5546 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005547{
5548 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005549 bufref_T newbufref;
5550 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005551 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005552 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005553 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005554
5555 /* Allocate a buffer without putting it in the buffer list. */
5556 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5557 if (newbuf == NULL)
5558 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005559 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005560
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005561 /* Init the options. */
5562 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5563
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005564 /* need to open the memfile before putting the buffer in a window */
5565 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005566 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005567 /* Make sure this buffer isn't wiped out by autocommands. */
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005568 ++newbuf->b_locked;
5569
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005570 /* set curwin/curbuf to buf and save a few things */
5571 aucmd_prepbuf(&aco, newbuf);
5572
5573 /* Need to set the filename for autocommands. */
5574 (void)setfname(curbuf, fname, NULL, FALSE);
5575
Bram Moolenaar81695252004-12-29 20:58:21 +00005576 /* Create swap file now to avoid the ATTENTION message. */
5577 check_need_swap(TRUE);
5578
5579 /* Remove the "dummy" flag, otherwise autocommands may not
5580 * work. */
5581 curbuf->b_flags &= ~BF_DUMMY;
5582
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005583 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005584 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005585 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005586 NULL, READ_NEW | READ_DUMMY);
5587 --newbuf->b_locked;
5588 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005589 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005590 && !(curbuf->b_flags & BF_NEW))
5591 {
5592 failed = FALSE;
5593 if (curbuf != newbuf)
5594 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005595 /* Bloody autocommands changed the buffer! Can happen when
5596 * using netrw and editing a remote file. Use the current
5597 * buffer instead, delete the dummy one after restoring the
5598 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005599 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005600 newbuf = curbuf;
5601 }
5602 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005603
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005604 /* restore curwin/curbuf and a few other things */
5605 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005606 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5607 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005608
5609 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5610 * skip it. */
5611 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005612 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005613
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005614 /*
5615 * When autocommands/'autochdir' option changed directory: go back.
5616 * Let the caller know what the resulting dir was first, in case it is
5617 * important.
5618 */
5619 mch_dirname(resulting_dir, MAXPATHL);
5620 restore_start_dir(dirname_start);
5621
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005622 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005623 return NULL;
5624 if (failed)
5625 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005626 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005627 return NULL;
5628 }
5629 return newbuf;
5630}
5631
5632/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005633 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5634 * directory to "dirname_start" prior to returning, if autocmds or the
5635 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005636 */
5637 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005638wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005639{
5640 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005641 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005642#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005643 cleanup_T cs;
5644
5645 /* Reset the error/interrupt/exception state here so that aborting()
5646 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5647 * work when got_int is set. */
5648 enter_cleanup(&cs);
5649#endif
5650
Bram Moolenaar81695252004-12-29 20:58:21 +00005651 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005652
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005653#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005654 /* Restore the error/interrupt/exception state if not discarded by a
5655 * new aborting error, interrupt, or uncaught exception. */
5656 leave_cleanup(&cs);
5657#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005658 /* When autocommands/'autochdir' option changed directory: go back. */
5659 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005660 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005661}
5662
5663/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005664 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5665 * directory to "dirname_start" prior to returning, if autocmds or the
5666 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005667 */
5668 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005669unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005670{
5671 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005672 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005673 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005674
5675 /* When autocommands/'autochdir' option changed directory: go back. */
5676 restore_start_dir(dirname_start);
5677 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005678}
5679
Bram Moolenaar05159a02005-02-26 23:04:13 +00005680#if defined(FEAT_EVAL) || defined(PROTO)
5681/*
5682 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005683 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005684 */
5685 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005686get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005687{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005688 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005689 dict_T *dict;
5690 char_u buf[2];
5691 qfline_T *qfp;
5692 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005693 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005694
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005695 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005696 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005697 qi = &ql_info;
5698 if (wp != NULL)
5699 {
5700 qi = GET_LOC_LIST(wp);
5701 if (qi == NULL)
5702 return FAIL;
5703 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005704 }
5705
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005706 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005707 qf_idx = qi->qf_curlist;
5708
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005709 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005710 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005711
Bram Moolenaard823fa92016-08-12 16:29:27 +02005712 qfp = qi->qf_lists[qf_idx].qf_start;
5713 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005714 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005715 /* Handle entries with a non-existing buffer number. */
5716 bufnum = qfp->qf_fnum;
5717 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5718 bufnum = 0;
5719
Bram Moolenaar05159a02005-02-26 23:04:13 +00005720 if ((dict = dict_alloc()) == NULL)
5721 return FAIL;
5722 if (list_append_dict(list, dict) == FAIL)
5723 return FAIL;
5724
5725 buf[0] = qfp->qf_type;
5726 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005727 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5728 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5729 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5730 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5731 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5732 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5733 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5734 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5735 || dict_add_string(dict, "type", buf) == FAIL
5736 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005737 return FAIL;
5738
5739 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005740 if (qfp == NULL)
5741 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005742 }
5743 return OK;
5744}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005745
5746/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005747 * Flags used by getqflist()/getloclist() to determine which fields to return.
5748 */
5749enum {
5750 QF_GETLIST_NONE = 0x0,
5751 QF_GETLIST_TITLE = 0x1,
5752 QF_GETLIST_ITEMS = 0x2,
5753 QF_GETLIST_NR = 0x4,
5754 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005755 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005756 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005757 QF_GETLIST_IDX = 0x40,
5758 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005759 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005760 QF_GETLIST_FILEWINID = 0x200,
5761 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005762};
5763
5764/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005765 * Parse text from 'di' and return the quickfix list items.
5766 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005767 */
5768 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005769qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005770{
5771 int status = FAIL;
5772 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005773 char_u *errorformat = p_efm;
5774 dictitem_T *efm_di;
5775 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005776
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005777 /* Only a List value is supported */
5778 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005779 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005780 /* If errorformat is supplied then use it, otherwise use the 'efm'
5781 * option setting
5782 */
5783 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5784 {
5785 if (efm_di->di_tv.v_type != VAR_STRING ||
5786 efm_di->di_tv.vval.v_string == NULL)
5787 return FAIL;
5788 errorformat = efm_di->di_tv.vval.v_string;
5789 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005790
Bram Moolenaar36538222017-09-02 19:51:44 +02005791 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005792 if (l == NULL)
5793 return FAIL;
5794
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005795 qi = ll_new_list();
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005796 if (qi != NULL)
5797 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005798 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005799 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5800 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005801 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005802 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005803 }
5804 free(qi);
5805 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005806 dict_add_list(retdict, "items", l);
5807 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005808 }
5809
5810 return status;
5811}
5812
5813/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005814 * Return the quickfix/location list window identifier in the current tabpage.
5815 */
5816 static int
5817qf_winid(qf_info_T *qi)
5818{
5819 win_T *win;
5820
5821 /* The quickfix window can be opened even if the quickfix list is not set
5822 * using ":copen". This is not true for location lists. */
5823 if (qi == NULL)
5824 return 0;
5825 win = qf_find_win(qi);
5826 if (win != NULL)
5827 return win->w_id;
5828 return 0;
5829}
5830
5831/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005832 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005833 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005834 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005835qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005836{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005837 int flags = QF_GETLIST_NONE;
5838
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005839 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005840 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005841 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005842 if (!loclist)
5843 // File window ID is applicable only to location list windows
5844 flags &= ~ QF_GETLIST_FILEWINID;
5845 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005846
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005847 if (dict_find(what, (char_u *)"title", -1) != NULL)
5848 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005849
Bram Moolenaara6d48492017-12-12 22:45:31 +01005850 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5851 flags |= QF_GETLIST_NR;
5852
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005853 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5854 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005855
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005856 if (dict_find(what, (char_u *)"context", -1) != NULL)
5857 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005858
Bram Moolenaara6d48492017-12-12 22:45:31 +01005859 if (dict_find(what, (char_u *)"id", -1) != NULL)
5860 flags |= QF_GETLIST_ID;
5861
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005862 if (dict_find(what, (char_u *)"items", -1) != NULL)
5863 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005864
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005865 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5866 flags |= QF_GETLIST_IDX;
5867
5868 if (dict_find(what, (char_u *)"size", -1) != NULL)
5869 flags |= QF_GETLIST_SIZE;
5870
Bram Moolenaarb254af32017-12-18 19:48:58 +01005871 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5872 flags |= QF_GETLIST_TICK;
5873
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005874 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5875 flags |= QF_GETLIST_FILEWINID;
5876
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005877 return flags;
5878}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005879
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005880/*
5881 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5882 * If 'nr' and 'id' are not present in 'what' then return the current
5883 * quickfix list index.
5884 * If 'nr' is zero then return the current quickfix list index.
5885 * If 'nr' is '$' then return the last quickfix list index.
5886 * If 'id' is present then return the index of the quickfix list with that id.
5887 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5888 * Return -1, if quickfix list is not present or if the stack is empty.
5889 */
5890 static int
5891qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5892{
5893 int qf_idx;
5894 dictitem_T *di;
5895
5896 qf_idx = qi->qf_curlist; /* default is the current list */
5897 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5898 {
5899 /* Use the specified quickfix/location list */
5900 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005901 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005902 /* for zero use the current list */
5903 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005904 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005905 qf_idx = di->di_tv.vval.v_number - 1;
5906 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005907 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005908 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005909 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005910 else if (di->di_tv.v_type == VAR_STRING
5911 && di->di_tv.vval.v_string != NULL
5912 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5913 /* Get the last quickfix list number */
5914 qf_idx = qi->qf_listcount - 1;
5915 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005916 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005917 }
5918
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005919 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5920 {
5921 /* Look for a list with the specified id */
5922 if (di->di_tv.v_type == VAR_NUMBER)
5923 {
5924 /*
5925 * For zero, use the current list or the list specified by 'nr'
5926 */
5927 if (di->di_tv.vval.v_number != 0)
5928 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5929 }
5930 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005931 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005932 }
5933
5934 return qf_idx;
5935}
5936
5937/*
5938 * Return default values for quickfix list properties in retdict.
5939 */
5940 static int
5941qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5942{
5943 int status = OK;
5944
5945 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005946 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005947 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5948 {
5949 list_T *l = list_alloc();
5950 if (l != NULL)
5951 status = dict_add_list(retdict, "items", l);
5952 else
5953 status = FAIL;
5954 }
5955 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005956 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005957 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005958 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005959 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005960 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005961 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005962 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005963 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005964 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005965 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005966 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005967 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005968 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005969 if ((status == OK) && (qi != &ql_info) && (flags & QF_GETLIST_FILEWINID))
5970 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005971
5972 return status;
5973}
5974
5975/*
5976 * Return the quickfix list title as 'title' in retdict
5977 */
5978 static int
5979qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5980{
Bram Moolenaare0be1672018-07-08 16:50:37 +02005981 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005982}
5983
5984/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005985 * Returns the identifier of the window used to display files from a location
5986 * list. If there is no associated window, then returns 0. Useful only when
5987 * called from a location list window.
5988 */
5989 static int
5990qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
5991{
5992 int winid = 0;
5993
5994 if (wp != NULL && IS_LL_WINDOW(wp))
5995 {
5996 win_T *ll_wp = qf_find_win_with_loclist(qi);
5997 if (ll_wp != NULL)
5998 winid = ll_wp->w_id;
5999 }
6000
6001 return dict_add_number(retdict, "filewinid", winid);
6002}
6003
6004/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006005 * Return the quickfix list items/entries as 'items' in retdict
6006 */
6007 static int
6008qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6009{
6010 int status = OK;
6011 list_T *l = list_alloc();
6012 if (l != NULL)
6013 {
6014 (void)get_errorlist(qi, NULL, qf_idx, l);
6015 dict_add_list(retdict, "items", l);
6016 }
6017 else
6018 status = FAIL;
6019
6020 return status;
6021}
6022
6023/*
6024 * Return the quickfix list context (if any) as 'context' in retdict.
6025 */
6026 static int
6027qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6028{
6029 int status;
6030 dictitem_T *di;
6031
6032 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
6033 {
6034 di = dictitem_alloc((char_u *)"context");
6035 if (di != NULL)
6036 {
6037 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
6038 status = dict_add(retdict, di);
6039 if (status == FAIL)
6040 dictitem_free(di);
6041 }
6042 else
6043 status = FAIL;
6044 }
6045 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006046 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006047
6048 return status;
6049}
6050
6051/*
6052 * Return the quickfix list index as 'idx' in retdict
6053 */
6054 static int
6055qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6056{
6057 int idx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006058 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006059 /* For empty lists, qf_index is set to 1 */
6060 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02006061 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006062}
6063
6064/*
6065 * Return quickfix/location list details (title) as a
6066 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6067 * then current list is used. Otherwise the specified list is used.
6068 */
6069 int
6070qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6071{
6072 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006073 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006074 int status = OK;
6075 int qf_idx;
6076 dictitem_T *di;
6077 int flags = QF_GETLIST_NONE;
6078
6079 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6080 return qf_get_list_from_lines(what, di, retdict);
6081
6082 if (wp != NULL)
6083 qi = GET_LOC_LIST(wp);
6084
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006085 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006086
6087 if (qi != NULL && qi->qf_listcount != 0)
6088 qf_idx = qf_getprop_qfidx(qi, what);
6089
Bram Moolenaara6d48492017-12-12 22:45:31 +01006090 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006091 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006092 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006093
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006094 qfl = &qi->qf_lists[qf_idx];
6095
Bram Moolenaard823fa92016-08-12 16:29:27 +02006096 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006097 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006098 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006099 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006100 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006101 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006102 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006103 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006104 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006105 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006106 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006107 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006108 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006109 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006110 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006111 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006112 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006113 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006114 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6115 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006116
Bram Moolenaard823fa92016-08-12 16:29:27 +02006117 return status;
6118}
6119
6120/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006121 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
6122 * items in the dict 'd'.
6123 */
6124 static int
6125qf_add_entry_from_dict(
6126 qf_info_T *qi,
6127 int qf_idx,
6128 dict_T *d,
6129 int first_entry)
6130{
6131 static int did_bufnr_emsg;
6132 char_u *filename, *module, *pattern, *text, *type;
6133 int bufnum, valid, status, col, vcol, nr;
6134 long lnum;
6135
6136 if (first_entry)
6137 did_bufnr_emsg = FALSE;
6138
6139 filename = get_dict_string(d, (char_u *)"filename", TRUE);
6140 module = get_dict_string(d, (char_u *)"module", TRUE);
6141 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6142 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6143 col = (int)get_dict_number(d, (char_u *)"col");
6144 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6145 nr = (int)get_dict_number(d, (char_u *)"nr");
6146 type = get_dict_string(d, (char_u *)"type", TRUE);
6147 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6148 text = get_dict_string(d, (char_u *)"text", TRUE);
6149 if (text == NULL)
6150 text = vim_strsave((char_u *)"");
6151
6152 valid = TRUE;
6153 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6154 valid = FALSE;
6155
6156 // Mark entries with non-existing buffer number as not valid. Give the
6157 // error message only once.
6158 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6159 {
6160 if (!did_bufnr_emsg)
6161 {
6162 did_bufnr_emsg = TRUE;
6163 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6164 }
6165 valid = FALSE;
6166 bufnum = 0;
6167 }
6168
6169 // If the 'valid' field is present it overrules the detected value.
6170 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6171 valid = (int)get_dict_number(d, (char_u *)"valid");
6172
6173 status = qf_add_entry(qi,
6174 qf_idx,
6175 NULL, // dir
6176 filename,
6177 module,
6178 bufnum,
6179 text,
6180 lnum,
6181 col,
6182 vcol, // vis_col
6183 pattern, // search pattern
6184 nr,
6185 type == NULL ? NUL : *type,
6186 valid);
6187
6188 vim_free(filename);
6189 vim_free(module);
6190 vim_free(pattern);
6191 vim_free(text);
6192 vim_free(type);
6193
6194 return status;
6195}
6196
6197/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006198 * Add list of entries to quickfix/location list. Each list entry is
6199 * a dictionary with item information.
6200 */
6201 static int
6202qf_add_entries(
6203 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006204 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006205 list_T *list,
6206 char_u *title,
6207 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006208{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006209 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006210 listitem_T *li;
6211 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006212 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006213 int retval = OK;
6214
Bram Moolenaara3921f42017-06-04 15:30:34 +02006215 if (action == ' ' || qf_idx == qi->qf_listcount)
6216 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006217 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006218 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006219 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006220 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006221 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006222 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006223 /* Adding to existing list, use last entry. */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006224 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006225 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006226 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006227 qf_free_items(qfl);
6228 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006229 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006230
6231 for (li = list->lv_first; li != NULL; li = li->li_next)
6232 {
6233 if (li->li_tv.v_type != VAR_DICT)
6234 continue; /* Skip non-dict items */
6235
6236 d = li->li_tv.vval.v_dict;
6237 if (d == NULL)
6238 continue;
6239
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006240 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first);
6241 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006242 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006243 }
6244
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006245 if (qfl->qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02006246 /* no valid entry */
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006247 qfl->qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006248 else
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006249 qfl->qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006250 if (action != 'a')
6251 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006252 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006253 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006254 qfl->qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006255 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006256
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006257 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02006258 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006259
6260 return retval;
6261}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006262
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006263/*
6264 * Get the quickfix list index from 'nr' or 'id'
6265 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006266 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006267qf_setprop_get_qfidx(
6268 qf_info_T *qi,
6269 dict_T *what,
6270 int action,
6271 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006272{
6273 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006274 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006275
Bram Moolenaard823fa92016-08-12 16:29:27 +02006276 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6277 {
6278 /* Use the specified quickfix/location list */
6279 if (di->di_tv.v_type == VAR_NUMBER)
6280 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006281 /* for zero use the current list */
6282 if (di->di_tv.vval.v_number != 0)
6283 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006284
Bram Moolenaar55b69262017-08-13 13:42:01 +02006285 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6286 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006287 /*
6288 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02006289 * non-available list and add the new list at the end of the
6290 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006291 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006292 *newlist = TRUE;
6293 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006294 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006295 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006296 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006297 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006298 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02006299 }
6300 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006301 && di->di_tv.vval.v_string != NULL
6302 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006303 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006304 if (qi->qf_listcount > 0)
6305 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006306 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006307 qf_idx = 0;
6308 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006309 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006310 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006311 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006312 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006313 }
6314
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006315 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006316 {
6317 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006318 if (di->di_tv.v_type != VAR_NUMBER)
6319 return INVALID_QFIDX;
6320
6321 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006322 }
6323
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006324 return qf_idx;
6325}
6326
6327/*
6328 * Set the quickfix list title.
6329 */
6330 static int
6331qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6332{
6333 if (di->di_tv.v_type != VAR_STRING)
6334 return FAIL;
6335
6336 vim_free(qi->qf_lists[qf_idx].qf_title);
6337 qi->qf_lists[qf_idx].qf_title =
6338 get_dict_string(what, (char_u *)"title", TRUE);
6339 if (qf_idx == qi->qf_curlist)
6340 qf_update_win_titlevar(qi);
6341
6342 return OK;
6343}
6344
6345/*
6346 * Set quickfix list items/entries.
6347 */
6348 static int
6349qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6350{
6351 int retval = FAIL;
6352 char_u *title_save;
6353
6354 if (di->di_tv.v_type != VAR_LIST)
6355 return FAIL;
6356
6357 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6358 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6359 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006360 vim_free(title_save);
6361
6362 return retval;
6363}
6364
6365/*
6366 * Set quickfix list items/entries from a list of lines.
6367 */
6368 static int
6369qf_setprop_items_from_lines(
6370 qf_info_T *qi,
6371 int qf_idx,
6372 dict_T *what,
6373 dictitem_T *di,
6374 int action)
6375{
6376 char_u *errorformat = p_efm;
6377 dictitem_T *efm_di;
6378 int retval = FAIL;
6379
6380 /* Use the user supplied errorformat settings (if present) */
6381 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6382 {
6383 if (efm_di->di_tv.v_type != VAR_STRING ||
6384 efm_di->di_tv.vval.v_string == NULL)
6385 return FAIL;
6386 errorformat = efm_di->di_tv.vval.v_string;
6387 }
6388
6389 /* Only a List value is supported */
6390 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6391 return FAIL;
6392
6393 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006394 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006395 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6396 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6397 retval = OK;
6398
6399 return retval;
6400}
6401
6402/*
6403 * Set quickfix list context.
6404 */
6405 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006406qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006407{
6408 typval_T *ctx;
6409
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006410 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006411 ctx = alloc_tv();
6412 if (ctx != NULL)
6413 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006414 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006415
6416 return OK;
6417}
6418
6419/*
6420 * Set quickfix/location list properties (title, items, context).
6421 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006422 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006423 */
6424 static int
6425qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6426{
6427 dictitem_T *di;
6428 int retval = FAIL;
6429 int qf_idx;
6430 int newlist = FALSE;
6431
6432 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
6433 newlist = TRUE;
6434
6435 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6436 if (qf_idx == INVALID_QFIDX) /* List not found */
6437 return FAIL;
6438
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006439 if (newlist)
6440 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006441 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006442 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006443 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006444 }
6445
6446 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006447 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006448 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006449 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006450 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006451 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006452 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006453 retval = qf_setprop_context(&qi->qf_lists[qf_idx], di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006454
Bram Moolenaarb254af32017-12-18 19:48:58 +01006455 if (retval == OK)
6456 qf_list_changed(qi, qf_idx);
6457
Bram Moolenaard823fa92016-08-12 16:29:27 +02006458 return retval;
6459}
6460
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006461/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006462 * Find the non-location list window with the specified location list in the
6463 * current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006464 */
6465 static win_T *
6466find_win_with_ll(qf_info_T *qi)
6467{
6468 win_T *wp = NULL;
6469
6470 FOR_ALL_WINDOWS(wp)
6471 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6472 return wp;
6473
6474 return NULL;
6475}
6476
6477/*
6478 * Free the entire quickfix/location list stack.
6479 * If the quickfix/location list window is open, then clear it.
6480 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006481 static void
6482qf_free_stack(win_T *wp, qf_info_T *qi)
6483{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006484 win_T *qfwin = qf_find_win(qi);
6485 win_T *llwin = NULL;
6486 win_T *orig_wp = wp;
6487
6488 if (qfwin != NULL)
6489 {
6490 /* If the quickfix/location list window is open, then clear it */
6491 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006492 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006493 qf_update_buffer(qi, NULL);
6494 }
6495
6496 if (wp != NULL && IS_LL_WINDOW(wp))
6497 {
6498 /* If in the location list window, then use the non-location list
6499 * window with this location list (if present)
6500 */
6501 llwin = find_win_with_ll(qi);
6502 if (llwin != NULL)
6503 wp = llwin;
6504 }
6505
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006506 qf_free_all(wp);
6507 if (wp == NULL)
6508 {
6509 /* quickfix list */
6510 qi->qf_curlist = 0;
6511 qi->qf_listcount = 0;
6512 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006513 else if (IS_LL_WINDOW(orig_wp))
6514 {
6515 /* If the location list window is open, then create a new empty
6516 * location list */
6517 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006518
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006519 /* first free the list reference in the location list window */
6520 ll_free_all(&orig_wp->w_llist_ref);
6521
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006522 orig_wp->w_llist_ref = new_ll;
6523 if (llwin != NULL)
6524 {
6525 llwin->w_llist = new_ll;
6526 new_ll->qf_refcount++;
6527 }
6528 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006529}
6530
Bram Moolenaard823fa92016-08-12 16:29:27 +02006531/*
6532 * Populate the quickfix list with the items supplied in the list
6533 * of dictionaries. "title" will be copied to w:quickfix_title.
6534 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6535 */
6536 int
6537set_errorlist(
6538 win_T *wp,
6539 list_T *list,
6540 int action,
6541 char_u *title,
6542 dict_T *what)
6543{
6544 qf_info_T *qi = &ql_info;
6545 int retval = OK;
6546
6547 if (wp != NULL)
6548 {
6549 qi = ll_get_or_alloc_list(wp);
6550 if (qi == NULL)
6551 return FAIL;
6552 }
6553
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006554 if (action == 'f')
6555 {
6556 /* Free the entire quickfix or location list stack */
6557 qf_free_stack(wp, qi);
6558 }
6559 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006560 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006561 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006562 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006563 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006564 if (retval == OK)
6565 qf_list_changed(qi, qi->qf_curlist);
6566 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006567
6568 return retval;
6569}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006570
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006571/*
6572 * Mark the context as in use for all the lists in a quickfix stack.
6573 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006574 static int
6575mark_quickfix_ctx(qf_info_T *qi, int copyID)
6576{
6577 int i;
6578 int abort = FALSE;
6579 typval_T *ctx;
6580
6581 for (i = 0; i < LISTCOUNT && !abort; ++i)
6582 {
6583 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006584 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6585 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006586 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6587 }
6588
6589 return abort;
6590}
6591
6592/*
6593 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006594 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006595 */
6596 int
6597set_ref_in_quickfix(int copyID)
6598{
6599 int abort = FALSE;
6600 tabpage_T *tp;
6601 win_T *win;
6602
6603 abort = mark_quickfix_ctx(&ql_info, copyID);
6604 if (abort)
6605 return abort;
6606
6607 FOR_ALL_TAB_WINDOWS(tp, win)
6608 {
6609 if (win->w_llist != NULL)
6610 {
6611 abort = mark_quickfix_ctx(win->w_llist, copyID);
6612 if (abort)
6613 return abort;
6614 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006615 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6616 {
6617 /* In a location list window and none of the other windows is
6618 * referring to this location list. Mark the location list
6619 * context as still in use.
6620 */
6621 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6622 if (abort)
6623 return abort;
6624 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006625 }
6626
6627 return abort;
6628}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006629#endif
6630
Bram Moolenaar81695252004-12-29 20:58:21 +00006631/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006632 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006633 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006634 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006635 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006636 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006637 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006638 */
6639 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006640ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006641{
6642 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006643 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006644 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006645 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006646 int_u save_qfid;
6647 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006648
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006649 switch (eap->cmdidx)
6650 {
6651 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6652 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6653 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6654 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6655 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6656 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6657 default: break;
6658 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006659 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6660 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006661 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006662#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006663 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006664 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006665#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006666 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006667
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006668 /* Must come after autocommands. */
Bram Moolenaar39665952018-08-15 20:59:48 +02006669 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006670 {
6671 qi = ll_get_or_alloc_list(curwin);
6672 if (qi == NULL)
6673 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006674 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006675 }
6676
Bram Moolenaar86b68352004-12-27 21:59:20 +00006677 if (*eap->arg == NUL)
6678 buf = curbuf;
6679 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6680 buf = buflist_findnr(atoi((char *)eap->arg));
6681 if (buf == NULL)
6682 EMSG(_(e_invarg));
6683 else if (buf->b_ml.ml_mfp == NULL)
6684 EMSG(_("E681: Buffer is not loaded"));
6685 else
6686 {
6687 if (eap->addr_count == 0)
6688 {
6689 eap->line1 = 1;
6690 eap->line2 = buf->b_ml.ml_line_count;
6691 }
6692 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6693 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6694 EMSG(_(e_invrange));
6695 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006696 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006697 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006698
6699 if (buf->b_sfname)
6700 {
6701 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6702 (char *)qf_title, (char *)buf->b_sfname);
6703 qf_title = IObuff;
6704 }
6705
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006706 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006707 (eap->cmdidx != CMD_caddbuffer
6708 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006709 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006710 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006711 if (res >= 0)
6712 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006713
6714 // Remember the current quickfix list identifier, so that we can
6715 // check for autocommands changing the current quickfix list.
6716 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006717 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006718 {
6719 buf_T *curbuf_old = curbuf;
6720
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006721 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6722 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006723 if (curbuf != curbuf_old)
6724 // Autocommands changed buffer, don't jump now, "qi" may
6725 // be invalid.
6726 res = 0;
6727 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006728 // Jump to the first error for a new list and if autocmds didn't
6729 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006730 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006731 eap->cmdidx == CMD_lbuffer)
6732 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006733 // display the first error
6734 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar754b5602006-02-09 23:53:20 +00006735 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006736 }
6737}
6738
Bram Moolenaar1e015462005-09-25 22:16:38 +00006739#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006740/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006741 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6742 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006743 */
6744 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006745ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006746{
6747 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006748 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006749 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006750 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006751 int_u save_qfid;
6752 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006753
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006754 switch (eap->cmdidx)
6755 {
6756 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6757 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6758 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6759 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6760 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6761 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6762 default: break;
6763 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006764 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6765 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006766 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006767#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006768 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006769 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006770#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006771 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006772
Bram Moolenaar39665952018-08-15 20:59:48 +02006773 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006774 {
6775 qi = ll_get_or_alloc_list(curwin);
6776 if (qi == NULL)
6777 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006778 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006779 }
6780
Bram Moolenaar4770d092006-01-12 23:22:24 +00006781 /* Evaluate the expression. When the result is a string or a list we can
6782 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006783 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006784 if (tv != NULL)
6785 {
6786 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6787 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6788 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006789 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006790 (eap->cmdidx != CMD_caddexpr
6791 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006792 (linenr_T)0, (linenr_T)0,
6793 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006794 if (res >= 0)
6795 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006796
6797 // Remember the current quickfix list identifier, so that we can
6798 // check for autocommands changing the current quickfix list.
6799 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006800 if (au_name != NULL)
6801 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6802 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006803
6804 // Jump to the first error for a new list and if autocmds didn't
6805 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006806 if (res > 0 && (eap->cmdidx == CMD_cexpr
6807 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006808 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006809 // display the first error
6810 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006811 }
6812 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006813 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006814 free_tv(tv);
6815 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006816}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006817#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006818
6819/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006820 * Get the location list for ":lhelpgrep"
6821 */
6822 static qf_info_T *
6823hgr_get_ll(int *new_ll)
6824{
6825 win_T *wp;
6826 qf_info_T *qi;
6827
6828 /* If the current window is a help window, then use it */
6829 if (bt_help(curwin->w_buffer))
6830 wp = curwin;
6831 else
6832 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006833 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006834
6835 if (wp == NULL) /* Help window not found */
6836 qi = NULL;
6837 else
6838 qi = wp->w_llist;
6839
6840 if (qi == NULL)
6841 {
6842 /* Allocate a new location list for help text matches */
6843 if ((qi = ll_new_list()) == NULL)
6844 return NULL;
6845 *new_ll = TRUE;
6846 }
6847
6848 return qi;
6849}
6850
6851/*
6852 * Search for a pattern in a help file.
6853 */
6854 static void
6855hgr_search_file(
6856 qf_info_T *qi,
6857 char_u *fname,
6858#ifdef FEAT_MBYTE
6859 vimconv_T *p_vc,
6860#endif
6861 regmatch_T *p_regmatch)
6862{
6863 FILE *fd;
6864 long lnum;
6865
6866 fd = mch_fopen((char *)fname, "r");
6867 if (fd == NULL)
6868 return;
6869
6870 lnum = 1;
6871 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6872 {
6873 char_u *line = IObuff;
6874#ifdef FEAT_MBYTE
6875 /* Convert a line if 'encoding' is not utf-8 and
6876 * the line contains a non-ASCII character. */
6877 if (p_vc->vc_type != CONV_NONE
6878 && has_non_ascii(IObuff))
6879 {
6880 line = string_convert(p_vc, IObuff, NULL);
6881 if (line == NULL)
6882 line = IObuff;
6883 }
6884#endif
6885
6886 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6887 {
6888 int l = (int)STRLEN(line);
6889
6890 /* remove trailing CR, LF, spaces, etc. */
6891 while (l > 0 && line[l - 1] <= ' ')
6892 line[--l] = NUL;
6893
6894 if (qf_add_entry(qi,
6895 qi->qf_curlist,
6896 NULL, /* dir */
6897 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006898 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006899 0,
6900 line,
6901 lnum,
6902 (int)(p_regmatch->startp[0] - line)
6903 + 1, /* col */
6904 FALSE, /* vis_col */
6905 NULL, /* search pattern */
6906 0, /* nr */
6907 1, /* type */
6908 TRUE /* valid */
6909 ) == FAIL)
6910 {
6911 got_int = TRUE;
6912#ifdef FEAT_MBYTE
6913 if (line != IObuff)
6914 vim_free(line);
6915#endif
6916 break;
6917 }
6918 }
6919#ifdef FEAT_MBYTE
6920 if (line != IObuff)
6921 vim_free(line);
6922#endif
6923 ++lnum;
6924 line_breakcheck();
6925 }
6926 fclose(fd);
6927}
6928
6929/*
6930 * Search for a pattern in all the help files in the doc directory under
6931 * the given directory.
6932 */
6933 static void
6934hgr_search_files_in_dir(
6935 qf_info_T *qi,
6936 char_u *dirname,
6937 regmatch_T *p_regmatch
6938#ifdef FEAT_MBYTE
6939 , vimconv_T *p_vc
6940#endif
6941#ifdef FEAT_MULTI_LANG
6942 , char_u *lang
6943#endif
6944 )
6945{
6946 int fcount;
6947 char_u **fnames;
6948 int fi;
6949
6950 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6951 add_pathsep(dirname);
6952 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6953 if (gen_expand_wildcards(1, &dirname, &fcount,
6954 &fnames, EW_FILE|EW_SILENT) == OK
6955 && fcount > 0)
6956 {
6957 for (fi = 0; fi < fcount && !got_int; ++fi)
6958 {
6959#ifdef FEAT_MULTI_LANG
6960 /* Skip files for a different language. */
6961 if (lang != NULL
6962 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006963 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006964 && !(STRNICMP(lang, "en", 2) == 0
6965 && STRNICMP("txt", fnames[fi]
6966 + STRLEN(fnames[fi]) - 3, 3) == 0))
6967 continue;
6968#endif
6969
6970 hgr_search_file(qi, fnames[fi],
6971#ifdef FEAT_MBYTE
6972 p_vc,
6973#endif
6974 p_regmatch);
6975 }
6976 FreeWild(fcount, fnames);
6977 }
6978}
6979
6980/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006981 * Search for a pattern in all the help files in the 'runtimepath'
6982 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006983 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006984 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006985 */
6986 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006987hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006988{
6989 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006990
6991#ifdef FEAT_MBYTE
6992 vimconv_T vc;
6993
6994 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6995 * differs. */
6996 vc.vc_type = CONV_NONE;
6997 if (!enc_utf8)
6998 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6999#endif
7000
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007001
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007002 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007003 p = p_rtp;
7004 while (*p != NUL && !got_int)
7005 {
7006 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7007
7008 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
7009#ifdef FEAT_MBYTE
7010 , &vc
7011#endif
7012#ifdef FEAT_MULTI_LANG
7013 , lang
7014#endif
7015 );
7016 }
7017
7018#ifdef FEAT_MBYTE
7019 if (vc.vc_type != CONV_NONE)
7020 convert_setup(&vc, NULL, NULL);
7021#endif
7022}
7023
7024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 * ":helpgrep {pattern}"
7026 */
7027 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007028ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 regmatch_T regmatch;
7031 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007032 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007033 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007034 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007035 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036
Bram Moolenaar73633f82012-01-20 13:39:07 +01007037 switch (eap->cmdidx)
7038 {
7039 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7040 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7041 default: break;
7042 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007043 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7044 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007045 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007046#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007047 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007048 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007049#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007050 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007051
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007052 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007053 save_cpo = p_cpo;
7054 p_cpo = empty_option;
7055
Bram Moolenaar39665952018-08-15 20:59:48 +02007056 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007057 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007058 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007059 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007060 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007061 }
7062
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007063#ifdef FEAT_MULTI_LANG
7064 // Check for a specified language
7065 lang = check_help_lang(eap->arg);
7066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7068 regmatch.rm_ic = FALSE;
7069 if (regmatch.regprog != NULL)
7070 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007071 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007072 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007074 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007075
Bram Moolenaar473de612013-06-08 18:19:48 +02007076 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007078 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
7079 qi->qf_lists[qi->qf_curlist].qf_ptr =
7080 qi->qf_lists[qi->qf_curlist].qf_start;
7081 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 }
7083
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007084 if (p_cpo == empty_option)
7085 p_cpo = save_cpo;
7086 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007087 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007088 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
Bram Moolenaarb254af32017-12-18 19:48:58 +01007090 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02007091 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
Bram Moolenaar73633f82012-01-20 13:39:07 +01007093 if (au_name != NULL)
7094 {
7095 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7096 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007097 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01007098 /* autocommands made "qi" invalid */
7099 return;
7100 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007101
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007103 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007104 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007105 else
7106 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007107
7108 if (eap->cmdidx == CMD_lhelpgrep)
7109 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007110 // If the help window is not opened or if it already points to the
7111 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007112 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007113 {
7114 if (new_qi)
7115 ll_free_all(&qi);
7116 }
7117 else if (curwin->w_llist == NULL)
7118 curwin->w_llist = qi;
7119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120}
7121
7122#endif /* FEAT_QUICKFIX */