blob: cbef377c90346ad71e14717e080d6f5bee5e45c4 [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 Moolenaar00bf8cd2018-10-07 20:26:20 +020030 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
36 char_u *qf_module; // module name for this error
37 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
42 // :helpgrep
43 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 Moolenaaree8188f2019-02-05 21:23:04 +010051#define INVALID_QFBUFNR (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020053/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010054 * Quickfix list type.
55 */
56typedef enum
57{
58 QFLT_QUICKFIX, // Quickfix list - global list
59 QFLT_LOCATION, // Location list - per window list
60 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
61} qfltype_T;
62
63/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020064 * Quickfix/Location list definition
65 * Contains a list of entries (qfline_T). qf_start points to the first entry
66 * and qf_last points to the last entry. qf_count contains the list size.
67 *
68 * Usually the list contains one or more entries. But an empty list can be
69 * created using setqflist()/setloclist() with a title and/or user context
70 * information and entries can be added later using setqflist()/setloclist().
71 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000072typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000073{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020074 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010075 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020076 qfline_T *qf_start; // pointer to the first error
77 qfline_T *qf_last; // pointer to the last error
78 qfline_T *qf_ptr; // pointer to the current error
79 int qf_count; // number of errors (0 means empty list)
80 int qf_index; // current index in the error list
81 int qf_nonevalid; // TRUE if not a single valid entry found
82 char_u *qf_title; // title derived from the command that created
83 // the error list or set by setqflist
84 typval_T *qf_ctx; // context set by setqflist/setloclist
Bram Moolenaara7df8c72017-07-19 13:23:06 +020085
86 struct dir_stack_T *qf_dir_stack;
87 char_u *qf_directory;
88 struct dir_stack_T *qf_file_stack;
89 char_u *qf_currfile;
90 int qf_multiline;
91 int qf_multiignore;
92 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010093 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000094} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000095
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020096/*
97 * Quickfix/Location list stack definition
98 * Contains a list of quickfix/location lists (qf_list_T)
99 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000100struct qf_info_S
101{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200102 // Count of references to this list. Used only for location lists.
103 // When a location list window reference this list, qf_refcount
104 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
105 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000106 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200107 int qf_listcount; // current number of lists
108 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000109 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100110 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100111 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000112};
113
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200114static qf_info_T ql_info; // global quickfix list
115static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200117#define FMT_PATTERNS 11 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
120 * Structure used to hold the info of one part of 'errorformat'
121 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000122typedef struct efm_S efm_T;
123struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200125 regprog_T *prog; // pre-formatted part of 'errorformat'
126 efm_T *next; // pointer to next (NULL if last)
127 char_u addr[FMT_PATTERNS]; // indices of used % patterns
128 char_u prefix; // prefix of this format line:
129 // 'D' enter directory
130 // 'X' leave directory
131 // 'A' start of multi-line message
132 // 'E' error message
133 // 'W' warning message
134 // 'I' informational message
135 // 'C' continuation line
136 // 'Z' end of multi-line message
137 // 'G' general, unspecific message
138 // 'P' push file (partial) message
139 // 'Q' pop/quit file (partial) message
140 // 'O' overread (partial) message
141 char_u flags; // additional flags given in prefix
142 // '-' do not include this line
143 // '+' include whole line in message
144 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145};
146
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200147// List of location lists to be deleted.
148// Used to delay the deletion of locations lists by autocmds.
149typedef struct qf_delq_S
150{
151 struct qf_delq_S *next;
152 qf_info_T *qi;
153} qf_delq_T;
154static qf_delq_T *qf_delq_head = NULL;
155
156// Counter to prevent autocmds from freeing up location lists when they are
157// still being used.
158static int quickfix_busy = 0;
159
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200160static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100161
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100162static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100163static int qf_add_entry(qf_list_T *qfl, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200164static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100166static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200167static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200169static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100172static win_T *qf_find_win(qf_info_T *qi);
173static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200174static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +0100175static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
177static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
178static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
179static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200181// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000182#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200183// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000184#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200185
186// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100187#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
188#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
189#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
190#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200191
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000192/*
193 * Return location list for window 'wp'
194 * For location list window, return the referenced location list
195 */
196#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
197
Bram Moolenaar95946f12019-03-31 15:31:59 +0200198// Macro to loop through all the items in a quickfix list
199// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100200#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
Bram Moolenaar95946f12019-03-31 15:31:59 +0200201 for (i = 1, qfp = qfl->qf_start; \
202 !got_int && i <= qfl->qf_count && qfp != NULL; \
Bram Moolenaara16123a2019-03-28 20:31:07 +0100203 ++i, qfp = qfp->qf_next)
204
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200206 * Looking up a buffer can be slow if there are many. Remember the last one
207 * to make this a lot faster if there are multiple matches in the same file.
208 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200209static char_u *qf_last_bufname = NULL;
210static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200211
Bram Moolenaar3c097222017-12-21 20:54:49 +0100212static char *e_loc_list_changed =
213 N_("E926: Current location list was changed");
214
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200215/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200216 * Maximum number of bytes allowed per line while reading a errorfile.
217 */
218#define LINE_MAXLEN 4096
219
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200220static struct fmtpattern
221{
222 char_u convchar;
223 char *pattern;
224} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200225 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200226 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200227 {'n', "\\d\\+"},
228 {'l', "\\d\\+"},
229 {'c', "\\d\\+"},
230 {'t', "."},
231 {'m', ".\\+"},
232 {'r', ".*"},
233 {'p', "[- .]*"},
234 {'v', "\\d\\+"},
235 {'s', ".\\+"},
236 {'o', ".\\+"}
237 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200238
239/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200240 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200241 * See fmt_pat definition above for the list of supported patterns. The
242 * pattern specifier is supplied in "efmpat". The converted pattern is stored
243 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200244 */
245 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200246efmpat_to_regpat(
247 char_u *efmpat,
248 char_u *regpat,
249 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200250 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100251 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200252{
253 char_u *srcptr;
254
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200255 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200256 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200257 // Each errorformat pattern can occur only once
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100258 semsg(_("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200259 return NULL;
260 }
261 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200262 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200263 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200264 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200265 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100266 semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200267 return NULL;
268 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200269 efminfo->addr[idx] = (char_u)++round;
270 *regpat++ = '\\';
271 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200272#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200273 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200274 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200275 // Also match "c:" in the file name, even when
276 // checking for a colon next: "%f:".
277 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200278 STRCPY(regpat, "\\%(\\a:\\)\\=");
279 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200280 }
281#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200282 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200283 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200284 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200285 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200286 // A file name may contain spaces, but this isn't
287 // in "\f". For "%f:%l:%m" there may be a ":" in
288 // the file name. Use ".\{-1,}x" instead (x is
289 // the next character), the requirement that :999:
290 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200291 STRCPY(regpat, ".\\{-1,}");
292 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200293 }
294 else
295 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200296 // File name followed by '\\' or '%': include as
297 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200298 STRCPY(regpat, "\\f\\+");
299 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200300 }
301 }
302 else
303 {
304 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200305 while ((*regpat = *srcptr++) != NUL)
306 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200307 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200308 *regpat++ = '\\';
309 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200310
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200311 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200312}
313
314/*
315 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200316 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200317 */
318 static char_u *
319scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200320 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200321 char_u *efm,
322 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100323 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200324{
325 char_u *efmp = *pefmp;
326
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200327 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200328 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200329 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200330 {
331 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200332 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200333 if (efmp < efm + len)
334 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200335 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200336 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200337 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200338 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200339 if (efmp == efm + len)
340 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100341 emsg(_("E374: Missing ] in format string"));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200342 return NULL;
343 }
344 }
345 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200346 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200347 *regpat++ = *++efmp;
348 *regpat++ = '\\';
349 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200350 }
351 else
352 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200353 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100354 semsg(_("E375: Unsupported %%%c in format string"), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200355 return NULL;
356 }
357
358 *pefmp = efmp;
359
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200360 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200361}
362
363/*
364 * Analyze/parse an errorformat prefix.
365 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200366 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100367efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200368{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200369 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200370 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200371 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200372 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200373 else
374 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100375 semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200376 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200377 }
378
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200379 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200380}
381
382/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200383 * Converts a 'errorformat' string part in 'efm' to a regular expression
384 * pattern. The resulting regex pattern is returned in "regpat". Additional
385 * information about the 'erroformat' pattern is returned in "fmt_ptr".
386 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200387 */
388 static int
389efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200390 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200391 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200392 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100393 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200394{
395 char_u *ptr;
396 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200397 int round;
398 int idx = 0;
399
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200400 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200401 ptr = regpat;
402 *ptr++ = '^';
403 round = 0;
404 for (efmp = efm; efmp < efm + len; ++efmp)
405 {
406 if (*efmp == '%')
407 {
408 ++efmp;
409 for (idx = 0; idx < FMT_PATTERNS; ++idx)
410 if (fmt_pat[idx].convchar == *efmp)
411 break;
412 if (idx < FMT_PATTERNS)
413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100414 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200415 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200416 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200417 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200418 }
419 else if (*efmp == '*')
420 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200421 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100422 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200423 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200424 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200425 }
426 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200427 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200428 else if (*efmp == '#')
429 *ptr++ = '*';
430 else if (*efmp == '>')
431 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200432 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200433 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200434 // prefix is allowed only at the beginning of the errorformat
435 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100436 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200437 if (efmp == NULL)
438 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200439 }
440 else
441 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100442 semsg(_("E377: Invalid %%%c in format string"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200443 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200444 }
445 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200446 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200447 {
448 if (*efmp == '\\' && efmp + 1 < efm + len)
449 ++efmp;
450 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200451 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200452 if (*efmp)
453 *ptr++ = *efmp;
454 }
455 }
456 *ptr++ = '$';
457 *ptr = NUL;
458
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200459 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200460}
461
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200462/*
463 * Free the 'errorformat' information list
464 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200465 static void
466free_efm_list(efm_T **efm_first)
467{
468 efm_T *efm_ptr;
469
470 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
471 {
472 *efm_first = efm_ptr->next;
473 vim_regfree(efm_ptr->prog);
474 vim_free(efm_ptr);
475 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100476 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200477}
478
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200479/*
480 * Compute the size of the buffer used to convert a 'errorformat' pattern into
481 * a regular expression pattern.
482 */
483 static int
484efm_regpat_bufsz(char_u *efm)
485{
486 int sz;
487 int i;
488
489 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
490 for (i = FMT_PATTERNS; i > 0; )
491 sz += (int)STRLEN(fmt_pat[--i].pattern);
492#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200493 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200494#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200495 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200496#endif
497
498 return sz;
499}
500
501/*
502 * Return the length of a 'errorformat' option part (separated by ",").
503 */
504 static int
505efm_option_part_len(char_u *efm)
506{
507 int len;
508
509 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
510 if (efm[len] == '\\' && efm[len + 1] != NUL)
511 ++len;
512
513 return len;
514}
515
516/*
517 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
518 * are parsed and converted to regular expressions. Returns information about
519 * the parsed 'errorformat' option.
520 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200521 static efm_T *
522parse_efm_option(char_u *efm)
523{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200524 efm_T *fmt_ptr = NULL;
525 efm_T *fmt_first = NULL;
526 efm_T *fmt_last = NULL;
527 char_u *fmtstr = NULL;
528 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200529 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200530
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200531 // Each part of the format string is copied and modified from errorformat
532 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200533
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200534 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200535 sz = efm_regpat_bufsz(efm);
536 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200537 goto parse_efm_error;
538
539 while (efm[0] != NUL)
540 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200541 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200542 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
543 if (fmt_ptr == NULL)
544 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200545 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200546 fmt_first = fmt_ptr;
547 else
548 fmt_last->next = fmt_ptr;
549 fmt_last = fmt_ptr;
550
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200551 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200552 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200553
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100554 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200555 goto parse_efm_error;
556 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
557 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200558 // Advance to next part
559 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200560 }
561
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200562 if (fmt_first == NULL) // nothing found
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100563 emsg(_("E378: 'errorformat' contains no pattern"));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200564
565 goto parse_efm_end;
566
567parse_efm_error:
568 free_efm_list(&fmt_first);
569
570parse_efm_end:
571 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200572
573 return fmt_first;
574}
575
Bram Moolenaare0d37972016-07-15 22:36:01 +0200576enum {
577 QF_FAIL = 0,
578 QF_OK = 1,
579 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200580 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200581 QF_IGNORE_LINE = 4,
582 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200583};
584
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200585/*
586 * State information used to parse lines and add entries to a quickfix/location
587 * list.
588 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200589typedef struct {
590 char_u *linebuf;
591 int linelen;
592 char_u *growbuf;
593 int growbufsiz;
594 FILE *fd;
595 typval_T *tv;
596 char_u *p_str;
597 listitem_T *p_li;
598 buf_T *buf;
599 linenr_T buflnum;
600 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100601 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200602} qfstate_T;
603
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200604/*
605 * Allocate more memory for the line buffer used for parsing lines.
606 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200607 static char_u *
608qf_grow_linebuf(qfstate_T *state, int newsz)
609{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200610 char_u *p;
611
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200612 // If the line exceeds LINE_MAXLEN exclude the last
613 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200614 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
615 if (state->growbuf == NULL)
616 {
617 state->growbuf = alloc(state->linelen + 1);
618 if (state->growbuf == NULL)
619 return NULL;
620 state->growbufsiz = state->linelen;
621 }
622 else if (state->linelen > state->growbufsiz)
623 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200624 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200625 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200626 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200627 state->growbufsiz = state->linelen;
628 }
629 return state->growbuf;
630}
631
632/*
633 * Get the next string (separated by newline) from state->p_str.
634 */
635 static int
636qf_get_next_str_line(qfstate_T *state)
637{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200638 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200639 char_u *p_str = state->p_str;
640 char_u *p;
641 int len;
642
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200643 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200644 return QF_END_OF_INPUT;
645
646 p = vim_strchr(p_str, '\n');
647 if (p != NULL)
648 len = (int)(p - p_str) + 1;
649 else
650 len = (int)STRLEN(p_str);
651
652 if (len > IOSIZE - 2)
653 {
654 state->linebuf = qf_grow_linebuf(state, len);
655 if (state->linebuf == NULL)
656 return QF_NOMEM;
657 }
658 else
659 {
660 state->linebuf = IObuff;
661 state->linelen = len;
662 }
663 vim_strncpy(state->linebuf, p_str, state->linelen);
664
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200665 // Increment using len in order to discard the rest of the
666 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200667 p_str += len;
668 state->p_str = p_str;
669
670 return QF_OK;
671}
672
673/*
674 * Get the next string from state->p_Li.
675 */
676 static int
677qf_get_next_list_line(qfstate_T *state)
678{
679 listitem_T *p_li = state->p_li;
680 int len;
681
682 while (p_li != NULL
683 && (p_li->li_tv.v_type != VAR_STRING
684 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200685 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200686
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200687 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200688 {
689 state->p_li = NULL;
690 return QF_END_OF_INPUT;
691 }
692
693 len = (int)STRLEN(p_li->li_tv.vval.v_string);
694 if (len > IOSIZE - 2)
695 {
696 state->linebuf = qf_grow_linebuf(state, len);
697 if (state->linebuf == NULL)
698 return QF_NOMEM;
699 }
700 else
701 {
702 state->linebuf = IObuff;
703 state->linelen = len;
704 }
705
706 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
707
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200708 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200709 return QF_OK;
710}
711
712/*
713 * Get the next string from state->buf.
714 */
715 static int
716qf_get_next_buf_line(qfstate_T *state)
717{
718 char_u *p_buf = NULL;
719 int len;
720
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200721 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200722 if (state->buflnum > state->lnumlast)
723 return QF_END_OF_INPUT;
724
725 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
726 state->buflnum += 1;
727
728 len = (int)STRLEN(p_buf);
729 if (len > IOSIZE - 2)
730 {
731 state->linebuf = qf_grow_linebuf(state, len);
732 if (state->linebuf == NULL)
733 return QF_NOMEM;
734 }
735 else
736 {
737 state->linebuf = IObuff;
738 state->linelen = len;
739 }
740 vim_strncpy(state->linebuf, p_buf, state->linelen);
741
742 return QF_OK;
743}
744
745/*
746 * Get the next string from file state->fd.
747 */
748 static int
749qf_get_next_file_line(qfstate_T *state)
750{
751 int discard;
752 int growbuflen;
753
754 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
755 return QF_END_OF_INPUT;
756
757 discard = FALSE;
758 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200759 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200760 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200761 // The current line exceeds IObuff, continue reading using
762 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200763 if (state->growbuf == NULL)
764 {
765 state->growbufsiz = 2 * (IOSIZE - 1);
766 state->growbuf = alloc(state->growbufsiz);
767 if (state->growbuf == NULL)
768 return QF_NOMEM;
769 }
770
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200771 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200772 memcpy(state->growbuf, IObuff, IOSIZE - 1);
773 growbuflen = state->linelen;
774
775 for (;;)
776 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200777 char_u *p;
778
Bram Moolenaare0d37972016-07-15 22:36:01 +0200779 if (fgets((char *)state->growbuf + growbuflen,
780 state->growbufsiz - growbuflen, state->fd) == NULL)
781 break;
782 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
783 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200784 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200785 break;
786 if (state->growbufsiz == LINE_MAXLEN)
787 {
788 discard = TRUE;
789 break;
790 }
791
792 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
793 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200794 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200795 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200796 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200797 }
798
799 while (discard)
800 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200801 // The current line is longer than LINE_MAXLEN, continue
802 // reading but discard everything until EOL or EOF is
803 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200804 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
805 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200806 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200807 break;
808 }
809
810 state->linebuf = state->growbuf;
811 state->linelen = growbuflen;
812 }
813 else
814 state->linebuf = IObuff;
815
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200816 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200817 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
818 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100819 char_u *line;
820
821 line = string_convert(&state->vc, state->linebuf, &state->linelen);
822 if (line != NULL)
823 {
824 if (state->linelen < IOSIZE)
825 {
826 STRCPY(state->linebuf, line);
827 vim_free(line);
828 }
829 else
830 {
831 vim_free(state->growbuf);
832 state->linebuf = state->growbuf = line;
833 state->growbufsiz = state->linelen < LINE_MAXLEN
834 ? state->linelen : LINE_MAXLEN;
835 }
836 }
837 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100838
Bram Moolenaare0d37972016-07-15 22:36:01 +0200839 return QF_OK;
840}
841
842/*
843 * Get the next string from a file/buffer/list/string.
844 */
845 static int
846qf_get_nextline(qfstate_T *state)
847{
848 int status = QF_FAIL;
849
850 if (state->fd == NULL)
851 {
852 if (state->tv != NULL)
853 {
854 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200855 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200856 status = qf_get_next_str_line(state);
857 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200858 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200859 status = qf_get_next_list_line(state);
860 }
861 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200862 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200863 status = qf_get_next_buf_line(state);
864 }
865 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200866 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200867 status = qf_get_next_file_line(state);
868
869 if (status != QF_OK)
870 return status;
871
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200872 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200873 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200874 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200875 state->linebuf[state->linelen - 1] = NUL;
876#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200877 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
878 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200879#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200880 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200881
Bram Moolenaare0d37972016-07-15 22:36:01 +0200882 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200883
884 return QF_OK;
885}
886
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200887typedef struct {
888 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200889 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200890 char_u *errmsg;
891 int errmsglen;
892 long lnum;
893 int col;
894 char_u use_viscol;
895 char_u *pattern;
896 int enr;
897 int type;
898 int valid;
899} qffields_T;
900
901/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200902 * Parse the match for filename ('%f') pattern in regmatch.
903 * Return the matched value in "fields->namebuf".
904 */
905 static int
906qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
907{
908 int c;
909
910 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
911 return QF_FAIL;
912
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200913 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200914 c = *rmp->endp[midx];
915 *rmp->endp[midx] = NUL;
916 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
917 *rmp->endp[midx] = c;
918
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200919 // For separate filename patterns (%O, %P and %Q), the specified file
920 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200921 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
922 && mch_getperm(fields->namebuf) == -1)
923 return QF_FAIL;
924
925 return QF_OK;
926}
927
928/*
929 * Parse the match for error number ('%n') pattern in regmatch.
930 * Return the matched value in "fields->enr".
931 */
932 static int
933qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
934{
935 if (rmp->startp[midx] == NULL)
936 return QF_FAIL;
937 fields->enr = (int)atol((char *)rmp->startp[midx]);
938 return QF_OK;
939}
940
941/*
942 * Parse the match for line number (%l') pattern in regmatch.
943 * Return the matched value in "fields->lnum".
944 */
945 static int
946qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
947{
948 if (rmp->startp[midx] == NULL)
949 return QF_FAIL;
950 fields->lnum = atol((char *)rmp->startp[midx]);
951 return QF_OK;
952}
953
954/*
955 * Parse the match for column number ('%c') pattern in regmatch.
956 * Return the matched value in "fields->col".
957 */
958 static int
959qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
960{
961 if (rmp->startp[midx] == NULL)
962 return QF_FAIL;
963 fields->col = (int)atol((char *)rmp->startp[midx]);
964 return QF_OK;
965}
966
967/*
968 * Parse the match for error type ('%t') pattern in regmatch.
969 * Return the matched value in "fields->type".
970 */
971 static int
972qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
973{
974 if (rmp->startp[midx] == NULL)
975 return QF_FAIL;
976 fields->type = *rmp->startp[midx];
977 return QF_OK;
978}
979
980/*
981 * Parse the match for '%+' format pattern. The whole matching line is included
982 * in the error string. Return the matched line in "fields->errmsg".
983 */
984 static int
985qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
986{
987 char_u *p;
988
989 if (linelen >= fields->errmsglen)
990 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200991 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200992 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
993 return QF_NOMEM;
994 fields->errmsg = p;
995 fields->errmsglen = linelen + 1;
996 }
997 vim_strncpy(fields->errmsg, linebuf, linelen);
998 return QF_OK;
999}
1000
1001/*
1002 * Parse the match for error message ('%m') pattern in regmatch.
1003 * Return the matched value in "fields->errmsg".
1004 */
1005 static int
1006qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1007{
1008 char_u *p;
1009 int len;
1010
1011 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1012 return QF_FAIL;
1013 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1014 if (len >= fields->errmsglen)
1015 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001016 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001017 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1018 return QF_NOMEM;
1019 fields->errmsg = p;
1020 fields->errmsglen = len + 1;
1021 }
1022 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1023 return QF_OK;
1024}
1025
1026/*
1027 * Parse the match for rest of a single-line file message ('%r') pattern.
1028 * Return the matched value in "tail".
1029 */
1030 static int
1031qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1032{
1033 if (rmp->startp[midx] == NULL)
1034 return QF_FAIL;
1035 *tail = rmp->startp[midx];
1036 return QF_OK;
1037}
1038
1039/*
1040 * Parse the match for the pointer line ('%p') pattern in regmatch.
1041 * Return the matched value in "fields->col".
1042 */
1043 static int
1044qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1045{
1046 char_u *match_ptr;
1047
1048 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1049 return QF_FAIL;
1050 fields->col = 0;
1051 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1052 ++match_ptr)
1053 {
1054 ++fields->col;
1055 if (*match_ptr == TAB)
1056 {
1057 fields->col += 7;
1058 fields->col -= fields->col % 8;
1059 }
1060 }
1061 ++fields->col;
1062 fields->use_viscol = TRUE;
1063 return QF_OK;
1064}
1065
1066/*
1067 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1068 * Return the matched value in "fields->col".
1069 */
1070 static int
1071qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1072{
1073 if (rmp->startp[midx] == NULL)
1074 return QF_FAIL;
1075 fields->col = (int)atol((char *)rmp->startp[midx]);
1076 fields->use_viscol = TRUE;
1077 return QF_OK;
1078}
1079
1080/*
1081 * Parse the match for the search text ('%s') pattern in regmatch.
1082 * Return the matched value in "fields->pattern".
1083 */
1084 static int
1085qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1086{
1087 int len;
1088
1089 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1090 return QF_FAIL;
1091 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1092 if (len > CMDBUFFSIZE - 5)
1093 len = CMDBUFFSIZE - 5;
1094 STRCPY(fields->pattern, "^\\V");
1095 STRNCAT(fields->pattern, rmp->startp[midx], len);
1096 fields->pattern[len + 3] = '\\';
1097 fields->pattern[len + 4] = '$';
1098 fields->pattern[len + 5] = NUL;
1099 return QF_OK;
1100}
1101
1102/*
1103 * Parse the match for the module ('%o') pattern in regmatch.
1104 * Return the matched value in "fields->module".
1105 */
1106 static int
1107qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1108{
1109 int len;
1110
1111 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1112 return QF_FAIL;
1113 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1114 if (len > CMDBUFFSIZE)
1115 len = CMDBUFFSIZE;
1116 STRNCAT(fields->module, rmp->startp[midx], len);
1117 return QF_OK;
1118}
1119
1120/*
1121 * 'errorformat' format pattern parser functions.
1122 * The '%f' and '%r' formats are parsed differently from other formats.
1123 * See qf_parse_match() for details.
1124 */
1125static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1126{
1127 NULL,
1128 qf_parse_fmt_n,
1129 qf_parse_fmt_l,
1130 qf_parse_fmt_c,
1131 qf_parse_fmt_t,
1132 qf_parse_fmt_m,
1133 NULL,
1134 qf_parse_fmt_p,
1135 qf_parse_fmt_v,
1136 qf_parse_fmt_s,
1137 qf_parse_fmt_o
1138};
1139
1140/*
1141 * Parse the error format pattern matches in "regmatch" and set the values in
1142 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1143 * match. Returns QF_OK if all the matches are successfully parsed. On
1144 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001145 */
1146 static int
1147qf_parse_match(
1148 char_u *linebuf,
1149 int linelen,
1150 efm_T *fmt_ptr,
1151 regmatch_T *regmatch,
1152 qffields_T *fields,
1153 int qf_multiline,
1154 int qf_multiscan,
1155 char_u **tail)
1156{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001157 int idx = fmt_ptr->prefix;
1158 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001159 int midx;
1160 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001161
1162 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1163 return QF_FAIL;
1164 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1165 fields->type = idx;
1166 else
1167 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001168
1169 // Extract error message data from matched line.
1170 // We check for an actual submatch, because "\[" and "\]" in
1171 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001172 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001173 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001174 status = QF_OK;
1175 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001176 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001177 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1178 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001179 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001180 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001181 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001182 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001183 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001184 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001185 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001186 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001187 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001188 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001189
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001190 if (status != QF_OK)
1191 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001192 }
1193
1194 return QF_OK;
1195}
1196
1197/*
1198 * Parse an error line in 'linebuf' using a single error format string in
1199 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1200 * Returns QF_OK if the efm format matches completely and the fields are
1201 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1202 */
1203 static int
1204qf_parse_get_fields(
1205 char_u *linebuf,
1206 int linelen,
1207 efm_T *fmt_ptr,
1208 qffields_T *fields,
1209 int qf_multiline,
1210 int qf_multiscan,
1211 char_u **tail)
1212{
1213 regmatch_T regmatch;
1214 int status = QF_FAIL;
1215 int r;
1216
1217 if (qf_multiscan &&
1218 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1219 return QF_FAIL;
1220
1221 fields->namebuf[0] = NUL;
1222 fields->module[0] = NUL;
1223 fields->pattern[0] = NUL;
1224 if (!qf_multiscan)
1225 fields->errmsg[0] = NUL;
1226 fields->lnum = 0;
1227 fields->col = 0;
1228 fields->use_viscol = FALSE;
1229 fields->enr = -1;
1230 fields->type = 0;
1231 *tail = NULL;
1232
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001233 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001234 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001235 regmatch.regprog = fmt_ptr->prog;
1236 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1237 fmt_ptr->prog = regmatch.regprog;
1238 if (r)
1239 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1240 fields, qf_multiline, qf_multiscan, tail);
1241
1242 return status;
1243}
1244
1245/*
1246 * Parse directory error format prefixes (%D and %X).
1247 * Push and pop directories from the directory stack when scanning directory
1248 * names.
1249 */
1250 static int
1251qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1252{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001253 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001254 {
1255 if (*fields->namebuf == NUL)
1256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001257 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001258 return QF_FAIL;
1259 }
1260 qfl->qf_directory =
1261 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1262 if (qfl->qf_directory == NULL)
1263 return QF_FAIL;
1264 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001265 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001266 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1267
1268 return QF_OK;
1269}
1270
1271/*
1272 * Parse global file name error format prefixes (%O, %P and %Q).
1273 */
1274 static int
1275qf_parse_file_pfx(
1276 int idx,
1277 qffields_T *fields,
1278 qf_list_T *qfl,
1279 char_u *tail)
1280{
1281 fields->valid = FALSE;
1282 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1283 {
1284 if (*fields->namebuf && idx == 'P')
1285 qfl->qf_currfile =
1286 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1287 else if (idx == 'Q')
1288 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1289 *fields->namebuf = NUL;
1290 if (tail && *tail)
1291 {
1292 STRMOVE(IObuff, skipwhite(tail));
1293 qfl->qf_multiscan = TRUE;
1294 return QF_MULTISCAN;
1295 }
1296 }
1297
1298 return QF_OK;
1299}
1300
1301/*
1302 * Parse a non-error line (a line which doesn't match any of the error
1303 * format in 'efm').
1304 */
1305 static int
1306qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1307{
1308 char_u *p;
1309
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001310 fields->namebuf[0] = NUL; // no match found, remove file name
1311 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001312 fields->valid = FALSE;
1313 if (linelen >= fields->errmsglen)
1314 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001315 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001316 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1317 return QF_NOMEM;
1318 fields->errmsg = p;
1319 fields->errmsglen = linelen + 1;
1320 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001321 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001322 vim_strncpy(fields->errmsg, linebuf, linelen);
1323
1324 return QF_OK;
1325}
1326
1327/*
1328 * Parse multi-line error format prefixes (%C and %Z)
1329 */
1330 static int
1331qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001332 int idx,
1333 qf_list_T *qfl,
1334 qffields_T *fields)
1335{
1336 char_u *ptr;
1337 int len;
1338
1339 if (!qfl->qf_multiignore)
1340 {
1341 qfline_T *qfprev = qfl->qf_last;
1342
1343 if (qfprev == NULL)
1344 return QF_FAIL;
1345 if (*fields->errmsg && !qfl->qf_multiignore)
1346 {
1347 len = (int)STRLEN(qfprev->qf_text);
1348 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1349 == NULL)
1350 return QF_FAIL;
1351 STRCPY(ptr, qfprev->qf_text);
1352 vim_free(qfprev->qf_text);
1353 qfprev->qf_text = ptr;
1354 *(ptr += len) = '\n';
1355 STRCPY(++ptr, fields->errmsg);
1356 }
1357 if (qfprev->qf_nr == -1)
1358 qfprev->qf_nr = fields->enr;
1359 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001360 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001361 qfprev->qf_type = fields->type;
1362
1363 if (!qfprev->qf_lnum)
1364 qfprev->qf_lnum = fields->lnum;
1365 if (!qfprev->qf_col)
1366 qfprev->qf_col = fields->col;
1367 qfprev->qf_viscol = fields->use_viscol;
1368 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001369 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001370 qfl->qf_directory,
1371 *fields->namebuf || qfl->qf_directory != NULL
1372 ? fields->namebuf
1373 : qfl->qf_currfile != NULL && fields->valid
1374 ? qfl->qf_currfile : 0);
1375 }
1376 if (idx == 'Z')
1377 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1378 line_breakcheck();
1379
1380 return QF_IGNORE_LINE;
1381}
1382
1383/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001384 * Parse a line and get the quickfix fields.
1385 * Return the QF_ status.
1386 */
1387 static int
1388qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001389 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001390 char_u *linebuf,
1391 int linelen,
1392 efm_T *fmt_first,
1393 qffields_T *fields)
1394{
1395 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001396 int idx = 0;
1397 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001398 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001399
Bram Moolenaare333e792018-04-08 13:27:39 +02001400restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001401 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001402 if (fmt_start == NULL)
1403 fmt_ptr = fmt_first;
1404 else
1405 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001406 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001407 fmt_ptr = fmt_start;
1408 fmt_start = NULL;
1409 }
1410
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001411 // Try to match each part of 'errorformat' until we find a complete
1412 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001413 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001414 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1415 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001416 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001417 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1418 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1419 if (status == QF_NOMEM)
1420 return status;
1421 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001422 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001423 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001424 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001425
1426 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1427 {
1428 if (fmt_ptr != NULL)
1429 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001430 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001431 status = qf_parse_dir_pfx(idx, fields, qfl);
1432 if (status != QF_OK)
1433 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001434 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001435
1436 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1437 if (status != QF_OK)
1438 return status;
1439
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001440 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001441 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001442 }
1443 else if (fmt_ptr != NULL)
1444 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001445 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001446 if (fmt_ptr->conthere)
1447 fmt_start = fmt_ptr;
1448
1449 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1450 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001451 qfl->qf_multiline = TRUE; // start of a multi-line message
1452 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001453 }
1454 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001455 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001456 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001457 if (status != QF_OK)
1458 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001459 }
1460 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001461 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001462 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1463 if (status == QF_MULTISCAN)
1464 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001465 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001466 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001467 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001468 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001469 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001470 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001471 return QF_IGNORE_LINE;
1472 }
1473 }
1474
1475 return QF_OK;
1476}
1477
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001478/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001479 * Returns TRUE if the specified quickfix/location stack is empty
1480 */
1481 static int
1482qf_stack_empty(qf_info_T *qi)
1483{
1484 return qi == NULL || qi->qf_listcount <= 0;
1485}
1486
1487/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001488 * Returns TRUE if the specified quickfix/location list is empty.
1489 */
1490 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001491qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001492{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001493 return qfl == NULL || qfl->qf_count <= 0;
1494}
1495
1496/*
1497 * Return a pointer to a list in the specified quickfix stack
1498 */
1499 static qf_list_T *
1500qf_get_list(qf_info_T *qi, int idx)
1501{
1502 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001503}
1504
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001505/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001506 * Allocate the fields used for parsing lines and populating a quickfix list.
1507 */
1508 static int
1509qf_alloc_fields(qffields_T *pfields)
1510{
1511 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1512 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1513 pfields->errmsglen = CMDBUFFSIZE + 1;
1514 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1515 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1516 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1517 || pfields->pattern == NULL || pfields->module == NULL)
1518 return FAIL;
1519
1520 return OK;
1521}
1522
1523/*
1524 * Free the fields used for parsing lines and populating a quickfix list.
1525 */
1526 static void
1527qf_free_fields(qffields_T *pfields)
1528{
1529 vim_free(pfields->namebuf);
1530 vim_free(pfields->module);
1531 vim_free(pfields->errmsg);
1532 vim_free(pfields->pattern);
1533}
1534
1535/*
1536 * Setup the state information used for parsing lines and populating a
1537 * quickfix list.
1538 */
1539 static int
1540qf_setup_state(
1541 qfstate_T *pstate,
1542 char_u *enc,
1543 char_u *efile,
1544 typval_T *tv,
1545 buf_T *buf,
1546 linenr_T lnumfirst,
1547 linenr_T lnumlast)
1548{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001549 pstate->vc.vc_type = CONV_NONE;
1550 if (enc != NULL && *enc != NUL)
1551 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001552
1553 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1554 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001555 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001556 return FAIL;
1557 }
1558
1559 if (tv != NULL)
1560 {
1561 if (tv->v_type == VAR_STRING)
1562 pstate->p_str = tv->vval.v_string;
1563 else if (tv->v_type == VAR_LIST)
1564 pstate->p_li = tv->vval.v_list->lv_first;
1565 pstate->tv = tv;
1566 }
1567 pstate->buf = buf;
1568 pstate->buflnum = lnumfirst;
1569 pstate->lnumlast = lnumlast;
1570
1571 return OK;
1572}
1573
1574/*
1575 * Cleanup the state information used for parsing lines and populating a
1576 * quickfix list.
1577 */
1578 static void
1579qf_cleanup_state(qfstate_T *pstate)
1580{
1581 if (pstate->fd != NULL)
1582 fclose(pstate->fd);
1583
1584 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001585 if (pstate->vc.vc_type != CONV_NONE)
1586 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001587}
1588
1589/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001590 * Process the next line from a file/buffer/list/string and add it
1591 * to the quickfix list 'qfl'.
1592 */
1593 static int
1594qf_init_process_nextline(
1595 qf_list_T *qfl,
1596 efm_T *fmt_first,
1597 qfstate_T *state,
1598 qffields_T *fields)
1599{
1600 int status;
1601
1602 // Get the next line from a file/buffer/list/string
1603 status = qf_get_nextline(state);
1604 if (status != QF_OK)
1605 return status;
1606
1607 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1608 fmt_first, fields);
1609 if (status != QF_OK)
1610 return status;
1611
1612 return qf_add_entry(qfl,
1613 qfl->qf_directory,
1614 (*fields->namebuf || qfl->qf_directory != NULL)
1615 ? fields->namebuf
1616 : ((qfl->qf_currfile != NULL && fields->valid)
1617 ? qfl->qf_currfile : (char_u *)NULL),
1618 fields->module,
1619 0,
1620 fields->errmsg,
1621 fields->lnum,
1622 fields->col,
1623 fields->use_viscol,
1624 fields->pattern,
1625 fields->enr,
1626 fields->type,
1627 fields->valid);
1628}
1629
1630/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001631 * Read the errorfile "efile" into memory, line by line, building the error
1632 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001633 * Alternative: when "efile" is NULL read errors from buffer "buf".
1634 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001635 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001636 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1637 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001638 * Return -1 for error, number of errors for success.
1639 */
1640 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001641qf_init_ext(
1642 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001643 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001644 char_u *efile,
1645 buf_T *buf,
1646 typval_T *tv,
1647 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001648 int newlist, // TRUE: start a new error list
1649 linenr_T lnumfirst, // first line number to use
1650 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001651 char_u *qf_title,
1652 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001653{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001654 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001655 qfstate_T state;
1656 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001657 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001658 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001659 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001661 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001662 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001663 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001665 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001666 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001667
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001668 vim_memset(&state, 0, sizeof(state));
1669 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001670 if ((qf_alloc_fields(&fields) == FAIL) ||
1671 (qf_setup_state(&state, enc, efile, tv, buf,
1672 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 goto qf_init_end;
1674
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001675 if (newlist || qf_idx == qi->qf_listcount)
1676 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001677 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001678 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001679 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001680 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001681 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001682 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001683 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001684 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001685 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001686 qfl = qf_get_list(qi, qf_idx);
1687 if (!qf_list_empty(qfl))
1688 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001691 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001692 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001693 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 else
1695 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001697 // If the errorformat didn't change between calls, then reuse the
1698 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001699 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1700 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001701 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001702 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001703 free_efm_list(&fmt_first);
1704
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001705 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001706 fmt_first = parse_efm_option(efm);
1707 if (fmt_first != NULL)
1708 last_efm = vim_strsave(efm);
1709 }
1710
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001711 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001714 // got_int is reset here, because it was probably set when killing the
1715 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 got_int = FALSE;
1717
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001718 // Read the lines in the error file one by one.
1719 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001720 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001722 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001723 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001724 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001725 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001726 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001727 if (status == QF_FAIL)
1728 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 line_breakcheck();
1731 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001732 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001734 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001736 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001737 qfl->qf_ptr = qfl->qf_start;
1738 qfl->qf_index = 1;
1739 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 }
1741 else
1742 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001743 qfl->qf_nonevalid = FALSE;
1744 if (qfl->qf_ptr == NULL)
1745 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001747 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001748 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001749 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001751 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001753 if (!adding)
1754 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001755 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001756 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001757 qi->qf_listcount--;
1758 if (qi->qf_curlist > 0)
1759 --qi->qf_curlist;
1760 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001761qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001762 if (qf_idx == qi->qf_curlist)
1763 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001764 qf_cleanup_state(&state);
1765 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766
1767 return retval;
1768}
1769
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001770/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001771 * Read the errorfile "efile" into memory, line by line, building the error
1772 * list. Set the error list's title to qf_title.
1773 * Return -1 for error, number of errors for success.
1774 */
1775 int
1776qf_init(win_T *wp,
1777 char_u *efile,
1778 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001779 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001780 char_u *qf_title,
1781 char_u *enc)
1782{
1783 qf_info_T *qi = &ql_info;
1784
1785 if (wp != NULL)
1786 {
1787 qi = ll_get_or_alloc_list(wp);
1788 if (qi == NULL)
1789 return FAIL;
1790 }
1791
1792 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1793 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1794}
1795
1796/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001797 * Set the title of the specified quickfix list. Frees the previous title.
1798 * Prepends ':' to the title.
1799 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001800 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001801qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001802{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001803 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001804
Bram Moolenaarfb604092014-07-23 15:55:00 +02001805 if (title != NULL)
1806 {
1807 char_u *p = alloc((int)STRLEN(title) + 2);
1808
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001809 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001810 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001811 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001812 }
1813}
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001816 * The title of a quickfix/location list is set, by default, to the command
1817 * that created the quickfix list with the ":" prefix.
1818 * Create a quickfix list title string by prepending ":" to a user command.
1819 * Returns a pointer to a static buffer with the title.
1820 */
1821 static char_u *
1822qf_cmdtitle(char_u *cmd)
1823{
1824 static char_u qftitle_str[IOSIZE];
1825
1826 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1827 return qftitle_str;
1828}
1829
1830/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001831 * Return a pointer to the current list in the specified quickfix stack
1832 */
1833 static qf_list_T *
1834qf_get_curlist(qf_info_T *qi)
1835{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001836 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001837}
1838
1839/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001840 * Prepare for adding a new quickfix list. If the current list is in the
1841 * middle of the stack, then all the following lists are freed and then
1842 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 */
1844 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001845qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846{
1847 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001848 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001850 // If the current entry is not the last entry, delete entries beyond
1851 // the current entry. This makes it possible to browse in a tree-like
1852 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001853 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001854 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001856 // When the stack is full, remove to oldest entry
1857 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001858 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001860 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001862 qi->qf_lists[i - 1] = qi->qf_lists[i];
1863 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 }
1865 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001866 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001867 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001868 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1869 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001870 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001871 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872}
1873
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001874/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001875 * Queue location list stack delete request.
1876 */
1877 static void
1878locstack_queue_delreq(qf_info_T *qi)
1879{
1880 qf_delq_T *q;
1881
1882 q = (qf_delq_T *)alloc((unsigned)sizeof(qf_delq_T));
1883 if (q != NULL)
1884 {
1885 q->qi = qi;
1886 q->next = qf_delq_head;
1887 qf_delq_head = q;
1888 }
1889}
1890
1891/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001892 * Return the global quickfix stack window buffer number.
1893 */
1894 int
1895qf_stack_get_bufnr(void)
1896{
1897 return ql_info.qf_bufnr;
1898}
1899
1900/*
1901 * Wipe the quickfix window buffer (if present) for the specified
1902 * quickfix/location list.
1903 */
1904 static void
1905wipe_qf_buffer(qf_info_T *qi)
1906{
1907 buf_T *qfbuf;
1908
1909 if (qi->qf_bufnr != INVALID_QFBUFNR)
1910 {
1911 qfbuf = buflist_findnr(qi->qf_bufnr);
1912 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1913 {
1914 // If the quickfix buffer is not loaded in any window, then
1915 // wipe the buffer.
1916 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE);
1917 qi->qf_bufnr = INVALID_QFBUFNR;
1918 }
1919 }
1920}
1921
1922/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001923 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001924 */
1925 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001926ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001927{
1928 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001929 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001930
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001931 qi = *pqi;
1932 if (qi == NULL)
1933 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001934 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001935
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001936 // If the location list is still in use, then queue the delete request
1937 // to be processed later.
1938 if (quickfix_busy > 0)
1939 {
1940 locstack_queue_delreq(qi);
1941 return;
1942 }
1943
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001944 qi->qf_refcount--;
1945 if (qi->qf_refcount < 1)
1946 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001947 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001948 // If the quickfix window buffer is loaded, then wipe it
1949 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001950
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001951 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001952 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001953 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001954 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001955}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001956
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001957/*
1958 * Free all the quickfix/location lists in the stack.
1959 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001960 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001961qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001962{
1963 int i;
1964 qf_info_T *qi = &ql_info;
1965
1966 if (wp != NULL)
1967 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001968 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001969 ll_free_all(&wp->w_llist);
1970 ll_free_all(&wp->w_llist_ref);
1971 }
1972 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001973 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001974 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001975 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001976}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001977
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001979 * Delay freeing of location list stacks when the quickfix code is running.
1980 * Used to avoid problems with autocmds freeing location list stacks when the
1981 * quickfix code is still referencing the stack.
1982 * Must always call decr_quickfix_busy() exactly once after this.
1983 */
1984 static void
1985incr_quickfix_busy(void)
1986{
1987 quickfix_busy++;
1988}
1989
1990/*
1991 * Safe to free location list stacks. Process any delayed delete requests.
1992 */
1993 static void
1994decr_quickfix_busy(void)
1995{
1996 if (--quickfix_busy == 0)
1997 {
1998 // No longer referencing the location lists. Process all the pending
1999 // delete requests.
2000 while (qf_delq_head != NULL)
2001 {
2002 qf_delq_T *q = qf_delq_head;
2003
2004 qf_delq_head = q->next;
2005 ll_free_all(&q->qi);
2006 vim_free(q);
2007 }
2008 }
2009#ifdef ABORT_ON_INTERNAL_ERROR
2010 if (quickfix_busy < 0)
2011 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002012 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002013 abort();
2014 }
2015#endif
2016}
2017
2018#if defined(EXITFREE) || defined(PROTO)
2019 void
2020check_quickfix_busy(void)
2021{
2022 if (quickfix_busy != 0)
2023 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002024 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002025# ifdef ABORT_ON_INTERNAL_ERROR
2026 abort();
2027# endif
2028 }
2029}
2030#endif
2031
2032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 * Add an entry to the end of the list of errors.
Bram Moolenaar95946f12019-03-31 15:31:59 +02002034 * Returns QF_OK or QF_FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 */
2036 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002037qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002038 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002039 char_u *dir, // optional directory name
2040 char_u *fname, // file name or NULL
2041 char_u *module, // module name or NULL
2042 int bufnum, // buffer number or zero
2043 char_u *mesg, // message
2044 long lnum, // line number
2045 int col, // column
2046 int vis_col, // using visual column
2047 char_u *pattern, // search pattern
2048 int nr, // error number
2049 int type, // type character
2050 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002052 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002053 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002055 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002056 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002057 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002058 {
2059 buf_T *buf = buflist_findnr(bufnum);
2060
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002061 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002062 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002063 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002064 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002065 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002066 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002067 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2069 {
2070 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002071 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
2073 qfp->qf_lnum = lnum;
2074 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002075 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002076 if (pattern == NULL || *pattern == NUL)
2077 qfp->qf_pattern = NULL;
2078 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2079 {
2080 vim_free(qfp->qf_text);
2081 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002082 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002083 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002084 if (module == NULL || *module == NUL)
2085 qfp->qf_module = NULL;
2086 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2087 {
2088 vim_free(qfp->qf_text);
2089 vim_free(qfp->qf_pattern);
2090 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002091 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002094 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 type = 0;
2096 qfp->qf_type = type;
2097 qfp->qf_valid = valid;
2098
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002099 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002100 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002102 qfl->qf_start = qfp;
2103 qfl->qf_ptr = qfp;
2104 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002105 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
2107 else
2108 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002109 qfp->qf_prev = *lastp;
2110 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002112 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002114 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002115 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002116 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002118 qfl->qf_index = qfl->qf_count;
2119 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
2121
Bram Moolenaar95946f12019-03-31 15:31:59 +02002122 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123}
2124
2125/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002126 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002127 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002128 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002129qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002130{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002131 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002132
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002133 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002134 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002135 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002136 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002137 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002138 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002139 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002140 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002141}
2142
2143/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002144 * Return the location list stack for window 'wp'.
2145 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002146 */
2147 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002148ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002149{
2150 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002151 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002152 return wp->w_llist_ref;
2153
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002154 // For a non-location list window, w_llist_ref should not point to a
2155 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002156 ll_free_all(&wp->w_llist_ref);
2157
2158 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002159 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002160 return wp->w_llist;
2161}
2162
2163/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002164 * Get the quickfix/location list stack to use for the specified Ex command.
2165 * For a location list command, returns the stack for the current window. If
2166 * the location list is not found, then returns NULL and prints an error
2167 * message if 'print_emsg' is TRUE.
2168 */
2169 static qf_info_T *
2170qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2171{
2172 qf_info_T *qi = &ql_info;
2173
2174 if (is_loclist_cmd(eap->cmdidx))
2175 {
2176 qi = GET_LOC_LIST(curwin);
2177 if (qi == NULL)
2178 {
2179 if (print_emsg)
2180 emsg(_(e_loclist));
2181 return NULL;
2182 }
2183 }
2184
2185 return qi;
2186}
2187
2188/*
2189 * Get the quickfix/location list stack to use for the specified Ex command.
2190 * For a location list command, returns the stack for the current window.
2191 * If the location list is not present, then allocates a new one.
2192 * Returns NULL if the allocation fails. For a location list command, sets
2193 * 'pwinp' to curwin.
2194 */
2195 static qf_info_T *
2196qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2197{
2198 qf_info_T *qi = &ql_info;
2199
2200 if (is_loclist_cmd(eap->cmdidx))
2201 {
2202 qi = ll_get_or_alloc_list(curwin);
2203 if (qi == NULL)
2204 return NULL;
2205 *pwinp = curwin;
2206 }
2207
2208 return qi;
2209}
2210
2211/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002212 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2213 */
2214 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002215copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002216{
2217 int i;
2218 qfline_T *from_qfp;
2219 qfline_T *prevp;
2220
2221 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002222 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002223 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002224 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002225 NULL,
2226 NULL,
2227 from_qfp->qf_module,
2228 0,
2229 from_qfp->qf_text,
2230 from_qfp->qf_lnum,
2231 from_qfp->qf_col,
2232 from_qfp->qf_viscol,
2233 from_qfp->qf_pattern,
2234 from_qfp->qf_nr,
2235 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002236 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002237 return FAIL;
2238
2239 // qf_add_entry() will not set the qf_num field, as the
2240 // directory and file names are not supplied. So the qf_fnum
2241 // field is copied here.
2242 prevp = to_qfl->qf_last;
2243 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2244 prevp->qf_type = from_qfp->qf_type; // error type
2245 if (from_qfl->qf_ptr == from_qfp)
2246 to_qfl->qf_ptr = prevp; // current location
2247 }
2248
2249 return OK;
2250}
2251
2252/*
2253 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2254 */
2255 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002256copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002257{
2258 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002259 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002260 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2261 to_qfl->qf_count = 0;
2262 to_qfl->qf_index = 0;
2263 to_qfl->qf_start = NULL;
2264 to_qfl->qf_last = NULL;
2265 to_qfl->qf_ptr = NULL;
2266 if (from_qfl->qf_title != NULL)
2267 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2268 else
2269 to_qfl->qf_title = NULL;
2270 if (from_qfl->qf_ctx != NULL)
2271 {
2272 to_qfl->qf_ctx = alloc_tv();
2273 if (to_qfl->qf_ctx != NULL)
2274 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2275 }
2276 else
2277 to_qfl->qf_ctx = NULL;
2278
2279 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002280 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002281 return FAIL;
2282
2283 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2284
2285 // Assign a new ID for the location list
2286 to_qfl->qf_id = ++last_qf_id;
2287 to_qfl->qf_changedtick = 0L;
2288
2289 // When no valid entries are present in the list, qf_ptr points to
2290 // the first item in the list
2291 if (to_qfl->qf_nonevalid)
2292 {
2293 to_qfl->qf_ptr = to_qfl->qf_start;
2294 to_qfl->qf_index = 1;
2295 }
2296
2297 return OK;
2298}
2299
2300/*
2301 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002302 */
2303 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002304copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002305{
2306 qf_info_T *qi;
2307 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002308
Bram Moolenaar09037502018-09-25 22:08:14 +02002309 // When copying from a location list window, copy the referenced
2310 // location list. For other windows, copy the location list for
2311 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002312 if (IS_LL_WINDOW(from))
2313 qi = from->w_llist_ref;
2314 else
2315 qi = from->w_llist;
2316
Bram Moolenaar09037502018-09-25 22:08:14 +02002317 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002318 return;
2319
Bram Moolenaar09037502018-09-25 22:08:14 +02002320 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002321 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002322 return;
2323
2324 to->w_llist->qf_listcount = qi->qf_listcount;
2325
Bram Moolenaar09037502018-09-25 22:08:14 +02002326 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002327 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002328 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002329 to->w_llist->qf_curlist = idx;
2330
Bram Moolenaar0398e002019-03-21 21:12:49 +01002331 if (copy_loclist(qf_get_list(qi, idx),
2332 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002333 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002334 qf_free_all(to);
2335 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002336 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002337 }
2338
Bram Moolenaar09037502018-09-25 22:08:14 +02002339 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002340}
2341
2342/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002343 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002344 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 */
2346 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002347qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
Bram Moolenaar82404332016-07-10 17:00:38 +02002349 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002350 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002351 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002352
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002353 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
Bram Moolenaare60acc12011-05-10 16:41:25 +02002356#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002357 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002358#endif
2359#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002360 if (directory != NULL)
2361 slash_adjust(directory);
2362 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002363#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002364 if (directory != NULL && !vim_isAbsName(fname)
2365 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2366 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002367 // Here we check if the file really exists.
2368 // This should normally be true, but if make works without
2369 // "leaving directory"-messages we might have missed a
2370 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002371 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002374 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002375 if (directory)
2376 ptr = concat_fnames(directory, fname, TRUE);
2377 else
2378 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002380 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002381 bufname = ptr;
2382 }
2383 else
2384 bufname = fname;
2385
2386 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002387 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002388 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002389 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002390 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002392 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002393 {
2394 vim_free(qf_last_bufname);
2395 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2396 if (bufname == ptr)
2397 qf_last_bufname = bufname;
2398 else
2399 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002400 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002401 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002402 if (buf == NULL)
2403 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002404
Bram Moolenaarc1542742016-07-20 21:44:37 +02002405 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002406 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002407 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408}
2409
2410/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002411 * Push dirbuf onto the directory stack and return pointer to actual dir or
2412 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 */
2414 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002415qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416{
2417 struct dir_stack_T *ds_new;
2418 struct dir_stack_T *ds_ptr;
2419
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002420 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2422 if (ds_new == NULL)
2423 return NULL;
2424
2425 ds_new->next = *stackptr;
2426 *stackptr = ds_new;
2427
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002428 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 if (vim_isAbsName(dirbuf)
2430 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002431 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 (*stackptr)->dirname = vim_strsave(dirbuf);
2433 else
2434 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002435 // Okay we don't have an absolute path.
2436 // dirbuf must be a subdir of one of the directories on the stack.
2437 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 ds_new = (*stackptr)->next;
2439 (*stackptr)->dirname = NULL;
2440 while (ds_new)
2441 {
2442 vim_free((*stackptr)->dirname);
2443 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2444 TRUE);
2445 if (mch_isdir((*stackptr)->dirname) == TRUE)
2446 break;
2447
2448 ds_new = ds_new->next;
2449 }
2450
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002451 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 while ((*stackptr)->next != ds_new)
2453 {
2454 ds_ptr = (*stackptr)->next;
2455 (*stackptr)->next = (*stackptr)->next->next;
2456 vim_free(ds_ptr->dirname);
2457 vim_free(ds_ptr);
2458 }
2459
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002460 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 if (ds_new == NULL)
2462 {
2463 vim_free((*stackptr)->dirname);
2464 (*stackptr)->dirname = vim_strsave(dirbuf);
2465 }
2466 }
2467
2468 if ((*stackptr)->dirname != NULL)
2469 return (*stackptr)->dirname;
2470 else
2471 {
2472 ds_ptr = *stackptr;
2473 *stackptr = (*stackptr)->next;
2474 vim_free(ds_ptr);
2475 return NULL;
2476 }
2477}
2478
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479/*
2480 * pop dirbuf from the directory stack and return previous directory or NULL if
2481 * stack is empty
2482 */
2483 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002484qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
2486 struct dir_stack_T *ds_ptr;
2487
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002488 // TODO: Should we check if dirbuf is the directory on top of the stack?
2489 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002491 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 if (*stackptr != NULL)
2493 {
2494 ds_ptr = *stackptr;
2495 *stackptr = (*stackptr)->next;
2496 vim_free(ds_ptr->dirname);
2497 vim_free(ds_ptr);
2498 }
2499
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002500 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 return *stackptr ? (*stackptr)->dirname : NULL;
2502}
2503
2504/*
2505 * clean up directory stack
2506 */
2507 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002508qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510 struct dir_stack_T *ds_ptr;
2511
2512 while ((ds_ptr = *stackptr) != NULL)
2513 {
2514 *stackptr = (*stackptr)->next;
2515 vim_free(ds_ptr->dirname);
2516 vim_free(ds_ptr);
2517 }
2518}
2519
2520/*
2521 * Check in which directory of the directory stack the given file can be
2522 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002523 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 * Cleans up intermediate directory entries.
2525 *
2526 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002527 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 * ./
2529 * ./aa
2530 * ./aa/bb
2531 * ./bb
2532 * ./bb/x.c
2533 * and make says:
2534 * making all in aa
2535 * making all in bb
2536 * x.c:9: Error
2537 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2538 * qf_guess_filepath will return NULL.
2539 */
2540 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002541qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
2543 struct dir_stack_T *ds_ptr;
2544 struct dir_stack_T *ds_tmp;
2545 char_u *fullname;
2546
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002547 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002548 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 return NULL;
2550
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002551 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 fullname = NULL;
2553 while (ds_ptr)
2554 {
2555 vim_free(fullname);
2556 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2557
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002558 // If concat_fnames failed, just go on. The worst thing that can happen
2559 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2561 break;
2562
2563 ds_ptr = ds_ptr->next;
2564 }
2565
2566 vim_free(fullname);
2567
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002568 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002569 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002571 ds_tmp = qfl->qf_dir_stack->next;
2572 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 vim_free(ds_tmp->dirname);
2574 vim_free(ds_tmp);
2575 }
2576
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002577 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578}
2579
2580/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002581 * Returns TRUE if a quickfix/location list with the given identifier exists.
2582 */
2583 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002584qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002585{
2586 qf_info_T *qi = &ql_info;
2587 int i;
2588
2589 if (wp != NULL)
2590 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002591 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002592 if (qi == NULL)
2593 return FALSE;
2594 }
2595
2596 for (i = 0; i < qi->qf_listcount; ++i)
2597 if (qi->qf_lists[i].qf_id == qf_id)
2598 return TRUE;
2599
2600 return FALSE;
2601}
2602
2603/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002604 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002605 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002606 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002607 * Similar to location list.
2608 */
2609 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002610is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002611{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002612 qfline_T *qfp;
2613 int i;
2614
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002615 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002616 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2617 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002618 break;
2619
Bram Moolenaar95946f12019-03-31 15:31:59 +02002620 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002621 return FALSE;
2622
2623 return TRUE;
2624}
2625
2626/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002627 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002628 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002629 */
2630 static qfline_T *
2631get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002632 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002633 qfline_T *qf_ptr,
2634 int *qf_index,
2635 int dir)
2636{
2637 int idx;
2638 int old_qf_fnum;
2639
2640 idx = *qf_index;
2641 old_qf_fnum = qf_ptr->qf_fnum;
2642
2643 do
2644 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002645 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002646 return NULL;
2647 ++idx;
2648 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002649 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002650 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2651
2652 *qf_index = idx;
2653 return qf_ptr;
2654}
2655
2656/*
2657 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002658 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002659 */
2660 static qfline_T *
2661get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002662 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002663 qfline_T *qf_ptr,
2664 int *qf_index,
2665 int dir)
2666{
2667 int idx;
2668 int old_qf_fnum;
2669
2670 idx = *qf_index;
2671 old_qf_fnum = qf_ptr->qf_fnum;
2672
2673 do
2674 {
2675 if (idx == 1 || qf_ptr->qf_prev == NULL)
2676 return NULL;
2677 --idx;
2678 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002679 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002680 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2681
2682 *qf_index = idx;
2683 return qf_ptr;
2684}
2685
2686/*
2687 * Get the n'th (errornr) previous/next valid entry from the current entry in
2688 * the quickfix list.
2689 * dir == FORWARD or FORWARD_FILE: next valid entry
2690 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2691 */
2692 static qfline_T *
2693get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002694 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002695 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002696 int dir,
2697 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002698{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002699 qfline_T *qf_ptr = qfl->qf_ptr;
2700 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002701 qfline_T *prev_qf_ptr;
2702 int prev_index;
2703 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2704 char_u *err = e_no_more_items;
2705
2706 while (errornr--)
2707 {
2708 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002709 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002710
2711 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002712 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002713 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002714 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002715 if (qf_ptr == NULL)
2716 {
2717 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002718 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002719 if (err != NULL)
2720 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002721 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002722 return NULL;
2723 }
2724 break;
2725 }
2726
2727 err = NULL;
2728 }
2729
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002730 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002731 return qf_ptr;
2732}
2733
2734/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002735 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2736 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002737 */
2738 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002739get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002740{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002741 qfline_T *qf_ptr = qfl->qf_ptr;
2742 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002743
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002744 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002745 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2746 {
2747 --qf_idx;
2748 qf_ptr = qf_ptr->qf_prev;
2749 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002750 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002751 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2752 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002753 {
2754 ++qf_idx;
2755 qf_ptr = qf_ptr->qf_next;
2756 }
2757
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002758 *new_qfidx = qf_idx;
2759 return qf_ptr;
2760}
2761
2762/*
2763 * Get a entry specied by 'errornr' and 'dir' from the current
2764 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2765 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2766 * Returns a pointer to the entry and the index of the new entry is stored in
2767 * 'new_qfidx'.
2768 */
2769 static qfline_T *
2770qf_get_entry(
2771 qf_list_T *qfl,
2772 int errornr,
2773 int dir,
2774 int *new_qfidx)
2775{
2776 qfline_T *qf_ptr = qfl->qf_ptr;
2777 int qfidx = qfl->qf_index;
2778
2779 if (dir != 0) // next/prev valid entry
2780 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2781 else if (errornr != 0) // go to specified number
2782 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2783
2784 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002785 return qf_ptr;
2786}
2787
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002788/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002789 * Find a window displaying a Vim help file.
2790 */
2791 static win_T *
2792qf_find_help_win(void)
2793{
2794 win_T *wp;
2795
2796 FOR_ALL_WINDOWS(wp)
2797 if (bt_help(wp->w_buffer))
2798 return wp;
2799
2800 return NULL;
2801}
2802
2803/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002804 * Set the location list for the specified window to 'qi'.
2805 */
2806 static void
2807win_set_loclist(win_T *wp, qf_info_T *qi)
2808{
2809 wp->w_llist = qi;
2810 qi->qf_refcount++;
2811}
2812
2813/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002814 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2815 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002816 */
2817 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002818jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002819{
2820 win_T *wp;
2821 int flags;
2822
Bram Moolenaarb2443732018-11-11 22:50:27 +01002823 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002824 wp = NULL;
2825 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002826 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002827 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2828 win_enter(wp, TRUE);
2829 else
2830 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002831 // Split off help window; put it at far top if no position
2832 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002833 flags = WSP_HELP;
2834 if (cmdmod.split == 0 && curwin->w_width != Columns
2835 && curwin->w_width < 80)
2836 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002837 // If the user asks to open a new window, then copy the location list.
2838 // Otherwise, don't copy the location list.
2839 if (IS_LL_STACK(qi) && !newwin)
2840 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002841
2842 if (win_split(0, flags) == FAIL)
2843 return FAIL;
2844
2845 *opened_window = TRUE;
2846
2847 if (curwin->w_height < p_hh)
2848 win_setheight((int)p_hh);
2849
Bram Moolenaarb2443732018-11-11 22:50:27 +01002850 // When using location list, the new window should use the supplied
2851 // location list. If the user asks to open a new window, then the new
2852 // window will get a copy of the location list.
2853 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002854 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002855 }
2856
2857 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002858 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002859
2860 return OK;
2861}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002862
2863/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002864 * Find a non-quickfix window in the current tabpage using the given location
2865 * list stack.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002866 * Returns NULL if a matching window is not found.
2867 */
2868 static win_T *
2869qf_find_win_with_loclist(qf_info_T *ll)
2870{
2871 win_T *wp;
2872
2873 FOR_ALL_WINDOWS(wp)
2874 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2875 return wp;
2876
2877 return NULL;
2878}
2879
2880/*
2881 * Find a window containing a normal buffer
2882 */
2883 static win_T *
2884qf_find_win_with_normal_buf(void)
2885{
2886 win_T *wp;
2887
2888 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002889 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002890 return wp;
2891
2892 return NULL;
2893}
2894
2895/*
2896 * Go to a window in any tabpage containing the specified file. Returns TRUE
2897 * if successfully jumped to the window. Otherwise returns FALSE.
2898 */
2899 static int
2900qf_goto_tabwin_with_file(int fnum)
2901{
2902 tabpage_T *tp;
2903 win_T *wp;
2904
2905 FOR_ALL_TAB_WINDOWS(tp, wp)
2906 if (wp->w_buffer->b_fnum == fnum)
2907 {
2908 goto_tabpage_win(tp, wp);
2909 return TRUE;
2910 }
2911
2912 return FALSE;
2913}
2914
2915/*
2916 * Create a new window to show a file above the quickfix window. Called when
2917 * only the quickfix window is present.
2918 */
2919 static int
2920qf_open_new_file_win(qf_info_T *ll_ref)
2921{
2922 int flags;
2923
2924 flags = WSP_ABOVE;
2925 if (ll_ref != NULL)
2926 flags |= WSP_NEWLOC;
2927 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002928 return FAIL; // not enough room for window
2929 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002930 swb_flags = 0;
2931 RESET_BINDING(curwin);
2932 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002933 // The new window should use the location list from the
2934 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002935 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002936 return OK;
2937}
2938
2939/*
2940 * Go to a window that shows the right buffer. If the window is not found, go
2941 * to the window just above the location list window. This is used for opening
2942 * a file from a location window and not from a quickfix window. If some usable
2943 * window is previously found, then it is supplied in 'use_win'.
2944 */
2945 static void
2946qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2947{
2948 win_T *win = use_win;
2949
2950 if (win == NULL)
2951 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002952 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002953 FOR_ALL_WINDOWS(win)
2954 if (win->w_buffer->b_fnum == qf_fnum)
2955 break;
2956 if (win == NULL)
2957 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002958 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002959 win = curwin;
2960 do
2961 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002962 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002963 break;
2964 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002965 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002966 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002967 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002968 } while (win != curwin);
2969 }
2970 }
2971 win_goto(win);
2972
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002973 // If the location list for the window is not set, then set it
2974 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01002975 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002976 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002977}
2978
2979/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002980 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2981 * not found, then go to the window just above the quickfix window. This is
2982 * used for opening a file from a quickfix window and not from a location
2983 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002984 */
2985 static void
2986qf_goto_win_with_qfl_file(int qf_fnum)
2987{
2988 win_T *win;
2989 win_T *altwin;
2990
2991 win = curwin;
2992 altwin = NULL;
2993 for (;;)
2994 {
2995 if (win->w_buffer->b_fnum == qf_fnum)
2996 break;
2997 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002998 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002999 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003000 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003001
3002 if (IS_QF_WINDOW(win))
3003 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003004 // Didn't find it, go to the window before the quickfix
3005 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003006 if (altwin != NULL)
3007 win = altwin;
3008 else if (curwin->w_prev != NULL)
3009 win = curwin->w_prev;
3010 else
3011 win = curwin->w_next;
3012 break;
3013 }
3014
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003015 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003016 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003017 altwin = win;
3018 }
3019
3020 win_goto(win);
3021}
3022
3023/*
3024 * Find a suitable window for opening a file (qf_fnum) from the
3025 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003026 * window, jump to it. Otherwise open a new window to display the file. If
3027 * 'newwin' is TRUE, then always open a new window. This is called from either
3028 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003029 */
3030 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003031qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003032{
3033 win_T *usable_win_ptr = NULL;
3034 int usable_win;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003035 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003036 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003037
3038 usable_win = 0;
3039
Bram Moolenaarb2443732018-11-11 22:50:27 +01003040 // If opening a new window, then don't use the location list referred by
3041 // the current window. Otherwise two windows will refer to the same
3042 // location list.
3043 if (!newwin)
3044 ll_ref = curwin->w_llist_ref;
3045
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003046 if (ll_ref != NULL)
3047 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003048 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003049 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
3050 if (usable_win_ptr != NULL)
3051 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003052 }
3053
3054 if (!usable_win)
3055 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003056 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003057 win = qf_find_win_with_normal_buf();
3058 if (win != NULL)
3059 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003060 }
3061
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003062 // If no usable window is found and 'switchbuf' contains "usetab"
3063 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003064 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003065 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003066
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003067 // If there is only one window and it is the quickfix window, create a
3068 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003069 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003070 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003071 if (qf_open_new_file_win(ll_ref) != OK)
3072 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003073 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003074 }
3075 else
3076 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003077 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003078 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003079 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003080 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003081 }
3082
3083 return OK;
3084}
3085
3086/*
3087 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003088 * Returns OK if successfully edited the file, FAIL on failing to open the
3089 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3090 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003091 */
3092 static int
3093qf_jump_edit_buffer(
3094 qf_info_T *qi,
3095 qfline_T *qf_ptr,
3096 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003097 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003098 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003099{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003100 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003101 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003102 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003103 int old_qf_curlist = qi->qf_curlist;
3104 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003105
3106 if (qf_ptr->qf_type == 1)
3107 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003108 // Open help file (do_ecmd() will set b_help flag, readfile() will
3109 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003110 if (!can_abandon(curbuf, forceit))
3111 {
3112 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003113 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003114 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003115
3116 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3117 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003118 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003119 }
3120 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003121 retval = buflist_getfile(qf_ptr->qf_fnum,
3122 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003123
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003124 // If a location list, check whether the associated window is still
3125 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003126 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003127 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003128 win_T *wp = win_id2wp(prev_winid);
3129 if (wp == NULL && curwin->w_llist != qi)
3130 {
3131 emsg(_("E924: Current window was closed"));
3132 *opened_window = FALSE;
3133 return NOTDONE;
3134 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003135 }
3136
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003137 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003138 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003139 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003140 return NOTDONE;
3141 }
3142
3143 if (old_qf_curlist != qi->qf_curlist
3144 || !is_qf_entry_present(qfl, qf_ptr))
3145 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003146 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003147 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003148 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003149 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003150 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003151 }
3152
3153 return retval;
3154}
3155
3156/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003157 * Go to the error line in the current file using either line/column number or
3158 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003159 */
3160 static void
3161qf_jump_goto_line(
3162 linenr_T qf_lnum,
3163 int qf_col,
3164 char_u qf_viscol,
3165 char_u *qf_pattern)
3166{
3167 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003168
3169 if (qf_pattern == NULL)
3170 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003171 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003172 i = qf_lnum;
3173 if (i > 0)
3174 {
3175 if (i > curbuf->b_ml.ml_line_count)
3176 i = curbuf->b_ml.ml_line_count;
3177 curwin->w_cursor.lnum = i;
3178 }
3179 if (qf_col > 0)
3180 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003181 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003182 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003183 coladvance(qf_col - 1);
3184 else
3185 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003186 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003187 check_cursor();
3188 }
3189 else
3190 beginline(BL_WHITE | BL_FIX);
3191 }
3192 else
3193 {
3194 pos_T save_cursor;
3195
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003196 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003197 save_cursor = curwin->w_cursor;
3198 curwin->w_cursor.lnum = 0;
3199 if (!do_search(NULL, '/', qf_pattern, (long)1,
3200 SEARCH_KEEP, NULL, NULL))
3201 curwin->w_cursor = save_cursor;
3202 }
3203}
3204
3205/*
3206 * Display quickfix list index and size message
3207 */
3208 static void
3209qf_jump_print_msg(
3210 qf_info_T *qi,
3211 int qf_index,
3212 qfline_T *qf_ptr,
3213 buf_T *old_curbuf,
3214 linenr_T old_lnum)
3215{
3216 linenr_T i;
3217 int len;
3218
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003219 // Update the screen before showing the message, unless the screen
3220 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003221 if (!msg_scrolled)
3222 update_topline_redraw();
3223 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003224 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003225 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3226 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003227 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003228 len = (int)STRLEN(IObuff);
3229 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3230
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003231 // Output the message. Overwrite to avoid scrolling when the 'O'
3232 // flag is present in 'shortmess'; But when not jumping, print the
3233 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003234 i = msg_scroll;
3235 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3236 msg_scroll = TRUE;
3237 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3238 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003239 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003240 msg_scroll = i;
3241}
3242
3243/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003244 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003245 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3246 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003247 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3248 * able to jump/open a window. Returns NOTDONE if a file is not associated
3249 * with the entry.
3250 */
3251 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003252qf_jump_open_window(
3253 qf_info_T *qi,
3254 qfline_T *qf_ptr,
3255 int newwin,
3256 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003257{
3258 // For ":helpgrep" find a help window or open one.
3259 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003260 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003261 return FAIL;
3262
3263 // If currently in the quickfix window, find another window to show the
3264 // file in.
3265 if (bt_quickfix(curbuf) && !*opened_window)
3266 {
3267 // If there is no file specified, we don't know where to go.
3268 // But do advance, otherwise ":cn" gets stuck.
3269 if (qf_ptr->qf_fnum == 0)
3270 return NOTDONE;
3271
Bram Moolenaarb2443732018-11-11 22:50:27 +01003272 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3273 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003274 return FAIL;
3275 }
3276
3277 return OK;
3278}
3279
3280/*
3281 * Edit a selected file from the quickfix/location list and jump to a
3282 * particular line/column, adjust the folds and display a message about the
3283 * jump.
3284 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3285 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3286 * the file.
3287 */
3288 static int
3289qf_jump_to_buffer(
3290 qf_info_T *qi,
3291 int qf_index,
3292 qfline_T *qf_ptr,
3293 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003294 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003295 int *opened_window,
3296 int openfold,
3297 int print_message)
3298{
3299 buf_T *old_curbuf;
3300 linenr_T old_lnum;
3301 int retval = OK;
3302
3303 // If there is a file name, read the wanted file if needed, and check
3304 // autowrite etc.
3305 old_curbuf = curbuf;
3306 old_lnum = curwin->w_cursor.lnum;
3307
3308 if (qf_ptr->qf_fnum != 0)
3309 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003310 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003311 opened_window);
3312 if (retval != OK)
3313 return retval;
3314 }
3315
3316 // When not switched to another buffer, still need to set pc mark
3317 if (curbuf == old_curbuf)
3318 setpcmark();
3319
3320 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3321 qf_ptr->qf_pattern);
3322
3323#ifdef FEAT_FOLDING
3324 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3325 foldOpenCursor();
3326#endif
3327 if (print_message)
3328 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3329
3330 return retval;
3331}
3332
3333/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003334 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 */
3336 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003337qf_jump(qf_info_T *qi,
3338 int dir,
3339 int errornr,
3340 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003342 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3343}
3344
3345/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003346 * Jump to a quickfix line.
3347 * If dir == 0 go to entry "errornr".
3348 * If dir == FORWARD go "errornr" valid entries forward.
3349 * If dir == BACKWARD go "errornr" valid entries backward.
3350 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3351 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3352 * else if "errornr" is zero, redisplay the same line
3353 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003354 * If 'newwin' is TRUE, then open the file in a new window.
3355 */
3356 void
3357qf_jump_newwin(qf_info_T *qi,
3358 int dir,
3359 int errornr,
3360 int forceit,
3361 int newwin)
3362{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003363 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003364 qfline_T *qf_ptr;
3365 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003368 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003369 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003370 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003374 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003376 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003378 if (qi == NULL)
3379 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003380
Bram Moolenaar0398e002019-03-21 21:12:49 +01003381 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003383 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 return;
3385 }
3386
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003387 incr_quickfix_busy();
3388
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003389 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003390
3391 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003393 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003395
3396 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3397 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003399 qf_ptr = old_qf_ptr;
3400 qf_index = old_qf_index;
3401 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003404 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003405 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003406 // No need to print the error message if it's visible in the error
3407 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 print_message = FALSE;
3409
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003410 prev_winid = curwin->w_id;
3411
Bram Moolenaarb2443732018-11-11 22:50:27 +01003412 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003413 if (retval == FAIL)
3414 goto failed;
3415 if (retval == NOTDONE)
3416 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003417
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003418 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003419 &opened_window, old_KeyTyped, print_message);
3420 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003422 // Quickfix/location list is freed by an autocmd
3423 qi = NULL;
3424 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003427 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003430 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003431 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003433 // Couldn't open file, so put index back where it was. This could
3434 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 qf_ptr = old_qf_ptr;
3437 qf_index = old_qf_index;
3438 }
3439 }
3440theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003441 if (qi != NULL)
3442 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003443 qfl->qf_ptr = qf_ptr;
3444 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003445 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003446 if (p_swb != old_swb)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003448 // Restore old 'switchbuf' value, but not when an autocommand or
3449 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003451 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003453 swb_flags = old_swb_flags;
3454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 else
3456 free_string_option(old_swb);
3457 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003458 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459}
3460
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003461// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003462static int qfFileAttr;
3463static int qfSepAttr;
3464static int qfLineAttr;
3465
3466/*
3467 * Display information about a single entry from the quickfix/location list.
3468 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003469 * 'cursel' will be set to TRUE for the currently selected entry in the
3470 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003471 */
3472 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003473qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003474{
3475 char_u *fname;
3476 buf_T *buf;
3477 int filter_entry;
3478
3479 fname = NULL;
3480 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3481 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3482 (char *)qfp->qf_module);
3483 else {
3484 if (qfp->qf_fnum != 0
3485 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3486 {
3487 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003488 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003489 fname = gettail(fname);
3490 }
3491 if (fname == NULL)
3492 sprintf((char *)IObuff, "%2d", qf_idx);
3493 else
3494 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3495 qf_idx, (char *)fname);
3496 }
3497
3498 // Support for filtering entries using :filter /pat/ clist
3499 // Match against the module name, file name, search pattern and
3500 // text of the entry.
3501 filter_entry = TRUE;
3502 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3503 filter_entry &= message_filtered(qfp->qf_module);
3504 if (filter_entry && fname != NULL)
3505 filter_entry &= message_filtered(fname);
3506 if (filter_entry && qfp->qf_pattern != NULL)
3507 filter_entry &= message_filtered(qfp->qf_pattern);
3508 if (filter_entry)
3509 filter_entry &= message_filtered(qfp->qf_text);
3510 if (filter_entry)
3511 return;
3512
3513 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003514 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003515
3516 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003517 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003518 if (qfp->qf_lnum == 0)
3519 IObuff[0] = NUL;
3520 else if (qfp->qf_col == 0)
3521 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3522 else
3523 sprintf((char *)IObuff, "%ld col %d",
3524 qfp->qf_lnum, qfp->qf_col);
3525 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3526 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003527 msg_puts_attr((char *)IObuff, qfLineAttr);
3528 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003529 if (qfp->qf_pattern != NULL)
3530 {
3531 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003532 msg_puts((char *)IObuff);
3533 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003534 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003535 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003536
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003537 // Remove newlines and leading whitespace from the text. For an
3538 // unrecognized line keep the indent, the compiler may mark a word
3539 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003540 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3541 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3542 IObuff, IOSIZE);
3543 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003544 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003545}
3546
3547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003549 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 */
3551 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003552qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003554 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003555 qfline_T *qfp;
3556 int i;
3557 int idx1 = 1;
3558 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003559 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003560 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003561 int all = eap->forceit; // if not :cl!, only show
3562 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003563 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003565 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3566 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003567
Bram Moolenaar0398e002019-03-21 21:12:49 +01003568 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003570 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 return;
3572 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003573 if (*arg == '+')
3574 {
3575 ++arg;
3576 plus = TRUE;
3577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003580 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 return;
3582 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003583 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003584 if (plus)
3585 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003586 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003587 idx2 = i + idx1;
3588 idx1 = i;
3589 }
3590 else
3591 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003592 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003593 if (idx1 < 0)
3594 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3595 if (idx2 < 0)
3596 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003599 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003600 shorten_fnames(FALSE);
3601
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003602 // Get the attributes for the different quickfix highlight items. Note
3603 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003604 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3605 if (qfFileAttr == 0)
3606 qfFileAttr = HL_ATTR(HLF_D);
3607 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3608 if (qfSepAttr == 0)
3609 qfSepAttr = HL_ATTR(HLF_D);
3610 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3611 if (qfLineAttr == 0)
3612 qfLineAttr = HL_ATTR(HLF_N);
3613
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003614 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003616 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003619 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 ui_breakcheck();
3622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623}
3624
3625/*
3626 * Remove newlines and leading whitespace from an error message.
3627 * Put the result in "buf[bufsize]".
3628 */
3629 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003630qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631{
3632 int i;
3633 char_u *p = text;
3634
3635 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3636 {
3637 if (*p == '\n')
3638 {
3639 buf[i] = ' ';
3640 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003641 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 break;
3643 }
3644 else
3645 buf[i] = *p++;
3646 }
3647 buf[i] = NUL;
3648}
3649
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003650/*
3651 * Display information (list number, list size and the title) about a
3652 * quickfix/location list.
3653 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003654 static void
3655qf_msg(qf_info_T *qi, int which, char *lead)
3656{
3657 char *title = (char *)qi->qf_lists[which].qf_title;
3658 int count = qi->qf_lists[which].qf_count;
3659 char_u buf[IOSIZE];
3660
3661 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3662 lead,
3663 which + 1,
3664 qi->qf_listcount,
3665 count);
3666
3667 if (title != NULL)
3668 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003669 size_t len = STRLEN(buf);
3670
3671 if (len < 34)
3672 {
3673 vim_memset(buf + len, ' ', 34 - len);
3674 buf[34] = NUL;
3675 }
3676 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003677 }
3678 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003679 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003680}
3681
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682/*
3683 * ":colder [count]": Up in the quickfix stack.
3684 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003685 * ":lolder [count]": Up in the location list stack.
3686 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 */
3688 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003689qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003691 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 int count;
3693
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003694 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3695 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003696
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (eap->addr_count != 0)
3698 count = eap->line2;
3699 else
3700 count = 1;
3701 while (count--)
3702 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003703 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003705 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003707 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003708 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003710 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
3712 else
3713 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003714 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003716 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003717 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003719 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
3721 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003722 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003723 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724}
3725
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003726/*
3727 * Display the information about all the quickfix/location lists in the stack
3728 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003729 void
3730qf_history(exarg_T *eap)
3731{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003732 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003733 int i;
3734
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003735 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003736 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003737 else
3738 for (i = 0; i < qi->qf_listcount; ++i)
3739 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3740}
3741
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003743 * Free all the entries in the error list "idx". Note that other information
3744 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 */
3746 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003747qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003749 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003750 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003751 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003753 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003755 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003756 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003757 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003758 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003759 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003760 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003761 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003762 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003763 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003764 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003765 // Somehow qf_count may have an incorrect value, set it to 1
3766 // to avoid crashing when it's wrong.
3767 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003768 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003769 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003770 qfl->qf_start = qfpnext;
3771 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003773
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003774 qfl->qf_index = 0;
3775 qfl->qf_start = NULL;
3776 qfl->qf_last = NULL;
3777 qfl->qf_ptr = NULL;
3778 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003779
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003780 qf_clean_dir_stack(&qfl->qf_dir_stack);
3781 qfl->qf_directory = NULL;
3782 qf_clean_dir_stack(&qfl->qf_file_stack);
3783 qfl->qf_currfile = NULL;
3784 qfl->qf_multiline = FALSE;
3785 qfl->qf_multiignore = FALSE;
3786 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787}
3788
3789/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003790 * Free error list "idx". Frees all the entries in the quickfix list,
3791 * associated context information and the title.
3792 */
3793 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003794qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003795{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003796 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003797
Bram Moolenaard23a8232018-02-10 18:45:26 +01003798 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003799 free_tv(qfl->qf_ctx);
3800 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003801 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003802 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003803}
3804
3805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 * qf_mark_adjust: adjust marks
3807 */
3808 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003809qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003810 win_T *wp,
3811 linenr_T line1,
3812 linenr_T line2,
3813 long amount,
3814 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003816 int i;
3817 qfline_T *qfp;
3818 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003819 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003820 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003821 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
Bram Moolenaarc1542742016-07-20 21:44:37 +02003823 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003824 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003825 if (wp != NULL)
3826 {
3827 if (wp->w_llist == NULL)
3828 return;
3829 qi = wp->w_llist;
3830 }
3831
3832 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003833 {
3834 qf_list_T *qfl = qf_get_list(qi, idx);
3835
3836 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003837 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 if (qfp->qf_fnum == curbuf->b_fnum)
3839 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003840 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3842 {
3843 if (amount == MAXLNUM)
3844 qfp->qf_cleared = TRUE;
3845 else
3846 qfp->qf_lnum += amount;
3847 }
3848 else if (amount_after && qfp->qf_lnum > line2)
3849 qfp->qf_lnum += amount_after;
3850 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003851 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003852
3853 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003854 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855}
3856
3857/*
3858 * Make a nice message out of the error character and the error number:
3859 * char number message
3860 * e or E 0 " error"
3861 * w or W 0 " warning"
3862 * i or I 0 " info"
3863 * 0 0 ""
3864 * other 0 " c"
3865 * e or E n " error n"
3866 * w or W n " warning n"
3867 * i or I n " info n"
3868 * 0 n " error n"
3869 * other n " c n"
3870 * 1 x "" :helpgrep
3871 */
3872 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003873qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874{
3875 static char_u buf[20];
3876 static char_u cc[3];
3877 char_u *p;
3878
3879 if (c == 'W' || c == 'w')
3880 p = (char_u *)" warning";
3881 else if (c == 'I' || c == 'i')
3882 p = (char_u *)" info";
3883 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3884 p = (char_u *)" error";
3885 else if (c == 0 || c == 1)
3886 p = (char_u *)"";
3887 else
3888 {
3889 cc[0] = ' ';
3890 cc[1] = c;
3891 cc[2] = NUL;
3892 p = cc;
3893 }
3894
3895 if (nr <= 0)
3896 return p;
3897
3898 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3899 return buf;
3900}
3901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003903 * When "split" is FALSE: Open the entry/result under the cursor.
3904 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3905 */
3906 void
3907qf_view_result(int split)
3908{
3909 qf_info_T *qi = &ql_info;
3910
3911 if (!bt_quickfix(curbuf))
3912 return;
3913
3914 if (IS_LL_WINDOW(curwin))
3915 qi = GET_LOC_LIST(curwin);
3916
Bram Moolenaar0398e002019-03-21 21:12:49 +01003917 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003919 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003920 return;
3921 }
3922
3923 if (split)
3924 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003925 // Open the selected entry in a new window
3926 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3927 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003928 return;
3929 }
3930
3931 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3932}
3933
3934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 * ":cwindow": open the quickfix window if we have errors to display,
3936 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003937 * ":lwindow": open the location list window if we have locations to display,
3938 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 */
3940 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003941ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003943 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003944 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 win_T *win;
3946
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003947 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3948 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003949
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003950 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003951
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003952 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003953 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003955 // If a quickfix window is open but we have no errors to display,
3956 // close the window. If a quickfix window is not open, then open
3957 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003958 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003959 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01003960 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
3962 if (win != NULL)
3963 ex_cclose(eap);
3964 }
3965 else if (win == NULL)
3966 ex_copen(eap);
3967}
3968
3969/*
3970 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003971 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003974ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003976 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003977 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003979 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
3980 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003982 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003983 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 if (win != NULL)
3985 win_close(win, FALSE);
3986}
3987
3988/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003989 * Set "w:quickfix_title" if "qi" has a title.
3990 */
3991 static void
3992qf_set_title_var(qf_list_T *qfl)
3993{
3994 if (qfl->qf_title != NULL)
3995 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3996}
3997
3998/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003999 * Goto a quickfix or location list window (if present).
4000 * Returns OK if the window is found, FAIL otherwise.
4001 */
4002 static int
4003qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4004{
4005 win_T *win;
4006
4007 win = qf_find_win(qi);
4008 if (win == NULL)
4009 return FAIL;
4010
4011 win_goto(win);
4012 if (resize)
4013 {
4014 if (vertsplit)
4015 {
4016 if (sz != win->w_width)
4017 win_setwidth(sz);
4018 }
4019 else if (sz != win->w_height)
4020 win_setheight(sz);
4021 }
4022
4023 return OK;
4024}
4025
4026/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004027 * Set options for the buffer in the quickfix or location list window.
4028 */
4029 static void
4030qf_set_cwindow_options(void)
4031{
4032 // switch off 'swapfile'
4033 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4034 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4035 OPT_LOCAL);
4036 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4037 RESET_BINDING(curwin);
4038#ifdef FEAT_DIFF
4039 curwin->w_p_diff = FALSE;
4040#endif
4041#ifdef FEAT_FOLDING
4042 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4043 OPT_LOCAL);
4044#endif
4045}
4046
4047/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004048 * Open a new quickfix or location list window, load the quickfix buffer and
4049 * set the appropriate options for the window.
4050 * Returns FAIL if the window could not be opened.
4051 */
4052 static int
4053qf_open_new_cwindow(qf_info_T *qi, int height)
4054{
4055 buf_T *qf_buf;
4056 win_T *oldwin = curwin;
4057 tabpage_T *prevtab = curtab;
4058 int flags = 0;
4059 win_T *win;
4060
4061 qf_buf = qf_find_buf(qi);
4062
4063 // The current window becomes the previous window afterwards.
4064 win = curwin;
4065
4066 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4067 // Create the new quickfix window at the very bottom, except when
4068 // :belowright or :aboveleft is used.
4069 win_goto(lastwin);
4070 // Default is to open the window below the current window
4071 if (cmdmod.split == 0)
4072 flags = WSP_BELOW;
4073 flags |= WSP_NEWLOC;
4074 if (win_split(height, flags) == FAIL)
4075 return FAIL; // not enough room for window
4076 RESET_BINDING(curwin);
4077
4078 if (IS_LL_STACK(qi))
4079 {
4080 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004081 // location list stack from the window 'win'.
4082 curwin->w_llist_ref = qi;
4083 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004084 }
4085
4086 if (oldwin != curwin)
4087 oldwin = NULL; // don't store info when in another window
4088 if (qf_buf != NULL)
4089 {
4090 // Use the existing quickfix buffer
4091 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4092 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4093 }
4094 else
4095 {
4096 // Create a new quickfix buffer
4097 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4098
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004099 // save the number of the new buffer
4100 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004101 }
4102
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004103 // Set the options for the quickfix buffer/window (if not already done)
4104 // Do this even if the quickfix buffer was already present, as an autocmd
4105 // might have previously deleted (:bdelete) the quickfix buffer.
4106 if (curbuf->b_p_bt[0] != 'q')
4107 qf_set_cwindow_options();
4108
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004109 // Only set the height when still in the same tab page and there is no
4110 // window to the side.
4111 if (curtab == prevtab && curwin->w_width == Columns)
4112 win_setheight(height);
4113 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4114 if (win_valid(win))
4115 prevwin = win;
4116
4117 return OK;
4118}
4119
4120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004122 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 */
4124 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004125ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004127 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004128 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004130 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004131 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004133 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4134 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004135
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004136 incr_quickfix_busy();
4137
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if (eap->addr_count != 0)
4139 height = eap->line2;
4140 else
4141 height = QF_WINHEIGHT;
4142
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004143 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144#ifdef FEAT_GUI
4145 need_mouse_correct = TRUE;
4146#endif
4147
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004148 // Find an existing quickfix window, or open a new one.
4149 if (cmdmod.tab == 0)
4150 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4151 cmdmod.split & WSP_VERT);
4152 if (status == FAIL)
4153 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004154 {
4155 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004156 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004159 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004160 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004161 // Save the current index here, as updating the quickfix buffer may free
4162 // the quickfix list
4163 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004164
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004165 // Fill the buffer with the quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004166 qf_fill_buffer(qfl, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004168 decr_quickfix_busy();
4169
4170 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 curwin->w_cursor.col = 0;
4172 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004173 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174}
4175
4176/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004177 * Move the cursor in the quickfix window to "lnum".
4178 */
4179 static void
4180qf_win_goto(win_T *win, linenr_T lnum)
4181{
4182 win_T *old_curwin = curwin;
4183
4184 curwin = win;
4185 curbuf = win->w_buffer;
4186 curwin->w_cursor.lnum = lnum;
4187 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004188 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004189 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004190 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004191 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004192 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004193 curwin = old_curwin;
4194 curbuf = curwin->w_buffer;
4195}
4196
4197/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004198 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004199 */
4200 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004201ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004202{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004203 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004204 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004205
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004206 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4207 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004208
4209 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004210 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4211 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4212}
4213
4214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 * Return the number of the current entry (line number in the quickfix
4216 * window).
4217 */
4218 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004219qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004221 qf_info_T *qi = &ql_info;
4222
4223 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004224 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004225 qi = wp->w_llist_ref;
4226
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004227 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228}
4229
4230/*
4231 * Update the cursor position in the quickfix window to the current error.
4232 * Return TRUE if there is a quickfix window.
4233 */
4234 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004235qf_win_pos_update(
4236 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004237 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
4239 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004240 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004242 // Put the cursor on the current error in the quickfix window, so that
4243 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004244 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 if (win != NULL
4246 && qf_index <= win->w_buffer->b_ml.ml_line_count
4247 && old_qf_index != qf_index)
4248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (qf_index > old_qf_index)
4250 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004251 win->w_redraw_top = old_qf_index;
4252 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 else
4255 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004256 win->w_redraw_top = qf_index;
4257 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004259 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261 return win != NULL;
4262}
4263
4264/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004265 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004266 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004267 */
4268 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004269is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004270{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004271 // A window displaying the quickfix buffer will have the w_llist_ref field
4272 // set to NULL.
4273 // A window displaying a location list buffer will have the w_llist_ref
4274 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004275 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004276 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4277 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004278 return TRUE;
4279
4280 return FALSE;
4281}
4282
4283/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004284 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004285 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004286 */
4287 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004288qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004289{
4290 win_T *win;
4291
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004292 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004293 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004294 return win;
4295 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004296}
4297
4298/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004299 * Find a quickfix buffer.
4300 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 */
4302 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004303qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004305 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004306 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004308 if (qi->qf_bufnr != INVALID_QFBUFNR)
4309 {
4310 buf_T *qfbuf;
4311 qfbuf = buflist_findnr(qi->qf_bufnr);
4312 if (qfbuf != NULL)
4313 return qfbuf;
4314 // buffer is no longer present
4315 qi->qf_bufnr = INVALID_QFBUFNR;
4316 }
4317
Bram Moolenaar9c102382006-05-03 21:26:49 +00004318 FOR_ALL_TAB_WINDOWS(tp, win)
4319 if (is_qf_win(win, qi))
4320 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004321
Bram Moolenaar9c102382006-05-03 21:26:49 +00004322 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323}
4324
4325/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004326 * Update the w:quickfix_title variable in the quickfix/location list window
4327 */
4328 static void
4329qf_update_win_titlevar(qf_info_T *qi)
4330{
4331 win_T *win;
4332 win_T *curwin_save;
4333
4334 if ((win = qf_find_win(qi)) != NULL)
4335 {
4336 curwin_save = curwin;
4337 curwin = win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004338 qf_set_title_var(qf_get_curlist(qi));
Bram Moolenaard823fa92016-08-12 16:29:27 +02004339 curwin = curwin_save;
4340 }
4341}
4342
4343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 * Find the quickfix buffer. If it exists, update the contents.
4345 */
4346 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004347qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348{
4349 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004350 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004353 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004354 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 if (buf != NULL)
4356 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004357 linenr_T old_line_count = buf->b_ml.ml_line_count;
4358
4359 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004360 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004361 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
Bram Moolenaard823fa92016-08-12 16:29:27 +02004363 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004364
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004365 qf_fill_buffer(qf_get_curlist(qi), buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004366 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004367
Bram Moolenaar864293a2016-06-02 13:40:04 +02004368 if (old_last == NULL)
4369 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004370 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004371
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004372 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004373 aucmd_restbuf(&aco);
4374 }
4375
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004376 // Only redraw when added lines are visible. This avoids flickering
4377 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004378 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4379 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 }
4381}
4382
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004383/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004384 * Add an error line to the quickfix buffer.
4385 */
4386 static int
4387qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4388{
4389 int len;
4390 buf_T *errbuf;
4391
4392 if (qfp->qf_module != NULL)
4393 {
4394 STRCPY(IObuff, qfp->qf_module);
4395 len = (int)STRLEN(IObuff);
4396 }
4397 else if (qfp->qf_fnum != 0
4398 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4399 && errbuf->b_fname != NULL)
4400 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004401 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004402 STRCPY(IObuff, gettail(errbuf->b_fname));
4403 else
4404 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004405 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004406 if (errbuf->b_sfname == NULL
4407 || mch_isFullName(errbuf->b_sfname))
4408 {
4409 if (*dirname == NUL)
4410 mch_dirname(dirname, MAXPATHL);
4411 shorten_buf_fname(errbuf, dirname, FALSE);
4412 }
4413 STRCPY(IObuff, errbuf->b_fname);
4414 }
4415 len = (int)STRLEN(IObuff);
4416 }
4417 else
4418 len = 0;
4419 IObuff[len++] = '|';
4420
4421 if (qfp->qf_lnum > 0)
4422 {
4423 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4424 len += (int)STRLEN(IObuff + len);
4425
4426 if (qfp->qf_col > 0)
4427 {
4428 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4429 len += (int)STRLEN(IObuff + len);
4430 }
4431
4432 sprintf((char *)IObuff + len, "%s",
4433 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4434 len += (int)STRLEN(IObuff + len);
4435 }
4436 else if (qfp->qf_pattern != NULL)
4437 {
4438 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4439 len += (int)STRLEN(IObuff + len);
4440 }
4441 IObuff[len++] = '|';
4442 IObuff[len++] = ' ';
4443
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004444 // Remove newlines and leading whitespace from the text.
4445 // For an unrecognized line keep the indent, the compiler may
4446 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004447 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4448 IObuff + len, IOSIZE - len);
4449
4450 if (ml_append_buf(buf, lnum, IObuff,
4451 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4452 return FAIL;
4453
4454 return OK;
4455}
4456
4457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 * Fill current buffer with quickfix errors, replacing any previous contents.
4459 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004460 * If "old_last" is not NULL append the items after this one.
4461 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4462 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 */
4464 static void
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004465qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004467 linenr_T lnum;
4468 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004469 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
Bram Moolenaar864293a2016-06-02 13:40:04 +02004471 if (old_last == NULL)
4472 {
4473 if (buf != curbuf)
4474 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004475 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004476 return;
4477 }
4478
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004479 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004480 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4481 (void)ml_delete((linenr_T)1, FALSE);
4482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004484 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004485 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004487 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004488
4489 *dirname = NUL;
4490
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004491 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004492 if (old_last == NULL)
4493 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004494 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004495 lnum = 0;
4496 }
4497 else
4498 {
4499 qfp = old_last->qf_next;
4500 lnum = buf->b_ml.ml_line_count;
4501 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004502 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004504 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004506
Bram Moolenaar864293a2016-06-02 13:40:04 +02004507 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004509 if (qfp == NULL)
4510 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004512
4513 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004514 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004515 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004518 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 check_lnums(TRUE);
4520
Bram Moolenaar864293a2016-06-02 13:40:04 +02004521 if (old_last == NULL)
4522 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004523 // Set the 'filetype' to "qf" each time after filling the buffer.
4524 // This resembles reading a file into a buffer, it's more logical when
4525 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004526 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004527 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4528 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004530 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004531 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004533 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004535 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004536 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004537
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004538 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004539 redraw_curbuf_later(NOT_VALID);
4540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004542 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 KeyTyped = old_KeyTyped;
4544}
4545
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004546/*
4547 * For every change made to the quickfix list, update the changed tick.
4548 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004549 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004550qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004551{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004552 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004553}
4554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004556 * Return the quickfix/location list number with the given identifier.
4557 * Returns -1 if list is not found.
4558 */
4559 static int
4560qf_id2nr(qf_info_T *qi, int_u qfid)
4561{
4562 int qf_idx;
4563
4564 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4565 if (qi->qf_lists[qf_idx].qf_id == qfid)
4566 return qf_idx;
4567 return INVALID_QFIDX;
4568}
4569
4570/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004571 * If the current list is not "save_qfid" and we can find the list with that ID
4572 * then make it the current list.
4573 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004574 * Returns OK if successfully restored the list. Returns FAIL if the list with
4575 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004576 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004577 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004578qf_restore_list(qf_info_T *qi, int_u save_qfid)
4579{
4580 int curlist;
4581
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004582 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004583 {
4584 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004585 if (curlist < 0)
4586 // list is not present
4587 return FAIL;
4588 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004589 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004590 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004591}
4592
4593/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004594 * Jump to the first entry if there is one.
4595 */
4596 static void
4597qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4598{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004599 if (qf_restore_list(qi, save_qfid) == FAIL)
4600 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004601
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004602 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004603 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004604 qf_jump(qi, 0, 0, forceit);
4605}
4606
4607/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004608 * Return TRUE when using ":vimgrep" for ":grep".
4609 */
4610 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004611grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004612{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004613 return ((cmdidx == CMD_grep
4614 || cmdidx == CMD_lgrep
4615 || cmdidx == CMD_grepadd
4616 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004617 && STRCMP("internal",
4618 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4619}
4620
4621/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004622 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004624 static char_u *
4625make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004627 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004628 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004629 case CMD_make: return (char_u *)"make";
4630 case CMD_lmake: return (char_u *)"lmake";
4631 case CMD_grep: return (char_u *)"grep";
4632 case CMD_lgrep: return (char_u *)"lgrep";
4633 case CMD_grepadd: return (char_u *)"grepadd";
4634 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4635 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637}
4638
4639/*
4640 * Return the name for the errorfile, in allocated memory.
4641 * Find a new unique name when 'makeef' contains "##".
4642 * Returns NULL for error.
4643 */
4644 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004645get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646{
4647 char_u *p;
4648 char_u *name;
4649 static int start = -1;
4650 static int off = 0;
4651#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004652 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653#endif
4654
4655 if (*p_mef == NUL)
4656 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004657 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004659 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 return name;
4661 }
4662
4663 for (p = p_mef; *p; ++p)
4664 if (p[0] == '#' && p[1] == '#')
4665 break;
4666
4667 if (*p == NUL)
4668 return vim_strsave(p_mef);
4669
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004670 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 for (;;)
4672 {
4673 if (start == -1)
4674 start = mch_get_pid();
4675 else
4676 off += 19;
4677
4678 name = alloc((unsigned)STRLEN(p_mef) + 30);
4679 if (name == NULL)
4680 break;
4681 STRCPY(name, p_mef);
4682 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4683 STRCAT(name, p + 2);
4684 if (mch_getperm(name) < 0
4685#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004686 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 && mch_lstat((char *)name, &sb) < 0
4688#endif
4689 )
4690 break;
4691 vim_free(name);
4692 }
4693 return name;
4694}
4695
4696/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004697 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4698 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4699 */
4700 static char_u *
4701make_get_fullcmd(char_u *makecmd, char_u *fname)
4702{
4703 char_u *cmd;
4704 unsigned len;
4705
4706 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4707 if (*p_sp != NUL)
4708 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4709 cmd = alloc(len);
4710 if (cmd == NULL)
4711 return NULL;
4712 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4713 (char *)p_shq);
4714
4715 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4716 if (*p_sp != NUL)
4717 append_redir(cmd, len, p_sp, fname);
4718
4719 // Display the fully formed command. Output a newline if there's something
4720 // else than the :make command that was typed (in which case the cursor is
4721 // in column 0).
4722 if (msg_col == 0)
4723 msg_didout = FALSE;
4724 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004725 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004726 msg_outtrans(cmd); // show what we are doing
4727
4728 return cmd;
4729}
4730
4731/*
4732 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4733 */
4734 void
4735ex_make(exarg_T *eap)
4736{
4737 char_u *fname;
4738 char_u *cmd;
4739 char_u *enc = NULL;
4740 win_T *wp = NULL;
4741 qf_info_T *qi = &ql_info;
4742 int res;
4743 char_u *au_name = NULL;
4744 int_u save_qfid;
4745
4746 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4747 if (grep_internal(eap->cmdidx))
4748 {
4749 ex_vimgrep(eap);
4750 return;
4751 }
4752
4753 au_name = make_get_auname(eap->cmdidx);
4754 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4755 curbuf->b_fname, TRUE, curbuf))
4756 {
4757#ifdef FEAT_EVAL
4758 if (aborting())
4759 return;
4760#endif
4761 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004762 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004763
4764 if (is_loclist_cmd(eap->cmdidx))
4765 wp = curwin;
4766
4767 autowrite_all();
4768 fname = get_mef_name();
4769 if (fname == NULL)
4770 return;
4771 mch_remove(fname); // in case it's not unique
4772
4773 cmd = make_get_fullcmd(eap->arg, fname);
4774 if (cmd == NULL)
4775 return;
4776
4777 // let the shell know if we are redirecting output or not
4778 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4779
4780#ifdef AMIGA
4781 out_flush();
4782 // read window status report and redraw before message
4783 (void)char_avail();
4784#endif
4785
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004786 incr_quickfix_busy();
4787
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004788 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4789 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4790 (eap->cmdidx != CMD_grepadd
4791 && eap->cmdidx != CMD_lgrepadd),
4792 qf_cmdtitle(*eap->cmdlinep), enc);
4793 if (wp != NULL)
4794 {
4795 qi = GET_LOC_LIST(wp);
4796 if (qi == NULL)
4797 goto cleanup;
4798 }
4799 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004800 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004801
4802 // Remember the current quickfix list identifier, so that we can
4803 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004804 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004805 if (au_name != NULL)
4806 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4807 curbuf->b_fname, TRUE, curbuf);
4808 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4809 // display the first error
4810 qf_jump_first(qi, save_qfid, FALSE);
4811
4812cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004813 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004814 mch_remove(fname);
4815 vim_free(fname);
4816 vim_free(cmd);
4817}
4818
4819/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004820 * Returns the number of valid entries in the current quickfix/location list.
4821 */
4822 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004823qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004824{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004825 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004826 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004827 qfline_T *qfp;
4828 int i, sz = 0;
4829 int prev_fnum = 0;
4830
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004831 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4832 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004833
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004834 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01004835 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004836 {
4837 if (qfp->qf_valid)
4838 {
4839 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004840 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004841 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4842 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004843 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004844 sz++;
4845 prev_fnum = qfp->qf_fnum;
4846 }
4847 }
4848 }
4849
4850 return sz;
4851}
4852
4853/*
4854 * Returns the current index of the quickfix/location list.
4855 * Returns 0 if there is an error.
4856 */
4857 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004858qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004859{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004860 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004861
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004862 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4863 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004864
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004865 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004866}
4867
4868/*
4869 * Returns the current index in the quickfix/location list (counting only valid
4870 * entries). If no valid entries are in the list, then returns 1.
4871 */
4872 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004873qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004874{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004875 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004876 qf_list_T *qfl;
4877 qfline_T *qfp;
4878 int i, eidx = 0;
4879 int prev_fnum = 0;
4880
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004881 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4882 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004883
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004884 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004885 qfp = qfl->qf_start;
4886
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004887 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004888 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4889 return 1;
4890
4891 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4892 {
4893 if (qfp->qf_valid)
4894 {
4895 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4896 {
4897 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4898 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004899 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004900 eidx++;
4901 prev_fnum = qfp->qf_fnum;
4902 }
4903 }
4904 else
4905 eidx++;
4906 }
4907 }
4908
4909 return eidx ? eidx : 1;
4910}
4911
4912/*
4913 * Get the 'n'th valid error entry in the quickfix or location list.
4914 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4915 * For :cdo and :ldo returns the 'n'th valid error entry.
4916 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4917 */
4918 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004919qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004920{
Bram Moolenaar95946f12019-03-31 15:31:59 +02004921 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004922 int i, eidx;
4923 int prev_fnum = 0;
4924
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004925 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004926 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4927 return 1;
4928
Bram Moolenaar95946f12019-03-31 15:31:59 +02004929 eidx = 0;
4930 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004931 {
4932 if (qfp->qf_valid)
4933 {
4934 if (fdo)
4935 {
4936 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4937 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004938 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004939 eidx++;
4940 prev_fnum = qfp->qf_fnum;
4941 }
4942 }
4943 else
4944 eidx++;
4945 }
4946
4947 if (eidx == n)
4948 break;
4949 }
4950
4951 if (i <= qfl->qf_count)
4952 return i;
4953 else
4954 return 1;
4955}
4956
4957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004959 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004960 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 */
4962 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004963ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004965 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004966 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004967
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004968 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4969 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004970
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004971 if (eap->addr_count > 0)
4972 errornr = (int)eap->line2;
4973 else
4974 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004975 switch (eap->cmdidx)
4976 {
4977 case CMD_cc: case CMD_ll:
4978 errornr = 0;
4979 break;
4980 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4981 case CMD_lfirst:
4982 errornr = 1;
4983 break;
4984 default:
4985 errornr = 32767;
4986 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004987 }
4988
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004989 // For cdo and ldo commands, jump to the nth valid error.
4990 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004991 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4992 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004993 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004994 eap->addr_count > 0 ? (int)eap->line1 : 1,
4995 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4996
4997 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998}
4999
5000/*
5001 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005002 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005003 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 */
5005 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005006ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005008 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005009 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005010 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005011
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005012 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5013 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005014
Bram Moolenaar55b69262017-08-13 13:42:01 +02005015 if (eap->addr_count > 0
5016 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5017 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005018 errornr = (int)eap->line2;
5019 else
5020 errornr = 1;
5021
Bram Moolenaar39665952018-08-15 20:59:48 +02005022 // Depending on the command jump to either next or previous entry/file.
5023 switch (eap->cmdidx)
5024 {
5025 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5026 dir = FORWARD;
5027 break;
5028 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5029 case CMD_lNext:
5030 dir = BACKWARD;
5031 break;
5032 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5033 dir = FORWARD_FILE;
5034 break;
5035 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5036 dir = BACKWARD_FILE;
5037 break;
5038 default:
5039 dir = FORWARD;
5040 break;
5041 }
5042
5043 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044}
5045
5046/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005047 * Return the autocmd name for the :cfile Ex commands
5048 */
5049 static char_u *
5050cfile_get_auname(cmdidx_T cmdidx)
5051{
5052 switch (cmdidx)
5053 {
5054 case CMD_cfile: return (char_u *)"cfile";
5055 case CMD_cgetfile: return (char_u *)"cgetfile";
5056 case CMD_caddfile: return (char_u *)"caddfile";
5057 case CMD_lfile: return (char_u *)"lfile";
5058 case CMD_lgetfile: return (char_u *)"lgetfile";
5059 case CMD_laddfile: return (char_u *)"laddfile";
5060 default: return NULL;
5061 }
5062}
5063
5064/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005065 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005066 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
5068 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005069ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005071 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005072 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005073 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005074 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005075 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005076 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005077
Bram Moolenaara16123a2019-03-28 20:31:07 +01005078 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005079 if (au_name != NULL)
5080 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005081
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005082 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005083#ifdef FEAT_BROWSE
5084 if (cmdmod.browse)
5085 {
5086 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005087 NULL, NULL,
5088 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005089 if (browse_file == NULL)
5090 return;
5091 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5092 vim_free(browse_file);
5093 }
5094 else
5095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005097 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005098
Bram Moolenaar39665952018-08-15 20:59:48 +02005099 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005100 wp = curwin;
5101
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005102 incr_quickfix_busy();
5103
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005104 // This function is used by the :cfile, :cgetfile and :caddfile
5105 // commands.
5106 // :cfile always creates a new quickfix list and jumps to the
5107 // first error.
5108 // :cgetfile creates a new quickfix list but doesn't jump to the
5109 // first error.
5110 // :caddfile adds to an existing quickfix list. If there is no
5111 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005112 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005113 && eap->cmdidx != CMD_laddfile),
5114 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005115 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005116 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005117 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005118 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005119 {
5120 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005121 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005122 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005123 }
5124 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005125 qf_list_changed(qf_get_curlist(qi));
5126 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005127 if (au_name != NULL)
5128 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005129
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005130 // Jump to the first error for a new list and if autocmds didn't
5131 // free the list.
5132 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5133 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005134 // display the first error
5135 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005136
5137 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005138}
5139
5140/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005141 * Return the vimgrep autocmd name.
5142 */
5143 static char_u *
5144vgr_get_auname(cmdidx_T cmdidx)
5145{
5146 switch (cmdidx)
5147 {
5148 case CMD_vimgrep: return (char_u *)"vimgrep";
5149 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5150 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5151 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5152 case CMD_grep: return (char_u *)"grep";
5153 case CMD_lgrep: return (char_u *)"lgrep";
5154 case CMD_grepadd: return (char_u *)"grepadd";
5155 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5156 default: return NULL;
5157 }
5158}
5159
5160/*
5161 * Initialize the regmatch used by vimgrep for pattern "s".
5162 */
5163 static void
5164vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5165{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005166 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005167 regmatch->regprog = NULL;
5168
5169 if (s == NULL || *s == NUL)
5170 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005171 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005172 if (last_search_pat() == NULL)
5173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005174 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005175 return;
5176 }
5177 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5178 }
5179 else
5180 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5181
5182 regmatch->rmm_ic = p_ic;
5183 regmatch->rmm_maxcol = 0;
5184}
5185
5186/*
5187 * Display a file name when vimgrep is running.
5188 */
5189 static void
5190vgr_display_fname(char_u *fname)
5191{
5192 char_u *p;
5193
5194 msg_start();
5195 p = msg_strtrunc(fname, TRUE);
5196 if (p == NULL)
5197 msg_outtrans(fname);
5198 else
5199 {
5200 msg_outtrans(p);
5201 vim_free(p);
5202 }
5203 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005204 msg_didout = FALSE; // overwrite this message
5205 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005206 msg_col = 0;
5207 out_flush();
5208}
5209
5210/*
5211 * Load a dummy buffer to search for a pattern using vimgrep.
5212 */
5213 static buf_T *
5214vgr_load_dummy_buf(
5215 char_u *fname,
5216 char_u *dirname_start,
5217 char_u *dirname_now)
5218{
5219 int save_mls;
5220#if defined(FEAT_SYN_HL)
5221 char_u *save_ei = NULL;
5222#endif
5223 buf_T *buf;
5224
5225#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005226 // Don't do Filetype autocommands to avoid loading syntax and
5227 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005228 save_ei = au_event_disable(",Filetype");
5229#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005230 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005231 save_mls = p_mls;
5232 p_mls = 0;
5233
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005234 // Load file into a buffer, so that 'fileencoding' is detected,
5235 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005236 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5237
5238 p_mls = save_mls;
5239#if defined(FEAT_SYN_HL)
5240 au_event_restore(save_ei);
5241#endif
5242
5243 return buf;
5244}
5245
5246/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005247 * Check whether a quickfix/location list is valid. Autocmds may remove or
5248 * change a quickfix list when vimgrep is running. If the list is not found,
5249 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005250 */
5251 static int
5252vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005253 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005254 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005255 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005256 char_u *title)
5257{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005258 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005259 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005260 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005261 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005262 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005263 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005264 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005265 return FALSE;
5266 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005267 else
5268 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005269 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005270 qf_new_list(qi, title);
5271 return TRUE;
5272 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005273 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005274
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005275 if (qf_restore_list(qi, qfid) == FAIL)
5276 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005277
5278 return TRUE;
5279}
5280
5281/*
5282 * Search for a pattern in all the lines in a buffer and add the matching lines
5283 * to a quickfix list.
5284 */
5285 static int
5286vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005287 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005288 char_u *fname,
5289 buf_T *buf,
5290 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005291 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005292 int duplicate_name,
5293 int flags)
5294{
5295 int found_match = FALSE;
5296 long lnum;
5297 colnr_T col;
5298
Bram Moolenaar1c299432018-10-28 14:36:09 +01005299 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005300 {
5301 col = 0;
5302 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5303 col, NULL, NULL) > 0)
5304 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005305 // Pass the buffer number so that it gets used even for a
5306 // dummy buffer, unless duplicate_name is set, then the
5307 // buffer will be wiped out below.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005308 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005309 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005310 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005311 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005312 duplicate_name ? 0 : buf->b_fnum,
5313 ml_get_buf(buf,
5314 regmatch->startpos[0].lnum + lnum, FALSE),
5315 regmatch->startpos[0].lnum + lnum,
5316 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005317 FALSE, // vis_col
5318 NULL, // search pattern
5319 0, // nr
5320 0, // type
5321 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02005322 ) == QF_FAIL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005323 {
5324 got_int = TRUE;
5325 break;
5326 }
5327 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005328 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005329 break;
5330 if ((flags & VGR_GLOBAL) == 0
5331 || regmatch->endpos[0].lnum > 0)
5332 break;
5333 col = regmatch->endpos[0].col
5334 + (col == regmatch->endpos[0].col);
5335 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5336 break;
5337 }
5338 line_breakcheck();
5339 if (got_int)
5340 break;
5341 }
5342
5343 return found_match;
5344}
5345
5346/*
5347 * Jump to the first match and update the directory.
5348 */
5349 static void
5350vgr_jump_to_match(
5351 qf_info_T *qi,
5352 int forceit,
5353 int *redraw_for_dummy,
5354 buf_T *first_match_buf,
5355 char_u *target_dir)
5356{
5357 buf_T *buf;
5358
5359 buf = curbuf;
5360 qf_jump(qi, 0, 0, forceit);
5361 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005362 // If we jumped to another buffer redrawing will already be
5363 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005364 *redraw_for_dummy = FALSE;
5365
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005366 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005367 if (curbuf == first_match_buf && target_dir != NULL)
5368 {
5369 exarg_T ea;
5370
5371 ea.arg = target_dir;
5372 ea.cmdidx = CMD_lcd;
5373 ex_cd(&ea);
5374 }
5375}
5376
5377/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005378 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005379 * ":vimgrepadd {pattern} file(s)"
5380 * ":lvimgrep {pattern} file(s)"
5381 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005382 */
5383 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005384ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005385{
Bram Moolenaar81695252004-12-29 20:58:21 +00005386 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005387 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005388 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005389 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005390 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005391 char_u *s;
5392 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005393 int fi;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005394 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005395 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005396 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005397 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005398 buf_T *buf;
5399 int duplicate_name = FALSE;
5400 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005401 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005402 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005403 buf_T *first_match_buf = NULL;
5404 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005405 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005406 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005407 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005408 char_u *dirname_start = NULL;
5409 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005410 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005411 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005412
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005413 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005414 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5415 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005416 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005417#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005418 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005419 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005420#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005421 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005422
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005423 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
5424 if (qi == NULL)
5425 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00005426
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005427 if (eap->addr_count > 0)
5428 tomatch = eap->line2;
5429 else
5430 tomatch = MAXLNUM;
5431
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005432 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005433 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005434 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005435 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005436 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005438 emsg(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005439 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005440 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005441
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005442 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005443 if (regmatch.regprog == NULL)
5444 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005445
5446 p = skipwhite(p);
5447 if (*p == NUL)
5448 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005449 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005450 goto theend;
5451 }
5452
Bram Moolenaar55b69262017-08-13 13:42:01 +02005453 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005454 && eap->cmdidx != CMD_vimgrepadd
5455 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005456 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005457 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005458 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005459
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005460 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005461 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005462 goto theend;
5463 if (fcount == 0)
5464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005465 emsg(_(e_nomatch));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005466 goto theend;
5467 }
5468
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005469 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5470 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005471 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005472 {
5473 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005474 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005475 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005476
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005477 // Remember the current directory, because a BufRead autocommand that does
5478 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005479 mch_dirname(dirname_start, MAXPATHL);
5480
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005481 incr_quickfix_busy();
5482
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005483 // Remember the current quickfix list identifier, so that we can check for
5484 // autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005485 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005486
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005487 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005488 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005489 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005490 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005491 if (time(NULL) > seconds)
5492 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005493 // Display the file name every second or so, show the user we are
5494 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005495 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005496 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005497 }
5498
Bram Moolenaar81695252004-12-29 20:58:21 +00005499 buf = buflist_findname_exp(fnames[fi]);
5500 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5501 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005502 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005503 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005504 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005505 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005506
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005507 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005508 }
5509 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005510 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005511 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005512
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005513 // Check whether the quickfix list is still valid. When loading a
5514 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005515 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005516 {
5517 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005518 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005519 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005520 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005521 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005522
Bram Moolenaar81695252004-12-29 20:58:21 +00005523 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005524 {
5525 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005526 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005527 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005528 else
5529 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005530 // Try for a match in all lines of the buffer.
5531 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005532 found_match = vgr_match_buflines(qf_get_curlist(qi),
5533 fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005534 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005535
Bram Moolenaar81695252004-12-29 20:58:21 +00005536 if (using_dummy)
5537 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005538 if (found_match && first_match_buf == NULL)
5539 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005540 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005541 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005542 // Never keep a dummy buffer if there is another buffer
5543 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005544 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005545 buf = NULL;
5546 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005547 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005548 || buf->b_p_bh[0] == 'u' // "unload"
5549 || buf->b_p_bh[0] == 'w' // "wipe"
5550 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005551 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005552 // When no match was found we don't need to remember the
5553 // buffer, wipe it out. If there was a match and it
5554 // wasn't the first one or we won't jump there: only
5555 // unload the buffer.
5556 // Ignore 'hidden' here, because it may lead to having too
5557 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005558 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005559 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005560 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005561 buf = NULL;
5562 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005563 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005564 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005565 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005566 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005567 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005568 buf = NULL;
5569 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005570 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005571
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005572 if (buf != NULL)
5573 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005574 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005575 buf->b_flags &= ~BF_DUMMY;
5576
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005577 // If the buffer is still loaded we need to use the
5578 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005579 if (buf == first_match_buf
5580 && target_dir == NULL
5581 && STRCMP(dirname_start, dirname_now) != 0)
5582 target_dir = vim_strsave(dirname_now);
5583
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005584 // The buffer is still loaded, the Filetype autocommands
5585 // need to be done now, in that buffer. And the modelines
5586 // need to be done (again). But not the window-local
5587 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005588 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005589#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005590 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5591 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005592#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005593 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005594 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005595 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005596 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005597 }
5598 }
5599
5600 FreeWild(fcount, fnames);
5601
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005602 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005603 qfl->qf_nonevalid = FALSE;
5604 qfl->qf_ptr = qfl->qf_start;
5605 qfl->qf_index = 1;
5606 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005607
Bram Moolenaar864293a2016-06-02 13:40:04 +02005608 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005609
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005610 if (au_name != NULL)
5611 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5612 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005613 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5614 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005615 if (!qflist_valid(wp, save_qfid)
5616 || qf_restore_list(qi, save_qfid) == FAIL)
5617 {
5618 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005619 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005620 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005621
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005622 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01005623 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005624 {
5625 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005626 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5627 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005628 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005629 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005630 semsg(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005631
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005632 decr_quickfix_busy();
5633
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005634 // If we loaded a dummy buffer into the current window, the autocommands
5635 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005636 if (redraw_for_dummy)
5637 {
5638#ifdef FEAT_FOLDING
5639 foldUpdateAll(curwin);
5640#else
5641 redraw_later(NOT_VALID);
5642#endif
5643 }
5644
Bram Moolenaar86b68352004-12-27 21:59:20 +00005645theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005646 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005647 vim_free(dirname_now);
5648 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005649 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005650 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005651}
5652
5653/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005654 * Restore current working directory to "dirname_start" if they differ, taking
5655 * into account whether it is set locally or globally.
5656 */
5657 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005658restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005659{
5660 char_u *dirname_now = alloc(MAXPATHL);
5661
5662 if (NULL != dirname_now)
5663 {
5664 mch_dirname(dirname_now, MAXPATHL);
5665 if (STRCMP(dirname_start, dirname_now) != 0)
5666 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005667 // If the directory has changed, change it back by building up an
5668 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005669 exarg_T ea;
5670
5671 ea.arg = dirname_start;
5672 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5673 ex_cd(&ea);
5674 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005675 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005676 }
5677}
5678
5679/*
5680 * Load file "fname" into a dummy buffer and return the buffer pointer,
5681 * placing the directory resulting from the buffer load into the
5682 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5683 * prior to calling this function. Restores directory to "dirname_start" prior
5684 * to returning, if autocmds or the 'autochdir' option have changed it.
5685 *
5686 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5687 * or wipe_dummy_buffer() later!
5688 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005689 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005690 */
5691 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005692load_dummy_buffer(
5693 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005694 char_u *dirname_start, // in: old directory
5695 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005696{
5697 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005698 bufref_T newbufref;
5699 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005700 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005701 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005702 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005703
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005704 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005705 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5706 if (newbuf == NULL)
5707 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005708 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005709
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005710 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005711 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5712
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005713 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005714 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005715 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005716 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005717 ++newbuf->b_locked;
5718
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005719 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005720 aucmd_prepbuf(&aco, newbuf);
5721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005722 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005723 (void)setfname(curbuf, fname, NULL, FALSE);
5724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005725 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005726 check_need_swap(TRUE);
5727
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005728 // Remove the "dummy" flag, otherwise autocommands may not
5729 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005730 curbuf->b_flags &= ~BF_DUMMY;
5731
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005732 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005733 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005734 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005735 NULL, READ_NEW | READ_DUMMY);
5736 --newbuf->b_locked;
5737 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005738 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005739 && !(curbuf->b_flags & BF_NEW))
5740 {
5741 failed = FALSE;
5742 if (curbuf != newbuf)
5743 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005744 // Bloody autocommands changed the buffer! Can happen when
5745 // using netrw and editing a remote file. Use the current
5746 // buffer instead, delete the dummy one after restoring the
5747 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005748 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005749 newbuf = curbuf;
5750 }
5751 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005752
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005753 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005754 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005755 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5756 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005757
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005758 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5759 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005760 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005761 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005762
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005763 // When autocommands/'autochdir' option changed directory: go back.
5764 // Let the caller know what the resulting dir was first, in case it is
5765 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005766 mch_dirname(resulting_dir, MAXPATHL);
5767 restore_start_dir(dirname_start);
5768
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005769 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005770 return NULL;
5771 if (failed)
5772 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005773 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005774 return NULL;
5775 }
5776 return newbuf;
5777}
5778
5779/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005780 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5781 * directory to "dirname_start" prior to returning, if autocmds or the
5782 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005783 */
5784 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005785wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005786{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005787 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005788 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005789#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005790 cleanup_T cs;
5791
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005792 // Reset the error/interrupt/exception state here so that aborting()
5793 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5794 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005795 enter_cleanup(&cs);
5796#endif
5797
Bram Moolenaar81695252004-12-29 20:58:21 +00005798 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005799
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005800#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005801 // Restore the error/interrupt/exception state if not discarded by a
5802 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005803 leave_cleanup(&cs);
5804#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005805 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005806 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005807 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005808}
5809
5810/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005811 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5812 * directory to "dirname_start" prior to returning, if autocmds or the
5813 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005814 */
5815 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005816unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005817{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005818 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005819 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005820 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005821
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005822 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005823 restore_start_dir(dirname_start);
5824 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005825}
5826
Bram Moolenaar05159a02005-02-26 23:04:13 +00005827#if defined(FEAT_EVAL) || defined(PROTO)
5828/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005829 * Copy the specified quickfix entry items into a new dict and appened the dict
5830 * to 'list'. Returns OK on success.
5831 */
5832 static int
5833get_qfline_items(qfline_T *qfp, list_T *list)
5834{
5835 int bufnum;
5836 dict_T *dict;
5837 char_u buf[2];
5838
5839 // Handle entries with a non-existing buffer number.
5840 bufnum = qfp->qf_fnum;
5841 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5842 bufnum = 0;
5843
5844 if ((dict = dict_alloc()) == NULL)
5845 return FAIL;
5846 if (list_append_dict(list, dict) == FAIL)
5847 return FAIL;
5848
5849 buf[0] = qfp->qf_type;
5850 buf[1] = NUL;
5851 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5852 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5853 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5854 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5855 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5856 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5857 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5858 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5859 || dict_add_string(dict, "type", buf) == FAIL
5860 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
5861 return FAIL;
5862
5863 return OK;
5864}
5865
5866/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005867 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005868 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005869 */
5870 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005871get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005872{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005873 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01005874 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005875 qfline_T *qfp;
5876 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005877
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005878 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005879 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005880 qi = &ql_info;
5881 if (wp != NULL)
5882 {
5883 qi = GET_LOC_LIST(wp);
5884 if (qi == NULL)
5885 return FAIL;
5886 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005887 }
5888
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005889 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005890 qf_idx = qi->qf_curlist;
5891
Bram Moolenaar0398e002019-03-21 21:12:49 +01005892 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005893 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005894
Bram Moolenaar0398e002019-03-21 21:12:49 +01005895 qfl = qf_get_list(qi, qf_idx);
5896 if (qf_list_empty(qfl))
5897 return FAIL;
5898
Bram Moolenaara16123a2019-03-28 20:31:07 +01005899 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005900 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01005901 if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005902 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005903 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005904
Bram Moolenaar05159a02005-02-26 23:04:13 +00005905 return OK;
5906}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005907
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005908// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005909enum {
5910 QF_GETLIST_NONE = 0x0,
5911 QF_GETLIST_TITLE = 0x1,
5912 QF_GETLIST_ITEMS = 0x2,
5913 QF_GETLIST_NR = 0x4,
5914 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005915 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005916 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005917 QF_GETLIST_IDX = 0x40,
5918 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005919 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005920 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005921 QF_GETLIST_QFBUFNR = 0x400,
5922 QF_GETLIST_ALL = 0x7FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005923};
5924
5925/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005926 * Parse text from 'di' and return the quickfix list items.
5927 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005928 */
5929 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005930qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005931{
5932 int status = FAIL;
5933 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005934 char_u *errorformat = p_efm;
5935 dictitem_T *efm_di;
5936 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005937
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005938 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005939 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005940 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005941 // If errorformat is supplied then use it, otherwise use the 'efm'
5942 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005943 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5944 {
5945 if (efm_di->di_tv.v_type != VAR_STRING ||
5946 efm_di->di_tv.vval.v_string == NULL)
5947 return FAIL;
5948 errorformat = efm_di->di_tv.vval.v_string;
5949 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005950
Bram Moolenaar36538222017-09-02 19:51:44 +02005951 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005952 if (l == NULL)
5953 return FAIL;
5954
Bram Moolenaar2d67d302018-11-16 18:46:02 +01005955 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005956 if (qi != NULL)
5957 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005958 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005959 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5960 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005961 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005962 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005963 }
5964 free(qi);
5965 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005966 dict_add_list(retdict, "items", l);
5967 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005968 }
5969
5970 return status;
5971}
5972
5973/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005974 * Return the quickfix/location list window identifier in the current tabpage.
5975 */
5976 static int
5977qf_winid(qf_info_T *qi)
5978{
5979 win_T *win;
5980
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005981 // The quickfix window can be opened even if the quickfix list is not set
5982 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005983 if (qi == NULL)
5984 return 0;
5985 win = qf_find_win(qi);
5986 if (win != NULL)
5987 return win->w_id;
5988 return 0;
5989}
5990
5991/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005992 * Returns the number of the buffer displayed in the quickfix/location list
5993 * window. If there is no buffer associated with the list, then returns 0.
5994 */
5995 static int
5996qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
5997{
5998 return dict_add_number(retdict, "qfbufnr",
5999 (qi == NULL) ? 0 : qi->qf_bufnr);
6000}
6001
6002/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006003 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006004 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006005 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006006qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006007{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006008 int flags = QF_GETLIST_NONE;
6009
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006010 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006011 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006012 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006013 if (!loclist)
6014 // File window ID is applicable only to location list windows
6015 flags &= ~ QF_GETLIST_FILEWINID;
6016 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006017
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006018 if (dict_find(what, (char_u *)"title", -1) != NULL)
6019 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006020
Bram Moolenaara6d48492017-12-12 22:45:31 +01006021 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6022 flags |= QF_GETLIST_NR;
6023
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006024 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6025 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006026
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006027 if (dict_find(what, (char_u *)"context", -1) != NULL)
6028 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006029
Bram Moolenaara6d48492017-12-12 22:45:31 +01006030 if (dict_find(what, (char_u *)"id", -1) != NULL)
6031 flags |= QF_GETLIST_ID;
6032
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006033 if (dict_find(what, (char_u *)"items", -1) != NULL)
6034 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006035
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006036 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6037 flags |= QF_GETLIST_IDX;
6038
6039 if (dict_find(what, (char_u *)"size", -1) != NULL)
6040 flags |= QF_GETLIST_SIZE;
6041
Bram Moolenaarb254af32017-12-18 19:48:58 +01006042 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6043 flags |= QF_GETLIST_TICK;
6044
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006045 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6046 flags |= QF_GETLIST_FILEWINID;
6047
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006048 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6049 flags |= QF_GETLIST_QFBUFNR;
6050
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006051 return flags;
6052}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006053
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006054/*
6055 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6056 * If 'nr' and 'id' are not present in 'what' then return the current
6057 * quickfix list index.
6058 * If 'nr' is zero then return the current quickfix list index.
6059 * If 'nr' is '$' then return the last quickfix list index.
6060 * If 'id' is present then return the index of the quickfix list with that id.
6061 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6062 * Return -1, if quickfix list is not present or if the stack is empty.
6063 */
6064 static int
6065qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6066{
6067 int qf_idx;
6068 dictitem_T *di;
6069
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006070 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006071 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6072 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006073 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006074 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006075 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006076 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006077 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006078 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006079 qf_idx = di->di_tv.vval.v_number - 1;
6080 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006081 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006082 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006083 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006084 else if (di->di_tv.v_type == VAR_STRING
6085 && di->di_tv.vval.v_string != NULL
6086 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006087 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006088 qf_idx = qi->qf_listcount - 1;
6089 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006090 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006091 }
6092
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006093 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6094 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006095 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006096 if (di->di_tv.v_type == VAR_NUMBER)
6097 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006098 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006099 if (di->di_tv.vval.v_number != 0)
6100 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6101 }
6102 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006103 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006104 }
6105
6106 return qf_idx;
6107}
6108
6109/*
6110 * Return default values for quickfix list properties in retdict.
6111 */
6112 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006113qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006114{
6115 int status = OK;
6116
6117 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006118 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006119 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6120 {
6121 list_T *l = list_alloc();
6122 if (l != NULL)
6123 status = dict_add_list(retdict, "items", l);
6124 else
6125 status = FAIL;
6126 }
6127 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006128 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006129 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006130 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006131 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006132 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006133 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006134 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006135 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006136 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006137 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006138 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006139 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006140 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006141 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006142 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006143 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6144 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006145
6146 return status;
6147}
6148
6149/*
6150 * Return the quickfix list title as 'title' in retdict
6151 */
6152 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006153qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006154{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006155 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006156}
6157
6158/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006159 * Returns the identifier of the window used to display files from a location
6160 * list. If there is no associated window, then returns 0. Useful only when
6161 * called from a location list window.
6162 */
6163 static int
6164qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6165{
6166 int winid = 0;
6167
6168 if (wp != NULL && IS_LL_WINDOW(wp))
6169 {
6170 win_T *ll_wp = qf_find_win_with_loclist(qi);
6171 if (ll_wp != NULL)
6172 winid = ll_wp->w_id;
6173 }
6174
6175 return dict_add_number(retdict, "filewinid", winid);
6176}
6177
6178/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006179 * Return the quickfix list items/entries as 'items' in retdict
6180 */
6181 static int
6182qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6183{
6184 int status = OK;
6185 list_T *l = list_alloc();
6186 if (l != NULL)
6187 {
6188 (void)get_errorlist(qi, NULL, qf_idx, l);
6189 dict_add_list(retdict, "items", l);
6190 }
6191 else
6192 status = FAIL;
6193
6194 return status;
6195}
6196
6197/*
6198 * Return the quickfix list context (if any) as 'context' in retdict.
6199 */
6200 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006201qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006202{
6203 int status;
6204 dictitem_T *di;
6205
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006206 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006207 {
6208 di = dictitem_alloc((char_u *)"context");
6209 if (di != NULL)
6210 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006211 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006212 status = dict_add(retdict, di);
6213 if (status == FAIL)
6214 dictitem_free(di);
6215 }
6216 else
6217 status = FAIL;
6218 }
6219 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006220 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006221
6222 return status;
6223}
6224
6225/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006226 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006227 */
6228 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01006229qf_getprop_idx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006230{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006231 int curidx = qfl->qf_index;
6232 if (qf_list_empty(qfl))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006233 // For empty lists, current index is set to 0
6234 curidx = 0;
6235 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006236}
6237
6238/*
6239 * Return quickfix/location list details (title) as a
6240 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6241 * then current list is used. Otherwise the specified list is used.
6242 */
6243 int
6244qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6245{
6246 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006247 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006248 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006249 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006250 dictitem_T *di;
6251 int flags = QF_GETLIST_NONE;
6252
6253 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6254 return qf_get_list_from_lines(what, di, retdict);
6255
6256 if (wp != NULL)
6257 qi = GET_LOC_LIST(wp);
6258
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006259 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006260
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006261 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006262 qf_idx = qf_getprop_qfidx(qi, what);
6263
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006264 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006265 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006266 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006267
Bram Moolenaar0398e002019-03-21 21:12:49 +01006268 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006269
Bram Moolenaard823fa92016-08-12 16:29:27 +02006270 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006271 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006272 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006273 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006274 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006275 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006276 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006277 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006278 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006279 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006280 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006281 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006282 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar0398e002019-03-21 21:12:49 +01006283 status = qf_getprop_idx(qfl, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006284 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006285 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006286 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006287 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006288 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6289 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006290 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6291 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006292
Bram Moolenaard823fa92016-08-12 16:29:27 +02006293 return status;
6294}
6295
6296/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006297 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006298 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6299 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006300 */
6301 static int
6302qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01006303 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006304 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006305 int first_entry,
6306 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006307{
6308 static int did_bufnr_emsg;
6309 char_u *filename, *module, *pattern, *text, *type;
6310 int bufnum, valid, status, col, vcol, nr;
6311 long lnum;
6312
6313 if (first_entry)
6314 did_bufnr_emsg = FALSE;
6315
Bram Moolenaar8f667172018-12-14 15:38:31 +01006316 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6317 module = dict_get_string(d, (char_u *)"module", TRUE);
6318 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6319 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6320 col = (int)dict_get_number(d, (char_u *)"col");
6321 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6322 nr = (int)dict_get_number(d, (char_u *)"nr");
6323 type = dict_get_string(d, (char_u *)"type", TRUE);
6324 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6325 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006326 if (text == NULL)
6327 text = vim_strsave((char_u *)"");
6328
6329 valid = TRUE;
6330 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6331 valid = FALSE;
6332
6333 // Mark entries with non-existing buffer number as not valid. Give the
6334 // error message only once.
6335 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6336 {
6337 if (!did_bufnr_emsg)
6338 {
6339 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006340 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006341 }
6342 valid = FALSE;
6343 bufnum = 0;
6344 }
6345
6346 // If the 'valid' field is present it overrules the detected value.
6347 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006348 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006349
Bram Moolenaar0398e002019-03-21 21:12:49 +01006350 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006351 NULL, // dir
6352 filename,
6353 module,
6354 bufnum,
6355 text,
6356 lnum,
6357 col,
6358 vcol, // vis_col
6359 pattern, // search pattern
6360 nr,
6361 type == NULL ? NUL : *type,
6362 valid);
6363
6364 vim_free(filename);
6365 vim_free(module);
6366 vim_free(pattern);
6367 vim_free(text);
6368 vim_free(type);
6369
Bram Moolenaar9752c722018-12-22 16:49:34 +01006370 if (valid)
6371 *valid_entry = TRUE;
6372
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006373 return status;
6374}
6375
6376/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006377 * Add list of entries to quickfix/location list. Each list entry is
6378 * a dictionary with item information.
6379 */
6380 static int
6381qf_add_entries(
6382 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006383 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006384 list_T *list,
6385 char_u *title,
6386 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006387{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006388 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006389 listitem_T *li;
6390 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006391 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006392 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006393 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006394
Bram Moolenaara3921f42017-06-04 15:30:34 +02006395 if (action == ' ' || qf_idx == qi->qf_listcount)
6396 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006397 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006398 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006399 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006400 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006401 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01006402 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006403 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006404 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006405 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006406 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006407 qf_free_items(qfl);
6408 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006409 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006410
6411 for (li = list->lv_first; li != NULL; li = li->li_next)
6412 {
6413 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006414 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006415
6416 d = li->li_tv.vval.v_dict;
6417 if (d == NULL)
6418 continue;
6419
Bram Moolenaar0398e002019-03-21 21:12:49 +01006420 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006421 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02006422 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006423 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006424 }
6425
Bram Moolenaar9752c722018-12-22 16:49:34 +01006426 // Check if any valid error entries are added to the list.
6427 if (valid_entry)
6428 qfl->qf_nonevalid = FALSE;
6429 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006430 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006431 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006432
6433 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006434 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006435 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006436
6437 // Update the current error index if not appending to the list or if the
6438 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006439 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01006440 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006441
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006442 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006443 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006444
6445 return retval;
6446}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006447
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006448/*
6449 * Get the quickfix list index from 'nr' or 'id'
6450 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006451 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006452qf_setprop_get_qfidx(
6453 qf_info_T *qi,
6454 dict_T *what,
6455 int action,
6456 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006457{
6458 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006459 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006460
Bram Moolenaard823fa92016-08-12 16:29:27 +02006461 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6462 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006463 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006464 if (di->di_tv.v_type == VAR_NUMBER)
6465 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006466 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006467 if (di->di_tv.vval.v_number != 0)
6468 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006469
Bram Moolenaar55b69262017-08-13 13:42:01 +02006470 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6471 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006472 // When creating a new list, accept qf_idx pointing to the next
6473 // non-available list and add the new list at the end of the
6474 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006475 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006476 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006477 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006478 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006479 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006480 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006481 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006482 }
6483 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006484 && di->di_tv.vval.v_string != NULL
6485 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006486 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006487 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006488 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006489 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006490 qf_idx = 0;
6491 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006492 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006493 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006494 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006495 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006496 }
6497
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006498 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006499 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006500 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006501 if (di->di_tv.v_type != VAR_NUMBER)
6502 return INVALID_QFIDX;
6503
6504 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006505 }
6506
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006507 return qf_idx;
6508}
6509
6510/*
6511 * Set the quickfix list title.
6512 */
6513 static int
6514qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6515{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006516 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006517
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006518 if (di->di_tv.v_type != VAR_STRING)
6519 return FAIL;
6520
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006521 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006522 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006523 if (qf_idx == qi->qf_curlist)
6524 qf_update_win_titlevar(qi);
6525
6526 return OK;
6527}
6528
6529/*
6530 * Set quickfix list items/entries.
6531 */
6532 static int
6533qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6534{
6535 int retval = FAIL;
6536 char_u *title_save;
6537
6538 if (di->di_tv.v_type != VAR_LIST)
6539 return FAIL;
6540
6541 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6542 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6543 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006544 vim_free(title_save);
6545
6546 return retval;
6547}
6548
6549/*
6550 * Set quickfix list items/entries from a list of lines.
6551 */
6552 static int
6553qf_setprop_items_from_lines(
6554 qf_info_T *qi,
6555 int qf_idx,
6556 dict_T *what,
6557 dictitem_T *di,
6558 int action)
6559{
6560 char_u *errorformat = p_efm;
6561 dictitem_T *efm_di;
6562 int retval = FAIL;
6563
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006564 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006565 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6566 {
6567 if (efm_di->di_tv.v_type != VAR_STRING ||
6568 efm_di->di_tv.vval.v_string == NULL)
6569 return FAIL;
6570 errorformat = efm_di->di_tv.vval.v_string;
6571 }
6572
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006573 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006574 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6575 return FAIL;
6576
6577 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006578 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006579 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6580 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6581 retval = OK;
6582
6583 return retval;
6584}
6585
6586/*
6587 * Set quickfix list context.
6588 */
6589 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006590qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006591{
6592 typval_T *ctx;
6593
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006594 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006595 ctx = alloc_tv();
6596 if (ctx != NULL)
6597 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006598 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006599
6600 return OK;
6601}
6602
6603/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006604 * Set the current index in the specified quickfix list
6605 */
6606 static int
6607qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
6608{
6609 int denote = FALSE;
6610 int newidx;
6611 int old_qfidx;
6612 qfline_T *qf_ptr;
6613
6614 // If the specified index is '$', then use the last entry
6615 if (di->di_tv.v_type == VAR_STRING
6616 && di->di_tv.vval.v_string != NULL
6617 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
6618 newidx = qfl->qf_count;
6619 else
6620 {
6621 // Otherwise use the specified index
6622 newidx = tv_get_number_chk(&di->di_tv, &denote);
6623 if (denote)
6624 return FAIL;
6625 }
6626
6627 if (newidx < 1) // sanity check
6628 return FAIL;
6629 if (newidx > qfl->qf_count)
6630 newidx = qfl->qf_count;
6631
6632 old_qfidx = qfl->qf_index;
6633 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
6634 if (qf_ptr == NULL)
6635 return FAIL;
6636 qfl->qf_ptr = qf_ptr;
6637 qfl->qf_index = newidx;
6638
6639 // If the current list is modified and it is displayed in the quickfix
6640 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006641 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006642 qf_win_pos_update(qi, old_qfidx);
6643
6644 return OK;
6645}
6646
6647/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006648 * Set quickfix/location list properties (title, items, context).
6649 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006650 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006651 */
6652 static int
6653qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6654{
6655 dictitem_T *di;
6656 int retval = FAIL;
6657 int qf_idx;
6658 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006659 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006660
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006661 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006662 newlist = TRUE;
6663
6664 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006665 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006666 return FAIL;
6667
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006668 if (newlist)
6669 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006670 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006671 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006672 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006673 }
6674
Bram Moolenaar0398e002019-03-21 21:12:49 +01006675 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006676 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006677 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006678 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006679 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006680 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006681 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006682 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006683 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006684 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6685 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006686
Bram Moolenaarb254af32017-12-18 19:48:58 +01006687 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006688 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006689
Bram Moolenaard823fa92016-08-12 16:29:27 +02006690 return retval;
6691}
6692
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006693/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006694 * Free the entire quickfix/location list stack.
6695 * If the quickfix/location list window is open, then clear it.
6696 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006697 static void
6698qf_free_stack(win_T *wp, qf_info_T *qi)
6699{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006700 win_T *qfwin = qf_find_win(qi);
6701 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006702
6703 if (qfwin != NULL)
6704 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006705 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006706 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006707 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006708 qf_update_buffer(qi, NULL);
6709 }
6710
6711 if (wp != NULL && IS_LL_WINDOW(wp))
6712 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006713 // If in the location list window, then use the non-location list
6714 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006715 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006716 if (llwin != NULL)
6717 wp = llwin;
6718 }
6719
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006720 qf_free_all(wp);
6721 if (wp == NULL)
6722 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006723 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006724 qi->qf_curlist = 0;
6725 qi->qf_listcount = 0;
6726 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006727 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006728 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006729 // If the location list window is open, then create a new empty
6730 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006731 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006732
Bram Moolenaar95946f12019-03-31 15:31:59 +02006733 if (new_ll != NULL)
6734 {
6735 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006736
Bram Moolenaar95946f12019-03-31 15:31:59 +02006737 // first free the list reference in the location list window
6738 ll_free_all(&qfwin->w_llist_ref);
6739
6740 qfwin->w_llist_ref = new_ll;
6741 if (wp != qfwin)
6742 win_set_loclist(wp, new_ll);
6743 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006744 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006745}
6746
Bram Moolenaard823fa92016-08-12 16:29:27 +02006747/*
6748 * Populate the quickfix list with the items supplied in the list
6749 * of dictionaries. "title" will be copied to w:quickfix_title.
6750 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6751 */
6752 int
6753set_errorlist(
6754 win_T *wp,
6755 list_T *list,
6756 int action,
6757 char_u *title,
6758 dict_T *what)
6759{
6760 qf_info_T *qi = &ql_info;
6761 int retval = OK;
6762
6763 if (wp != NULL)
6764 {
6765 qi = ll_get_or_alloc_list(wp);
6766 if (qi == NULL)
6767 return FAIL;
6768 }
6769
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006770 if (action == 'f')
6771 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006772 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006773 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006774 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006775 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006776
6777 incr_quickfix_busy();
6778
6779 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006780 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006781 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006782 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006783 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006784 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006785 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01006786 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006787
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006788 decr_quickfix_busy();
6789
Bram Moolenaard823fa92016-08-12 16:29:27 +02006790 return retval;
6791}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006792
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006793/*
6794 * Mark the context as in use for all the lists in a quickfix stack.
6795 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006796 static int
6797mark_quickfix_ctx(qf_info_T *qi, int copyID)
6798{
6799 int i;
6800 int abort = FALSE;
6801 typval_T *ctx;
6802
6803 for (i = 0; i < LISTCOUNT && !abort; ++i)
6804 {
6805 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006806 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6807 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006808 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6809 }
6810
6811 return abort;
6812}
6813
6814/*
6815 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006816 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006817 */
6818 int
6819set_ref_in_quickfix(int copyID)
6820{
6821 int abort = FALSE;
6822 tabpage_T *tp;
6823 win_T *win;
6824
6825 abort = mark_quickfix_ctx(&ql_info, copyID);
6826 if (abort)
6827 return abort;
6828
6829 FOR_ALL_TAB_WINDOWS(tp, win)
6830 {
6831 if (win->w_llist != NULL)
6832 {
6833 abort = mark_quickfix_ctx(win->w_llist, copyID);
6834 if (abort)
6835 return abort;
6836 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006837 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6838 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006839 // In a location list window and none of the other windows is
6840 // referring to this location list. Mark the location list
6841 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006842 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6843 if (abort)
6844 return abort;
6845 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006846 }
6847
6848 return abort;
6849}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006850#endif
6851
Bram Moolenaar81695252004-12-29 20:58:21 +00006852/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01006853 * Return the autocmd name for the :cbuffer Ex commands
6854 */
6855 static char_u *
6856cbuffer_get_auname(cmdidx_T cmdidx)
6857{
6858 switch (cmdidx)
6859 {
6860 case CMD_cbuffer: return (char_u *)"cbuffer";
6861 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
6862 case CMD_caddbuffer: return (char_u *)"caddbuffer";
6863 case CMD_lbuffer: return (char_u *)"lbuffer";
6864 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
6865 case CMD_laddbuffer: return (char_u *)"laddbuffer";
6866 default: return NULL;
6867 }
6868}
6869
6870/*
6871 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
6872 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
6873 */
6874 static int
6875cbuffer_process_args(
6876 exarg_T *eap,
6877 buf_T **bufp,
6878 linenr_T *line1,
6879 linenr_T *line2)
6880{
6881 buf_T *buf = NULL;
6882
6883 if (*eap->arg == NUL)
6884 buf = curbuf;
6885 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6886 buf = buflist_findnr(atoi((char *)eap->arg));
6887
6888 if (buf == NULL)
6889 {
6890 emsg(_(e_invarg));
6891 return FAIL;
6892 }
6893
6894 if (buf->b_ml.ml_mfp == NULL)
6895 {
6896 emsg(_("E681: Buffer is not loaded"));
6897 return FAIL;
6898 }
6899
6900 if (eap->addr_count == 0)
6901 {
6902 eap->line1 = 1;
6903 eap->line2 = buf->b_ml.ml_line_count;
6904 }
6905
6906 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6907 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6908 {
6909 emsg(_(e_invrange));
6910 return FAIL;
6911 }
6912
6913 *line1 = eap->line1;
6914 *line2 = eap->line2;
6915 *bufp = buf;
6916
6917 return OK;
6918}
6919
6920/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006921 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006922 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006923 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006924 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006925 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006926 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006927 */
6928 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006929ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006930{
6931 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02006932 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006933 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006934 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006935 int_u save_qfid;
6936 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01006937 char_u *qf_title;
6938 linenr_T line1;
6939 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006940
Bram Moolenaara16123a2019-03-28 20:31:07 +01006941 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01006942 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01006943 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006944 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006945#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006946 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006947 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006948#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006949 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006950
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006951 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02006952 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6953 if (qi == NULL)
6954 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006955
Bram Moolenaara16123a2019-03-28 20:31:07 +01006956 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
6957 return;
6958
6959 qf_title = qf_cmdtitle(*eap->cmdlinep);
6960
6961 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006962 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01006963 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6964 (char *)qf_title, (char *)buf->b_sfname);
6965 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006966 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006967
6968 incr_quickfix_busy();
6969
6970 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
6971 (eap->cmdidx != CMD_caddbuffer
6972 && eap->cmdidx != CMD_laddbuffer),
6973 line1, line2,
6974 qf_title, NULL);
6975 if (qf_stack_empty(qi))
6976 {
6977 decr_quickfix_busy();
6978 return;
6979 }
6980 if (res >= 0)
6981 qf_list_changed(qf_get_curlist(qi));
6982
6983 // Remember the current quickfix list identifier, so that we can
6984 // check for autocommands changing the current quickfix list.
6985 save_qfid = qf_get_curlist(qi)->qf_id;
6986 if (au_name != NULL)
6987 {
6988 buf_T *curbuf_old = curbuf;
6989
6990 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
6991 TRUE, curbuf);
6992 if (curbuf != curbuf_old)
6993 // Autocommands changed buffer, don't jump now, "qi" may
6994 // be invalid.
6995 res = 0;
6996 }
6997 // Jump to the first error for a new list and if autocmds didn't
6998 // free the list.
6999 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7000 eap->cmdidx == CMD_lbuffer)
7001 && qflist_valid(wp, save_qfid))
7002 // display the first error
7003 qf_jump_first(qi, save_qfid, eap->forceit);
7004
7005 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007006}
7007
Bram Moolenaar1e015462005-09-25 22:16:38 +00007008#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007009/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007010 * Return the autocmd name for the :cexpr Ex commands.
7011 */
7012 static char_u *
7013cexpr_get_auname(cmdidx_T cmdidx)
7014{
7015 switch (cmdidx)
7016 {
7017 case CMD_cexpr: return (char_u *)"cexpr";
7018 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7019 case CMD_caddexpr: return (char_u *)"caddexpr";
7020 case CMD_lexpr: return (char_u *)"lexpr";
7021 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7022 case CMD_laddexpr: return (char_u *)"laddexpr";
7023 default: return NULL;
7024 }
7025}
7026
7027/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007028 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7029 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007030 */
7031 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007032ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007033{
7034 typval_T *tv;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007035 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007036 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007037 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007038 int_u save_qfid;
7039 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007040
Bram Moolenaara16123a2019-03-28 20:31:07 +01007041 au_name = cexpr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007042 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7043 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007044 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007045#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007046 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007047 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007048#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007049 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007050
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007051 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7052 if (qi == NULL)
7053 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007054
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007055 // Evaluate the expression. When the result is a string or a list we can
7056 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007057 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007058 if (tv != NULL)
7059 {
7060 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7061 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7062 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007063 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007064 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00007065 (eap->cmdidx != CMD_caddexpr
7066 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007067 (linenr_T)0, (linenr_T)0,
7068 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007069 if (qf_stack_empty(qi))
7070 {
7071 decr_quickfix_busy();
7072 goto cleanup;
7073 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01007074 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007075 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007076
7077 // Remember the current quickfix list identifier, so that we can
7078 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007079 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007080 if (au_name != NULL)
7081 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7082 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007083
7084 // Jump to the first error for a new list and if autocmds didn't
7085 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02007086 if (res > 0 && (eap->cmdidx == CMD_cexpr
7087 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007088 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02007089 // display the first error
7090 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007091 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00007092 }
7093 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007094 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007095cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00007096 free_tv(tv);
7097 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007098}
Bram Moolenaar1e015462005-09-25 22:16:38 +00007099#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007100
7101/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007102 * Get the location list for ":lhelpgrep"
7103 */
7104 static qf_info_T *
7105hgr_get_ll(int *new_ll)
7106{
7107 win_T *wp;
7108 qf_info_T *qi;
7109
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007110 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007111 if (bt_help(curwin->w_buffer))
7112 wp = curwin;
7113 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007114 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007115 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007116
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007117 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007118 qi = NULL;
7119 else
7120 qi = wp->w_llist;
7121
7122 if (qi == NULL)
7123 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007124 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007125 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007126 return NULL;
7127 *new_ll = TRUE;
7128 }
7129
7130 return qi;
7131}
7132
7133/*
7134 * Search for a pattern in a help file.
7135 */
7136 static void
7137hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007138 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007139 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007140 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007141 regmatch_T *p_regmatch)
7142{
7143 FILE *fd;
7144 long lnum;
7145
7146 fd = mch_fopen((char *)fname, "r");
7147 if (fd == NULL)
7148 return;
7149
7150 lnum = 1;
7151 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7152 {
7153 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007154
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007155 // Convert a line if 'encoding' is not utf-8 and
7156 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007157 if (p_vc->vc_type != CONV_NONE
7158 && has_non_ascii(IObuff))
7159 {
7160 line = string_convert(p_vc, IObuff, NULL);
7161 if (line == NULL)
7162 line = IObuff;
7163 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007164
7165 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7166 {
7167 int l = (int)STRLEN(line);
7168
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007169 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007170 while (l > 0 && line[l - 1] <= ' ')
7171 line[--l] = NUL;
7172
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007173 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007174 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007175 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007176 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007177 0,
7178 line,
7179 lnum,
7180 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007181 + 1, // col
7182 FALSE, // vis_col
7183 NULL, // search pattern
7184 0, // nr
7185 1, // type
7186 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02007187 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007188 {
7189 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007190 if (line != IObuff)
7191 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007192 break;
7193 }
7194 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007195 if (line != IObuff)
7196 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007197 ++lnum;
7198 line_breakcheck();
7199 }
7200 fclose(fd);
7201}
7202
7203/*
7204 * Search for a pattern in all the help files in the doc directory under
7205 * the given directory.
7206 */
7207 static void
7208hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007209 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007210 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007211 regmatch_T *p_regmatch,
7212 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007213#ifdef FEAT_MULTI_LANG
7214 , char_u *lang
7215#endif
7216 )
7217{
7218 int fcount;
7219 char_u **fnames;
7220 int fi;
7221
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007222 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007223 add_pathsep(dirname);
7224 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7225 if (gen_expand_wildcards(1, &dirname, &fcount,
7226 &fnames, EW_FILE|EW_SILENT) == OK
7227 && fcount > 0)
7228 {
7229 for (fi = 0; fi < fcount && !got_int; ++fi)
7230 {
7231#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007232 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007233 if (lang != NULL
7234 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007235 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007236 && !(STRNICMP(lang, "en", 2) == 0
7237 && STRNICMP("txt", fnames[fi]
7238 + STRLEN(fnames[fi]) - 3, 3) == 0))
7239 continue;
7240#endif
7241
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007242 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007243 }
7244 FreeWild(fcount, fnames);
7245 }
7246}
7247
7248/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007249 * Search for a pattern in all the help files in the 'runtimepath'
7250 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007251 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007252 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007253 */
7254 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007255hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007256{
7257 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007258
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007259 vimconv_T vc;
7260
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007261 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7262 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007263 vc.vc_type = CONV_NONE;
7264 if (!enc_utf8)
7265 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007266
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007267 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007268 p = p_rtp;
7269 while (*p != NUL && !got_int)
7270 {
7271 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7272
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007273 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007274#ifdef FEAT_MULTI_LANG
7275 , lang
7276#endif
7277 );
7278 }
7279
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007280 if (vc.vc_type != CONV_NONE)
7281 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007282}
7283
7284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 * ":helpgrep {pattern}"
7286 */
7287 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007288ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289{
7290 regmatch_T regmatch;
7291 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007292 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007293 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007294 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007295 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
Bram Moolenaar73633f82012-01-20 13:39:07 +01007297 switch (eap->cmdidx)
7298 {
7299 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7300 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7301 default: break;
7302 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007303 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7304 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007305 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007306#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007307 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007308 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007309#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007310 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007311
Bram Moolenaar39665952018-08-15 20:59:48 +02007312 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007313 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007314 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007315 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007316 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007317 }
7318
Bram Moolenaara16123a2019-03-28 20:31:07 +01007319 // Make 'cpoptions' empty, the 'l' flag should not be used here.
7320 save_cpo = p_cpo;
7321 p_cpo = empty_option;
7322
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007323 incr_quickfix_busy();
7324
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007325#ifdef FEAT_MULTI_LANG
7326 // Check for a specified language
7327 lang = check_help_lang(eap->arg);
7328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7330 regmatch.rm_ic = FALSE;
7331 if (regmatch.regprog != NULL)
7332 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007333 qf_list_T *qfl;
7334
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007335 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007336 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007337 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007339 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007340
Bram Moolenaar473de612013-06-08 18:19:48 +02007341 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007343 qfl->qf_nonevalid = FALSE;
7344 qfl->qf_ptr = qfl->qf_start;
7345 qfl->qf_index = 1;
7346 qf_list_changed(qfl);
7347 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 }
7349
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007350 if (p_cpo == empty_option)
7351 p_cpo = save_cpo;
7352 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007353 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007354 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355
Bram Moolenaar73633f82012-01-20 13:39:07 +01007356 if (au_name != NULL)
7357 {
7358 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7359 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007360 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007361 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007362 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007363 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007364 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007365 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007366 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007367
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007368 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007369 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007370 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007371 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007372 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007373
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007374 decr_quickfix_busy();
7375
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007376 if (eap->cmdidx == CMD_lhelpgrep)
7377 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007378 // If the help window is not opened or if it already points to the
7379 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007380 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007381 {
7382 if (new_qi)
7383 ll_free_all(&qi);
7384 }
7385 else if (curwin->w_llist == NULL)
7386 curwin->w_llist = qi;
7387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388}
7389
7390#endif /* FEAT_QUICKFIX */