blob: fe333766df2fc1b314573ae3dc8599b4254c8942 [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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void qf_free(qf_info_T *qi, int idx);
140static 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 Moolenaara7df8c72017-07-19 13:23:06 +0200144static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, 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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200151static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static char_u *get_mef_name(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
154static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
155static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
156static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000158/* Quickfix window check helper macro */
159#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
160/* Location list window check helper macro */
161#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200162
163// Quickfix and location list stack check helper macros
164#define IS_QF_STACK(qi) (qi == &ql_info)
165#define IS_LL_STACK(qi) (qi != &ql_info)
166
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000167/*
168 * Return location list for window 'wp'
169 * For location list window, return the referenced location list
170 */
171#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
172
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200174 * Looking up a buffer can be slow if there are many. Remember the last one
175 * to make this a lot faster if there are multiple matches in the same file.
176 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200177static char_u *qf_last_bufname = NULL;
178static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200179
Bram Moolenaar3c097222017-12-21 20:54:49 +0100180static char *e_loc_list_changed =
181 N_("E926: Current location list was changed");
182
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200183/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200184 * Maximum number of bytes allowed per line while reading a errorfile.
185 */
186#define LINE_MAXLEN 4096
187
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200188static struct fmtpattern
189{
190 char_u convchar;
191 char *pattern;
192} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200193 {
194 {'f', ".\\+"}, /* only used when at end */
195 {'n', "\\d\\+"},
196 {'l', "\\d\\+"},
197 {'c', "\\d\\+"},
198 {'t', "."},
199 {'m', ".\\+"},
200 {'r', ".*"},
201 {'p', "[- .]*"},
202 {'v', "\\d\\+"},
203 {'s', ".\\+"},
204 {'o', ".\\+"}
205 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200206
207/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200208 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200209 * See fmt_pat definition above for the list of supported patterns. The
210 * pattern specifier is supplied in "efmpat". The converted pattern is stored
211 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200212 */
213 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200214efmpat_to_regpat(
215 char_u *efmpat,
216 char_u *regpat,
217 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200218 int idx,
219 int round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200220 char_u *errmsg)
221{
222 char_u *srcptr;
223
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200224 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200225 {
226 /* Each errorformat pattern can occur only once */
227 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200228 _("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200229 EMSG(errmsg);
230 return NULL;
231 }
232 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200233 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200234 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200235 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200236 {
237 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200238 _("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200239 EMSG(errmsg);
240 return NULL;
241 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200242 efminfo->addr[idx] = (char_u)++round;
243 *regpat++ = '\\';
244 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200245#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200246 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200247 {
248 /* Also match "c:" in the file name, even when
249 * checking for a colon next: "%f:".
250 * "\%(\a:\)\=" */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200251 STRCPY(regpat, "\\%(\\a:\\)\\=");
252 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200253 }
254#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200255 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200256 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200257 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200258 {
259 /* A file name may contain spaces, but this isn't
260 * in "\f". For "%f:%l:%m" there may be a ":" in
261 * the file name. Use ".\{-1,}x" instead (x is
262 * the next character), the requirement that :999:
263 * follows should work. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200264 STRCPY(regpat, ".\\{-1,}");
265 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200266 }
267 else
268 {
269 /* File name followed by '\\' or '%': include as
270 * many file name chars as possible. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200271 STRCPY(regpat, "\\f\\+");
272 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200273 }
274 }
275 else
276 {
277 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200278 while ((*regpat = *srcptr++) != NUL)
279 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200280 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200281 *regpat++ = '\\';
282 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200283
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200284 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200285}
286
287/*
288 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200289 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200290 */
291 static char_u *
292scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200293 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200294 char_u *efm,
295 int len,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200296 char_u *regpat,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200297 char_u *errmsg)
298{
299 char_u *efmp = *pefmp;
300
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200301 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200302 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200303 if ((*regpat++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200304 {
305 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200306 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200307 if (efmp < efm + len)
308 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200309 *regpat++ = *++efmp; /* could be ']' */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200310 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200311 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200312 /* skip */;
313 if (efmp == efm + len)
314 {
315 EMSG(_("E374: Missing ] in format string"));
316 return NULL;
317 }
318 }
319 }
320 else if (efmp < efm + len) /* %*\D, %*\s etc. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200321 *regpat++ = *++efmp;
322 *regpat++ = '\\';
323 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200324 }
325 else
326 {
327 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
328 sprintf((char *)errmsg,
329 _("E375: Unsupported %%%c in format string"), *efmp);
330 EMSG(errmsg);
331 return NULL;
332 }
333
334 *pefmp = efmp;
335
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200336 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200337}
338
339/*
340 * Analyze/parse an errorformat prefix.
341 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200342 static char_u *
343efm_analyze_prefix(char_u *efmp, efm_T *efminfo, char_u *errmsg)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200344{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200345 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200346 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200347 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200348 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200349 else
350 {
351 sprintf((char *)errmsg,
352 _("E376: Invalid %%%c in format string prefix"), *efmp);
353 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200354 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200355 }
356
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200357 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200358}
359
360/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200361 * Converts a 'errorformat' string part in 'efm' to a regular expression
362 * pattern. The resulting regex pattern is returned in "regpat". Additional
363 * information about the 'erroformat' pattern is returned in "fmt_ptr".
364 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200365 */
366 static int
367efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200368 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200369 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200370 efm_T *fmt_ptr,
371 char_u *regpat,
372 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200373{
374 char_u *ptr;
375 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200376 int round;
377 int idx = 0;
378
379 /*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200380 * Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200381 */
382 ptr = regpat;
383 *ptr++ = '^';
384 round = 0;
385 for (efmp = efm; efmp < efm + len; ++efmp)
386 {
387 if (*efmp == '%')
388 {
389 ++efmp;
390 for (idx = 0; idx < FMT_PATTERNS; ++idx)
391 if (fmt_pat[idx].convchar == *efmp)
392 break;
393 if (idx < FMT_PATTERNS)
394 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200395 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200396 errmsg);
397 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200398 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200399 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200400 }
401 else if (*efmp == '*')
402 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200403 ++efmp;
404 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr, errmsg);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200405 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200406 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200407 }
408 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
409 *ptr++ = *efmp; /* regexp magic characters */
410 else if (*efmp == '#')
411 *ptr++ = '*';
412 else if (*efmp == '>')
413 fmt_ptr->conthere = TRUE;
414 else if (efmp == efm + 1) /* analyse prefix */
415 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200416 /*
417 * prefix is allowed only at the beginning of the errorformat
418 * option part
419 */
420 efmp = efm_analyze_prefix(efmp, fmt_ptr, errmsg);
421 if (efmp == NULL)
422 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200423 }
424 else
425 {
426 sprintf((char *)errmsg,
427 _("E377: Invalid %%%c in format string"), *efmp);
428 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200429 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200430 }
431 }
432 else /* copy normal character */
433 {
434 if (*efmp == '\\' && efmp + 1 < efm + len)
435 ++efmp;
436 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
437 *ptr++ = '\\'; /* escape regexp atoms */
438 if (*efmp)
439 *ptr++ = *efmp;
440 }
441 }
442 *ptr++ = '$';
443 *ptr = NUL;
444
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200445 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200446}
447
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200448/*
449 * Free the 'errorformat' information list
450 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200451 static void
452free_efm_list(efm_T **efm_first)
453{
454 efm_T *efm_ptr;
455
456 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
457 {
458 *efm_first = efm_ptr->next;
459 vim_regfree(efm_ptr->prog);
460 vim_free(efm_ptr);
461 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100462 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200463}
464
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200465/*
466 * Compute the size of the buffer used to convert a 'errorformat' pattern into
467 * a regular expression pattern.
468 */
469 static int
470efm_regpat_bufsz(char_u *efm)
471{
472 int sz;
473 int i;
474
475 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
476 for (i = FMT_PATTERNS; i > 0; )
477 sz += (int)STRLEN(fmt_pat[--i].pattern);
478#ifdef BACKSLASH_IN_FILENAME
479 sz += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
480#else
481 sz += 2; /* "%f" can become two chars longer */
482#endif
483
484 return sz;
485}
486
487/*
488 * Return the length of a 'errorformat' option part (separated by ",").
489 */
490 static int
491efm_option_part_len(char_u *efm)
492{
493 int len;
494
495 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
496 if (efm[len] == '\\' && efm[len + 1] != NUL)
497 ++len;
498
499 return len;
500}
501
502/*
503 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
504 * are parsed and converted to regular expressions. Returns information about
505 * the parsed 'errorformat' option.
506 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200507 static efm_T *
508parse_efm_option(char_u *efm)
509{
510 char_u *errmsg = NULL;
511 int errmsglen;
512 efm_T *fmt_ptr = NULL;
513 efm_T *fmt_first = NULL;
514 efm_T *fmt_last = NULL;
515 char_u *fmtstr = NULL;
516 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200517 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200518
519 errmsglen = CMDBUFFSIZE + 1;
520 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
521 if (errmsg == NULL)
522 goto parse_efm_end;
523
524 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200525 * Each part of the format string is copied and modified from errorformat
526 * to regex prog. Only a few % characters are allowed.
527 */
528
529 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200530 * Get some space to modify the format string into.
531 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200532 sz = efm_regpat_bufsz(efm);
533 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200534 goto parse_efm_error;
535
536 while (efm[0] != NUL)
537 {
538 /*
539 * Allocate a new eformat structure and put it at the end of the list
540 */
541 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
542 if (fmt_ptr == NULL)
543 goto parse_efm_error;
544 if (fmt_first == NULL) /* first one */
545 fmt_first = fmt_ptr;
546 else
547 fmt_last->next = fmt_ptr;
548 fmt_last = fmt_ptr;
549
550 /*
551 * Isolate one part in the 'errorformat' option
552 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200553 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200554
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200555 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200556 goto parse_efm_error;
557 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
558 goto parse_efm_error;
559 /*
560 * Advance to next part
561 */
562 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
563 }
564
565 if (fmt_first == NULL) /* nothing found */
566 EMSG(_("E378: 'errorformat' contains no pattern"));
567
568 goto parse_efm_end;
569
570parse_efm_error:
571 free_efm_list(&fmt_first);
572
573parse_efm_end:
574 vim_free(fmtstr);
575 vim_free(errmsg);
576
577 return fmt_first;
578}
579
Bram Moolenaare0d37972016-07-15 22:36:01 +0200580enum {
581 QF_FAIL = 0,
582 QF_OK = 1,
583 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200584 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200585 QF_IGNORE_LINE = 4,
586 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200587};
588
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200589/*
590 * State information used to parse lines and add entries to a quickfix/location
591 * list.
592 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200593typedef struct {
594 char_u *linebuf;
595 int linelen;
596 char_u *growbuf;
597 int growbufsiz;
598 FILE *fd;
599 typval_T *tv;
600 char_u *p_str;
601 listitem_T *p_li;
602 buf_T *buf;
603 linenr_T buflnum;
604 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100605 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200606} qfstate_T;
607
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200608/*
609 * Allocate more memory for the line buffer used for parsing lines.
610 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200611 static char_u *
612qf_grow_linebuf(qfstate_T *state, int newsz)
613{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200614 char_u *p;
615
Bram Moolenaare0d37972016-07-15 22:36:01 +0200616 /*
617 * If the line exceeds LINE_MAXLEN exclude the last
618 * byte since it's not a NL character.
619 */
620 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
621 if (state->growbuf == NULL)
622 {
623 state->growbuf = alloc(state->linelen + 1);
624 if (state->growbuf == NULL)
625 return NULL;
626 state->growbufsiz = state->linelen;
627 }
628 else if (state->linelen > state->growbufsiz)
629 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200630 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200631 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200632 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200633 state->growbufsiz = state->linelen;
634 }
635 return state->growbuf;
636}
637
638/*
639 * Get the next string (separated by newline) from state->p_str.
640 */
641 static int
642qf_get_next_str_line(qfstate_T *state)
643{
644 /* Get the next line from the supplied string */
645 char_u *p_str = state->p_str;
646 char_u *p;
647 int len;
648
649 if (*p_str == NUL) /* Reached the end of the string */
650 return QF_END_OF_INPUT;
651
652 p = vim_strchr(p_str, '\n');
653 if (p != NULL)
654 len = (int)(p - p_str) + 1;
655 else
656 len = (int)STRLEN(p_str);
657
658 if (len > IOSIZE - 2)
659 {
660 state->linebuf = qf_grow_linebuf(state, len);
661 if (state->linebuf == NULL)
662 return QF_NOMEM;
663 }
664 else
665 {
666 state->linebuf = IObuff;
667 state->linelen = len;
668 }
669 vim_strncpy(state->linebuf, p_str, state->linelen);
670
671 /*
672 * Increment using len in order to discard the rest of the
673 * line if it exceeds LINE_MAXLEN.
674 */
675 p_str += len;
676 state->p_str = p_str;
677
678 return QF_OK;
679}
680
681/*
682 * Get the next string from state->p_Li.
683 */
684 static int
685qf_get_next_list_line(qfstate_T *state)
686{
687 listitem_T *p_li = state->p_li;
688 int len;
689
690 while (p_li != NULL
691 && (p_li->li_tv.v_type != VAR_STRING
692 || p_li->li_tv.vval.v_string == NULL))
693 p_li = p_li->li_next; /* Skip non-string items */
694
695 if (p_li == NULL) /* End of the list */
696 {
697 state->p_li = NULL;
698 return QF_END_OF_INPUT;
699 }
700
701 len = (int)STRLEN(p_li->li_tv.vval.v_string);
702 if (len > IOSIZE - 2)
703 {
704 state->linebuf = qf_grow_linebuf(state, len);
705 if (state->linebuf == NULL)
706 return QF_NOMEM;
707 }
708 else
709 {
710 state->linebuf = IObuff;
711 state->linelen = len;
712 }
713
714 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
715
716 state->p_li = p_li->li_next; /* next item */
717 return QF_OK;
718}
719
720/*
721 * Get the next string from state->buf.
722 */
723 static int
724qf_get_next_buf_line(qfstate_T *state)
725{
726 char_u *p_buf = NULL;
727 int len;
728
729 /* Get the next line from the supplied buffer */
730 if (state->buflnum > state->lnumlast)
731 return QF_END_OF_INPUT;
732
733 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
734 state->buflnum += 1;
735
736 len = (int)STRLEN(p_buf);
737 if (len > IOSIZE - 2)
738 {
739 state->linebuf = qf_grow_linebuf(state, len);
740 if (state->linebuf == NULL)
741 return QF_NOMEM;
742 }
743 else
744 {
745 state->linebuf = IObuff;
746 state->linelen = len;
747 }
748 vim_strncpy(state->linebuf, p_buf, state->linelen);
749
750 return QF_OK;
751}
752
753/*
754 * Get the next string from file state->fd.
755 */
756 static int
757qf_get_next_file_line(qfstate_T *state)
758{
759 int discard;
760 int growbuflen;
761
762 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
763 return QF_END_OF_INPUT;
764
765 discard = FALSE;
766 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200767 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200768 {
769 /*
770 * The current line exceeds IObuff, continue reading using
771 * growbuf until EOL or LINE_MAXLEN bytes is read.
772 */
773 if (state->growbuf == NULL)
774 {
775 state->growbufsiz = 2 * (IOSIZE - 1);
776 state->growbuf = alloc(state->growbufsiz);
777 if (state->growbuf == NULL)
778 return QF_NOMEM;
779 }
780
781 /* Copy the read part of the line, excluding null-terminator */
782 memcpy(state->growbuf, IObuff, IOSIZE - 1);
783 growbuflen = state->linelen;
784
785 for (;;)
786 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200787 char_u *p;
788
Bram Moolenaare0d37972016-07-15 22:36:01 +0200789 if (fgets((char *)state->growbuf + growbuflen,
790 state->growbufsiz - growbuflen, state->fd) == NULL)
791 break;
792 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
793 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200794 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200795 break;
796 if (state->growbufsiz == LINE_MAXLEN)
797 {
798 discard = TRUE;
799 break;
800 }
801
802 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
803 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200804 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200805 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200806 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200807 }
808
809 while (discard)
810 {
811 /*
812 * The current line is longer than LINE_MAXLEN, continue
813 * reading but discard everything until EOL or EOF is
814 * reached.
815 */
816 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
817 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200818 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200819 break;
820 }
821
822 state->linebuf = state->growbuf;
823 state->linelen = growbuflen;
824 }
825 else
826 state->linebuf = IObuff;
827
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100828#ifdef FEAT_MBYTE
829 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200830 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
831 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100832 char_u *line;
833
834 line = string_convert(&state->vc, state->linebuf, &state->linelen);
835 if (line != NULL)
836 {
837 if (state->linelen < IOSIZE)
838 {
839 STRCPY(state->linebuf, line);
840 vim_free(line);
841 }
842 else
843 {
844 vim_free(state->growbuf);
845 state->linebuf = state->growbuf = line;
846 state->growbufsiz = state->linelen < LINE_MAXLEN
847 ? state->linelen : LINE_MAXLEN;
848 }
849 }
850 }
851#endif
852
Bram Moolenaare0d37972016-07-15 22:36:01 +0200853 return QF_OK;
854}
855
856/*
857 * Get the next string from a file/buffer/list/string.
858 */
859 static int
860qf_get_nextline(qfstate_T *state)
861{
862 int status = QF_FAIL;
863
864 if (state->fd == NULL)
865 {
866 if (state->tv != NULL)
867 {
868 if (state->tv->v_type == VAR_STRING)
869 /* Get the next line from the supplied string */
870 status = qf_get_next_str_line(state);
871 else if (state->tv->v_type == VAR_LIST)
872 /* Get the next line from the supplied list */
873 status = qf_get_next_list_line(state);
874 }
875 else
876 /* Get the next line from the supplied buffer */
877 status = qf_get_next_buf_line(state);
878 }
879 else
880 /* Get the next line from the supplied file */
881 status = qf_get_next_file_line(state);
882
883 if (status != QF_OK)
884 return status;
885
886 /* remove newline/CR from the line */
887 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200888 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200889 state->linebuf[state->linelen - 1] = NUL;
890#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200891 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
892 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200893#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200894 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200895
896#ifdef FEAT_MBYTE
897 remove_bom(state->linebuf);
898#endif
899
900 return QF_OK;
901}
902
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200903typedef struct {
904 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200905 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200906 char_u *errmsg;
907 int errmsglen;
908 long lnum;
909 int col;
910 char_u use_viscol;
911 char_u *pattern;
912 int enr;
913 int type;
914 int valid;
915} qffields_T;
916
917/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200918 * Parse the match for filename ('%f') pattern in regmatch.
919 * Return the matched value in "fields->namebuf".
920 */
921 static int
922qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
923{
924 int c;
925
926 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
927 return QF_FAIL;
928
929 /* Expand ~/file and $HOME/file to full path. */
930 c = *rmp->endp[midx];
931 *rmp->endp[midx] = NUL;
932 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
933 *rmp->endp[midx] = c;
934
935 /*
936 * For separate filename patterns (%O, %P and %Q), the specified file
937 * should exist.
938 */
939 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
940 && mch_getperm(fields->namebuf) == -1)
941 return QF_FAIL;
942
943 return QF_OK;
944}
945
946/*
947 * Parse the match for error number ('%n') pattern in regmatch.
948 * Return the matched value in "fields->enr".
949 */
950 static int
951qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
952{
953 if (rmp->startp[midx] == NULL)
954 return QF_FAIL;
955 fields->enr = (int)atol((char *)rmp->startp[midx]);
956 return QF_OK;
957}
958
959/*
960 * Parse the match for line number (%l') pattern in regmatch.
961 * Return the matched value in "fields->lnum".
962 */
963 static int
964qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
965{
966 if (rmp->startp[midx] == NULL)
967 return QF_FAIL;
968 fields->lnum = atol((char *)rmp->startp[midx]);
969 return QF_OK;
970}
971
972/*
973 * Parse the match for column number ('%c') pattern in regmatch.
974 * Return the matched value in "fields->col".
975 */
976 static int
977qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
978{
979 if (rmp->startp[midx] == NULL)
980 return QF_FAIL;
981 fields->col = (int)atol((char *)rmp->startp[midx]);
982 return QF_OK;
983}
984
985/*
986 * Parse the match for error type ('%t') pattern in regmatch.
987 * Return the matched value in "fields->type".
988 */
989 static int
990qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
991{
992 if (rmp->startp[midx] == NULL)
993 return QF_FAIL;
994 fields->type = *rmp->startp[midx];
995 return QF_OK;
996}
997
998/*
999 * Parse the match for '%+' format pattern. The whole matching line is included
1000 * in the error string. Return the matched line in "fields->errmsg".
1001 */
1002 static int
1003qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
1004{
1005 char_u *p;
1006
1007 if (linelen >= fields->errmsglen)
1008 {
1009 /* linelen + null terminator */
1010 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1011 return QF_NOMEM;
1012 fields->errmsg = p;
1013 fields->errmsglen = linelen + 1;
1014 }
1015 vim_strncpy(fields->errmsg, linebuf, linelen);
1016 return QF_OK;
1017}
1018
1019/*
1020 * Parse the match for error message ('%m') pattern in regmatch.
1021 * Return the matched value in "fields->errmsg".
1022 */
1023 static int
1024qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1025{
1026 char_u *p;
1027 int len;
1028
1029 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1030 return QF_FAIL;
1031 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1032 if (len >= fields->errmsglen)
1033 {
1034 /* len + null terminator */
1035 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1036 return QF_NOMEM;
1037 fields->errmsg = p;
1038 fields->errmsglen = len + 1;
1039 }
1040 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1041 return QF_OK;
1042}
1043
1044/*
1045 * Parse the match for rest of a single-line file message ('%r') pattern.
1046 * Return the matched value in "tail".
1047 */
1048 static int
1049qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1050{
1051 if (rmp->startp[midx] == NULL)
1052 return QF_FAIL;
1053 *tail = rmp->startp[midx];
1054 return QF_OK;
1055}
1056
1057/*
1058 * Parse the match for the pointer line ('%p') pattern in regmatch.
1059 * Return the matched value in "fields->col".
1060 */
1061 static int
1062qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1063{
1064 char_u *match_ptr;
1065
1066 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1067 return QF_FAIL;
1068 fields->col = 0;
1069 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1070 ++match_ptr)
1071 {
1072 ++fields->col;
1073 if (*match_ptr == TAB)
1074 {
1075 fields->col += 7;
1076 fields->col -= fields->col % 8;
1077 }
1078 }
1079 ++fields->col;
1080 fields->use_viscol = TRUE;
1081 return QF_OK;
1082}
1083
1084/*
1085 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1086 * Return the matched value in "fields->col".
1087 */
1088 static int
1089qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1090{
1091 if (rmp->startp[midx] == NULL)
1092 return QF_FAIL;
1093 fields->col = (int)atol((char *)rmp->startp[midx]);
1094 fields->use_viscol = TRUE;
1095 return QF_OK;
1096}
1097
1098/*
1099 * Parse the match for the search text ('%s') pattern in regmatch.
1100 * Return the matched value in "fields->pattern".
1101 */
1102 static int
1103qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1104{
1105 int len;
1106
1107 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1108 return QF_FAIL;
1109 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1110 if (len > CMDBUFFSIZE - 5)
1111 len = CMDBUFFSIZE - 5;
1112 STRCPY(fields->pattern, "^\\V");
1113 STRNCAT(fields->pattern, rmp->startp[midx], len);
1114 fields->pattern[len + 3] = '\\';
1115 fields->pattern[len + 4] = '$';
1116 fields->pattern[len + 5] = NUL;
1117 return QF_OK;
1118}
1119
1120/*
1121 * Parse the match for the module ('%o') pattern in regmatch.
1122 * Return the matched value in "fields->module".
1123 */
1124 static int
1125qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1126{
1127 int len;
1128
1129 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1130 return QF_FAIL;
1131 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1132 if (len > CMDBUFFSIZE)
1133 len = CMDBUFFSIZE;
1134 STRNCAT(fields->module, rmp->startp[midx], len);
1135 return QF_OK;
1136}
1137
1138/*
1139 * 'errorformat' format pattern parser functions.
1140 * The '%f' and '%r' formats are parsed differently from other formats.
1141 * See qf_parse_match() for details.
1142 */
1143static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1144{
1145 NULL,
1146 qf_parse_fmt_n,
1147 qf_parse_fmt_l,
1148 qf_parse_fmt_c,
1149 qf_parse_fmt_t,
1150 qf_parse_fmt_m,
1151 NULL,
1152 qf_parse_fmt_p,
1153 qf_parse_fmt_v,
1154 qf_parse_fmt_s,
1155 qf_parse_fmt_o
1156};
1157
1158/*
1159 * Parse the error format pattern matches in "regmatch" and set the values in
1160 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1161 * match. Returns QF_OK if all the matches are successfully parsed. On
1162 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001163 */
1164 static int
1165qf_parse_match(
1166 char_u *linebuf,
1167 int linelen,
1168 efm_T *fmt_ptr,
1169 regmatch_T *regmatch,
1170 qffields_T *fields,
1171 int qf_multiline,
1172 int qf_multiscan,
1173 char_u **tail)
1174{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001175 int idx = fmt_ptr->prefix;
1176 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001177 int midx;
1178 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001179
1180 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1181 return QF_FAIL;
1182 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1183 fields->type = idx;
1184 else
1185 fields->type = 0;
1186 /*
1187 * Extract error message data from matched line.
1188 * We check for an actual submatch, because "\[" and "\]" in
1189 * the 'errorformat' may cause the wrong submatch to be used.
1190 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001191 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001192 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001193 status = QF_OK;
1194 midx = (int)fmt_ptr->addr[i];
1195 if (i == 0 && midx > 0) /* %f */
1196 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1197 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001198 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001199 if (fmt_ptr->flags == '+' && !qf_multiscan) /* %+ */
1200 status = qf_parse_fmt_plus(linebuf, linelen, fields);
1201 else if (midx > 0) /* %m */
1202 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001203 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001204 else if (i == 6 && midx > 0) /* %r */
1205 status = qf_parse_fmt_r(regmatch, midx, tail);
1206 else if (midx > 0) /* others */
1207 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001208
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001209 if (status != QF_OK)
1210 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001211 }
1212
1213 return QF_OK;
1214}
1215
1216/*
1217 * Parse an error line in 'linebuf' using a single error format string in
1218 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1219 * Returns QF_OK if the efm format matches completely and the fields are
1220 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1221 */
1222 static int
1223qf_parse_get_fields(
1224 char_u *linebuf,
1225 int linelen,
1226 efm_T *fmt_ptr,
1227 qffields_T *fields,
1228 int qf_multiline,
1229 int qf_multiscan,
1230 char_u **tail)
1231{
1232 regmatch_T regmatch;
1233 int status = QF_FAIL;
1234 int r;
1235
1236 if (qf_multiscan &&
1237 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1238 return QF_FAIL;
1239
1240 fields->namebuf[0] = NUL;
1241 fields->module[0] = NUL;
1242 fields->pattern[0] = NUL;
1243 if (!qf_multiscan)
1244 fields->errmsg[0] = NUL;
1245 fields->lnum = 0;
1246 fields->col = 0;
1247 fields->use_viscol = FALSE;
1248 fields->enr = -1;
1249 fields->type = 0;
1250 *tail = NULL;
1251
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001252 /* Always ignore case when looking for a matching error. */
1253 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001254 regmatch.regprog = fmt_ptr->prog;
1255 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1256 fmt_ptr->prog = regmatch.regprog;
1257 if (r)
1258 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1259 fields, qf_multiline, qf_multiscan, tail);
1260
1261 return status;
1262}
1263
1264/*
1265 * Parse directory error format prefixes (%D and %X).
1266 * Push and pop directories from the directory stack when scanning directory
1267 * names.
1268 */
1269 static int
1270qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1271{
1272 if (idx == 'D') /* enter directory */
1273 {
1274 if (*fields->namebuf == NUL)
1275 {
1276 EMSG(_("E379: Missing or empty directory name"));
1277 return QF_FAIL;
1278 }
1279 qfl->qf_directory =
1280 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1281 if (qfl->qf_directory == NULL)
1282 return QF_FAIL;
1283 }
1284 else if (idx == 'X') /* leave directory */
1285 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1286
1287 return QF_OK;
1288}
1289
1290/*
1291 * Parse global file name error format prefixes (%O, %P and %Q).
1292 */
1293 static int
1294qf_parse_file_pfx(
1295 int idx,
1296 qffields_T *fields,
1297 qf_list_T *qfl,
1298 char_u *tail)
1299{
1300 fields->valid = FALSE;
1301 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1302 {
1303 if (*fields->namebuf && idx == 'P')
1304 qfl->qf_currfile =
1305 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1306 else if (idx == 'Q')
1307 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1308 *fields->namebuf = NUL;
1309 if (tail && *tail)
1310 {
1311 STRMOVE(IObuff, skipwhite(tail));
1312 qfl->qf_multiscan = TRUE;
1313 return QF_MULTISCAN;
1314 }
1315 }
1316
1317 return QF_OK;
1318}
1319
1320/*
1321 * Parse a non-error line (a line which doesn't match any of the error
1322 * format in 'efm').
1323 */
1324 static int
1325qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1326{
1327 char_u *p;
1328
1329 fields->namebuf[0] = NUL; /* no match found, remove file name */
1330 fields->lnum = 0; /* don't jump to this line */
1331 fields->valid = FALSE;
1332 if (linelen >= fields->errmsglen)
1333 {
1334 /* linelen + null terminator */
1335 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1336 return QF_NOMEM;
1337 fields->errmsg = p;
1338 fields->errmsglen = linelen + 1;
1339 }
1340 /* copy whole line to error message */
1341 vim_strncpy(fields->errmsg, linebuf, linelen);
1342
1343 return QF_OK;
1344}
1345
1346/*
1347 * Parse multi-line error format prefixes (%C and %Z)
1348 */
1349 static int
1350qf_parse_multiline_pfx(
1351 qf_info_T *qi,
1352 int qf_idx,
1353 int idx,
1354 qf_list_T *qfl,
1355 qffields_T *fields)
1356{
1357 char_u *ptr;
1358 int len;
1359
1360 if (!qfl->qf_multiignore)
1361 {
1362 qfline_T *qfprev = qfl->qf_last;
1363
1364 if (qfprev == NULL)
1365 return QF_FAIL;
1366 if (*fields->errmsg && !qfl->qf_multiignore)
1367 {
1368 len = (int)STRLEN(qfprev->qf_text);
1369 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1370 == NULL)
1371 return QF_FAIL;
1372 STRCPY(ptr, qfprev->qf_text);
1373 vim_free(qfprev->qf_text);
1374 qfprev->qf_text = ptr;
1375 *(ptr += len) = '\n';
1376 STRCPY(++ptr, fields->errmsg);
1377 }
1378 if (qfprev->qf_nr == -1)
1379 qfprev->qf_nr = fields->enr;
1380 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1381 /* only printable chars allowed */
1382 qfprev->qf_type = fields->type;
1383
1384 if (!qfprev->qf_lnum)
1385 qfprev->qf_lnum = fields->lnum;
1386 if (!qfprev->qf_col)
1387 qfprev->qf_col = fields->col;
1388 qfprev->qf_viscol = fields->use_viscol;
1389 if (!qfprev->qf_fnum)
1390 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1391 qfl->qf_directory,
1392 *fields->namebuf || qfl->qf_directory != NULL
1393 ? fields->namebuf
1394 : qfl->qf_currfile != NULL && fields->valid
1395 ? qfl->qf_currfile : 0);
1396 }
1397 if (idx == 'Z')
1398 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1399 line_breakcheck();
1400
1401 return QF_IGNORE_LINE;
1402}
1403
1404/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001405 * Parse a line and get the quickfix fields.
1406 * Return the QF_ status.
1407 */
1408 static int
1409qf_parse_line(
1410 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001411 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001412 char_u *linebuf,
1413 int linelen,
1414 efm_T *fmt_first,
1415 qffields_T *fields)
1416{
1417 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001418 int idx = 0;
1419 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001420 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001421 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001422
Bram Moolenaare333e792018-04-08 13:27:39 +02001423restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001424 /* If there was no %> item start at the first pattern */
1425 if (fmt_start == NULL)
1426 fmt_ptr = fmt_first;
1427 else
1428 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001429 /* Otherwise start from the last used pattern */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001430 fmt_ptr = fmt_start;
1431 fmt_start = NULL;
1432 }
1433
1434 /*
1435 * Try to match each part of 'errorformat' until we find a complete
1436 * match or no match.
1437 */
1438 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001439 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1440 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001441 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001442 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1443 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1444 if (status == QF_NOMEM)
1445 return status;
1446 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001447 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001448 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001449 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001450
1451 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1452 {
1453 if (fmt_ptr != NULL)
1454 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001455 /* 'D' and 'X' directory specifiers */
1456 status = qf_parse_dir_pfx(idx, fields, qfl);
1457 if (status != QF_OK)
1458 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001459 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001460
1461 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1462 if (status != QF_OK)
1463 return status;
1464
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001465 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001466 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001467 }
1468 else if (fmt_ptr != NULL)
1469 {
1470 /* honor %> item */
1471 if (fmt_ptr->conthere)
1472 fmt_start = fmt_ptr;
1473
1474 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1475 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001476 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1477 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001478 }
1479 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1480 { /* continuation of multi-line msg */
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001481 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1482 if (status != QF_OK)
1483 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001484 }
1485 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001486 { /* global file names */
1487 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1488 if (status == QF_MULTISCAN)
1489 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001490 }
1491 if (fmt_ptr->flags == '-') /* generally exclude this line */
1492 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001493 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001494 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001495 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001496 return QF_IGNORE_LINE;
1497 }
1498 }
1499
1500 return QF_OK;
1501}
1502
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001503/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001504 * Returns TRUE if the specified quickfix/location list is empty.
1505 */
1506 static int
1507qf_list_empty(qf_info_T *qi, int qf_idx)
1508{
1509 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1510 return TRUE;
1511 return qi->qf_lists[qf_idx].qf_count <= 0;
1512}
1513
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001514/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001515 * Allocate the fields used for parsing lines and populating a quickfix list.
1516 */
1517 static int
1518qf_alloc_fields(qffields_T *pfields)
1519{
1520 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1521 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1522 pfields->errmsglen = CMDBUFFSIZE + 1;
1523 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1524 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1525 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1526 || pfields->pattern == NULL || pfields->module == NULL)
1527 return FAIL;
1528
1529 return OK;
1530}
1531
1532/*
1533 * Free the fields used for parsing lines and populating a quickfix list.
1534 */
1535 static void
1536qf_free_fields(qffields_T *pfields)
1537{
1538 vim_free(pfields->namebuf);
1539 vim_free(pfields->module);
1540 vim_free(pfields->errmsg);
1541 vim_free(pfields->pattern);
1542}
1543
1544/*
1545 * Setup the state information used for parsing lines and populating a
1546 * quickfix list.
1547 */
1548 static int
1549qf_setup_state(
1550 qfstate_T *pstate,
1551 char_u *enc,
1552 char_u *efile,
1553 typval_T *tv,
1554 buf_T *buf,
1555 linenr_T lnumfirst,
1556 linenr_T lnumlast)
1557{
1558#ifdef FEAT_MBYTE
1559 pstate->vc.vc_type = CONV_NONE;
1560 if (enc != NULL && *enc != NUL)
1561 convert_setup(&pstate->vc, enc, p_enc);
1562#endif
1563
1564 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1565 {
1566 EMSG2(_(e_openerrf), efile);
1567 return FAIL;
1568 }
1569
1570 if (tv != NULL)
1571 {
1572 if (tv->v_type == VAR_STRING)
1573 pstate->p_str = tv->vval.v_string;
1574 else if (tv->v_type == VAR_LIST)
1575 pstate->p_li = tv->vval.v_list->lv_first;
1576 pstate->tv = tv;
1577 }
1578 pstate->buf = buf;
1579 pstate->buflnum = lnumfirst;
1580 pstate->lnumlast = lnumlast;
1581
1582 return OK;
1583}
1584
1585/*
1586 * Cleanup the state information used for parsing lines and populating a
1587 * quickfix list.
1588 */
1589 static void
1590qf_cleanup_state(qfstate_T *pstate)
1591{
1592 if (pstate->fd != NULL)
1593 fclose(pstate->fd);
1594
1595 vim_free(pstate->growbuf);
1596#ifdef FEAT_MBYTE
1597 if (pstate->vc.vc_type != CONV_NONE)
1598 convert_setup(&pstate->vc, NULL, NULL);
1599#endif
1600}
1601
1602/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001603 * Read the errorfile "efile" into memory, line by line, building the error
1604 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001605 * Alternative: when "efile" is NULL read errors from buffer "buf".
1606 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001607 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001608 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1609 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001610 * Return -1 for error, number of errors for success.
1611 */
1612 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001613qf_init_ext(
1614 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001615 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001616 char_u *efile,
1617 buf_T *buf,
1618 typval_T *tv,
1619 char_u *errorformat,
1620 int newlist, /* TRUE: start a new error list */
1621 linenr_T lnumfirst, /* first line number to use */
1622 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001623 char_u *qf_title,
1624 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001625{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001626 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001627 qfstate_T state;
1628 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001629 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001630 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001631 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001633 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001635 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001637 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001638 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001639
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001640 vim_memset(&state, 0, sizeof(state));
1641 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001642 if ((qf_alloc_fields(&fields) == FAIL) ||
1643 (qf_setup_state(&state, enc, efile, tv, buf,
1644 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 goto qf_init_end;
1646
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001647 if (newlist || qf_idx == qi->qf_listcount)
1648 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001650 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001651 qf_idx = qi->qf_curlist;
1652 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001653 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001654 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001655 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001656 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001657 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001658 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001661 qfl = &qi->qf_lists[qf_idx];
1662
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001664 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001665 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 else
1667 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001669 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001670 * If the errorformat didn't change between calls, then reuse the
1671 * previously parsed values.
1672 */
1673 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1674 {
1675 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001676 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001677 free_efm_list(&fmt_first);
1678
1679 /* parse the current 'efm' */
1680 fmt_first = parse_efm_option(efm);
1681 if (fmt_first != NULL)
1682 last_efm = vim_strsave(efm);
1683 }
1684
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687
1688 /*
1689 * got_int is reset here, because it was probably set when killing the
1690 * ":make" command, but we still want to read the errorfile then.
1691 */
1692 got_int = FALSE;
1693
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 /*
1695 * Read the lines in the error file one by one.
1696 * Try to recognize one of the error formats in each line.
1697 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001698 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001700 /* Get the next line from a file/buffer/list/string */
1701 status = qf_get_nextline(&state);
1702 if (status == QF_NOMEM) /* memory alloc failure */
1703 goto qf_init_end;
1704 if (status == QF_END_OF_INPUT) /* end of input */
1705 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001707 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1708 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001709 if (status == QF_FAIL)
1710 goto error2;
1711 if (status == QF_NOMEM)
1712 goto qf_init_end;
1713 if (status == QF_IGNORE_LINE)
1714 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001716 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001717 qf_idx,
1718 qfl->qf_directory,
1719 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001720 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001721 : ((qfl->qf_currfile != NULL && fields.valid)
1722 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001723 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001724 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001725 fields.errmsg,
1726 fields.lnum,
1727 fields.col,
1728 fields.use_viscol,
1729 fields.pattern,
1730 fields.enr,
1731 fields.type,
1732 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 goto error2;
1734 line_breakcheck();
1735 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001736 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001738 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001740 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001741 qfl->qf_ptr = qfl->qf_start;
1742 qfl->qf_index = 1;
1743 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 }
1745 else
1746 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001747 qfl->qf_nonevalid = FALSE;
1748 if (qfl->qf_ptr == NULL)
1749 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001751 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001752 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001753 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
1755 EMSG(_(e_readerrf));
1756error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001757 if (!adding)
1758 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001759 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001760 qf_free(qi, qi->qf_curlist);
1761 qi->qf_listcount--;
1762 if (qi->qf_curlist > 0)
1763 --qi->qf_curlist;
1764 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001765qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001766 if (qf_idx == qi->qf_curlist)
1767 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001768 qf_cleanup_state(&state);
1769 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
1771 return retval;
1772}
1773
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001774/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001775 * Read the errorfile "efile" into memory, line by line, building the error
1776 * list. Set the error list's title to qf_title.
1777 * Return -1 for error, number of errors for success.
1778 */
1779 int
1780qf_init(win_T *wp,
1781 char_u *efile,
1782 char_u *errorformat,
1783 int newlist, /* TRUE: start a new error list */
1784 char_u *qf_title,
1785 char_u *enc)
1786{
1787 qf_info_T *qi = &ql_info;
1788
1789 if (wp != NULL)
1790 {
1791 qi = ll_get_or_alloc_list(wp);
1792 if (qi == NULL)
1793 return FAIL;
1794 }
1795
1796 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1797 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1798}
1799
1800/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001801 * Set the title of the specified quickfix list. Frees the previous title.
1802 * Prepends ':' to the title.
1803 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001804 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001805qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001806{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001807 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001808
Bram Moolenaarfb604092014-07-23 15:55:00 +02001809 if (title != NULL)
1810 {
1811 char_u *p = alloc((int)STRLEN(title) + 2);
1812
Bram Moolenaara3921f42017-06-04 15:30:34 +02001813 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001814 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001815 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001816 }
1817}
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001820 * The title of a quickfix/location list is set, by default, to the command
1821 * that created the quickfix list with the ":" prefix.
1822 * Create a quickfix list title string by prepending ":" to a user command.
1823 * Returns a pointer to a static buffer with the title.
1824 */
1825 static char_u *
1826qf_cmdtitle(char_u *cmd)
1827{
1828 static char_u qftitle_str[IOSIZE];
1829
1830 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1831 return qftitle_str;
1832}
1833
1834/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001835 * Prepare for adding a new quickfix list. If the current list is in the
1836 * middle of the stack, then all the following lists are freed and then
1837 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 */
1839 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001840qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
1842 int i;
1843
1844 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001845 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 * the current entry. This makes it possible to browse in a tree-like
1847 * way with ":grep'.
1848 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001849 while (qi->qf_listcount > qi->qf_curlist + 1)
1850 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
1852 /*
1853 * When the stack is full, remove to oldest entry
1854 * Otherwise, add a new entry.
1855 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001856 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001858 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001860 qi->qf_lists[i - 1] = qi->qf_lists[i];
1861 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 }
1863 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001864 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001865 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001866 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001867 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868}
1869
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001870/*
1871 * Free a location list
1872 */
1873 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001874ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001875{
1876 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001877 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001878
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001879 qi = *pqi;
1880 if (qi == NULL)
1881 return;
1882 *pqi = NULL; /* Remove reference to this list */
1883
1884 qi->qf_refcount--;
1885 if (qi->qf_refcount < 1)
1886 {
1887 /* No references to this location list */
1888 for (i = 0; i < qi->qf_listcount; ++i)
1889 qf_free(qi, i);
1890 vim_free(qi);
1891 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001892}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001893
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001894/*
1895 * Free all the quickfix/location lists in the stack.
1896 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001897 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001898qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001899{
1900 int i;
1901 qf_info_T *qi = &ql_info;
1902
1903 if (wp != NULL)
1904 {
1905 /* location list */
1906 ll_free_all(&wp->w_llist);
1907 ll_free_all(&wp->w_llist_ref);
1908 }
1909 else
1910 /* quickfix list */
1911 for (i = 0; i < qi->qf_listcount; ++i)
1912 qf_free(qi, i);
1913}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001914
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915/*
1916 * Add an entry to the end of the list of errors.
1917 * Returns OK or FAIL.
1918 */
1919 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001920qf_add_entry(
1921 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001922 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001923 char_u *dir, /* optional directory name */
1924 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001925 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001926 int bufnum, /* buffer number or zero */
1927 char_u *mesg, /* message */
1928 long lnum, /* line number */
1929 int col, /* column */
1930 int vis_col, /* using visual column */
1931 char_u *pattern, /* search pattern */
1932 int nr, /* error number */
1933 int type, /* type character */
1934 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935{
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 Moolenaara3921f42017-06-04 15:30:34 +02001983 lastp = &qi->qf_lists[qf_idx].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 Moolenaara3921f42017-06-04 15:30:34 +02001986 qi->qf_lists[qf_idx].qf_start = qfp;
1987 qi->qf_lists[qf_idx].qf_ptr = qfp;
1988 qi->qf_lists[qf_idx].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 Moolenaara3921f42017-06-04 15:30:34 +02001999 ++qi->qf_lists[qf_idx].qf_count;
2000 if (qi->qf_lists[qf_idx].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 Moolenaara3921f42017-06-04 15:30:34 +02002003 qi->qf_lists[qf_idx].qf_index =
2004 qi->qf_lists[qf_idx].qf_count;
2005 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
2007
2008 return OK;
2009}
2010
2011/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002012 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002013 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002014 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002015ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002016{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002017 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002018
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002019 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002020 if (qi != NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002021 qi->qf_refcount++;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002022 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002023}
2024
2025/*
2026 * Return the location list for window 'wp'.
2027 * If not present, allocate a location list
2028 */
2029 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002030ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002031{
2032 if (IS_LL_WINDOW(wp))
2033 /* For a location list window, use the referenced location list */
2034 return wp->w_llist_ref;
2035
2036 /*
2037 * For a non-location list window, w_llist_ref should not point to a
2038 * location list.
2039 */
2040 ll_free_all(&wp->w_llist_ref);
2041
2042 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002043 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002044 return wp->w_llist;
2045}
2046
2047/*
2048 * Copy the location list from window "from" to window "to".
2049 */
2050 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002051copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002052{
2053 qf_info_T *qi;
2054 int idx;
2055 int i;
2056
2057 /*
2058 * When copying from a location list window, copy the referenced
2059 * location list. For other windows, copy the location list for
2060 * that window.
2061 */
2062 if (IS_LL_WINDOW(from))
2063 qi = from->w_llist_ref;
2064 else
2065 qi = from->w_llist;
2066
2067 if (qi == NULL) /* no location list to copy */
2068 return;
2069
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002070 /* allocate a new location list */
2071 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002072 return;
2073
2074 to->w_llist->qf_listcount = qi->qf_listcount;
2075
2076 /* Copy the location lists one at a time */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002077 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002078 {
2079 qf_list_T *from_qfl;
2080 qf_list_T *to_qfl;
2081
2082 to->w_llist->qf_curlist = idx;
2083
2084 from_qfl = &qi->qf_lists[idx];
2085 to_qfl = &to->w_llist->qf_lists[idx];
2086
2087 /* Some of the fields are populated by qf_add_entry() */
2088 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2089 to_qfl->qf_count = 0;
2090 to_qfl->qf_index = 0;
2091 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002092 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002093 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002094 if (from_qfl->qf_title != NULL)
2095 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2096 else
2097 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002098 if (from_qfl->qf_ctx != NULL)
2099 {
2100 to_qfl->qf_ctx = alloc_tv();
2101 if (to_qfl->qf_ctx != NULL)
2102 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2103 }
2104 else
2105 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002106
2107 if (from_qfl->qf_count)
2108 {
2109 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002110 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002111
2112 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002113 for (i = 0, from_qfp = from_qfl->qf_start;
2114 i < from_qfl->qf_count && from_qfp != NULL;
2115 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002116 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002117 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02002118 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002119 NULL,
2120 NULL,
Bram Moolenaard76ce852018-05-01 15:02:04 +02002121 from_qfp->qf_module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002122 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002123 from_qfp->qf_text,
2124 from_qfp->qf_lnum,
2125 from_qfp->qf_col,
2126 from_qfp->qf_viscol,
2127 from_qfp->qf_pattern,
2128 from_qfp->qf_nr,
2129 0,
2130 from_qfp->qf_valid) == FAIL)
2131 {
2132 qf_free_all(to);
2133 return;
2134 }
2135 /*
2136 * qf_add_entry() will not set the qf_num field, as the
2137 * directory and file names are not supplied. So the qf_fnum
2138 * field is copied here.
2139 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002140 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002141 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
2142 prevp->qf_type = from_qfp->qf_type; /* error type */
2143 if (from_qfl->qf_ptr == from_qfp)
2144 to_qfl->qf_ptr = prevp; /* current location */
2145 }
2146 }
2147
2148 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
2149
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002150 /* Assign a new ID for the location list */
2151 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01002152 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002153
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002154 /* When no valid entries are present in the list, qf_ptr points to
2155 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02002156 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002157 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002158 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002159 to_qfl->qf_index = 1;
2160 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002161 }
2162
2163 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
2164}
2165
2166/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002167 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002168 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 */
2170 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002171qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
Bram Moolenaar82404332016-07-10 17:00:38 +02002173 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002174 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002175 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002176
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 if (fname == NULL || *fname == NUL) /* no file name */
2178 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
Bram Moolenaare60acc12011-05-10 16:41:25 +02002180#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002181 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002182#endif
2183#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002184 if (directory != NULL)
2185 slash_adjust(directory);
2186 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002187#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002188 if (directory != NULL && !vim_isAbsName(fname)
2189 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2190 {
2191 /*
2192 * Here we check if the file really exists.
2193 * This should normally be true, but if make works without
2194 * "leaving directory"-messages we might have missed a
2195 * directory change.
2196 */
2197 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002200 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002201 if (directory)
2202 ptr = concat_fnames(directory, fname, TRUE);
2203 else
2204 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002206 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02002207 bufname = ptr;
2208 }
2209 else
2210 bufname = fname;
2211
2212 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002213 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002214 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002215 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002216 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002218 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002219 {
2220 vim_free(qf_last_bufname);
2221 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2222 if (bufname == ptr)
2223 qf_last_bufname = bufname;
2224 else
2225 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002226 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002227 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002228 if (buf == NULL)
2229 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002230
Bram Moolenaarc1542742016-07-20 21:44:37 +02002231 buf->b_has_qf_entry =
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002232 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002233 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234}
2235
2236/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002237 * Push dirbuf onto the directory stack and return pointer to actual dir or
2238 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 */
2240 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002241qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242{
2243 struct dir_stack_T *ds_new;
2244 struct dir_stack_T *ds_ptr;
2245
2246 /* allocate new stack element and hook it in */
2247 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2248 if (ds_new == NULL)
2249 return NULL;
2250
2251 ds_new->next = *stackptr;
2252 *stackptr = ds_new;
2253
2254 /* store directory on the stack */
2255 if (vim_isAbsName(dirbuf)
2256 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002257 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 (*stackptr)->dirname = vim_strsave(dirbuf);
2259 else
2260 {
2261 /* Okay we don't have an absolute path.
2262 * dirbuf must be a subdir of one of the directories on the stack.
2263 * Let's search...
2264 */
2265 ds_new = (*stackptr)->next;
2266 (*stackptr)->dirname = NULL;
2267 while (ds_new)
2268 {
2269 vim_free((*stackptr)->dirname);
2270 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2271 TRUE);
2272 if (mch_isdir((*stackptr)->dirname) == TRUE)
2273 break;
2274
2275 ds_new = ds_new->next;
2276 }
2277
2278 /* clean up all dirs we already left */
2279 while ((*stackptr)->next != ds_new)
2280 {
2281 ds_ptr = (*stackptr)->next;
2282 (*stackptr)->next = (*stackptr)->next->next;
2283 vim_free(ds_ptr->dirname);
2284 vim_free(ds_ptr);
2285 }
2286
2287 /* Nothing found -> it must be on top level */
2288 if (ds_new == NULL)
2289 {
2290 vim_free((*stackptr)->dirname);
2291 (*stackptr)->dirname = vim_strsave(dirbuf);
2292 }
2293 }
2294
2295 if ((*stackptr)->dirname != NULL)
2296 return (*stackptr)->dirname;
2297 else
2298 {
2299 ds_ptr = *stackptr;
2300 *stackptr = (*stackptr)->next;
2301 vim_free(ds_ptr);
2302 return NULL;
2303 }
2304}
2305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306/*
2307 * pop dirbuf from the directory stack and return previous directory or NULL if
2308 * stack is empty
2309 */
2310 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002311qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312{
2313 struct dir_stack_T *ds_ptr;
2314
2315 /* TODO: Should we check if dirbuf is the directory on top of the stack?
2316 * What to do if it isn't? */
2317
2318 /* pop top element and free it */
2319 if (*stackptr != NULL)
2320 {
2321 ds_ptr = *stackptr;
2322 *stackptr = (*stackptr)->next;
2323 vim_free(ds_ptr->dirname);
2324 vim_free(ds_ptr);
2325 }
2326
2327 /* return NEW top element as current dir or NULL if stack is empty*/
2328 return *stackptr ? (*stackptr)->dirname : NULL;
2329}
2330
2331/*
2332 * clean up directory stack
2333 */
2334 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002335qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336{
2337 struct dir_stack_T *ds_ptr;
2338
2339 while ((ds_ptr = *stackptr) != NULL)
2340 {
2341 *stackptr = (*stackptr)->next;
2342 vim_free(ds_ptr->dirname);
2343 vim_free(ds_ptr);
2344 }
2345}
2346
2347/*
2348 * Check in which directory of the directory stack the given file can be
2349 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002350 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 * Cleans up intermediate directory entries.
2352 *
2353 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002354 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 * ./
2356 * ./aa
2357 * ./aa/bb
2358 * ./bb
2359 * ./bb/x.c
2360 * and make says:
2361 * making all in aa
2362 * making all in bb
2363 * x.c:9: Error
2364 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2365 * qf_guess_filepath will return NULL.
2366 */
2367 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002368qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370 struct dir_stack_T *ds_ptr;
2371 struct dir_stack_T *ds_tmp;
2372 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002373 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374
2375 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002376 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 return NULL;
2378
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002379 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 fullname = NULL;
2381 while (ds_ptr)
2382 {
2383 vim_free(fullname);
2384 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2385
2386 /* If concat_fnames failed, just go on. The worst thing that can happen
2387 * is that we delete the entire stack.
2388 */
2389 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2390 break;
2391
2392 ds_ptr = ds_ptr->next;
2393 }
2394
2395 vim_free(fullname);
2396
2397 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002398 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002400 ds_tmp = qfl->qf_dir_stack->next;
2401 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 vim_free(ds_tmp->dirname);
2403 vim_free(ds_tmp);
2404 }
2405
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002406 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407}
2408
2409/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002410 * Returns TRUE if a quickfix/location list with the given identifier exists.
2411 */
2412 static int
2413qflist_valid (win_T *wp, int_u qf_id)
2414{
2415 qf_info_T *qi = &ql_info;
2416 int i;
2417
2418 if (wp != NULL)
2419 {
2420 qi = GET_LOC_LIST(wp); /* Location list */
2421 if (qi == NULL)
2422 return FALSE;
2423 }
2424
2425 for (i = 0; i < qi->qf_listcount; ++i)
2426 if (qi->qf_lists[i].qf_id == qf_id)
2427 return TRUE;
2428
2429 return FALSE;
2430}
2431
2432/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002433 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002434 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002435 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002436 * Similar to location list.
2437 */
2438 static int
2439is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
2440{
2441 qf_list_T *qfl;
2442 qfline_T *qfp;
2443 int i;
2444
2445 qfl = &qi->qf_lists[qi->qf_curlist];
2446
2447 /* Search for the entry in the current list */
2448 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2449 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002450 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002451 break;
2452
2453 if (i == qfl->qf_count) /* Entry is not found */
2454 return FALSE;
2455
2456 return TRUE;
2457}
2458
2459/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002460 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002461 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002462 */
2463 static qfline_T *
2464get_next_valid_entry(
2465 qf_info_T *qi,
2466 qfline_T *qf_ptr,
2467 int *qf_index,
2468 int dir)
2469{
2470 int idx;
2471 int old_qf_fnum;
2472
2473 idx = *qf_index;
2474 old_qf_fnum = qf_ptr->qf_fnum;
2475
2476 do
2477 {
2478 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2479 || qf_ptr->qf_next == NULL)
2480 return NULL;
2481 ++idx;
2482 qf_ptr = qf_ptr->qf_next;
2483 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2484 && !qf_ptr->qf_valid)
2485 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2486
2487 *qf_index = idx;
2488 return qf_ptr;
2489}
2490
2491/*
2492 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002493 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002494 */
2495 static qfline_T *
2496get_prev_valid_entry(
2497 qf_info_T *qi,
2498 qfline_T *qf_ptr,
2499 int *qf_index,
2500 int dir)
2501{
2502 int idx;
2503 int old_qf_fnum;
2504
2505 idx = *qf_index;
2506 old_qf_fnum = qf_ptr->qf_fnum;
2507
2508 do
2509 {
2510 if (idx == 1 || qf_ptr->qf_prev == NULL)
2511 return NULL;
2512 --idx;
2513 qf_ptr = qf_ptr->qf_prev;
2514 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2515 && !qf_ptr->qf_valid)
2516 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2517
2518 *qf_index = idx;
2519 return qf_ptr;
2520}
2521
2522/*
2523 * Get the n'th (errornr) previous/next valid entry from the current entry in
2524 * the quickfix list.
2525 * dir == FORWARD or FORWARD_FILE: next valid entry
2526 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2527 */
2528 static qfline_T *
2529get_nth_valid_entry(
2530 qf_info_T *qi,
2531 int errornr,
2532 qfline_T *qf_ptr,
2533 int *qf_index,
2534 int dir)
2535{
2536 qfline_T *prev_qf_ptr;
2537 int prev_index;
2538 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2539 char_u *err = e_no_more_items;
2540
2541 while (errornr--)
2542 {
2543 prev_qf_ptr = qf_ptr;
2544 prev_index = *qf_index;
2545
2546 if (dir == FORWARD || dir == FORWARD_FILE)
2547 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2548 else
2549 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2550 if (qf_ptr == NULL)
2551 {
2552 qf_ptr = prev_qf_ptr;
2553 *qf_index = prev_index;
2554 if (err != NULL)
2555 {
2556 EMSG(_(err));
2557 return NULL;
2558 }
2559 break;
2560 }
2561
2562 err = NULL;
2563 }
2564
2565 return qf_ptr;
2566}
2567
2568/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002569 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002570 */
2571 static qfline_T *
2572get_nth_entry(
2573 qf_info_T *qi,
2574 int errornr,
2575 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002576 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002577{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002578 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002579
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002580 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002581 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2582 {
2583 --qf_idx;
2584 qf_ptr = qf_ptr->qf_prev;
2585 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002586 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002587 while (errornr > qf_idx &&
2588 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2589 qf_ptr->qf_next != NULL)
2590 {
2591 ++qf_idx;
2592 qf_ptr = qf_ptr->qf_next;
2593 }
2594
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002595 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002596 return qf_ptr;
2597}
2598
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002599/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002600 * Find a window displaying a Vim help file.
2601 */
2602 static win_T *
2603qf_find_help_win(void)
2604{
2605 win_T *wp;
2606
2607 FOR_ALL_WINDOWS(wp)
2608 if (bt_help(wp->w_buffer))
2609 return wp;
2610
2611 return NULL;
2612}
2613
2614/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002615 * Find a help window or open one.
2616 */
2617 static int
2618jump_to_help_window(qf_info_T *qi, int *opened_window)
2619{
2620 win_T *wp;
2621 int flags;
2622
2623 if (cmdmod.tab != 0)
2624 wp = NULL;
2625 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002626 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002627 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2628 win_enter(wp, TRUE);
2629 else
2630 {
2631 /*
2632 * Split off help window; put it at far top if no position
2633 * specified, the current window is vertically split and narrow.
2634 */
2635 flags = WSP_HELP;
2636 if (cmdmod.split == 0 && curwin->w_width != Columns
2637 && curwin->w_width < 80)
2638 flags |= WSP_TOP;
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002639 if (IS_LL_STACK(qi))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002640 flags |= WSP_NEWLOC; /* don't copy the location list */
2641
2642 if (win_split(0, flags) == FAIL)
2643 return FAIL;
2644
2645 *opened_window = TRUE;
2646
2647 if (curwin->w_height < p_hh)
2648 win_setheight((int)p_hh);
2649
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002650 if (IS_LL_STACK(qi)) // not a quickfix list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002651 {
2652 /* The new window should use the supplied location list */
2653 curwin->w_llist = qi;
2654 qi->qf_refcount++;
2655 }
2656 }
2657
2658 if (!p_im)
2659 restart_edit = 0; /* don't want insert mode in help file */
2660
2661 return OK;
2662}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002663
2664/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002665 * Find a non-quickfix window using the given location list.
2666 * Returns NULL if a matching window is not found.
2667 */
2668 static win_T *
2669qf_find_win_with_loclist(qf_info_T *ll)
2670{
2671 win_T *wp;
2672
2673 FOR_ALL_WINDOWS(wp)
2674 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2675 return wp;
2676
2677 return NULL;
2678}
2679
2680/*
2681 * Find a window containing a normal buffer
2682 */
2683 static win_T *
2684qf_find_win_with_normal_buf(void)
2685{
2686 win_T *wp;
2687
2688 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002689 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002690 return wp;
2691
2692 return NULL;
2693}
2694
2695/*
2696 * Go to a window in any tabpage containing the specified file. Returns TRUE
2697 * if successfully jumped to the window. Otherwise returns FALSE.
2698 */
2699 static int
2700qf_goto_tabwin_with_file(int fnum)
2701{
2702 tabpage_T *tp;
2703 win_T *wp;
2704
2705 FOR_ALL_TAB_WINDOWS(tp, wp)
2706 if (wp->w_buffer->b_fnum == fnum)
2707 {
2708 goto_tabpage_win(tp, wp);
2709 return TRUE;
2710 }
2711
2712 return FALSE;
2713}
2714
2715/*
2716 * Create a new window to show a file above the quickfix window. Called when
2717 * only the quickfix window is present.
2718 */
2719 static int
2720qf_open_new_file_win(qf_info_T *ll_ref)
2721{
2722 int flags;
2723
2724 flags = WSP_ABOVE;
2725 if (ll_ref != NULL)
2726 flags |= WSP_NEWLOC;
2727 if (win_split(0, flags) == FAIL)
2728 return FAIL; /* not enough room for window */
2729 p_swb = empty_option; /* don't split again */
2730 swb_flags = 0;
2731 RESET_BINDING(curwin);
2732 if (ll_ref != NULL)
2733 {
2734 /* The new window should use the location list from the
2735 * location list window */
2736 curwin->w_llist = ll_ref;
2737 ll_ref->qf_refcount++;
2738 }
2739 return OK;
2740}
2741
2742/*
2743 * Go to a window that shows the right buffer. If the window is not found, go
2744 * to the window just above the location list window. This is used for opening
2745 * a file from a location window and not from a quickfix window. If some usable
2746 * window is previously found, then it is supplied in 'use_win'.
2747 */
2748 static void
2749qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2750{
2751 win_T *win = use_win;
2752
2753 if (win == NULL)
2754 {
2755 /* Find the window showing the selected file */
2756 FOR_ALL_WINDOWS(win)
2757 if (win->w_buffer->b_fnum == qf_fnum)
2758 break;
2759 if (win == NULL)
2760 {
2761 /* Find a previous usable window */
2762 win = curwin;
2763 do
2764 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002765 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002766 break;
2767 if (win->w_prev == NULL)
2768 win = lastwin; /* wrap around the top */
2769 else
2770 win = win->w_prev; /* go to previous window */
2771 } while (win != curwin);
2772 }
2773 }
2774 win_goto(win);
2775
2776 /* If the location list for the window is not set, then set it
2777 * to the location list from the location window */
2778 if (win->w_llist == NULL)
2779 {
2780 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002781 if (ll_ref != NULL)
2782 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002783 }
2784}
2785
2786/*
2787 * Go to a window that shows the specified file. If a window is not found, go
2788 * to the window just above the quickfix window. This is used for opening a
2789 * file from a quickfix window and not from a location window.
2790 */
2791 static void
2792qf_goto_win_with_qfl_file(int qf_fnum)
2793{
2794 win_T *win;
2795 win_T *altwin;
2796
2797 win = curwin;
2798 altwin = NULL;
2799 for (;;)
2800 {
2801 if (win->w_buffer->b_fnum == qf_fnum)
2802 break;
2803 if (win->w_prev == NULL)
2804 win = lastwin; /* wrap around the top */
2805 else
2806 win = win->w_prev; /* go to previous window */
2807
2808 if (IS_QF_WINDOW(win))
2809 {
2810 /* Didn't find it, go to the window before the quickfix
2811 * window. */
2812 if (altwin != NULL)
2813 win = altwin;
2814 else if (curwin->w_prev != NULL)
2815 win = curwin->w_prev;
2816 else
2817 win = curwin->w_next;
2818 break;
2819 }
2820
2821 /* Remember a usable window. */
Bram Moolenaar91335e52018-08-01 17:53:12 +02002822 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002823 altwin = win;
2824 }
2825
2826 win_goto(win);
2827}
2828
2829/*
2830 * Find a suitable window for opening a file (qf_fnum) from the
2831 * quickfix/location list and jump to it. If the file is already opened in a
2832 * window, jump to it. Otherwise open a new window to display the file. This is
2833 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002834 */
2835 static int
2836qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2837{
2838 win_T *usable_win_ptr = NULL;
2839 int usable_win;
2840 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002841 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002842
2843 usable_win = 0;
2844
2845 ll_ref = curwin->w_llist_ref;
2846 if (ll_ref != NULL)
2847 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002848 /* Find a non-quickfix window with this location list */
2849 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2850 if (usable_win_ptr != NULL)
2851 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002852 }
2853
2854 if (!usable_win)
2855 {
2856 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002857 win = qf_find_win_with_normal_buf();
2858 if (win != NULL)
2859 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002860 }
2861
2862 /*
2863 * If no usable window is found and 'switchbuf' contains "usetab"
2864 * then search in other tabs.
2865 */
2866 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002867 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002868
2869 /*
2870 * If there is only one window and it is the quickfix window, create a
2871 * new one above the quickfix window.
2872 */
2873 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2874 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002875 if (qf_open_new_file_win(ll_ref) != OK)
2876 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002877 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002878 }
2879 else
2880 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002881 if (curwin->w_llist_ref != NULL) /* In a location window */
2882 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2883 else /* In a quickfix window */
2884 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002885 }
2886
2887 return OK;
2888}
2889
2890/*
2891 * Edit the selected file or help file.
2892 */
2893 static int
2894qf_jump_edit_buffer(
2895 qf_info_T *qi,
2896 qfline_T *qf_ptr,
2897 int forceit,
2898 win_T *oldwin,
2899 int *opened_window,
2900 int *abort)
2901{
2902 int retval = OK;
2903
2904 if (qf_ptr->qf_type == 1)
2905 {
2906 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2907 * set b_p_ro flag). */
2908 if (!can_abandon(curbuf, forceit))
2909 {
2910 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002911 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002912 }
2913 else
2914 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2915 ECMD_HIDE + ECMD_SET_HELP,
2916 oldwin == curwin ? curwin : NULL);
2917 }
2918 else
2919 {
2920 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002921 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002922
2923 retval = buflist_getfile(qf_ptr->qf_fnum,
2924 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002925
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002926 if (IS_LL_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002927 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002928 /*
2929 * Location list. Check whether the associated window is still
2930 * present and the list is still valid.
2931 */
2932 if (!win_valid_any_tab(oldwin))
2933 {
2934 EMSG(_("E924: Current window was closed"));
2935 *abort = TRUE;
2936 *opened_window = FALSE;
2937 }
2938 else if (!qflist_valid(oldwin, save_qfid))
2939 {
2940 EMSG(_(e_loc_list_changed));
2941 *abort = TRUE;
2942 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002943 }
2944 else if (old_qf_curlist != qi->qf_curlist
2945 || !is_qf_entry_present(qi, qf_ptr))
2946 {
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002947 if (IS_QF_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002948 EMSG(_("E925: Current quickfix was changed"));
2949 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002950 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002951 *abort = TRUE;
2952 }
2953
2954 if (*abort)
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002955 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002956 }
2957
2958 return retval;
2959}
2960
2961/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002962 * Go to the error line in the current file using either line/column number or
2963 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002964 */
2965 static void
2966qf_jump_goto_line(
2967 linenr_T qf_lnum,
2968 int qf_col,
2969 char_u qf_viscol,
2970 char_u *qf_pattern)
2971{
2972 linenr_T i;
2973 char_u *line;
2974 colnr_T screen_col;
2975 colnr_T char_col;
2976
2977 if (qf_pattern == NULL)
2978 {
2979 /*
2980 * Go to line with error, unless qf_lnum is 0.
2981 */
2982 i = qf_lnum;
2983 if (i > 0)
2984 {
2985 if (i > curbuf->b_ml.ml_line_count)
2986 i = curbuf->b_ml.ml_line_count;
2987 curwin->w_cursor.lnum = i;
2988 }
2989 if (qf_col > 0)
2990 {
2991 curwin->w_cursor.col = qf_col - 1;
2992#ifdef FEAT_VIRTUALEDIT
2993 curwin->w_cursor.coladd = 0;
2994#endif
2995 if (qf_viscol == TRUE)
2996 {
2997 /*
2998 * Check each character from the beginning of the error
2999 * line up to the error column. For each tab character
3000 * found, reduce the error column value by the length of
3001 * a tab character.
3002 */
3003 line = ml_get_curline();
3004 screen_col = 0;
3005 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3006 {
3007 if (*line == NUL)
3008 break;
3009 if (*line++ == '\t')
3010 {
3011 curwin->w_cursor.col -= 7 - (screen_col % 8);
3012 screen_col += 8 - (screen_col % 8);
3013 }
3014 else
3015 ++screen_col;
3016 }
3017 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003018 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003019 check_cursor();
3020 }
3021 else
3022 beginline(BL_WHITE | BL_FIX);
3023 }
3024 else
3025 {
3026 pos_T save_cursor;
3027
3028 /* Move the cursor to the first line in the buffer */
3029 save_cursor = curwin->w_cursor;
3030 curwin->w_cursor.lnum = 0;
3031 if (!do_search(NULL, '/', qf_pattern, (long)1,
3032 SEARCH_KEEP, NULL, NULL))
3033 curwin->w_cursor = save_cursor;
3034 }
3035}
3036
3037/*
3038 * Display quickfix list index and size message
3039 */
3040 static void
3041qf_jump_print_msg(
3042 qf_info_T *qi,
3043 int qf_index,
3044 qfline_T *qf_ptr,
3045 buf_T *old_curbuf,
3046 linenr_T old_lnum)
3047{
3048 linenr_T i;
3049 int len;
3050
3051 /* Update the screen before showing the message, unless the screen
3052 * scrolled up. */
3053 if (!msg_scrolled)
3054 update_topline_redraw();
3055 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3056 qi->qf_lists[qi->qf_curlist].qf_count,
3057 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3058 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
3059 /* Add the message, skipping leading whitespace and newlines. */
3060 len = (int)STRLEN(IObuff);
3061 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3062
3063 /* Output the message. Overwrite to avoid scrolling when the 'O'
3064 * flag is present in 'shortmess'; But when not jumping, print the
3065 * whole message. */
3066 i = msg_scroll;
3067 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3068 msg_scroll = TRUE;
3069 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3070 msg_scroll = FALSE;
3071 msg_attr_keep(IObuff, 0, TRUE);
3072 msg_scroll = i;
3073}
3074
3075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 * jump to a quickfix line
3077 * if dir == FORWARD go "errornr" valid entries forward
3078 * if dir == BACKWARD go "errornr" valid entries backward
3079 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3080 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3081 * else if "errornr" is zero, redisplay the same line
3082 * else go to entry "errornr"
3083 */
3084 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003085qf_jump(qf_info_T *qi,
3086 int dir,
3087 int errornr,
3088 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003090 qfline_T *qf_ptr;
3091 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 buf_T *old_curbuf;
3095 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003096 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003097 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003099 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101#ifdef FEAT_FOLDING
3102 int old_KeyTyped = KeyTyped; /* getting file may reset it */
3103#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003104 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003106 if (qi == NULL)
3107 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003108
3109 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003110 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
3112 EMSG(_(e_quickfix));
3113 return;
3114 }
3115
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003116 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003118 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02003120 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003122 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
3123 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003125 qf_ptr = old_qf_ptr;
3126 qf_index = old_qf_index;
3127 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 }
3129 }
3130 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003131 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003133 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
3134 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 /* No need to print the error message if it's visible in the error
3136 * window */
3137 print_message = FALSE;
3138
3139 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003140 * For ":helpgrep" find a help window or open one.
3141 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003142 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003143 if (jump_to_help_window(qi, &opened_window) == FAIL)
3144 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003145
3146 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 * If currently in the quickfix window, find another window to show the
3148 * file in.
3149 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003150 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 {
3152 /*
3153 * If there is no file specified, we don't know where to go.
3154 * But do advance, otherwise ":cn" gets stuck.
3155 */
3156 if (qf_ptr->qf_fnum == 0)
3157 goto theend;
3158
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003159 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
3160 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
3163 /*
3164 * If there is a file name,
3165 * read the wanted file if needed, and check autowrite etc.
3166 */
3167 old_curbuf = curbuf;
3168 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003169
3170 if (qf_ptr->qf_fnum != 0)
3171 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003172 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003173
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003174 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3175 &opened_window, &abort);
3176 if (abort)
3177 {
3178 qi = NULL;
3179 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003180 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003181 }
3182
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003183 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
3185 /* When not switched to another buffer, still need to set pc mark */
3186 if (curbuf == old_curbuf)
3187 setpcmark();
3188
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003189 if (qf_ptr != NULL)
3190 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col,
3191 qf_ptr->qf_viscol, qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
3193#ifdef FEAT_FOLDING
3194 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
3195 foldOpenCursor();
3196#endif
3197 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003198 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
3200 else
3201 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 if (opened_window)
3203 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01003204 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
3206 /*
3207 * Couldn't open file, so put index back where it was. This could
3208 * happen if the file was readonly and we changed something.
3209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 qf_ptr = old_qf_ptr;
3212 qf_index = old_qf_index;
3213 }
3214 }
3215theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003216 if (qi != NULL)
3217 {
3218 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
3219 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
3220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 if (p_swb != old_swb && opened_window)
3222 {
3223 /* Restore old 'switchbuf' value, but not when an autocommand or
3224 * modeline has changed the value. */
3225 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003228 swb_flags = old_swb_flags;
3229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 else
3231 free_string_option(old_swb);
3232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233}
3234
3235/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003236 * Highlight attributes used for displaying entries from the quickfix list.
3237 */
3238static int qfFileAttr;
3239static int qfSepAttr;
3240static int qfLineAttr;
3241
3242/*
3243 * Display information about a single entry from the quickfix/location list.
3244 * Used by ":clist/:llist" commands.
3245 */
3246 static void
3247qf_list_entry(qf_info_T *qi, qfline_T *qfp, int qf_idx)
3248{
3249 char_u *fname;
3250 buf_T *buf;
3251 int filter_entry;
3252
3253 fname = NULL;
3254 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3255 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3256 (char *)qfp->qf_module);
3257 else {
3258 if (qfp->qf_fnum != 0
3259 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3260 {
3261 fname = buf->b_fname;
3262 if (qfp->qf_type == 1) /* :helpgrep */
3263 fname = gettail(fname);
3264 }
3265 if (fname == NULL)
3266 sprintf((char *)IObuff, "%2d", qf_idx);
3267 else
3268 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3269 qf_idx, (char *)fname);
3270 }
3271
3272 // Support for filtering entries using :filter /pat/ clist
3273 // Match against the module name, file name, search pattern and
3274 // text of the entry.
3275 filter_entry = TRUE;
3276 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3277 filter_entry &= message_filtered(qfp->qf_module);
3278 if (filter_entry && fname != NULL)
3279 filter_entry &= message_filtered(fname);
3280 if (filter_entry && qfp->qf_pattern != NULL)
3281 filter_entry &= message_filtered(qfp->qf_pattern);
3282 if (filter_entry)
3283 filter_entry &= message_filtered(qfp->qf_text);
3284 if (filter_entry)
3285 return;
3286
3287 msg_putchar('\n');
3288 msg_outtrans_attr(IObuff, qf_idx == qi->qf_lists[qi->qf_curlist].qf_index
3289 ? HL_ATTR(HLF_QFL) : qfFileAttr);
3290
3291 if (qfp->qf_lnum != 0)
3292 msg_puts_attr((char_u *)":", qfSepAttr);
3293 if (qfp->qf_lnum == 0)
3294 IObuff[0] = NUL;
3295 else if (qfp->qf_col == 0)
3296 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3297 else
3298 sprintf((char *)IObuff, "%ld col %d",
3299 qfp->qf_lnum, qfp->qf_col);
3300 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3301 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3302 msg_puts_attr(IObuff, qfLineAttr);
3303 msg_puts_attr((char_u *)":", qfSepAttr);
3304 if (qfp->qf_pattern != NULL)
3305 {
3306 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3307 msg_puts(IObuff);
3308 msg_puts_attr((char_u *)":", qfSepAttr);
3309 }
3310 msg_puts((char_u *)" ");
3311
3312 /* Remove newlines and leading whitespace from the text. For an
3313 * unrecognized line keep the indent, the compiler may mark a word
3314 * with ^^^^. */
3315 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3316 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3317 IObuff, IOSIZE);
3318 msg_prt_line(IObuff, FALSE);
3319 out_flush(); /* show one line at a time */
3320}
3321
3322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003324 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 */
3326 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003327qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003329 qfline_T *qfp;
3330 int i;
3331 int idx1 = 1;
3332 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003333 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003334 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003335 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003337 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar39665952018-08-15 20:59:48 +02003339 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003340 {
3341 qi = GET_LOC_LIST(curwin);
3342 if (qi == NULL)
3343 {
3344 EMSG(_(e_loclist));
3345 return;
3346 }
3347 }
3348
3349 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003350 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 {
3352 EMSG(_(e_quickfix));
3353 return;
3354 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003355 if (*arg == '+')
3356 {
3357 ++arg;
3358 plus = TRUE;
3359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3361 {
3362 EMSG(_(e_trailing));
3363 return;
3364 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003365 if (plus)
3366 {
3367 i = qi->qf_lists[qi->qf_curlist].qf_index;
3368 idx2 = i + idx1;
3369 idx1 = i;
3370 }
3371 else
3372 {
3373 i = qi->qf_lists[qi->qf_curlist].qf_count;
3374 if (idx1 < 0)
3375 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3376 if (idx2 < 0)
3377 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379
Bram Moolenaara796d462018-05-01 14:30:36 +02003380 /* Shorten all the file names, so that it is easy to read */
3381 shorten_fnames(FALSE);
3382
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003383 /*
3384 * Get the attributes for the different quickfix highlight items. Note
3385 * that this depends on syntax items defined in the qf.vim syntax file
3386 */
3387 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3388 if (qfFileAttr == 0)
3389 qfFileAttr = HL_ATTR(HLF_D);
3390 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3391 if (qfSepAttr == 0)
3392 qfSepAttr = HL_ATTR(HLF_D);
3393 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3394 if (qfLineAttr == 0)
3395 qfLineAttr = HL_ATTR(HLF_N);
3396
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003397 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003399 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3400 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
3402 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3403 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003404 if (got_int)
3405 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003406
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003407 qf_list_entry(qi, qfp, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003409
3410 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003411 if (qfp == NULL)
3412 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003413 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 ui_breakcheck();
3415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416}
3417
3418/*
3419 * Remove newlines and leading whitespace from an error message.
3420 * Put the result in "buf[bufsize]".
3421 */
3422 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003423qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
3425 int i;
3426 char_u *p = text;
3427
3428 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3429 {
3430 if (*p == '\n')
3431 {
3432 buf[i] = ' ';
3433 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003434 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 break;
3436 }
3437 else
3438 buf[i] = *p++;
3439 }
3440 buf[i] = NUL;
3441}
3442
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003443/*
3444 * Display information (list number, list size and the title) about a
3445 * quickfix/location list.
3446 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003447 static void
3448qf_msg(qf_info_T *qi, int which, char *lead)
3449{
3450 char *title = (char *)qi->qf_lists[which].qf_title;
3451 int count = qi->qf_lists[which].qf_count;
3452 char_u buf[IOSIZE];
3453
3454 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3455 lead,
3456 which + 1,
3457 qi->qf_listcount,
3458 count);
3459
3460 if (title != NULL)
3461 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003462 size_t len = STRLEN(buf);
3463
3464 if (len < 34)
3465 {
3466 vim_memset(buf + len, ' ', 34 - len);
3467 buf[34] = NUL;
3468 }
3469 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003470 }
3471 trunc_string(buf, buf, Columns - 1, IOSIZE);
3472 msg(buf);
3473}
3474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475/*
3476 * ":colder [count]": Up in the quickfix stack.
3477 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003478 * ":lolder [count]": Up in the location list stack.
3479 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 */
3481 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003482qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003484 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 int count;
3486
Bram Moolenaar39665952018-08-15 20:59:48 +02003487 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003488 {
3489 qi = GET_LOC_LIST(curwin);
3490 if (qi == NULL)
3491 {
3492 EMSG(_(e_loclist));
3493 return;
3494 }
3495 }
3496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (eap->addr_count != 0)
3498 count = eap->line2;
3499 else
3500 count = 1;
3501 while (count--)
3502 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003503 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003505 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
3507 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003510 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
3512 else
3513 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003514 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
3516 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003517 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003519 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003522 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003523 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524}
3525
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003526/*
3527 * Display the information about all the quickfix/location lists in the stack
3528 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003529 void
3530qf_history(exarg_T *eap)
3531{
3532 qf_info_T *qi = &ql_info;
3533 int i;
3534
Bram Moolenaar39665952018-08-15 20:59:48 +02003535 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003536 qi = GET_LOC_LIST(curwin);
3537 if (qi == NULL || (qi->qf_listcount == 0
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003538 && qf_list_empty(qi, qi->qf_curlist)))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003539 MSG(_("No entries"));
3540 else
3541 for (i = 0; i < qi->qf_listcount; ++i)
3542 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3543}
3544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003546 * Free all the entries in the error list "idx". Note that other information
3547 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 */
3549 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003550qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003552 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003553 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003554 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003555 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003557 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003559 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003560 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003561 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003562 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003563 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003564 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003565 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003566 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003567 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003568 if (stop)
3569 /* Somehow qf_count may have an incorrect value, set it to 1
3570 * to avoid crashing when it's wrong.
3571 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003572 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003573 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003574 qfl->qf_start = qfpnext;
3575 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003577
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003578 qfl->qf_index = 0;
3579 qfl->qf_start = NULL;
3580 qfl->qf_last = NULL;
3581 qfl->qf_ptr = NULL;
3582 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003583
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003584 qf_clean_dir_stack(&qfl->qf_dir_stack);
3585 qfl->qf_directory = NULL;
3586 qf_clean_dir_stack(&qfl->qf_file_stack);
3587 qfl->qf_currfile = NULL;
3588 qfl->qf_multiline = FALSE;
3589 qfl->qf_multiignore = FALSE;
3590 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591}
3592
3593/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003594 * Free error list "idx". Frees all the entries in the quickfix list,
3595 * associated context information and the title.
3596 */
3597 static void
3598qf_free(qf_info_T *qi, int idx)
3599{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003600 qf_list_T *qfl = &qi->qf_lists[idx];
3601
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003602 qf_free_items(qi, idx);
3603
Bram Moolenaard23a8232018-02-10 18:45:26 +01003604 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003605 free_tv(qfl->qf_ctx);
3606 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003607 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003608 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003609}
3610
3611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 * qf_mark_adjust: adjust marks
3613 */
3614 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003615qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003616 win_T *wp,
3617 linenr_T line1,
3618 linenr_T line2,
3619 long amount,
3620 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003622 int i;
3623 qfline_T *qfp;
3624 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003625 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003626 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003627 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
Bram Moolenaarc1542742016-07-20 21:44:37 +02003629 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003630 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003631 if (wp != NULL)
3632 {
3633 if (wp->w_llist == NULL)
3634 return;
3635 qi = wp->w_llist;
3636 }
3637
3638 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003639 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003640 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003641 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3642 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 if (qfp->qf_fnum == curbuf->b_fnum)
3644 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003645 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3647 {
3648 if (amount == MAXLNUM)
3649 qfp->qf_cleared = TRUE;
3650 else
3651 qfp->qf_lnum += amount;
3652 }
3653 else if (amount_after && qfp->qf_lnum > line2)
3654 qfp->qf_lnum += amount_after;
3655 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003656
3657 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003658 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659}
3660
3661/*
3662 * Make a nice message out of the error character and the error number:
3663 * char number message
3664 * e or E 0 " error"
3665 * w or W 0 " warning"
3666 * i or I 0 " info"
3667 * 0 0 ""
3668 * other 0 " c"
3669 * e or E n " error n"
3670 * w or W n " warning n"
3671 * i or I n " info n"
3672 * 0 n " error n"
3673 * other n " c n"
3674 * 1 x "" :helpgrep
3675 */
3676 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003677qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
3679 static char_u buf[20];
3680 static char_u cc[3];
3681 char_u *p;
3682
3683 if (c == 'W' || c == 'w')
3684 p = (char_u *)" warning";
3685 else if (c == 'I' || c == 'i')
3686 p = (char_u *)" info";
3687 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3688 p = (char_u *)" error";
3689 else if (c == 0 || c == 1)
3690 p = (char_u *)"";
3691 else
3692 {
3693 cc[0] = ' ';
3694 cc[1] = c;
3695 cc[2] = NUL;
3696 p = cc;
3697 }
3698
3699 if (nr <= 0)
3700 return p;
3701
3702 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3703 return buf;
3704}
3705
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003707 * When "split" is FALSE: Open the entry/result under the cursor.
3708 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3709 */
3710 void
3711qf_view_result(int split)
3712{
3713 qf_info_T *qi = &ql_info;
3714
3715 if (!bt_quickfix(curbuf))
3716 return;
3717
3718 if (IS_LL_WINDOW(curwin))
3719 qi = GET_LOC_LIST(curwin);
3720
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003721 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003722 {
3723 EMSG(_(e_quickfix));
3724 return;
3725 }
3726
3727 if (split)
3728 {
3729 char_u cmd[32];
3730
3731 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3732 (long)curwin->w_cursor.lnum,
3733 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3734 if (do_cmdline_cmd(cmd) == OK)
3735 do_cmdline_cmd((char_u *) "clearjumps");
3736 return;
3737 }
3738
3739 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3740}
3741
3742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 * ":cwindow": open the quickfix window if we have errors to display,
3744 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003745 * ":lwindow": open the location list window if we have locations to display,
3746 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 */
3748 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003749ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003751 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 win_T *win;
3753
Bram Moolenaar39665952018-08-15 20:59:48 +02003754 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003755 {
3756 qi = GET_LOC_LIST(curwin);
3757 if (qi == NULL)
3758 return;
3759 }
3760
3761 /* Look for an existing quickfix window. */
3762 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
3764 /*
3765 * If a quickfix window is open but we have no errors to display,
3766 * close the window. If a quickfix window is not open, then open
3767 * it if we have errors; otherwise, leave it closed.
3768 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003769 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003770 || qf_list_empty(qi, qi->qf_curlist)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003771 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 {
3773 if (win != NULL)
3774 ex_cclose(eap);
3775 }
3776 else if (win == NULL)
3777 ex_copen(eap);
3778}
3779
3780/*
3781 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003782 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003785ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003787 win_T *win = NULL;
3788 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789
Bram Moolenaar39665952018-08-15 20:59:48 +02003790 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003791 {
3792 qi = GET_LOC_LIST(curwin);
3793 if (qi == NULL)
3794 return;
3795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003797 /* Find existing quickfix window and close it. */
3798 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 if (win != NULL)
3800 win_close(win, FALSE);
3801}
3802
3803/*
3804 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003805 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 */
3807 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003808ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003810 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003813 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003814 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003815 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816
Bram Moolenaar39665952018-08-15 20:59:48 +02003817 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003818 {
3819 qi = GET_LOC_LIST(curwin);
3820 if (qi == NULL)
3821 {
3822 EMSG(_(e_loclist));
3823 return;
3824 }
3825 }
3826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (eap->addr_count != 0)
3828 height = eap->line2;
3829 else
3830 height = QF_WINHEIGHT;
3831
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833#ifdef FEAT_GUI
3834 need_mouse_correct = TRUE;
3835#endif
3836
3837 /*
3838 * Find existing quickfix window, or open a new one.
3839 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003840 win = qf_find_win(qi);
3841
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003842 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003845 if (eap->addr_count != 0)
3846 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003847 if (cmdmod.split & WSP_VERT)
3848 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003849 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003850 win_setwidth(height);
3851 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003852 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003853 win_setheight(height);
3854 }
3855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 else
3857 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003858 int flags = 0;
3859
Bram Moolenaar9c102382006-05-03 21:26:49 +00003860 qf_buf = qf_find_buf(qi);
3861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 /* The current window becomes the previous window afterwards. */
3863 win = curwin;
3864
Bram Moolenaar77642c02012-11-20 17:55:10 +01003865 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3866 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003867 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003868 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003869 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003870 /* Default is to open the window below the current window */
3871 if (cmdmod.split == 0)
3872 flags = WSP_BELOW;
3873 flags |= WSP_NEWLOC;
3874 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003876 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003878 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003880 /*
3881 * For the location list window, create a reference to the
3882 * location list from the window 'win'.
3883 */
3884 curwin->w_llist_ref = win->w_llist;
3885 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003887
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003888 if (oldwin != curwin)
3889 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003890 if (qf_buf != NULL)
3891 /* Use the existing quickfix buffer */
3892 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003893 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003894 else
3895 {
3896 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003897 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003898 /* switch off 'swapfile' */
3899 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3900 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003901 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003902 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003903 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003904#ifdef FEAT_DIFF
3905 curwin->w_p_diff = FALSE;
3906#endif
3907#ifdef FEAT_FOLDING
3908 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3909 OPT_LOCAL);
3910#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003913 /* Only set the height when still in the same tab page and there is no
3914 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003915 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 win_setheight(height);
3917 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3918 if (win_valid(win))
3919 prevwin = win;
3920 }
3921
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003922 qf_set_title_var(qi);
3923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 /*
3925 * Fill the buffer with the quickfix list.
3926 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003927 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003929 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 curwin->w_cursor.col = 0;
3931 check_cursor();
3932 update_topline(); /* scroll to show the line */
3933}
3934
3935/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003936 * Move the cursor in the quickfix window to "lnum".
3937 */
3938 static void
3939qf_win_goto(win_T *win, linenr_T lnum)
3940{
3941 win_T *old_curwin = curwin;
3942
3943 curwin = win;
3944 curbuf = win->w_buffer;
3945 curwin->w_cursor.lnum = lnum;
3946 curwin->w_cursor.col = 0;
3947#ifdef FEAT_VIRTUALEDIT
3948 curwin->w_cursor.coladd = 0;
3949#endif
3950 curwin->w_curswant = 0;
3951 update_topline(); /* scroll to show the line */
3952 redraw_later(VALID);
3953 curwin->w_redr_status = TRUE; /* update ruler */
3954 curwin = old_curwin;
3955 curbuf = curwin->w_buffer;
3956}
3957
3958/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003959 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003960 */
3961 void
Bram Moolenaar39665952018-08-15 20:59:48 +02003962ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02003963{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003964 qf_info_T *qi = &ql_info;
3965 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003966
Bram Moolenaar39665952018-08-15 20:59:48 +02003967 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02003968 {
3969 qi = GET_LOC_LIST(curwin);
3970 if (qi == NULL)
3971 {
3972 EMSG(_(e_loclist));
3973 return;
3974 }
3975 }
3976
3977 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003978 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3979 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3980}
3981
3982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 * Return the number of the current entry (line number in the quickfix
3984 * window).
3985 */
3986 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003987qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003989 qf_info_T *qi = &ql_info;
3990
3991 if (IS_LL_WINDOW(wp))
3992 /* In the location list window, use the referenced location list */
3993 qi = wp->w_llist_ref;
3994
3995 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996}
3997
3998/*
3999 * Update the cursor position in the quickfix window to the current error.
4000 * Return TRUE if there is a quickfix window.
4001 */
4002 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004003qf_win_pos_update(
4004 qf_info_T *qi,
4005 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
4007 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004008 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 /*
4011 * Put the cursor on the current error in the quickfix window, so that
4012 * it's viewable.
4013 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004014 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 if (win != NULL
4016 && qf_index <= win->w_buffer->b_ml.ml_line_count
4017 && old_qf_index != qf_index)
4018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 if (qf_index > old_qf_index)
4020 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004021 win->w_redraw_top = old_qf_index;
4022 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 }
4024 else
4025 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004026 win->w_redraw_top = qf_index;
4027 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004029 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031 return win != NULL;
4032}
4033
4034/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004035 * Check whether the given window is displaying the specified quickfix/location
4036 * list buffer
4037 */
4038 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004039is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004040{
4041 /*
4042 * A window displaying the quickfix buffer will have the w_llist_ref field
4043 * set to NULL.
4044 * A window displaying a location list buffer will have the w_llist_ref
4045 * pointing to the location list.
4046 */
4047 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004048 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4049 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004050 return TRUE;
4051
4052 return FALSE;
4053}
4054
4055/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004056 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004057 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004058 */
4059 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004060qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004061{
4062 win_T *win;
4063
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004064 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004065 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004066 return win;
4067 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004068}
4069
4070/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004071 * Find a quickfix buffer.
4072 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 */
4074 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004075qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004077 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004078 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079
Bram Moolenaar9c102382006-05-03 21:26:49 +00004080 FOR_ALL_TAB_WINDOWS(tp, win)
4081 if (is_qf_win(win, qi))
4082 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004083
Bram Moolenaar9c102382006-05-03 21:26:49 +00004084 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085}
4086
4087/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004088 * Update the w:quickfix_title variable in the quickfix/location list window
4089 */
4090 static void
4091qf_update_win_titlevar(qf_info_T *qi)
4092{
4093 win_T *win;
4094 win_T *curwin_save;
4095
4096 if ((win = qf_find_win(qi)) != NULL)
4097 {
4098 curwin_save = curwin;
4099 curwin = win;
4100 qf_set_title_var(qi);
4101 curwin = curwin_save;
4102 }
4103}
4104
4105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 * Find the quickfix buffer. If it exists, update the contents.
4107 */
4108 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004109qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110{
4111 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004112 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
4115 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004116 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (buf != NULL)
4118 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004119 linenr_T old_line_count = buf->b_ml.ml_line_count;
4120
4121 if (old_last == NULL)
4122 /* set curwin/curbuf to buf and save a few things */
4123 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
Bram Moolenaard823fa92016-08-12 16:29:27 +02004125 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004126
Bram Moolenaar864293a2016-06-02 13:40:04 +02004127 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004128 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004129
Bram Moolenaar864293a2016-06-02 13:40:04 +02004130 if (old_last == NULL)
4131 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004132 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004133
4134 /* restore curwin/curbuf and a few other things */
4135 aucmd_restbuf(&aco);
4136 }
4137
4138 /* Only redraw when added lines are visible. This avoids flickering
4139 * when the added lines are not visible. */
4140 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4141 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143}
4144
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004145/*
4146 * Set "w:quickfix_title" if "qi" has a title.
4147 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004148 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004149qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004150{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004151 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
4152 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004153 qi->qf_lists[qi->qf_curlist].qf_title);
4154}
4155
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004157 * Add an error line to the quickfix buffer.
4158 */
4159 static int
4160qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4161{
4162 int len;
4163 buf_T *errbuf;
4164
4165 if (qfp->qf_module != NULL)
4166 {
4167 STRCPY(IObuff, qfp->qf_module);
4168 len = (int)STRLEN(IObuff);
4169 }
4170 else if (qfp->qf_fnum != 0
4171 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4172 && errbuf->b_fname != NULL)
4173 {
4174 if (qfp->qf_type == 1) /* :helpgrep */
4175 STRCPY(IObuff, gettail(errbuf->b_fname));
4176 else
4177 {
4178 /* shorten the file name if not done already */
4179 if (errbuf->b_sfname == NULL
4180 || mch_isFullName(errbuf->b_sfname))
4181 {
4182 if (*dirname == NUL)
4183 mch_dirname(dirname, MAXPATHL);
4184 shorten_buf_fname(errbuf, dirname, FALSE);
4185 }
4186 STRCPY(IObuff, errbuf->b_fname);
4187 }
4188 len = (int)STRLEN(IObuff);
4189 }
4190 else
4191 len = 0;
4192 IObuff[len++] = '|';
4193
4194 if (qfp->qf_lnum > 0)
4195 {
4196 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4197 len += (int)STRLEN(IObuff + len);
4198
4199 if (qfp->qf_col > 0)
4200 {
4201 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4202 len += (int)STRLEN(IObuff + len);
4203 }
4204
4205 sprintf((char *)IObuff + len, "%s",
4206 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4207 len += (int)STRLEN(IObuff + len);
4208 }
4209 else if (qfp->qf_pattern != NULL)
4210 {
4211 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4212 len += (int)STRLEN(IObuff + len);
4213 }
4214 IObuff[len++] = '|';
4215 IObuff[len++] = ' ';
4216
4217 /* Remove newlines and leading whitespace from the text.
4218 * For an unrecognized line keep the indent, the compiler may
4219 * mark a word with ^^^^. */
4220 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4221 IObuff + len, IOSIZE - len);
4222
4223 if (ml_append_buf(buf, lnum, IObuff,
4224 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4225 return FAIL;
4226
4227 return OK;
4228}
4229
4230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 * Fill current buffer with quickfix errors, replacing any previous contents.
4232 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004233 * If "old_last" is not NULL append the items after this one.
4234 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4235 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 */
4237 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004238qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004240 linenr_T lnum;
4241 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004242 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
Bram Moolenaar864293a2016-06-02 13:40:04 +02004244 if (old_last == NULL)
4245 {
4246 if (buf != curbuf)
4247 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004248 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004249 return;
4250 }
4251
4252 /* delete all existing lines */
4253 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4254 (void)ml_delete((linenr_T)1, FALSE);
4255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004258 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
Bram Moolenaara796d462018-05-01 14:30:36 +02004260 char_u dirname[MAXPATHL];
4261
4262 *dirname = NUL;
4263
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004265 if (old_last == NULL)
4266 {
4267 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4268 lnum = 0;
4269 }
4270 else
4271 {
4272 qfp = old_last->qf_next;
4273 lnum = buf->b_ml.ml_line_count;
4274 }
4275 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004277 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004279
Bram Moolenaar864293a2016-06-02 13:40:04 +02004280 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004282 if (qfp == NULL)
4283 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004285
4286 if (old_last == NULL)
4287 /* Delete the empty line which is now at the end */
4288 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290
4291 /* correct cursor position */
4292 check_lnums(TRUE);
4293
Bram Moolenaar864293a2016-06-02 13:40:04 +02004294 if (old_last == NULL)
4295 {
4296 /* Set the 'filetype' to "qf" each time after filling the buffer.
4297 * This resembles reading a file into a buffer, it's more logical when
4298 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02004299 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004300 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4301 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
Bram Moolenaar864293a2016-06-02 13:40:04 +02004303 keep_filetype = TRUE; /* don't detect 'filetype' */
4304 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004306 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004308 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004309 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004310
Bram Moolenaar864293a2016-06-02 13:40:04 +02004311 /* make sure it will be redrawn */
4312 redraw_curbuf_later(NOT_VALID);
4313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314
4315 /* Restore KeyTyped, setting 'filetype' may reset it. */
4316 KeyTyped = old_KeyTyped;
4317}
4318
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004319/*
4320 * For every change made to the quickfix list, update the changed tick.
4321 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004322 static void
4323qf_list_changed(qf_info_T *qi, int qf_idx)
4324{
4325 qi->qf_lists[qf_idx].qf_changedtick++;
4326}
4327
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004329 * Return the quickfix/location list number with the given identifier.
4330 * Returns -1 if list is not found.
4331 */
4332 static int
4333qf_id2nr(qf_info_T *qi, int_u qfid)
4334{
4335 int qf_idx;
4336
4337 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4338 if (qi->qf_lists[qf_idx].qf_id == qfid)
4339 return qf_idx;
4340 return INVALID_QFIDX;
4341}
4342
4343/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004344 * If the current list is not "save_qfid" and we can find the list with that ID
4345 * then make it the current list.
4346 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004347 * Returns OK if successfully restored the list. Returns FAIL if the list with
4348 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004349 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004350 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004351qf_restore_list(qf_info_T *qi, int_u save_qfid)
4352{
4353 int curlist;
4354
4355 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4356 {
4357 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004358 if (curlist < 0)
4359 // list is not present
4360 return FAIL;
4361 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004362 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004363 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004364}
4365
4366/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004367 * Jump to the first entry if there is one.
4368 */
4369 static void
4370qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4371{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004372 if (qf_restore_list(qi, save_qfid) == FAIL)
4373 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004374
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004375 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004376 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004377 qf_jump(qi, 0, 0, forceit);
4378}
4379
4380/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004381 * Return TRUE when using ":vimgrep" for ":grep".
4382 */
4383 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004384grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004385{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004386 return ((cmdidx == CMD_grep
4387 || cmdidx == CMD_lgrep
4388 || cmdidx == CMD_grepadd
4389 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004390 && STRCMP("internal",
4391 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4392}
4393
4394/*
Bram Moolenaara6557602006-02-04 22:43:20 +00004395 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 */
4397 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004398ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
Bram Moolenaar7c626922005-02-07 22:01:03 +00004400 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004402 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00004404 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004405 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004406 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004407 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004408 int_u save_qfid;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004409
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004410 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
4411 if (grep_internal(eap->cmdidx))
4412 {
4413 ex_vimgrep(eap);
4414 return;
4415 }
4416
Bram Moolenaar7c626922005-02-07 22:01:03 +00004417 switch (eap->cmdidx)
4418 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00004419 case CMD_make: au_name = (char_u *)"make"; break;
4420 case CMD_lmake: au_name = (char_u *)"lmake"; break;
4421 case CMD_grep: au_name = (char_u *)"grep"; break;
4422 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4423 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4424 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004425 default: break;
4426 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004427 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4428 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004429 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004430#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004431 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004432 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004433#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004434 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004435#ifdef FEAT_MBYTE
4436 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
Bram Moolenaar39665952018-08-15 20:59:48 +02004439 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00004440 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004441
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00004443 fname = get_mef_name();
4444 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004446 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448 /*
4449 * If 'shellpipe' empty: don't redirect to 'errorfile'.
4450 */
4451 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
4452 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00004453 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 cmd = alloc(len);
4455 if (cmd == NULL)
4456 return;
4457 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
4458 (char *)p_shq);
4459 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004460 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 /*
4462 * Output a newline if there's something else than the :make command that
4463 * was typed (in which case the cursor is in column 0).
4464 */
4465 if (msg_col == 0)
4466 msg_didout = FALSE;
4467 msg_start();
4468 MSG_PUTS(":!");
4469 msg_outtrans(cmd); /* show what we are doing */
4470
4471 /* let the shell know if we are redirecting output or not */
4472 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4473
4474#ifdef AMIGA
4475 out_flush();
4476 /* read window status report and redraw before message */
4477 (void)char_avail();
4478#endif
4479
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004480 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00004481 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4482 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004483 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004484 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004485 if (wp != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004486 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004487 qi = GET_LOC_LIST(wp);
4488 if (qi == NULL)
4489 goto cleanup;
4490 }
4491 if (res >= 0)
4492 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004493
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004494 // Remember the current quickfix list identifier, so that we can
4495 // check for autocommands changing the current quickfix list.
4496 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4497 if (au_name != NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004498 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4499 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004500 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004501 // display the first error
4502 qf_jump_first(qi, save_qfid, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004504cleanup:
Bram Moolenaar7c626922005-02-07 22:01:03 +00004505 mch_remove(fname);
4506 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 vim_free(cmd);
4508}
4509
4510/*
4511 * Return the name for the errorfile, in allocated memory.
4512 * Find a new unique name when 'makeef' contains "##".
4513 * Returns NULL for error.
4514 */
4515 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004516get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517{
4518 char_u *p;
4519 char_u *name;
4520 static int start = -1;
4521 static int off = 0;
4522#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004523 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524#endif
4525
4526 if (*p_mef == NUL)
4527 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004528 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 if (name == NULL)
4530 EMSG(_(e_notmp));
4531 return name;
4532 }
4533
4534 for (p = p_mef; *p; ++p)
4535 if (p[0] == '#' && p[1] == '#')
4536 break;
4537
4538 if (*p == NUL)
4539 return vim_strsave(p_mef);
4540
4541 /* Keep trying until the name doesn't exist yet. */
4542 for (;;)
4543 {
4544 if (start == -1)
4545 start = mch_get_pid();
4546 else
4547 off += 19;
4548
4549 name = alloc((unsigned)STRLEN(p_mef) + 30);
4550 if (name == NULL)
4551 break;
4552 STRCPY(name, p_mef);
4553 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4554 STRCAT(name, p + 2);
4555 if (mch_getperm(name) < 0
4556#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02004557 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 && mch_lstat((char *)name, &sb) < 0
4559#endif
4560 )
4561 break;
4562 vim_free(name);
4563 }
4564 return name;
4565}
4566
4567/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004568 * Returns the number of valid entries in the current quickfix/location list.
4569 */
4570 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004571qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004572{
4573 qf_info_T *qi = &ql_info;
4574 qfline_T *qfp;
4575 int i, sz = 0;
4576 int prev_fnum = 0;
4577
Bram Moolenaar39665952018-08-15 20:59:48 +02004578 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004579 {
4580 /* Location list */
4581 qi = GET_LOC_LIST(curwin);
4582 if (qi == NULL)
4583 return 0;
4584 }
4585
4586 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004587 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004588 ++i, qfp = qfp->qf_next)
4589 {
4590 if (qfp->qf_valid)
4591 {
4592 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4593 sz++; /* Count all valid entries */
4594 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4595 {
4596 /* Count the number of files */
4597 sz++;
4598 prev_fnum = qfp->qf_fnum;
4599 }
4600 }
4601 }
4602
4603 return sz;
4604}
4605
4606/*
4607 * Returns the current index of the quickfix/location list.
4608 * Returns 0 if there is an error.
4609 */
4610 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004611qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004612{
4613 qf_info_T *qi = &ql_info;
4614
Bram Moolenaar39665952018-08-15 20:59:48 +02004615 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004616 {
4617 /* Location list */
4618 qi = GET_LOC_LIST(curwin);
4619 if (qi == NULL)
4620 return 0;
4621 }
4622
4623 return qi->qf_lists[qi->qf_curlist].qf_index;
4624}
4625
4626/*
4627 * Returns the current index in the quickfix/location list (counting only valid
4628 * entries). If no valid entries are in the list, then returns 1.
4629 */
4630 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004631qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004632{
4633 qf_info_T *qi = &ql_info;
4634 qf_list_T *qfl;
4635 qfline_T *qfp;
4636 int i, eidx = 0;
4637 int prev_fnum = 0;
4638
Bram Moolenaar39665952018-08-15 20:59:48 +02004639 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004640 {
4641 /* Location list */
4642 qi = GET_LOC_LIST(curwin);
4643 if (qi == NULL)
4644 return 1;
4645 }
4646
4647 qfl = &qi->qf_lists[qi->qf_curlist];
4648 qfp = qfl->qf_start;
4649
4650 /* check if the list has valid errors */
4651 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4652 return 1;
4653
4654 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4655 {
4656 if (qfp->qf_valid)
4657 {
4658 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4659 {
4660 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4661 {
4662 /* Count the number of files */
4663 eidx++;
4664 prev_fnum = qfp->qf_fnum;
4665 }
4666 }
4667 else
4668 eidx++;
4669 }
4670 }
4671
4672 return eidx ? eidx : 1;
4673}
4674
4675/*
4676 * Get the 'n'th valid error entry in the quickfix or location list.
4677 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4678 * For :cdo and :ldo returns the 'n'th valid error entry.
4679 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4680 */
4681 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004682qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004683{
4684 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4685 qfline_T *qfp = qfl->qf_start;
4686 int i, eidx;
4687 int prev_fnum = 0;
4688
4689 /* check if the list has valid errors */
4690 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4691 return 1;
4692
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004693 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004694 i++, qfp = qfp->qf_next)
4695 {
4696 if (qfp->qf_valid)
4697 {
4698 if (fdo)
4699 {
4700 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4701 {
4702 /* Count the number of files */
4703 eidx++;
4704 prev_fnum = qfp->qf_fnum;
4705 }
4706 }
4707 else
4708 eidx++;
4709 }
4710
4711 if (eidx == n)
4712 break;
4713 }
4714
4715 if (i <= qfl->qf_count)
4716 return i;
4717 else
4718 return 1;
4719}
4720
4721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004723 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004724 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 */
4726 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004727ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004729 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004730 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004731
Bram Moolenaar39665952018-08-15 20:59:48 +02004732 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004733 {
4734 qi = GET_LOC_LIST(curwin);
4735 if (qi == NULL)
4736 {
4737 EMSG(_(e_loclist));
4738 return;
4739 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004740 }
4741
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004742 if (eap->addr_count > 0)
4743 errornr = (int)eap->line2;
4744 else
4745 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004746 switch (eap->cmdidx)
4747 {
4748 case CMD_cc: case CMD_ll:
4749 errornr = 0;
4750 break;
4751 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4752 case CMD_lfirst:
4753 errornr = 1;
4754 break;
4755 default:
4756 errornr = 32767;
4757 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004758 }
4759
4760 /* For cdo and ldo commands, jump to the nth valid error.
4761 * For cfdo and lfdo commands, jump to the nth valid file entry.
4762 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004763 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4764 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004765 errornr = qf_get_nth_valid_entry(qi,
4766 eap->addr_count > 0 ? (int)eap->line1 : 1,
4767 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4768
4769 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770}
4771
4772/*
4773 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004774 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004775 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 */
4777 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004778ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004780 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004781 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004782 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004783
Bram Moolenaar39665952018-08-15 20:59:48 +02004784 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004785 {
4786 qi = GET_LOC_LIST(curwin);
4787 if (qi == NULL)
4788 {
4789 EMSG(_(e_loclist));
4790 return;
4791 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004792 }
4793
Bram Moolenaar55b69262017-08-13 13:42:01 +02004794 if (eap->addr_count > 0
4795 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4796 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004797 errornr = (int)eap->line2;
4798 else
4799 errornr = 1;
4800
Bram Moolenaar39665952018-08-15 20:59:48 +02004801 // Depending on the command jump to either next or previous entry/file.
4802 switch (eap->cmdidx)
4803 {
4804 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4805 dir = FORWARD;
4806 break;
4807 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4808 case CMD_lNext:
4809 dir = BACKWARD;
4810 break;
4811 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4812 dir = FORWARD_FILE;
4813 break;
4814 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4815 dir = BACKWARD_FILE;
4816 break;
4817 default:
4818 dir = FORWARD;
4819 break;
4820 }
4821
4822 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823}
4824
4825/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004826 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004827 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 */
4829 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004830ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004832 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004833 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004834 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004835 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004836 int_u save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004837 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004838
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004839 switch (eap->cmdidx)
4840 {
4841 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4842 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4843 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4844 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4845 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4846 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4847 default: break;
4848 }
4849 if (au_name != NULL)
4850 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004851#ifdef FEAT_MBYTE
4852 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4853#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004854#ifdef FEAT_BROWSE
4855 if (cmdmod.browse)
4856 {
4857 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004858 NULL, NULL,
4859 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004860 if (browse_file == NULL)
4861 return;
4862 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4863 vim_free(browse_file);
4864 }
4865 else
4866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004868 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004869
Bram Moolenaar39665952018-08-15 20:59:48 +02004870 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004871 wp = curwin;
4872
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004873 /*
4874 * This function is used by the :cfile, :cgetfile and :caddfile
4875 * commands.
4876 * :cfile always creates a new quickfix list and jumps to the
4877 * first error.
4878 * :cgetfile creates a new quickfix list but doesn't jump to the
4879 * first error.
4880 * :caddfile adds to an existing quickfix list. If there is no
4881 * quickfix list then a new list is created.
4882 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004883 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004884 && eap->cmdidx != CMD_laddfile),
4885 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004886 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004887 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01004888 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004889 if (qi == NULL)
4890 return;
4891 }
4892 if (res >= 0)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004893 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004894 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004895 if (au_name != NULL)
4896 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004897
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004898 // Jump to the first error for a new list and if autocmds didn't
4899 // free the list.
4900 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
4901 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004902 // display the first error
4903 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004904}
4905
4906/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004907 * Return the vimgrep autocmd name.
4908 */
4909 static char_u *
4910vgr_get_auname(cmdidx_T cmdidx)
4911{
4912 switch (cmdidx)
4913 {
4914 case CMD_vimgrep: return (char_u *)"vimgrep";
4915 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4916 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4917 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4918 case CMD_grep: return (char_u *)"grep";
4919 case CMD_lgrep: return (char_u *)"lgrep";
4920 case CMD_grepadd: return (char_u *)"grepadd";
4921 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4922 default: return NULL;
4923 }
4924}
4925
4926/*
4927 * Initialize the regmatch used by vimgrep for pattern "s".
4928 */
4929 static void
4930vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4931{
4932 /* Get the search pattern: either white-separated or enclosed in // */
4933 regmatch->regprog = NULL;
4934
4935 if (s == NULL || *s == NUL)
4936 {
4937 /* Pattern is empty, use last search pattern. */
4938 if (last_search_pat() == NULL)
4939 {
4940 EMSG(_(e_noprevre));
4941 return;
4942 }
4943 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4944 }
4945 else
4946 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4947
4948 regmatch->rmm_ic = p_ic;
4949 regmatch->rmm_maxcol = 0;
4950}
4951
4952/*
4953 * Display a file name when vimgrep is running.
4954 */
4955 static void
4956vgr_display_fname(char_u *fname)
4957{
4958 char_u *p;
4959
4960 msg_start();
4961 p = msg_strtrunc(fname, TRUE);
4962 if (p == NULL)
4963 msg_outtrans(fname);
4964 else
4965 {
4966 msg_outtrans(p);
4967 vim_free(p);
4968 }
4969 msg_clr_eos();
4970 msg_didout = FALSE; /* overwrite this message */
4971 msg_nowait = TRUE; /* don't wait for this message */
4972 msg_col = 0;
4973 out_flush();
4974}
4975
4976/*
4977 * Load a dummy buffer to search for a pattern using vimgrep.
4978 */
4979 static buf_T *
4980vgr_load_dummy_buf(
4981 char_u *fname,
4982 char_u *dirname_start,
4983 char_u *dirname_now)
4984{
4985 int save_mls;
4986#if defined(FEAT_SYN_HL)
4987 char_u *save_ei = NULL;
4988#endif
4989 buf_T *buf;
4990
4991#if defined(FEAT_SYN_HL)
4992 /* Don't do Filetype autocommands to avoid loading syntax and
4993 * indent scripts, a great speed improvement. */
4994 save_ei = au_event_disable(",Filetype");
4995#endif
4996 /* Don't use modelines here, it's useless. */
4997 save_mls = p_mls;
4998 p_mls = 0;
4999
5000 /* Load file into a buffer, so that 'fileencoding' is detected,
5001 * autocommands applied, etc. */
5002 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5003
5004 p_mls = save_mls;
5005#if defined(FEAT_SYN_HL)
5006 au_event_restore(save_ei);
5007#endif
5008
5009 return buf;
5010}
5011
5012/*
5013 * Check whether a quickfix/location list valid. Autocmds may remove or change
5014 * a quickfix list when vimgrep is running. If the list is not found, create a
5015 * new list.
5016 */
5017 static int
5018vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005019 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005020 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005021 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005022 char_u *title)
5023{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005024 /* Verify that the quickfix/location list was not freed by an autocmd */
5025 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005026 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005027 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005028 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005029 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005030 EMSG(_(e_loc_list_changed));
5031 return FALSE;
5032 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005033 else
5034 {
5035 /* Quickfix list is not found, create a new one. */
5036 qf_new_list(qi, title);
5037 return TRUE;
5038 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005039 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005040
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005041 if (qf_restore_list(qi, qfid) == FAIL)
5042 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005043
5044 return TRUE;
5045}
5046
5047/*
5048 * Search for a pattern in all the lines in a buffer and add the matching lines
5049 * to a quickfix list.
5050 */
5051 static int
5052vgr_match_buflines(
5053 qf_info_T *qi,
5054 char_u *fname,
5055 buf_T *buf,
5056 regmmatch_T *regmatch,
5057 long tomatch,
5058 int duplicate_name,
5059 int flags)
5060{
5061 int found_match = FALSE;
5062 long lnum;
5063 colnr_T col;
5064
5065 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5066 {
5067 col = 0;
5068 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5069 col, NULL, NULL) > 0)
5070 {
5071 /* Pass the buffer number so that it gets used even for a
5072 * dummy buffer, unless duplicate_name is set, then the
5073 * buffer will be wiped out below. */
5074 if (qf_add_entry(qi,
5075 qi->qf_curlist,
5076 NULL, /* dir */
5077 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005078 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005079 duplicate_name ? 0 : buf->b_fnum,
5080 ml_get_buf(buf,
5081 regmatch->startpos[0].lnum + lnum, FALSE),
5082 regmatch->startpos[0].lnum + lnum,
5083 regmatch->startpos[0].col + 1,
5084 FALSE, /* vis_col */
5085 NULL, /* search pattern */
5086 0, /* nr */
5087 0, /* type */
5088 TRUE /* valid */
5089 ) == FAIL)
5090 {
5091 got_int = TRUE;
5092 break;
5093 }
5094 found_match = TRUE;
5095 if (--tomatch == 0)
5096 break;
5097 if ((flags & VGR_GLOBAL) == 0
5098 || regmatch->endpos[0].lnum > 0)
5099 break;
5100 col = regmatch->endpos[0].col
5101 + (col == regmatch->endpos[0].col);
5102 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5103 break;
5104 }
5105 line_breakcheck();
5106 if (got_int)
5107 break;
5108 }
5109
5110 return found_match;
5111}
5112
5113/*
5114 * Jump to the first match and update the directory.
5115 */
5116 static void
5117vgr_jump_to_match(
5118 qf_info_T *qi,
5119 int forceit,
5120 int *redraw_for_dummy,
5121 buf_T *first_match_buf,
5122 char_u *target_dir)
5123{
5124 buf_T *buf;
5125
5126 buf = curbuf;
5127 qf_jump(qi, 0, 0, forceit);
5128 if (buf != curbuf)
5129 /* If we jumped to another buffer redrawing will already be
5130 * taken care of. */
5131 *redraw_for_dummy = FALSE;
5132
5133 /* Jump to the directory used after loading the buffer. */
5134 if (curbuf == first_match_buf && target_dir != NULL)
5135 {
5136 exarg_T ea;
5137
5138 ea.arg = target_dir;
5139 ea.cmdidx = CMD_lcd;
5140 ex_cd(&ea);
5141 }
5142}
5143
5144/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005145 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005146 * ":vimgrepadd {pattern} file(s)"
5147 * ":lvimgrep {pattern} file(s)"
5148 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005149 */
5150 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005151ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005152{
Bram Moolenaar81695252004-12-29 20:58:21 +00005153 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005154 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005155 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005156 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005157 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005158 char_u *s;
5159 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005160 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005161 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005162 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005163 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005164 buf_T *buf;
5165 int duplicate_name = FALSE;
5166 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005167 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005168 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005169 buf_T *first_match_buf = NULL;
5170 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005171 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005172 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005173 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005174 char_u *dirname_start = NULL;
5175 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005176 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005177 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005178
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005179 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005180 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5181 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005182 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005183#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005184 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005185 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005186#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005187 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005188
Bram Moolenaar39665952018-08-15 20:59:48 +02005189 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005190 {
5191 qi = ll_get_or_alloc_list(curwin);
5192 if (qi == NULL)
5193 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005194 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005195 }
5196
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005197 if (eap->addr_count > 0)
5198 tomatch = eap->line2;
5199 else
5200 tomatch = MAXLNUM;
5201
Bram Moolenaar81695252004-12-29 20:58:21 +00005202 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005203 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005204 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005205 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005206 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005207 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005208 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005209 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005210 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005211
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005212 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005213 if (regmatch.regprog == NULL)
5214 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005215
5216 p = skipwhite(p);
5217 if (*p == NUL)
5218 {
5219 EMSG(_("E683: File name missing or invalid pattern"));
5220 goto theend;
5221 }
5222
Bram Moolenaar55b69262017-08-13 13:42:01 +02005223 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005224 && eap->cmdidx != CMD_vimgrepadd
5225 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005226 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005227 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005228 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005229
5230 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005231 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005232 goto theend;
5233 if (fcount == 0)
5234 {
5235 EMSG(_(e_nomatch));
5236 goto theend;
5237 }
5238
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005239 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5240 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005241 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005242 {
5243 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005244 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005245 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005246
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005247 /* Remember the current directory, because a BufRead autocommand that does
5248 * ":lcd %:p:h" changes the meaning of short path names. */
5249 mch_dirname(dirname_start, MAXPATHL);
5250
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005251 /* Remember the current quickfix list identifier, so that we can check for
5252 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005253 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005254
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005255 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005256 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005257 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005258 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005259 if (time(NULL) > seconds)
5260 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005261 /* Display the file name every second or so, show the user we are
5262 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005263 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005264 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005265 }
5266
Bram Moolenaar81695252004-12-29 20:58:21 +00005267 buf = buflist_findname_exp(fnames[fi]);
5268 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5269 {
5270 /* Remember that a buffer with this name already exists. */
5271 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005272 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005273 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005274
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005275 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005276 }
5277 else
5278 /* Use existing, loaded buffer. */
5279 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005280
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005281 /* Check whether the quickfix list is still valid. When loading a
5282 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005283 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005284 {
5285 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005286 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005287 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005288 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005289
Bram Moolenaar81695252004-12-29 20:58:21 +00005290 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005291 {
5292 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005293 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005294 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005295 else
5296 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005297 /* Try for a match in all lines of the buffer.
5298 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005299 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5300 tomatch, duplicate_name, flags);
5301
Bram Moolenaar81695252004-12-29 20:58:21 +00005302 if (using_dummy)
5303 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005304 if (found_match && first_match_buf == NULL)
5305 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005306 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005307 {
Bram Moolenaar81695252004-12-29 20:58:21 +00005308 /* Never keep a dummy buffer if there is another buffer
5309 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005310 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005311 buf = NULL;
5312 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005313 else if (!cmdmod.hide
5314 || buf->b_p_bh[0] == 'u' /* "unload" */
5315 || buf->b_p_bh[0] == 'w' /* "wipe" */
5316 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00005317 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005318 /* When no match was found we don't need to remember the
5319 * buffer, wipe it out. If there was a match and it
5320 * wasn't the first one or we won't jump there: only
5321 * unload the buffer.
5322 * Ignore 'hidden' here, because it may lead to having too
5323 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00005324 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005325 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005326 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005327 buf = NULL;
5328 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005329 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005330 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005331 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02005332 /* Keeping the buffer, remove the dummy flag. */
5333 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005334 buf = NULL;
5335 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005336 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005337
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005338 if (buf != NULL)
5339 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02005340 /* Keeping the buffer, remove the dummy flag. */
5341 buf->b_flags &= ~BF_DUMMY;
5342
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005343 /* If the buffer is still loaded we need to use the
5344 * directory we jumped to below. */
5345 if (buf == first_match_buf
5346 && target_dir == NULL
5347 && STRCMP(dirname_start, dirname_now) != 0)
5348 target_dir = vim_strsave(dirname_now);
5349
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005350 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005351 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00005352 * need to be done (again). But not the window-local
5353 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005354 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005355#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005356 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5357 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005358#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005359 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005360 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005361 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005362 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005363 }
5364 }
5365
5366 FreeWild(fcount, fnames);
5367
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005368 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5369 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5370 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005371 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005372
Bram Moolenaar864293a2016-06-02 13:40:04 +02005373 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005374
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005375 if (au_name != NULL)
5376 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5377 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005378 /*
5379 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5380 * is still valid.
5381 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005382 if (!qflist_valid(wp, save_qfid))
5383 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005384
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005385 if (qf_restore_list(qi, save_qfid) == FAIL)
5386 goto theend;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005387
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005388 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005389 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005390 {
5391 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005392 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5393 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005394 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005395 else
5396 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005397
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005398 /* If we loaded a dummy buffer into the current window, the autocommands
5399 * may have messed up things, need to redraw and recompute folds. */
5400 if (redraw_for_dummy)
5401 {
5402#ifdef FEAT_FOLDING
5403 foldUpdateAll(curwin);
5404#else
5405 redraw_later(NOT_VALID);
5406#endif
5407 }
5408
Bram Moolenaar86b68352004-12-27 21:59:20 +00005409theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005410 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005411 vim_free(dirname_now);
5412 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005413 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005414 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005415}
5416
5417/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005418 * Restore current working directory to "dirname_start" if they differ, taking
5419 * into account whether it is set locally or globally.
5420 */
5421 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005422restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005423{
5424 char_u *dirname_now = alloc(MAXPATHL);
5425
5426 if (NULL != dirname_now)
5427 {
5428 mch_dirname(dirname_now, MAXPATHL);
5429 if (STRCMP(dirname_start, dirname_now) != 0)
5430 {
5431 /* If the directory has changed, change it back by building up an
5432 * appropriate ex command and executing it. */
5433 exarg_T ea;
5434
5435 ea.arg = dirname_start;
5436 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5437 ex_cd(&ea);
5438 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005439 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005440 }
5441}
5442
5443/*
5444 * Load file "fname" into a dummy buffer and return the buffer pointer,
5445 * placing the directory resulting from the buffer load into the
5446 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5447 * prior to calling this function. Restores directory to "dirname_start" prior
5448 * to returning, if autocmds or the 'autochdir' option have changed it.
5449 *
5450 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5451 * or wipe_dummy_buffer() later!
5452 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005453 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005454 */
5455 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005456load_dummy_buffer(
5457 char_u *fname,
5458 char_u *dirname_start, /* in: old directory */
5459 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005460{
5461 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005462 bufref_T newbufref;
5463 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005464 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005465 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005466 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005467
5468 /* Allocate a buffer without putting it in the buffer list. */
5469 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5470 if (newbuf == NULL)
5471 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005472 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005473
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005474 /* Init the options. */
5475 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5476
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005477 /* need to open the memfile before putting the buffer in a window */
5478 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005479 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005480 /* Make sure this buffer isn't wiped out by autocommands. */
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005481 ++newbuf->b_locked;
5482
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005483 /* set curwin/curbuf to buf and save a few things */
5484 aucmd_prepbuf(&aco, newbuf);
5485
5486 /* Need to set the filename for autocommands. */
5487 (void)setfname(curbuf, fname, NULL, FALSE);
5488
Bram Moolenaar81695252004-12-29 20:58:21 +00005489 /* Create swap file now to avoid the ATTENTION message. */
5490 check_need_swap(TRUE);
5491
5492 /* Remove the "dummy" flag, otherwise autocommands may not
5493 * work. */
5494 curbuf->b_flags &= ~BF_DUMMY;
5495
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005496 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005497 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005498 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005499 NULL, READ_NEW | READ_DUMMY);
5500 --newbuf->b_locked;
5501 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005502 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005503 && !(curbuf->b_flags & BF_NEW))
5504 {
5505 failed = FALSE;
5506 if (curbuf != newbuf)
5507 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005508 /* Bloody autocommands changed the buffer! Can happen when
5509 * using netrw and editing a remote file. Use the current
5510 * buffer instead, delete the dummy one after restoring the
5511 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005512 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005513 newbuf = curbuf;
5514 }
5515 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005516
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005517 /* restore curwin/curbuf and a few other things */
5518 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005519 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5520 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005521
5522 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5523 * skip it. */
5524 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005525 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005526
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005527 /*
5528 * When autocommands/'autochdir' option changed directory: go back.
5529 * Let the caller know what the resulting dir was first, in case it is
5530 * important.
5531 */
5532 mch_dirname(resulting_dir, MAXPATHL);
5533 restore_start_dir(dirname_start);
5534
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005535 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005536 return NULL;
5537 if (failed)
5538 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005539 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005540 return NULL;
5541 }
5542 return newbuf;
5543}
5544
5545/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005546 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5547 * directory to "dirname_start" prior to returning, if autocmds or the
5548 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005549 */
5550 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005551wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005552{
5553 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005554 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005555#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005556 cleanup_T cs;
5557
5558 /* Reset the error/interrupt/exception state here so that aborting()
5559 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5560 * work when got_int is set. */
5561 enter_cleanup(&cs);
5562#endif
5563
Bram Moolenaar81695252004-12-29 20:58:21 +00005564 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005565
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005566#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005567 /* Restore the error/interrupt/exception state if not discarded by a
5568 * new aborting error, interrupt, or uncaught exception. */
5569 leave_cleanup(&cs);
5570#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005571 /* When autocommands/'autochdir' option changed directory: go back. */
5572 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005573 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005574}
5575
5576/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005577 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5578 * directory to "dirname_start" prior to returning, if autocmds or the
5579 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005580 */
5581 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005582unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005583{
5584 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005585 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005586 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005587
5588 /* When autocommands/'autochdir' option changed directory: go back. */
5589 restore_start_dir(dirname_start);
5590 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005591}
5592
Bram Moolenaar05159a02005-02-26 23:04:13 +00005593#if defined(FEAT_EVAL) || defined(PROTO)
5594/*
5595 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005596 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005597 */
5598 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005599get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005600{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005601 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005602 dict_T *dict;
5603 char_u buf[2];
5604 qfline_T *qfp;
5605 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005606 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005607
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005608 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005609 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005610 qi = &ql_info;
5611 if (wp != NULL)
5612 {
5613 qi = GET_LOC_LIST(wp);
5614 if (qi == NULL)
5615 return FAIL;
5616 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005617 }
5618
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005619 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005620 qf_idx = qi->qf_curlist;
5621
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005622 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005623 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005624
Bram Moolenaard823fa92016-08-12 16:29:27 +02005625 qfp = qi->qf_lists[qf_idx].qf_start;
5626 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005627 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005628 /* Handle entries with a non-existing buffer number. */
5629 bufnum = qfp->qf_fnum;
5630 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5631 bufnum = 0;
5632
Bram Moolenaar05159a02005-02-26 23:04:13 +00005633 if ((dict = dict_alloc()) == NULL)
5634 return FAIL;
5635 if (list_append_dict(list, dict) == FAIL)
5636 return FAIL;
5637
5638 buf[0] = qfp->qf_type;
5639 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005640 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5641 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5642 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5643 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5644 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5645 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5646 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5647 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5648 || dict_add_string(dict, "type", buf) == FAIL
5649 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005650 return FAIL;
5651
5652 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005653 if (qfp == NULL)
5654 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005655 }
5656 return OK;
5657}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005658
5659/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005660 * Flags used by getqflist()/getloclist() to determine which fields to return.
5661 */
5662enum {
5663 QF_GETLIST_NONE = 0x0,
5664 QF_GETLIST_TITLE = 0x1,
5665 QF_GETLIST_ITEMS = 0x2,
5666 QF_GETLIST_NR = 0x4,
5667 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005668 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005669 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005670 QF_GETLIST_IDX = 0x40,
5671 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005672 QF_GETLIST_TICK = 0x100,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005673 QF_GETLIST_ALL = 0x1FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005674};
5675
5676/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005677 * Parse text from 'di' and return the quickfix list items.
5678 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005679 */
5680 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005681qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005682{
5683 int status = FAIL;
5684 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005685 char_u *errorformat = p_efm;
5686 dictitem_T *efm_di;
5687 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005688
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005689 /* Only a List value is supported */
5690 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005691 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005692 /* If errorformat is supplied then use it, otherwise use the 'efm'
5693 * option setting
5694 */
5695 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5696 {
5697 if (efm_di->di_tv.v_type != VAR_STRING ||
5698 efm_di->di_tv.vval.v_string == NULL)
5699 return FAIL;
5700 errorformat = efm_di->di_tv.vval.v_string;
5701 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005702
Bram Moolenaar36538222017-09-02 19:51:44 +02005703 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005704 if (l == NULL)
5705 return FAIL;
5706
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005707 qi = ll_new_list();
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005708 if (qi != NULL)
5709 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005710 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005711 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5712 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005713 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005714 qf_free(qi, 0);
5715 }
5716 free(qi);
5717 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005718 dict_add_list(retdict, "items", l);
5719 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005720 }
5721
5722 return status;
5723}
5724
5725/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005726 * Return the quickfix/location list window identifier in the current tabpage.
5727 */
5728 static int
5729qf_winid(qf_info_T *qi)
5730{
5731 win_T *win;
5732
5733 /* The quickfix window can be opened even if the quickfix list is not set
5734 * using ":copen". This is not true for location lists. */
5735 if (qi == NULL)
5736 return 0;
5737 win = qf_find_win(qi);
5738 if (win != NULL)
5739 return win->w_id;
5740 return 0;
5741}
5742
5743/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005744 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005745 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005746 static int
5747qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005748{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005749 int flags = QF_GETLIST_NONE;
5750
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005751 if (dict_find(what, (char_u *)"all", -1) != NULL)
5752 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005753
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005754 if (dict_find(what, (char_u *)"title", -1) != NULL)
5755 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005756
Bram Moolenaara6d48492017-12-12 22:45:31 +01005757 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5758 flags |= QF_GETLIST_NR;
5759
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005760 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5761 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005762
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005763 if (dict_find(what, (char_u *)"context", -1) != NULL)
5764 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005765
Bram Moolenaara6d48492017-12-12 22:45:31 +01005766 if (dict_find(what, (char_u *)"id", -1) != NULL)
5767 flags |= QF_GETLIST_ID;
5768
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005769 if (dict_find(what, (char_u *)"items", -1) != NULL)
5770 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005771
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005772 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5773 flags |= QF_GETLIST_IDX;
5774
5775 if (dict_find(what, (char_u *)"size", -1) != NULL)
5776 flags |= QF_GETLIST_SIZE;
5777
Bram Moolenaarb254af32017-12-18 19:48:58 +01005778 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5779 flags |= QF_GETLIST_TICK;
5780
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005781 return flags;
5782}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005783
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005784/*
5785 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5786 * If 'nr' and 'id' are not present in 'what' then return the current
5787 * quickfix list index.
5788 * If 'nr' is zero then return the current quickfix list index.
5789 * If 'nr' is '$' then return the last quickfix list index.
5790 * If 'id' is present then return the index of the quickfix list with that id.
5791 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5792 * Return -1, if quickfix list is not present or if the stack is empty.
5793 */
5794 static int
5795qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5796{
5797 int qf_idx;
5798 dictitem_T *di;
5799
5800 qf_idx = qi->qf_curlist; /* default is the current list */
5801 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5802 {
5803 /* Use the specified quickfix/location list */
5804 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005805 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005806 /* for zero use the current list */
5807 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005808 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005809 qf_idx = di->di_tv.vval.v_number - 1;
5810 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005811 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005812 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005813 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005814 else if (di->di_tv.v_type == VAR_STRING
5815 && di->di_tv.vval.v_string != NULL
5816 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5817 /* Get the last quickfix list number */
5818 qf_idx = qi->qf_listcount - 1;
5819 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005820 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005821 }
5822
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005823 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5824 {
5825 /* Look for a list with the specified id */
5826 if (di->di_tv.v_type == VAR_NUMBER)
5827 {
5828 /*
5829 * For zero, use the current list or the list specified by 'nr'
5830 */
5831 if (di->di_tv.vval.v_number != 0)
5832 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5833 }
5834 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005835 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005836 }
5837
5838 return qf_idx;
5839}
5840
5841/*
5842 * Return default values for quickfix list properties in retdict.
5843 */
5844 static int
5845qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5846{
5847 int status = OK;
5848
5849 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005850 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005851 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5852 {
5853 list_T *l = list_alloc();
5854 if (l != NULL)
5855 status = dict_add_list(retdict, "items", l);
5856 else
5857 status = FAIL;
5858 }
5859 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005860 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005861 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005862 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005863 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005864 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005865 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005866 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005867 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005868 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005869 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005870 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005871 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005872 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005873
5874 return status;
5875}
5876
5877/*
5878 * Return the quickfix list title as 'title' in retdict
5879 */
5880 static int
5881qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5882{
Bram Moolenaare0be1672018-07-08 16:50:37 +02005883 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005884}
5885
5886/*
5887 * Return the quickfix list items/entries as 'items' in retdict
5888 */
5889 static int
5890qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5891{
5892 int status = OK;
5893 list_T *l = list_alloc();
5894 if (l != NULL)
5895 {
5896 (void)get_errorlist(qi, NULL, qf_idx, l);
5897 dict_add_list(retdict, "items", l);
5898 }
5899 else
5900 status = FAIL;
5901
5902 return status;
5903}
5904
5905/*
5906 * Return the quickfix list context (if any) as 'context' in retdict.
5907 */
5908 static int
5909qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5910{
5911 int status;
5912 dictitem_T *di;
5913
5914 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5915 {
5916 di = dictitem_alloc((char_u *)"context");
5917 if (di != NULL)
5918 {
5919 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5920 status = dict_add(retdict, di);
5921 if (status == FAIL)
5922 dictitem_free(di);
5923 }
5924 else
5925 status = FAIL;
5926 }
5927 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02005928 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005929
5930 return status;
5931}
5932
5933/*
5934 * Return the quickfix list index as 'idx' in retdict
5935 */
5936 static int
5937qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5938{
5939 int idx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005940 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005941 /* For empty lists, qf_index is set to 1 */
5942 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005943 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005944}
5945
5946/*
5947 * Return quickfix/location list details (title) as a
5948 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5949 * then current list is used. Otherwise the specified list is used.
5950 */
5951 int
5952qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5953{
5954 qf_info_T *qi = &ql_info;
5955 int status = OK;
5956 int qf_idx;
5957 dictitem_T *di;
5958 int flags = QF_GETLIST_NONE;
5959
5960 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5961 return qf_get_list_from_lines(what, di, retdict);
5962
5963 if (wp != NULL)
5964 qi = GET_LOC_LIST(wp);
5965
5966 flags = qf_getprop_keys2flags(what);
5967
5968 if (qi != NULL && qi->qf_listcount != 0)
5969 qf_idx = qf_getprop_qfidx(qi, what);
5970
Bram Moolenaara6d48492017-12-12 22:45:31 +01005971 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005972 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005973 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005974
Bram Moolenaard823fa92016-08-12 16:29:27 +02005975 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005976 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005977 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005978 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005979 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005980 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005981 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005982 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005983 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005984 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005985 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005986 status = dict_add_number(retdict, "id", qi->qf_lists[qf_idx].qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005987 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005988 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005989 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005990 status = dict_add_number(retdict, "size",
5991 qi->qf_lists[qf_idx].qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005992 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005993 status = dict_add_number(retdict, "changedtick",
5994 qi->qf_lists[qf_idx].qf_changedtick);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005995
Bram Moolenaard823fa92016-08-12 16:29:27 +02005996 return status;
5997}
5998
5999/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006000 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
6001 * items in the dict 'd'.
6002 */
6003 static int
6004qf_add_entry_from_dict(
6005 qf_info_T *qi,
6006 int qf_idx,
6007 dict_T *d,
6008 int first_entry)
6009{
6010 static int did_bufnr_emsg;
6011 char_u *filename, *module, *pattern, *text, *type;
6012 int bufnum, valid, status, col, vcol, nr;
6013 long lnum;
6014
6015 if (first_entry)
6016 did_bufnr_emsg = FALSE;
6017
6018 filename = get_dict_string(d, (char_u *)"filename", TRUE);
6019 module = get_dict_string(d, (char_u *)"module", TRUE);
6020 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6021 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6022 col = (int)get_dict_number(d, (char_u *)"col");
6023 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6024 nr = (int)get_dict_number(d, (char_u *)"nr");
6025 type = get_dict_string(d, (char_u *)"type", TRUE);
6026 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6027 text = get_dict_string(d, (char_u *)"text", TRUE);
6028 if (text == NULL)
6029 text = vim_strsave((char_u *)"");
6030
6031 valid = TRUE;
6032 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6033 valid = FALSE;
6034
6035 // Mark entries with non-existing buffer number as not valid. Give the
6036 // error message only once.
6037 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6038 {
6039 if (!did_bufnr_emsg)
6040 {
6041 did_bufnr_emsg = TRUE;
6042 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6043 }
6044 valid = FALSE;
6045 bufnum = 0;
6046 }
6047
6048 // If the 'valid' field is present it overrules the detected value.
6049 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6050 valid = (int)get_dict_number(d, (char_u *)"valid");
6051
6052 status = qf_add_entry(qi,
6053 qf_idx,
6054 NULL, // dir
6055 filename,
6056 module,
6057 bufnum,
6058 text,
6059 lnum,
6060 col,
6061 vcol, // vis_col
6062 pattern, // search pattern
6063 nr,
6064 type == NULL ? NUL : *type,
6065 valid);
6066
6067 vim_free(filename);
6068 vim_free(module);
6069 vim_free(pattern);
6070 vim_free(text);
6071 vim_free(type);
6072
6073 return status;
6074}
6075
6076/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006077 * Add list of entries to quickfix/location list. Each list entry is
6078 * a dictionary with item information.
6079 */
6080 static int
6081qf_add_entries(
6082 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006083 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006084 list_T *list,
6085 char_u *title,
6086 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006087{
6088 listitem_T *li;
6089 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006090 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006091 int retval = OK;
6092
Bram Moolenaara3921f42017-06-04 15:30:34 +02006093 if (action == ' ' || qf_idx == qi->qf_listcount)
6094 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006095 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006096 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006097 qf_idx = qi->qf_curlist;
6098 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006099 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006100 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02006101 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006102 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006103 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006104 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006105 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006106 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006107
6108 for (li = list->lv_first; li != NULL; li = li->li_next)
6109 {
6110 if (li->li_tv.v_type != VAR_DICT)
6111 continue; /* Skip non-dict items */
6112
6113 d = li->li_tv.vval.v_dict;
6114 if (d == NULL)
6115 continue;
6116
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006117 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first);
6118 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006119 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006120 }
6121
Bram Moolenaara3921f42017-06-04 15:30:34 +02006122 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02006123 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02006124 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006125 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02006126 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006127 if (action != 'a')
6128 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006129 qi->qf_lists[qf_idx].qf_ptr =
6130 qi->qf_lists[qf_idx].qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006131 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara3921f42017-06-04 15:30:34 +02006132 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006133 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006134
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006135 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02006136 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006137
6138 return retval;
6139}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006140
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006141/*
6142 * Get the quickfix list index from 'nr' or 'id'
6143 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006144 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006145qf_setprop_get_qfidx(
6146 qf_info_T *qi,
6147 dict_T *what,
6148 int action,
6149 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006150{
6151 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006152 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006153
Bram Moolenaard823fa92016-08-12 16:29:27 +02006154 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6155 {
6156 /* Use the specified quickfix/location list */
6157 if (di->di_tv.v_type == VAR_NUMBER)
6158 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006159 /* for zero use the current list */
6160 if (di->di_tv.vval.v_number != 0)
6161 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006162
Bram Moolenaar55b69262017-08-13 13:42:01 +02006163 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6164 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006165 /*
6166 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02006167 * non-available list and add the new list at the end of the
6168 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006169 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006170 *newlist = TRUE;
6171 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006172 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006173 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006174 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006175 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006176 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02006177 }
6178 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006179 && di->di_tv.vval.v_string != NULL
6180 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006181 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006182 if (qi->qf_listcount > 0)
6183 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006184 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006185 qf_idx = 0;
6186 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006187 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006188 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006189 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006190 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006191 }
6192
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006193 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006194 {
6195 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006196 if (di->di_tv.v_type != VAR_NUMBER)
6197 return INVALID_QFIDX;
6198
6199 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006200 }
6201
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006202 return qf_idx;
6203}
6204
6205/*
6206 * Set the quickfix list title.
6207 */
6208 static int
6209qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6210{
6211 if (di->di_tv.v_type != VAR_STRING)
6212 return FAIL;
6213
6214 vim_free(qi->qf_lists[qf_idx].qf_title);
6215 qi->qf_lists[qf_idx].qf_title =
6216 get_dict_string(what, (char_u *)"title", TRUE);
6217 if (qf_idx == qi->qf_curlist)
6218 qf_update_win_titlevar(qi);
6219
6220 return OK;
6221}
6222
6223/*
6224 * Set quickfix list items/entries.
6225 */
6226 static int
6227qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6228{
6229 int retval = FAIL;
6230 char_u *title_save;
6231
6232 if (di->di_tv.v_type != VAR_LIST)
6233 return FAIL;
6234
6235 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6236 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6237 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006238 vim_free(title_save);
6239
6240 return retval;
6241}
6242
6243/*
6244 * Set quickfix list items/entries from a list of lines.
6245 */
6246 static int
6247qf_setprop_items_from_lines(
6248 qf_info_T *qi,
6249 int qf_idx,
6250 dict_T *what,
6251 dictitem_T *di,
6252 int action)
6253{
6254 char_u *errorformat = p_efm;
6255 dictitem_T *efm_di;
6256 int retval = FAIL;
6257
6258 /* Use the user supplied errorformat settings (if present) */
6259 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6260 {
6261 if (efm_di->di_tv.v_type != VAR_STRING ||
6262 efm_di->di_tv.vval.v_string == NULL)
6263 return FAIL;
6264 errorformat = efm_di->di_tv.vval.v_string;
6265 }
6266
6267 /* Only a List value is supported */
6268 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6269 return FAIL;
6270
6271 if (action == 'r')
6272 qf_free_items(qi, qf_idx);
6273 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6274 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6275 retval = OK;
6276
6277 return retval;
6278}
6279
6280/*
6281 * Set quickfix list context.
6282 */
6283 static int
6284qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di)
6285{
6286 typval_T *ctx;
6287
6288 free_tv(qi->qf_lists[qf_idx].qf_ctx);
6289 ctx = alloc_tv();
6290 if (ctx != NULL)
6291 copy_tv(&di->di_tv, ctx);
6292 qi->qf_lists[qf_idx].qf_ctx = ctx;
6293
6294 return OK;
6295}
6296
6297/*
6298 * Set quickfix/location list properties (title, items, context).
6299 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006300 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006301 */
6302 static int
6303qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6304{
6305 dictitem_T *di;
6306 int retval = FAIL;
6307 int qf_idx;
6308 int newlist = FALSE;
6309
6310 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
6311 newlist = TRUE;
6312
6313 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6314 if (qf_idx == INVALID_QFIDX) /* List not found */
6315 return FAIL;
6316
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006317 if (newlist)
6318 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006319 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006320 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006321 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006322 }
6323
6324 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006325 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006326 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006327 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006328 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006329 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006330 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006331 retval = qf_setprop_context(qi, qf_idx, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006332
Bram Moolenaarb254af32017-12-18 19:48:58 +01006333 if (retval == OK)
6334 qf_list_changed(qi, qf_idx);
6335
Bram Moolenaard823fa92016-08-12 16:29:27 +02006336 return retval;
6337}
6338
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006339/*
6340 * Find the non-location list window with the specified location list.
6341 */
6342 static win_T *
6343find_win_with_ll(qf_info_T *qi)
6344{
6345 win_T *wp = NULL;
6346
6347 FOR_ALL_WINDOWS(wp)
6348 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6349 return wp;
6350
6351 return NULL;
6352}
6353
6354/*
6355 * Free the entire quickfix/location list stack.
6356 * If the quickfix/location list window is open, then clear it.
6357 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006358 static void
6359qf_free_stack(win_T *wp, qf_info_T *qi)
6360{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006361 win_T *qfwin = qf_find_win(qi);
6362 win_T *llwin = NULL;
6363 win_T *orig_wp = wp;
6364
6365 if (qfwin != NULL)
6366 {
6367 /* If the quickfix/location list window is open, then clear it */
6368 if (qi->qf_curlist < qi->qf_listcount)
6369 qf_free(qi, qi->qf_curlist);
6370 qf_update_buffer(qi, NULL);
6371 }
6372
6373 if (wp != NULL && IS_LL_WINDOW(wp))
6374 {
6375 /* If in the location list window, then use the non-location list
6376 * window with this location list (if present)
6377 */
6378 llwin = find_win_with_ll(qi);
6379 if (llwin != NULL)
6380 wp = llwin;
6381 }
6382
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006383 qf_free_all(wp);
6384 if (wp == NULL)
6385 {
6386 /* quickfix list */
6387 qi->qf_curlist = 0;
6388 qi->qf_listcount = 0;
6389 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006390 else if (IS_LL_WINDOW(orig_wp))
6391 {
6392 /* If the location list window is open, then create a new empty
6393 * location list */
6394 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006395
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006396 /* first free the list reference in the location list window */
6397 ll_free_all(&orig_wp->w_llist_ref);
6398
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006399 orig_wp->w_llist_ref = new_ll;
6400 if (llwin != NULL)
6401 {
6402 llwin->w_llist = new_ll;
6403 new_ll->qf_refcount++;
6404 }
6405 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006406}
6407
Bram Moolenaard823fa92016-08-12 16:29:27 +02006408/*
6409 * Populate the quickfix list with the items supplied in the list
6410 * of dictionaries. "title" will be copied to w:quickfix_title.
6411 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6412 */
6413 int
6414set_errorlist(
6415 win_T *wp,
6416 list_T *list,
6417 int action,
6418 char_u *title,
6419 dict_T *what)
6420{
6421 qf_info_T *qi = &ql_info;
6422 int retval = OK;
6423
6424 if (wp != NULL)
6425 {
6426 qi = ll_get_or_alloc_list(wp);
6427 if (qi == NULL)
6428 return FAIL;
6429 }
6430
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006431 if (action == 'f')
6432 {
6433 /* Free the entire quickfix or location list stack */
6434 qf_free_stack(wp, qi);
6435 }
6436 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006437 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006438 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006439 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006440 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006441 if (retval == OK)
6442 qf_list_changed(qi, qi->qf_curlist);
6443 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006444
6445 return retval;
6446}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006447
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006448/*
6449 * Mark the context as in use for all the lists in a quickfix stack.
6450 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006451 static int
6452mark_quickfix_ctx(qf_info_T *qi, int copyID)
6453{
6454 int i;
6455 int abort = FALSE;
6456 typval_T *ctx;
6457
6458 for (i = 0; i < LISTCOUNT && !abort; ++i)
6459 {
6460 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006461 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6462 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006463 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6464 }
6465
6466 return abort;
6467}
6468
6469/*
6470 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006471 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006472 */
6473 int
6474set_ref_in_quickfix(int copyID)
6475{
6476 int abort = FALSE;
6477 tabpage_T *tp;
6478 win_T *win;
6479
6480 abort = mark_quickfix_ctx(&ql_info, copyID);
6481 if (abort)
6482 return abort;
6483
6484 FOR_ALL_TAB_WINDOWS(tp, win)
6485 {
6486 if (win->w_llist != NULL)
6487 {
6488 abort = mark_quickfix_ctx(win->w_llist, copyID);
6489 if (abort)
6490 return abort;
6491 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006492 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6493 {
6494 /* In a location list window and none of the other windows is
6495 * referring to this location list. Mark the location list
6496 * context as still in use.
6497 */
6498 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6499 if (abort)
6500 return abort;
6501 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006502 }
6503
6504 return abort;
6505}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006506#endif
6507
Bram Moolenaar81695252004-12-29 20:58:21 +00006508/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006509 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006510 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006511 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006512 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006513 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006514 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006515 */
6516 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006517ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006518{
6519 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006520 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006521 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006522 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006523 int_u save_qfid;
6524 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006525
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006526 switch (eap->cmdidx)
6527 {
6528 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6529 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6530 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6531 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6532 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6533 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6534 default: break;
6535 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006536 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6537 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006538 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006539#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006540 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006541 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006542#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006543 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006544
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006545 /* Must come after autocommands. */
Bram Moolenaar39665952018-08-15 20:59:48 +02006546 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006547 {
6548 qi = ll_get_or_alloc_list(curwin);
6549 if (qi == NULL)
6550 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006551 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006552 }
6553
Bram Moolenaar86b68352004-12-27 21:59:20 +00006554 if (*eap->arg == NUL)
6555 buf = curbuf;
6556 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6557 buf = buflist_findnr(atoi((char *)eap->arg));
6558 if (buf == NULL)
6559 EMSG(_(e_invarg));
6560 else if (buf->b_ml.ml_mfp == NULL)
6561 EMSG(_("E681: Buffer is not loaded"));
6562 else
6563 {
6564 if (eap->addr_count == 0)
6565 {
6566 eap->line1 = 1;
6567 eap->line2 = buf->b_ml.ml_line_count;
6568 }
6569 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6570 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6571 EMSG(_(e_invrange));
6572 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006573 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006574 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006575
6576 if (buf->b_sfname)
6577 {
6578 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6579 (char *)qf_title, (char *)buf->b_sfname);
6580 qf_title = IObuff;
6581 }
6582
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006583 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006584 (eap->cmdidx != CMD_caddbuffer
6585 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006586 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006587 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006588 if (res >= 0)
6589 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006590
6591 // Remember the current quickfix list identifier, so that we can
6592 // check for autocommands changing the current quickfix list.
6593 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006594 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006595 {
6596 buf_T *curbuf_old = curbuf;
6597
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006598 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6599 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006600 if (curbuf != curbuf_old)
6601 // Autocommands changed buffer, don't jump now, "qi" may
6602 // be invalid.
6603 res = 0;
6604 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006605 // Jump to the first error for a new list and if autocmds didn't
6606 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006607 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006608 eap->cmdidx == CMD_lbuffer)
6609 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006610 // display the first error
6611 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar754b5602006-02-09 23:53:20 +00006612 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006613 }
6614}
6615
Bram Moolenaar1e015462005-09-25 22:16:38 +00006616#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006617/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006618 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6619 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006620 */
6621 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006622ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006623{
6624 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006625 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006626 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006627 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006628 int_u save_qfid;
6629 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006630
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006631 switch (eap->cmdidx)
6632 {
6633 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6634 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6635 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6636 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6637 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6638 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6639 default: break;
6640 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006641 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6642 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006643 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006644#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006645 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006646 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006647#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006648 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006649
Bram Moolenaar39665952018-08-15 20:59:48 +02006650 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006651 {
6652 qi = ll_get_or_alloc_list(curwin);
6653 if (qi == NULL)
6654 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006655 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006656 }
6657
Bram Moolenaar4770d092006-01-12 23:22:24 +00006658 /* Evaluate the expression. When the result is a string or a list we can
6659 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006660 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006661 if (tv != NULL)
6662 {
6663 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6664 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6665 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006666 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006667 (eap->cmdidx != CMD_caddexpr
6668 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006669 (linenr_T)0, (linenr_T)0,
6670 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006671 if (res >= 0)
6672 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006673
6674 // Remember the current quickfix list identifier, so that we can
6675 // check for autocommands changing the current quickfix list.
6676 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006677 if (au_name != NULL)
6678 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6679 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006680
6681 // Jump to the first error for a new list and if autocmds didn't
6682 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006683 if (res > 0 && (eap->cmdidx == CMD_cexpr
6684 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006685 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006686 // display the first error
6687 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006688 }
6689 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006690 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006691 free_tv(tv);
6692 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006693}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006694#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006695
6696/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006697 * Get the location list for ":lhelpgrep"
6698 */
6699 static qf_info_T *
6700hgr_get_ll(int *new_ll)
6701{
6702 win_T *wp;
6703 qf_info_T *qi;
6704
6705 /* If the current window is a help window, then use it */
6706 if (bt_help(curwin->w_buffer))
6707 wp = curwin;
6708 else
6709 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006710 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006711
6712 if (wp == NULL) /* Help window not found */
6713 qi = NULL;
6714 else
6715 qi = wp->w_llist;
6716
6717 if (qi == NULL)
6718 {
6719 /* Allocate a new location list for help text matches */
6720 if ((qi = ll_new_list()) == NULL)
6721 return NULL;
6722 *new_ll = TRUE;
6723 }
6724
6725 return qi;
6726}
6727
6728/*
6729 * Search for a pattern in a help file.
6730 */
6731 static void
6732hgr_search_file(
6733 qf_info_T *qi,
6734 char_u *fname,
6735#ifdef FEAT_MBYTE
6736 vimconv_T *p_vc,
6737#endif
6738 regmatch_T *p_regmatch)
6739{
6740 FILE *fd;
6741 long lnum;
6742
6743 fd = mch_fopen((char *)fname, "r");
6744 if (fd == NULL)
6745 return;
6746
6747 lnum = 1;
6748 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6749 {
6750 char_u *line = IObuff;
6751#ifdef FEAT_MBYTE
6752 /* Convert a line if 'encoding' is not utf-8 and
6753 * the line contains a non-ASCII character. */
6754 if (p_vc->vc_type != CONV_NONE
6755 && has_non_ascii(IObuff))
6756 {
6757 line = string_convert(p_vc, IObuff, NULL);
6758 if (line == NULL)
6759 line = IObuff;
6760 }
6761#endif
6762
6763 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6764 {
6765 int l = (int)STRLEN(line);
6766
6767 /* remove trailing CR, LF, spaces, etc. */
6768 while (l > 0 && line[l - 1] <= ' ')
6769 line[--l] = NUL;
6770
6771 if (qf_add_entry(qi,
6772 qi->qf_curlist,
6773 NULL, /* dir */
6774 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006775 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006776 0,
6777 line,
6778 lnum,
6779 (int)(p_regmatch->startp[0] - line)
6780 + 1, /* col */
6781 FALSE, /* vis_col */
6782 NULL, /* search pattern */
6783 0, /* nr */
6784 1, /* type */
6785 TRUE /* valid */
6786 ) == FAIL)
6787 {
6788 got_int = TRUE;
6789#ifdef FEAT_MBYTE
6790 if (line != IObuff)
6791 vim_free(line);
6792#endif
6793 break;
6794 }
6795 }
6796#ifdef FEAT_MBYTE
6797 if (line != IObuff)
6798 vim_free(line);
6799#endif
6800 ++lnum;
6801 line_breakcheck();
6802 }
6803 fclose(fd);
6804}
6805
6806/*
6807 * Search for a pattern in all the help files in the doc directory under
6808 * the given directory.
6809 */
6810 static void
6811hgr_search_files_in_dir(
6812 qf_info_T *qi,
6813 char_u *dirname,
6814 regmatch_T *p_regmatch
6815#ifdef FEAT_MBYTE
6816 , vimconv_T *p_vc
6817#endif
6818#ifdef FEAT_MULTI_LANG
6819 , char_u *lang
6820#endif
6821 )
6822{
6823 int fcount;
6824 char_u **fnames;
6825 int fi;
6826
6827 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6828 add_pathsep(dirname);
6829 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6830 if (gen_expand_wildcards(1, &dirname, &fcount,
6831 &fnames, EW_FILE|EW_SILENT) == OK
6832 && fcount > 0)
6833 {
6834 for (fi = 0; fi < fcount && !got_int; ++fi)
6835 {
6836#ifdef FEAT_MULTI_LANG
6837 /* Skip files for a different language. */
6838 if (lang != NULL
6839 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006840 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006841 && !(STRNICMP(lang, "en", 2) == 0
6842 && STRNICMP("txt", fnames[fi]
6843 + STRLEN(fnames[fi]) - 3, 3) == 0))
6844 continue;
6845#endif
6846
6847 hgr_search_file(qi, fnames[fi],
6848#ifdef FEAT_MBYTE
6849 p_vc,
6850#endif
6851 p_regmatch);
6852 }
6853 FreeWild(fcount, fnames);
6854 }
6855}
6856
6857/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006858 * Search for a pattern in all the help files in the 'runtimepath'
6859 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006860 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006861 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006862 */
6863 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006864hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006865{
6866 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006867
6868#ifdef FEAT_MBYTE
6869 vimconv_T vc;
6870
6871 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6872 * differs. */
6873 vc.vc_type = CONV_NONE;
6874 if (!enc_utf8)
6875 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6876#endif
6877
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006878
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006879 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006880 p = p_rtp;
6881 while (*p != NUL && !got_int)
6882 {
6883 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6884
6885 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6886#ifdef FEAT_MBYTE
6887 , &vc
6888#endif
6889#ifdef FEAT_MULTI_LANG
6890 , lang
6891#endif
6892 );
6893 }
6894
6895#ifdef FEAT_MBYTE
6896 if (vc.vc_type != CONV_NONE)
6897 convert_setup(&vc, NULL, NULL);
6898#endif
6899}
6900
6901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 * ":helpgrep {pattern}"
6903 */
6904 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006905ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906{
6907 regmatch_T regmatch;
6908 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006909 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006910 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006911 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006912 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913
Bram Moolenaar73633f82012-01-20 13:39:07 +01006914 switch (eap->cmdidx)
6915 {
6916 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6917 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6918 default: break;
6919 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006920 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6921 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006922 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006923#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006924 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006925 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006926#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006927 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006928
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006929 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01006930 save_cpo = p_cpo;
6931 p_cpo = empty_option;
6932
Bram Moolenaar39665952018-08-15 20:59:48 +02006933 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006934 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006935 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006936 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006937 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006938 }
6939
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006940#ifdef FEAT_MULTI_LANG
6941 // Check for a specified language
6942 lang = check_help_lang(eap->arg);
6943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6945 regmatch.rm_ic = FALSE;
6946 if (regmatch.regprog != NULL)
6947 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006948 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006949 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006951 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006952
Bram Moolenaar473de612013-06-08 18:19:48 +02006953 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006955 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6956 qi->qf_lists[qi->qf_curlist].qf_ptr =
6957 qi->qf_lists[qi->qf_curlist].qf_start;
6958 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006961 if (p_cpo == empty_option)
6962 p_cpo = save_cpo;
6963 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006964 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006965 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
Bram Moolenaarb254af32017-12-18 19:48:58 +01006967 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006968 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969
Bram Moolenaar73633f82012-01-20 13:39:07 +01006970 if (au_name != NULL)
6971 {
6972 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6973 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02006974 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006975 /* autocommands made "qi" invalid */
6976 return;
6977 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006978
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006980 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006981 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006982 else
6983 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006984
6985 if (eap->cmdidx == CMD_lhelpgrep)
6986 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006987 // If the help window is not opened or if it already points to the
6988 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006989 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006990 {
6991 if (new_qi)
6992 ll_free_all(&qi);
6993 }
6994 else if (curwin->w_llist == NULL)
6995 curwin->w_llist = qi;
6996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997}
6998
6999#endif /* FEAT_QUICKFIX */