blob: f90934387938e90498c5a063cf71908d498a6c09 [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 }
Bram Moolenaar36d50222019-05-02 20:17:40 +02004019 else if (sz != win->w_height
4020 && win->w_height + win->w_status_height < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004021 win_setheight(sz);
4022 }
4023
4024 return OK;
4025}
4026
4027/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004028 * Set options for the buffer in the quickfix or location list window.
4029 */
4030 static void
4031qf_set_cwindow_options(void)
4032{
4033 // switch off 'swapfile'
4034 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4035 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4036 OPT_LOCAL);
4037 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4038 RESET_BINDING(curwin);
4039#ifdef FEAT_DIFF
4040 curwin->w_p_diff = FALSE;
4041#endif
4042#ifdef FEAT_FOLDING
4043 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4044 OPT_LOCAL);
4045#endif
4046}
4047
4048/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004049 * Open a new quickfix or location list window, load the quickfix buffer and
4050 * set the appropriate options for the window.
4051 * Returns FAIL if the window could not be opened.
4052 */
4053 static int
4054qf_open_new_cwindow(qf_info_T *qi, int height)
4055{
4056 buf_T *qf_buf;
4057 win_T *oldwin = curwin;
4058 tabpage_T *prevtab = curtab;
4059 int flags = 0;
4060 win_T *win;
4061
4062 qf_buf = qf_find_buf(qi);
4063
4064 // The current window becomes the previous window afterwards.
4065 win = curwin;
4066
4067 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4068 // Create the new quickfix window at the very bottom, except when
4069 // :belowright or :aboveleft is used.
4070 win_goto(lastwin);
4071 // Default is to open the window below the current window
4072 if (cmdmod.split == 0)
4073 flags = WSP_BELOW;
4074 flags |= WSP_NEWLOC;
4075 if (win_split(height, flags) == FAIL)
4076 return FAIL; // not enough room for window
4077 RESET_BINDING(curwin);
4078
4079 if (IS_LL_STACK(qi))
4080 {
4081 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004082 // location list stack from the window 'win'.
4083 curwin->w_llist_ref = qi;
4084 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004085 }
4086
4087 if (oldwin != curwin)
4088 oldwin = NULL; // don't store info when in another window
4089 if (qf_buf != NULL)
4090 {
4091 // Use the existing quickfix buffer
4092 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4093 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4094 }
4095 else
4096 {
4097 // Create a new quickfix buffer
4098 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4099
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004100 // save the number of the new buffer
4101 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004102 }
4103
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004104 // Set the options for the quickfix buffer/window (if not already done)
4105 // Do this even if the quickfix buffer was already present, as an autocmd
4106 // might have previously deleted (:bdelete) the quickfix buffer.
4107 if (curbuf->b_p_bt[0] != 'q')
4108 qf_set_cwindow_options();
4109
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004110 // Only set the height when still in the same tab page and there is no
4111 // window to the side.
4112 if (curtab == prevtab && curwin->w_width == Columns)
4113 win_setheight(height);
4114 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4115 if (win_valid(win))
4116 prevwin = win;
4117
4118 return OK;
4119}
4120
4121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004123 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 */
4125 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004126ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004128 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004129 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004131 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004132 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004134 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4135 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004136
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004137 incr_quickfix_busy();
4138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 if (eap->addr_count != 0)
4140 height = eap->line2;
4141 else
4142 height = QF_WINHEIGHT;
4143
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004144 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145#ifdef FEAT_GUI
4146 need_mouse_correct = TRUE;
4147#endif
4148
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004149 // Find an existing quickfix window, or open a new one.
4150 if (cmdmod.tab == 0)
4151 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4152 cmdmod.split & WSP_VERT);
4153 if (status == FAIL)
4154 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004155 {
4156 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004157 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004160 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004161 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004162 // Save the current index here, as updating the quickfix buffer may free
4163 // the quickfix list
4164 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004165
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004166 // Fill the buffer with the quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004167 qf_fill_buffer(qfl, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004169 decr_quickfix_busy();
4170
4171 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 curwin->w_cursor.col = 0;
4173 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004174 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175}
4176
4177/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004178 * Move the cursor in the quickfix window to "lnum".
4179 */
4180 static void
4181qf_win_goto(win_T *win, linenr_T lnum)
4182{
4183 win_T *old_curwin = curwin;
4184
4185 curwin = win;
4186 curbuf = win->w_buffer;
4187 curwin->w_cursor.lnum = lnum;
4188 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004189 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004190 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004191 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004192 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004193 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004194 curwin = old_curwin;
4195 curbuf = curwin->w_buffer;
4196}
4197
4198/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004199 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004200 */
4201 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004202ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004203{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004204 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004205 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004206
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004207 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4208 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004209
4210 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004211 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4212 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4213}
4214
4215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 * Return the number of the current entry (line number in the quickfix
4217 * window).
4218 */
4219 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004220qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004222 qf_info_T *qi = &ql_info;
4223
4224 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004225 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004226 qi = wp->w_llist_ref;
4227
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004228 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229}
4230
4231/*
4232 * Update the cursor position in the quickfix window to the current error.
4233 * Return TRUE if there is a quickfix window.
4234 */
4235 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004236qf_win_pos_update(
4237 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004238 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
4240 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004241 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004243 // Put the cursor on the current error in the quickfix window, so that
4244 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004245 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 if (win != NULL
4247 && qf_index <= win->w_buffer->b_ml.ml_line_count
4248 && old_qf_index != qf_index)
4249 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 if (qf_index > old_qf_index)
4251 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004252 win->w_redraw_top = old_qf_index;
4253 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
4255 else
4256 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004257 win->w_redraw_top = qf_index;
4258 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004260 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 }
4262 return win != NULL;
4263}
4264
4265/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004266 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004267 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004268 */
4269 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004270is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004271{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004272 // A window displaying the quickfix buffer will have the w_llist_ref field
4273 // set to NULL.
4274 // A window displaying a location list buffer will have the w_llist_ref
4275 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004276 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004277 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4278 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004279 return TRUE;
4280
4281 return FALSE;
4282}
4283
4284/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004285 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004286 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004287 */
4288 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004289qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004290{
4291 win_T *win;
4292
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004293 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004294 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004295 return win;
4296 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004297}
4298
4299/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004300 * Find a quickfix buffer.
4301 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 */
4303 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004304qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004306 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004307 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004309 if (qi->qf_bufnr != INVALID_QFBUFNR)
4310 {
4311 buf_T *qfbuf;
4312 qfbuf = buflist_findnr(qi->qf_bufnr);
4313 if (qfbuf != NULL)
4314 return qfbuf;
4315 // buffer is no longer present
4316 qi->qf_bufnr = INVALID_QFBUFNR;
4317 }
4318
Bram Moolenaar9c102382006-05-03 21:26:49 +00004319 FOR_ALL_TAB_WINDOWS(tp, win)
4320 if (is_qf_win(win, qi))
4321 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004322
Bram Moolenaar9c102382006-05-03 21:26:49 +00004323 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324}
4325
4326/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004327 * Update the w:quickfix_title variable in the quickfix/location list window
4328 */
4329 static void
4330qf_update_win_titlevar(qf_info_T *qi)
4331{
4332 win_T *win;
4333 win_T *curwin_save;
4334
4335 if ((win = qf_find_win(qi)) != NULL)
4336 {
4337 curwin_save = curwin;
4338 curwin = win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004339 qf_set_title_var(qf_get_curlist(qi));
Bram Moolenaard823fa92016-08-12 16:29:27 +02004340 curwin = curwin_save;
4341 }
4342}
4343
4344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 * Find the quickfix buffer. If it exists, update the contents.
4346 */
4347 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004348qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349{
4350 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004351 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004354 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004355 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 if (buf != NULL)
4357 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004358 linenr_T old_line_count = buf->b_ml.ml_line_count;
4359
4360 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004361 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004362 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
Bram Moolenaard823fa92016-08-12 16:29:27 +02004364 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004365
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004366 qf_fill_buffer(qf_get_curlist(qi), buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004367 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004368
Bram Moolenaar864293a2016-06-02 13:40:04 +02004369 if (old_last == NULL)
4370 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004371 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004372
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004373 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004374 aucmd_restbuf(&aco);
4375 }
4376
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004377 // Only redraw when added lines are visible. This avoids flickering
4378 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004379 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4380 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 }
4382}
4383
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004384/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004385 * Add an error line to the quickfix buffer.
4386 */
4387 static int
4388qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4389{
4390 int len;
4391 buf_T *errbuf;
4392
4393 if (qfp->qf_module != NULL)
4394 {
4395 STRCPY(IObuff, qfp->qf_module);
4396 len = (int)STRLEN(IObuff);
4397 }
4398 else if (qfp->qf_fnum != 0
4399 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4400 && errbuf->b_fname != NULL)
4401 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004402 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004403 STRCPY(IObuff, gettail(errbuf->b_fname));
4404 else
4405 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004406 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004407 if (errbuf->b_sfname == NULL
4408 || mch_isFullName(errbuf->b_sfname))
4409 {
4410 if (*dirname == NUL)
4411 mch_dirname(dirname, MAXPATHL);
4412 shorten_buf_fname(errbuf, dirname, FALSE);
4413 }
4414 STRCPY(IObuff, errbuf->b_fname);
4415 }
4416 len = (int)STRLEN(IObuff);
4417 }
4418 else
4419 len = 0;
4420 IObuff[len++] = '|';
4421
4422 if (qfp->qf_lnum > 0)
4423 {
4424 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4425 len += (int)STRLEN(IObuff + len);
4426
4427 if (qfp->qf_col > 0)
4428 {
4429 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4430 len += (int)STRLEN(IObuff + len);
4431 }
4432
4433 sprintf((char *)IObuff + len, "%s",
4434 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4435 len += (int)STRLEN(IObuff + len);
4436 }
4437 else if (qfp->qf_pattern != NULL)
4438 {
4439 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4440 len += (int)STRLEN(IObuff + len);
4441 }
4442 IObuff[len++] = '|';
4443 IObuff[len++] = ' ';
4444
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004445 // Remove newlines and leading whitespace from the text.
4446 // For an unrecognized line keep the indent, the compiler may
4447 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004448 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4449 IObuff + len, IOSIZE - len);
4450
4451 if (ml_append_buf(buf, lnum, IObuff,
4452 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4453 return FAIL;
4454
4455 return OK;
4456}
4457
4458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 * Fill current buffer with quickfix errors, replacing any previous contents.
4460 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004461 * If "old_last" is not NULL append the items after this one.
4462 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4463 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 */
4465 static void
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004466qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004468 linenr_T lnum;
4469 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004470 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
Bram Moolenaar864293a2016-06-02 13:40:04 +02004472 if (old_last == NULL)
4473 {
4474 if (buf != curbuf)
4475 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004476 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004477 return;
4478 }
4479
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004480 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004481 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4482 (void)ml_delete((linenr_T)1, FALSE);
4483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004485 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004486 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004488 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004489
4490 *dirname = NUL;
4491
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004492 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004493 if (old_last == NULL)
4494 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004495 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004496 lnum = 0;
4497 }
4498 else
4499 {
4500 qfp = old_last->qf_next;
4501 lnum = buf->b_ml.ml_line_count;
4502 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004503 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004505 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004507
Bram Moolenaar864293a2016-06-02 13:40:04 +02004508 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004510 if (qfp == NULL)
4511 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004513
4514 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004515 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004516 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 }
4518
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004519 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 check_lnums(TRUE);
4521
Bram Moolenaar864293a2016-06-02 13:40:04 +02004522 if (old_last == NULL)
4523 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004524 // Set the 'filetype' to "qf" each time after filling the buffer.
4525 // This resembles reading a file into a buffer, it's more logical when
4526 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004527 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004528 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4529 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004531 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004532 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004534 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004536 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004537 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004538
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004539 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004540 redraw_curbuf_later(NOT_VALID);
4541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004543 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 KeyTyped = old_KeyTyped;
4545}
4546
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004547/*
4548 * For every change made to the quickfix list, update the changed tick.
4549 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004550 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004551qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004552{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004553 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004554}
4555
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004557 * Return the quickfix/location list number with the given identifier.
4558 * Returns -1 if list is not found.
4559 */
4560 static int
4561qf_id2nr(qf_info_T *qi, int_u qfid)
4562{
4563 int qf_idx;
4564
4565 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4566 if (qi->qf_lists[qf_idx].qf_id == qfid)
4567 return qf_idx;
4568 return INVALID_QFIDX;
4569}
4570
4571/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004572 * If the current list is not "save_qfid" and we can find the list with that ID
4573 * then make it the current list.
4574 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004575 * Returns OK if successfully restored the list. Returns FAIL if the list with
4576 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004577 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004578 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004579qf_restore_list(qf_info_T *qi, int_u save_qfid)
4580{
4581 int curlist;
4582
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004583 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004584 {
4585 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004586 if (curlist < 0)
4587 // list is not present
4588 return FAIL;
4589 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004590 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004591 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004592}
4593
4594/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004595 * Jump to the first entry if there is one.
4596 */
4597 static void
4598qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4599{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004600 if (qf_restore_list(qi, save_qfid) == FAIL)
4601 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004602
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004603 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004604 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004605 qf_jump(qi, 0, 0, forceit);
4606}
4607
4608/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004609 * Return TRUE when using ":vimgrep" for ":grep".
4610 */
4611 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004612grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004613{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004614 return ((cmdidx == CMD_grep
4615 || cmdidx == CMD_lgrep
4616 || cmdidx == CMD_grepadd
4617 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004618 && STRCMP("internal",
4619 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4620}
4621
4622/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004623 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004625 static char_u *
4626make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004628 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004629 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004630 case CMD_make: return (char_u *)"make";
4631 case CMD_lmake: return (char_u *)"lmake";
4632 case CMD_grep: return (char_u *)"grep";
4633 case CMD_lgrep: return (char_u *)"lgrep";
4634 case CMD_grepadd: return (char_u *)"grepadd";
4635 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4636 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638}
4639
4640/*
4641 * Return the name for the errorfile, in allocated memory.
4642 * Find a new unique name when 'makeef' contains "##".
4643 * Returns NULL for error.
4644 */
4645 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004646get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647{
4648 char_u *p;
4649 char_u *name;
4650 static int start = -1;
4651 static int off = 0;
4652#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004653 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654#endif
4655
4656 if (*p_mef == NUL)
4657 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004658 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004660 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 return name;
4662 }
4663
4664 for (p = p_mef; *p; ++p)
4665 if (p[0] == '#' && p[1] == '#')
4666 break;
4667
4668 if (*p == NUL)
4669 return vim_strsave(p_mef);
4670
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004671 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 for (;;)
4673 {
4674 if (start == -1)
4675 start = mch_get_pid();
4676 else
4677 off += 19;
4678
4679 name = alloc((unsigned)STRLEN(p_mef) + 30);
4680 if (name == NULL)
4681 break;
4682 STRCPY(name, p_mef);
4683 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4684 STRCAT(name, p + 2);
4685 if (mch_getperm(name) < 0
4686#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004687 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 && mch_lstat((char *)name, &sb) < 0
4689#endif
4690 )
4691 break;
4692 vim_free(name);
4693 }
4694 return name;
4695}
4696
4697/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004698 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4699 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4700 */
4701 static char_u *
4702make_get_fullcmd(char_u *makecmd, char_u *fname)
4703{
4704 char_u *cmd;
4705 unsigned len;
4706
4707 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4708 if (*p_sp != NUL)
4709 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4710 cmd = alloc(len);
4711 if (cmd == NULL)
4712 return NULL;
4713 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4714 (char *)p_shq);
4715
4716 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4717 if (*p_sp != NUL)
4718 append_redir(cmd, len, p_sp, fname);
4719
4720 // Display the fully formed command. Output a newline if there's something
4721 // else than the :make command that was typed (in which case the cursor is
4722 // in column 0).
4723 if (msg_col == 0)
4724 msg_didout = FALSE;
4725 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004726 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004727 msg_outtrans(cmd); // show what we are doing
4728
4729 return cmd;
4730}
4731
4732/*
4733 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4734 */
4735 void
4736ex_make(exarg_T *eap)
4737{
4738 char_u *fname;
4739 char_u *cmd;
4740 char_u *enc = NULL;
4741 win_T *wp = NULL;
4742 qf_info_T *qi = &ql_info;
4743 int res;
4744 char_u *au_name = NULL;
4745 int_u save_qfid;
4746
4747 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4748 if (grep_internal(eap->cmdidx))
4749 {
4750 ex_vimgrep(eap);
4751 return;
4752 }
4753
4754 au_name = make_get_auname(eap->cmdidx);
4755 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4756 curbuf->b_fname, TRUE, curbuf))
4757 {
4758#ifdef FEAT_EVAL
4759 if (aborting())
4760 return;
4761#endif
4762 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004763 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004764
4765 if (is_loclist_cmd(eap->cmdidx))
4766 wp = curwin;
4767
4768 autowrite_all();
4769 fname = get_mef_name();
4770 if (fname == NULL)
4771 return;
4772 mch_remove(fname); // in case it's not unique
4773
4774 cmd = make_get_fullcmd(eap->arg, fname);
4775 if (cmd == NULL)
4776 return;
4777
4778 // let the shell know if we are redirecting output or not
4779 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4780
4781#ifdef AMIGA
4782 out_flush();
4783 // read window status report and redraw before message
4784 (void)char_avail();
4785#endif
4786
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004787 incr_quickfix_busy();
4788
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004789 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4790 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4791 (eap->cmdidx != CMD_grepadd
4792 && eap->cmdidx != CMD_lgrepadd),
4793 qf_cmdtitle(*eap->cmdlinep), enc);
4794 if (wp != NULL)
4795 {
4796 qi = GET_LOC_LIST(wp);
4797 if (qi == NULL)
4798 goto cleanup;
4799 }
4800 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004801 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004802
4803 // Remember the current quickfix list identifier, so that we can
4804 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004805 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004806 if (au_name != NULL)
4807 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4808 curbuf->b_fname, TRUE, curbuf);
4809 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4810 // display the first error
4811 qf_jump_first(qi, save_qfid, FALSE);
4812
4813cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004814 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004815 mch_remove(fname);
4816 vim_free(fname);
4817 vim_free(cmd);
4818}
4819
4820/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004821 * Returns the number of valid entries in the current quickfix/location list.
4822 */
4823 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004824qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004825{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004826 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004827 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004828 qfline_T *qfp;
4829 int i, sz = 0;
4830 int prev_fnum = 0;
4831
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004832 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4833 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004834
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004835 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01004836 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004837 {
4838 if (qfp->qf_valid)
4839 {
4840 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004841 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004842 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4843 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004844 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004845 sz++;
4846 prev_fnum = qfp->qf_fnum;
4847 }
4848 }
4849 }
4850
4851 return sz;
4852}
4853
4854/*
4855 * Returns the current index of the quickfix/location list.
4856 * Returns 0 if there is an error.
4857 */
4858 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004859qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004860{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004861 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004862
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004863 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4864 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004865
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004866 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004867}
4868
4869/*
4870 * Returns the current index in the quickfix/location list (counting only valid
4871 * entries). If no valid entries are in the list, then returns 1.
4872 */
4873 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004874qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004875{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004876 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004877 qf_list_T *qfl;
4878 qfline_T *qfp;
4879 int i, eidx = 0;
4880 int prev_fnum = 0;
4881
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004882 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4883 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004884
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004885 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004886 qfp = qfl->qf_start;
4887
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004888 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004889 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4890 return 1;
4891
4892 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4893 {
4894 if (qfp->qf_valid)
4895 {
4896 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4897 {
4898 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4899 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004900 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004901 eidx++;
4902 prev_fnum = qfp->qf_fnum;
4903 }
4904 }
4905 else
4906 eidx++;
4907 }
4908 }
4909
4910 return eidx ? eidx : 1;
4911}
4912
4913/*
4914 * Get the 'n'th valid error entry in the quickfix or location list.
4915 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4916 * For :cdo and :ldo returns the 'n'th valid error entry.
4917 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4918 */
4919 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004920qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004921{
Bram Moolenaar95946f12019-03-31 15:31:59 +02004922 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004923 int i, eidx;
4924 int prev_fnum = 0;
4925
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004926 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004927 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4928 return 1;
4929
Bram Moolenaar95946f12019-03-31 15:31:59 +02004930 eidx = 0;
4931 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004932 {
4933 if (qfp->qf_valid)
4934 {
4935 if (fdo)
4936 {
4937 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4938 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004939 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004940 eidx++;
4941 prev_fnum = qfp->qf_fnum;
4942 }
4943 }
4944 else
4945 eidx++;
4946 }
4947
4948 if (eidx == n)
4949 break;
4950 }
4951
4952 if (i <= qfl->qf_count)
4953 return i;
4954 else
4955 return 1;
4956}
4957
4958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004960 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004961 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 */
4963 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004964ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004966 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004967 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004968
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004969 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4970 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004971
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004972 if (eap->addr_count > 0)
4973 errornr = (int)eap->line2;
4974 else
4975 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004976 switch (eap->cmdidx)
4977 {
4978 case CMD_cc: case CMD_ll:
4979 errornr = 0;
4980 break;
4981 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4982 case CMD_lfirst:
4983 errornr = 1;
4984 break;
4985 default:
4986 errornr = 32767;
4987 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004988 }
4989
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004990 // For cdo and ldo commands, jump to the nth valid error.
4991 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004992 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4993 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004994 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004995 eap->addr_count > 0 ? (int)eap->line1 : 1,
4996 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4997
4998 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999}
5000
5001/*
5002 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005003 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005004 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 */
5006 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005007ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005009 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005010 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005011 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005012
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005013 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5014 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005015
Bram Moolenaar55b69262017-08-13 13:42:01 +02005016 if (eap->addr_count > 0
5017 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5018 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005019 errornr = (int)eap->line2;
5020 else
5021 errornr = 1;
5022
Bram Moolenaar39665952018-08-15 20:59:48 +02005023 // Depending on the command jump to either next or previous entry/file.
5024 switch (eap->cmdidx)
5025 {
5026 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5027 dir = FORWARD;
5028 break;
5029 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5030 case CMD_lNext:
5031 dir = BACKWARD;
5032 break;
5033 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5034 dir = FORWARD_FILE;
5035 break;
5036 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5037 dir = BACKWARD_FILE;
5038 break;
5039 default:
5040 dir = FORWARD;
5041 break;
5042 }
5043
5044 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045}
5046
5047/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005048 * Return the autocmd name for the :cfile Ex commands
5049 */
5050 static char_u *
5051cfile_get_auname(cmdidx_T cmdidx)
5052{
5053 switch (cmdidx)
5054 {
5055 case CMD_cfile: return (char_u *)"cfile";
5056 case CMD_cgetfile: return (char_u *)"cgetfile";
5057 case CMD_caddfile: return (char_u *)"caddfile";
5058 case CMD_lfile: return (char_u *)"lfile";
5059 case CMD_lgetfile: return (char_u *)"lgetfile";
5060 case CMD_laddfile: return (char_u *)"laddfile";
5061 default: return NULL;
5062 }
5063}
5064
5065/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005066 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005067 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 */
5069 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005070ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005072 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005073 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005074 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005075 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005076 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005077 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005078
Bram Moolenaara16123a2019-03-28 20:31:07 +01005079 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005080 if (au_name != NULL)
5081 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005082
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005083 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005084#ifdef FEAT_BROWSE
5085 if (cmdmod.browse)
5086 {
5087 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005088 NULL, NULL,
5089 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005090 if (browse_file == NULL)
5091 return;
5092 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5093 vim_free(browse_file);
5094 }
5095 else
5096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005098 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005099
Bram Moolenaar39665952018-08-15 20:59:48 +02005100 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005101 wp = curwin;
5102
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005103 incr_quickfix_busy();
5104
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005105 // This function is used by the :cfile, :cgetfile and :caddfile
5106 // commands.
5107 // :cfile always creates a new quickfix list and jumps to the
5108 // first error.
5109 // :cgetfile creates a new quickfix list but doesn't jump to the
5110 // first error.
5111 // :caddfile adds to an existing quickfix list. If there is no
5112 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005113 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005114 && eap->cmdidx != CMD_laddfile),
5115 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005116 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005117 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005118 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005119 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005120 {
5121 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005122 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005123 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005124 }
5125 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005126 qf_list_changed(qf_get_curlist(qi));
5127 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005128 if (au_name != NULL)
5129 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005130
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005131 // Jump to the first error for a new list and if autocmds didn't
5132 // free the list.
5133 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5134 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005135 // display the first error
5136 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005137
5138 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005139}
5140
5141/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005142 * Return the vimgrep autocmd name.
5143 */
5144 static char_u *
5145vgr_get_auname(cmdidx_T cmdidx)
5146{
5147 switch (cmdidx)
5148 {
5149 case CMD_vimgrep: return (char_u *)"vimgrep";
5150 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5151 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5152 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5153 case CMD_grep: return (char_u *)"grep";
5154 case CMD_lgrep: return (char_u *)"lgrep";
5155 case CMD_grepadd: return (char_u *)"grepadd";
5156 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5157 default: return NULL;
5158 }
5159}
5160
5161/*
5162 * Initialize the regmatch used by vimgrep for pattern "s".
5163 */
5164 static void
5165vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5166{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005167 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005168 regmatch->regprog = NULL;
5169
5170 if (s == NULL || *s == NUL)
5171 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005172 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005173 if (last_search_pat() == NULL)
5174 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005175 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005176 return;
5177 }
5178 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5179 }
5180 else
5181 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5182
5183 regmatch->rmm_ic = p_ic;
5184 regmatch->rmm_maxcol = 0;
5185}
5186
5187/*
5188 * Display a file name when vimgrep is running.
5189 */
5190 static void
5191vgr_display_fname(char_u *fname)
5192{
5193 char_u *p;
5194
5195 msg_start();
5196 p = msg_strtrunc(fname, TRUE);
5197 if (p == NULL)
5198 msg_outtrans(fname);
5199 else
5200 {
5201 msg_outtrans(p);
5202 vim_free(p);
5203 }
5204 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005205 msg_didout = FALSE; // overwrite this message
5206 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005207 msg_col = 0;
5208 out_flush();
5209}
5210
5211/*
5212 * Load a dummy buffer to search for a pattern using vimgrep.
5213 */
5214 static buf_T *
5215vgr_load_dummy_buf(
5216 char_u *fname,
5217 char_u *dirname_start,
5218 char_u *dirname_now)
5219{
5220 int save_mls;
5221#if defined(FEAT_SYN_HL)
5222 char_u *save_ei = NULL;
5223#endif
5224 buf_T *buf;
5225
5226#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005227 // Don't do Filetype autocommands to avoid loading syntax and
5228 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005229 save_ei = au_event_disable(",Filetype");
5230#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005231 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005232 save_mls = p_mls;
5233 p_mls = 0;
5234
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005235 // Load file into a buffer, so that 'fileencoding' is detected,
5236 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005237 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5238
5239 p_mls = save_mls;
5240#if defined(FEAT_SYN_HL)
5241 au_event_restore(save_ei);
5242#endif
5243
5244 return buf;
5245}
5246
5247/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005248 * Check whether a quickfix/location list is valid. Autocmds may remove or
5249 * change a quickfix list when vimgrep is running. If the list is not found,
5250 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005251 */
5252 static int
5253vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005254 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005255 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005256 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005257 char_u *title)
5258{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005259 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005260 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005261 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005262 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005263 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005264 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005265 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005266 return FALSE;
5267 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005268 else
5269 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005270 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005271 qf_new_list(qi, title);
5272 return TRUE;
5273 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005274 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005275
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005276 if (qf_restore_list(qi, qfid) == FAIL)
5277 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005278
5279 return TRUE;
5280}
5281
5282/*
5283 * Search for a pattern in all the lines in a buffer and add the matching lines
5284 * to a quickfix list.
5285 */
5286 static int
5287vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005288 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005289 char_u *fname,
5290 buf_T *buf,
5291 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005292 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005293 int duplicate_name,
5294 int flags)
5295{
5296 int found_match = FALSE;
5297 long lnum;
5298 colnr_T col;
5299
Bram Moolenaar1c299432018-10-28 14:36:09 +01005300 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005301 {
5302 col = 0;
5303 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5304 col, NULL, NULL) > 0)
5305 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005306 // Pass the buffer number so that it gets used even for a
5307 // dummy buffer, unless duplicate_name is set, then the
5308 // buffer will be wiped out below.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005309 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005310 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005311 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005312 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005313 duplicate_name ? 0 : buf->b_fnum,
5314 ml_get_buf(buf,
5315 regmatch->startpos[0].lnum + lnum, FALSE),
5316 regmatch->startpos[0].lnum + lnum,
5317 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005318 FALSE, // vis_col
5319 NULL, // search pattern
5320 0, // nr
5321 0, // type
5322 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02005323 ) == QF_FAIL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005324 {
5325 got_int = TRUE;
5326 break;
5327 }
5328 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005329 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005330 break;
5331 if ((flags & VGR_GLOBAL) == 0
5332 || regmatch->endpos[0].lnum > 0)
5333 break;
5334 col = regmatch->endpos[0].col
5335 + (col == regmatch->endpos[0].col);
5336 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5337 break;
5338 }
5339 line_breakcheck();
5340 if (got_int)
5341 break;
5342 }
5343
5344 return found_match;
5345}
5346
5347/*
5348 * Jump to the first match and update the directory.
5349 */
5350 static void
5351vgr_jump_to_match(
5352 qf_info_T *qi,
5353 int forceit,
5354 int *redraw_for_dummy,
5355 buf_T *first_match_buf,
5356 char_u *target_dir)
5357{
5358 buf_T *buf;
5359
5360 buf = curbuf;
5361 qf_jump(qi, 0, 0, forceit);
5362 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005363 // If we jumped to another buffer redrawing will already be
5364 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005365 *redraw_for_dummy = FALSE;
5366
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005367 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005368 if (curbuf == first_match_buf && target_dir != NULL)
5369 {
5370 exarg_T ea;
5371
5372 ea.arg = target_dir;
5373 ea.cmdidx = CMD_lcd;
5374 ex_cd(&ea);
5375 }
5376}
5377
5378/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005379 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005380 * ":vimgrepadd {pattern} file(s)"
5381 * ":lvimgrep {pattern} file(s)"
5382 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005383 */
5384 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005385ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005386{
Bram Moolenaar81695252004-12-29 20:58:21 +00005387 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005388 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005389 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005390 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005391 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005392 char_u *s;
5393 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005394 int fi;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005395 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005396 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005397 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005398 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005399 buf_T *buf;
5400 int duplicate_name = FALSE;
5401 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005402 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005403 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005404 buf_T *first_match_buf = NULL;
5405 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005406 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005407 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005408 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005409 char_u *dirname_start = NULL;
5410 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005411 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005412 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005413
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005414 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005415 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5416 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005417 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005418#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005419 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005420 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005421#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005422 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005423
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005424 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
5425 if (qi == NULL)
5426 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00005427
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005428 if (eap->addr_count > 0)
5429 tomatch = eap->line2;
5430 else
5431 tomatch = MAXLNUM;
5432
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005433 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005434 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005435 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005436 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005437 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005438 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005439 emsg(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005440 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005441 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005442
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005443 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005444 if (regmatch.regprog == NULL)
5445 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005446
5447 p = skipwhite(p);
5448 if (*p == NUL)
5449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005450 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005451 goto theend;
5452 }
5453
Bram Moolenaar55b69262017-08-13 13:42:01 +02005454 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005455 && eap->cmdidx != CMD_vimgrepadd
5456 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005457 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005458 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005459 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005460
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005461 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005462 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005463 goto theend;
5464 if (fcount == 0)
5465 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005466 emsg(_(e_nomatch));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005467 goto theend;
5468 }
5469
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005470 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5471 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005472 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005473 {
5474 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005475 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005476 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005477
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005478 // Remember the current directory, because a BufRead autocommand that does
5479 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005480 mch_dirname(dirname_start, MAXPATHL);
5481
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005482 incr_quickfix_busy();
5483
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005484 // Remember the current quickfix list identifier, so that we can check for
5485 // autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005486 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005487
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005488 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005489 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005490 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005491 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005492 if (time(NULL) > seconds)
5493 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005494 // Display the file name every second or so, show the user we are
5495 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005496 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005497 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005498 }
5499
Bram Moolenaar81695252004-12-29 20:58:21 +00005500 buf = buflist_findname_exp(fnames[fi]);
5501 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5502 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005503 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005504 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005505 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005506 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005507
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005508 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005509 }
5510 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005511 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005512 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005513
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005514 // Check whether the quickfix list is still valid. When loading a
5515 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005516 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005517 {
5518 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005519 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005520 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005521 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005522 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005523
Bram Moolenaar81695252004-12-29 20:58:21 +00005524 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005525 {
5526 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005527 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005528 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005529 else
5530 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005531 // Try for a match in all lines of the buffer.
5532 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005533 found_match = vgr_match_buflines(qf_get_curlist(qi),
5534 fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005535 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005536
Bram Moolenaar81695252004-12-29 20:58:21 +00005537 if (using_dummy)
5538 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005539 if (found_match && first_match_buf == NULL)
5540 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005541 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005542 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005543 // Never keep a dummy buffer if there is another buffer
5544 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005545 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005546 buf = NULL;
5547 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005548 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005549 || buf->b_p_bh[0] == 'u' // "unload"
5550 || buf->b_p_bh[0] == 'w' // "wipe"
5551 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005552 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005553 // When no match was found we don't need to remember the
5554 // buffer, wipe it out. If there was a match and it
5555 // wasn't the first one or we won't jump there: only
5556 // unload the buffer.
5557 // Ignore 'hidden' here, because it may lead to having too
5558 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005559 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005560 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005561 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005562 buf = NULL;
5563 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005564 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005565 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005566 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005567 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005568 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005569 buf = NULL;
5570 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005571 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005572
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005573 if (buf != NULL)
5574 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005575 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005576 buf->b_flags &= ~BF_DUMMY;
5577
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005578 // If the buffer is still loaded we need to use the
5579 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005580 if (buf == first_match_buf
5581 && target_dir == NULL
5582 && STRCMP(dirname_start, dirname_now) != 0)
5583 target_dir = vim_strsave(dirname_now);
5584
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005585 // The buffer is still loaded, the Filetype autocommands
5586 // need to be done now, in that buffer. And the modelines
5587 // need to be done (again). But not the window-local
5588 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005589 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005590#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005591 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5592 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005593#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005594 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005595 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005596 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005597 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005598 }
5599 }
5600
5601 FreeWild(fcount, fnames);
5602
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005603 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005604 qfl->qf_nonevalid = FALSE;
5605 qfl->qf_ptr = qfl->qf_start;
5606 qfl->qf_index = 1;
5607 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005608
Bram Moolenaar864293a2016-06-02 13:40:04 +02005609 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005610
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005611 if (au_name != NULL)
5612 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5613 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005614 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5615 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005616 if (!qflist_valid(wp, save_qfid)
5617 || qf_restore_list(qi, save_qfid) == FAIL)
5618 {
5619 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005620 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005621 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005622
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005623 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01005624 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005625 {
5626 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005627 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5628 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005629 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005630 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005631 semsg(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005632
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005633 decr_quickfix_busy();
5634
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005635 // If we loaded a dummy buffer into the current window, the autocommands
5636 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005637 if (redraw_for_dummy)
5638 {
5639#ifdef FEAT_FOLDING
5640 foldUpdateAll(curwin);
5641#else
5642 redraw_later(NOT_VALID);
5643#endif
5644 }
5645
Bram Moolenaar86b68352004-12-27 21:59:20 +00005646theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005647 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005648 vim_free(dirname_now);
5649 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005650 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005651 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005652}
5653
5654/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005655 * Restore current working directory to "dirname_start" if they differ, taking
5656 * into account whether it is set locally or globally.
5657 */
5658 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005659restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005660{
5661 char_u *dirname_now = alloc(MAXPATHL);
5662
5663 if (NULL != dirname_now)
5664 {
5665 mch_dirname(dirname_now, MAXPATHL);
5666 if (STRCMP(dirname_start, dirname_now) != 0)
5667 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005668 // If the directory has changed, change it back by building up an
5669 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005670 exarg_T ea;
5671
5672 ea.arg = dirname_start;
5673 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5674 ex_cd(&ea);
5675 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005676 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005677 }
5678}
5679
5680/*
5681 * Load file "fname" into a dummy buffer and return the buffer pointer,
5682 * placing the directory resulting from the buffer load into the
5683 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5684 * prior to calling this function. Restores directory to "dirname_start" prior
5685 * to returning, if autocmds or the 'autochdir' option have changed it.
5686 *
5687 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5688 * or wipe_dummy_buffer() later!
5689 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005690 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005691 */
5692 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005693load_dummy_buffer(
5694 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005695 char_u *dirname_start, // in: old directory
5696 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005697{
5698 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005699 bufref_T newbufref;
5700 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005701 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005702 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005703 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005704
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005705 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005706 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5707 if (newbuf == NULL)
5708 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005709 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005710
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005711 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005712 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5713
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005714 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005715 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005716 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005717 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005718 ++newbuf->b_locked;
5719
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005720 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005721 aucmd_prepbuf(&aco, newbuf);
5722
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005723 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005724 (void)setfname(curbuf, fname, NULL, FALSE);
5725
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005726 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005727 check_need_swap(TRUE);
5728
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005729 // Remove the "dummy" flag, otherwise autocommands may not
5730 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005731 curbuf->b_flags &= ~BF_DUMMY;
5732
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005733 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005734 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005735 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005736 NULL, READ_NEW | READ_DUMMY);
5737 --newbuf->b_locked;
5738 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005739 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005740 && !(curbuf->b_flags & BF_NEW))
5741 {
5742 failed = FALSE;
5743 if (curbuf != newbuf)
5744 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005745 // Bloody autocommands changed the buffer! Can happen when
5746 // using netrw and editing a remote file. Use the current
5747 // buffer instead, delete the dummy one after restoring the
5748 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005749 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005750 newbuf = curbuf;
5751 }
5752 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005753
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005754 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005755 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005756 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5757 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005758
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005759 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5760 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005761 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005762 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005763
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005764 // When autocommands/'autochdir' option changed directory: go back.
5765 // Let the caller know what the resulting dir was first, in case it is
5766 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005767 mch_dirname(resulting_dir, MAXPATHL);
5768 restore_start_dir(dirname_start);
5769
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005770 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005771 return NULL;
5772 if (failed)
5773 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005774 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005775 return NULL;
5776 }
5777 return newbuf;
5778}
5779
5780/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005781 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5782 * directory to "dirname_start" prior to returning, if autocmds or the
5783 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005784 */
5785 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005786wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005787{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005788 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005789 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005790#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005791 cleanup_T cs;
5792
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005793 // Reset the error/interrupt/exception state here so that aborting()
5794 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5795 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005796 enter_cleanup(&cs);
5797#endif
5798
Bram Moolenaar81695252004-12-29 20:58:21 +00005799 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005800
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005801#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005802 // Restore the error/interrupt/exception state if not discarded by a
5803 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005804 leave_cleanup(&cs);
5805#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005806 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005807 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005808 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005809}
5810
5811/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005812 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5813 * directory to "dirname_start" prior to returning, if autocmds or the
5814 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005815 */
5816 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005817unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005818{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005819 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005820 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005821 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005822
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005823 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005824 restore_start_dir(dirname_start);
5825 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005826}
5827
Bram Moolenaar05159a02005-02-26 23:04:13 +00005828#if defined(FEAT_EVAL) || defined(PROTO)
5829/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005830 * Copy the specified quickfix entry items into a new dict and appened the dict
5831 * to 'list'. Returns OK on success.
5832 */
5833 static int
5834get_qfline_items(qfline_T *qfp, list_T *list)
5835{
5836 int bufnum;
5837 dict_T *dict;
5838 char_u buf[2];
5839
5840 // Handle entries with a non-existing buffer number.
5841 bufnum = qfp->qf_fnum;
5842 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5843 bufnum = 0;
5844
5845 if ((dict = dict_alloc()) == NULL)
5846 return FAIL;
5847 if (list_append_dict(list, dict) == FAIL)
5848 return FAIL;
5849
5850 buf[0] = qfp->qf_type;
5851 buf[1] = NUL;
5852 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5853 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5854 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5855 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5856 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5857 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5858 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5859 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5860 || dict_add_string(dict, "type", buf) == FAIL
5861 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
5862 return FAIL;
5863
5864 return OK;
5865}
5866
5867/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005868 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005869 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005870 */
5871 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005872get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005873{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005874 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01005875 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005876 qfline_T *qfp;
5877 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005878
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005879 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005880 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005881 qi = &ql_info;
5882 if (wp != NULL)
5883 {
5884 qi = GET_LOC_LIST(wp);
5885 if (qi == NULL)
5886 return FAIL;
5887 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005888 }
5889
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005890 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005891 qf_idx = qi->qf_curlist;
5892
Bram Moolenaar0398e002019-03-21 21:12:49 +01005893 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005894 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005895
Bram Moolenaar0398e002019-03-21 21:12:49 +01005896 qfl = qf_get_list(qi, qf_idx);
5897 if (qf_list_empty(qfl))
5898 return FAIL;
5899
Bram Moolenaara16123a2019-03-28 20:31:07 +01005900 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005901 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01005902 if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005903 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005904 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005905
Bram Moolenaar05159a02005-02-26 23:04:13 +00005906 return OK;
5907}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005908
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005909// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005910enum {
5911 QF_GETLIST_NONE = 0x0,
5912 QF_GETLIST_TITLE = 0x1,
5913 QF_GETLIST_ITEMS = 0x2,
5914 QF_GETLIST_NR = 0x4,
5915 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005916 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005917 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005918 QF_GETLIST_IDX = 0x40,
5919 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005920 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005921 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005922 QF_GETLIST_QFBUFNR = 0x400,
5923 QF_GETLIST_ALL = 0x7FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005924};
5925
5926/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005927 * Parse text from 'di' and return the quickfix list items.
5928 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005929 */
5930 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005931qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005932{
5933 int status = FAIL;
5934 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005935 char_u *errorformat = p_efm;
5936 dictitem_T *efm_di;
5937 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005938
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005939 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005940 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005941 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005942 // If errorformat is supplied then use it, otherwise use the 'efm'
5943 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005944 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5945 {
5946 if (efm_di->di_tv.v_type != VAR_STRING ||
5947 efm_di->di_tv.vval.v_string == NULL)
5948 return FAIL;
5949 errorformat = efm_di->di_tv.vval.v_string;
5950 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005951
Bram Moolenaar36538222017-09-02 19:51:44 +02005952 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005953 if (l == NULL)
5954 return FAIL;
5955
Bram Moolenaar2d67d302018-11-16 18:46:02 +01005956 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005957 if (qi != NULL)
5958 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005959 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005960 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5961 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005962 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005963 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005964 }
5965 free(qi);
5966 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005967 dict_add_list(retdict, "items", l);
5968 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005969 }
5970
5971 return status;
5972}
5973
5974/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005975 * Return the quickfix/location list window identifier in the current tabpage.
5976 */
5977 static int
5978qf_winid(qf_info_T *qi)
5979{
5980 win_T *win;
5981
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005982 // The quickfix window can be opened even if the quickfix list is not set
5983 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005984 if (qi == NULL)
5985 return 0;
5986 win = qf_find_win(qi);
5987 if (win != NULL)
5988 return win->w_id;
5989 return 0;
5990}
5991
5992/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005993 * Returns the number of the buffer displayed in the quickfix/location list
5994 * window. If there is no buffer associated with the list, then returns 0.
5995 */
5996 static int
5997qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
5998{
5999 return dict_add_number(retdict, "qfbufnr",
6000 (qi == NULL) ? 0 : qi->qf_bufnr);
6001}
6002
6003/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006004 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006005 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006006 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006007qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006008{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006009 int flags = QF_GETLIST_NONE;
6010
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006011 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006012 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006013 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006014 if (!loclist)
6015 // File window ID is applicable only to location list windows
6016 flags &= ~ QF_GETLIST_FILEWINID;
6017 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006018
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006019 if (dict_find(what, (char_u *)"title", -1) != NULL)
6020 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006021
Bram Moolenaara6d48492017-12-12 22:45:31 +01006022 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6023 flags |= QF_GETLIST_NR;
6024
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006025 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6026 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006027
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006028 if (dict_find(what, (char_u *)"context", -1) != NULL)
6029 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006030
Bram Moolenaara6d48492017-12-12 22:45:31 +01006031 if (dict_find(what, (char_u *)"id", -1) != NULL)
6032 flags |= QF_GETLIST_ID;
6033
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006034 if (dict_find(what, (char_u *)"items", -1) != NULL)
6035 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006036
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006037 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6038 flags |= QF_GETLIST_IDX;
6039
6040 if (dict_find(what, (char_u *)"size", -1) != NULL)
6041 flags |= QF_GETLIST_SIZE;
6042
Bram Moolenaarb254af32017-12-18 19:48:58 +01006043 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6044 flags |= QF_GETLIST_TICK;
6045
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006046 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6047 flags |= QF_GETLIST_FILEWINID;
6048
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006049 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6050 flags |= QF_GETLIST_QFBUFNR;
6051
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006052 return flags;
6053}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006054
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006055/*
6056 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6057 * If 'nr' and 'id' are not present in 'what' then return the current
6058 * quickfix list index.
6059 * If 'nr' is zero then return the current quickfix list index.
6060 * If 'nr' is '$' then return the last quickfix list index.
6061 * If 'id' is present then return the index of the quickfix list with that id.
6062 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6063 * Return -1, if quickfix list is not present or if the stack is empty.
6064 */
6065 static int
6066qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6067{
6068 int qf_idx;
6069 dictitem_T *di;
6070
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006071 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006072 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6073 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006074 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006075 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006076 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006077 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006078 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006079 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006080 qf_idx = di->di_tv.vval.v_number - 1;
6081 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006082 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006083 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006084 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006085 else if (di->di_tv.v_type == VAR_STRING
6086 && di->di_tv.vval.v_string != NULL
6087 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006088 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006089 qf_idx = qi->qf_listcount - 1;
6090 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006091 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006092 }
6093
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006094 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6095 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006096 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006097 if (di->di_tv.v_type == VAR_NUMBER)
6098 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006099 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006100 if (di->di_tv.vval.v_number != 0)
6101 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6102 }
6103 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006104 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006105 }
6106
6107 return qf_idx;
6108}
6109
6110/*
6111 * Return default values for quickfix list properties in retdict.
6112 */
6113 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006114qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006115{
6116 int status = OK;
6117
6118 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006119 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006120 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6121 {
6122 list_T *l = list_alloc();
6123 if (l != NULL)
6124 status = dict_add_list(retdict, "items", l);
6125 else
6126 status = FAIL;
6127 }
6128 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006129 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006130 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006131 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006132 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006133 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006134 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006135 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006136 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006137 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006138 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006139 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006140 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006141 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006142 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006143 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006144 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6145 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006146
6147 return status;
6148}
6149
6150/*
6151 * Return the quickfix list title as 'title' in retdict
6152 */
6153 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006154qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006155{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006156 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006157}
6158
6159/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006160 * Returns the identifier of the window used to display files from a location
6161 * list. If there is no associated window, then returns 0. Useful only when
6162 * called from a location list window.
6163 */
6164 static int
6165qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6166{
6167 int winid = 0;
6168
6169 if (wp != NULL && IS_LL_WINDOW(wp))
6170 {
6171 win_T *ll_wp = qf_find_win_with_loclist(qi);
6172 if (ll_wp != NULL)
6173 winid = ll_wp->w_id;
6174 }
6175
6176 return dict_add_number(retdict, "filewinid", winid);
6177}
6178
6179/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006180 * Return the quickfix list items/entries as 'items' in retdict
6181 */
6182 static int
6183qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6184{
6185 int status = OK;
6186 list_T *l = list_alloc();
6187 if (l != NULL)
6188 {
6189 (void)get_errorlist(qi, NULL, qf_idx, l);
6190 dict_add_list(retdict, "items", l);
6191 }
6192 else
6193 status = FAIL;
6194
6195 return status;
6196}
6197
6198/*
6199 * Return the quickfix list context (if any) as 'context' in retdict.
6200 */
6201 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006202qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006203{
6204 int status;
6205 dictitem_T *di;
6206
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006207 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006208 {
6209 di = dictitem_alloc((char_u *)"context");
6210 if (di != NULL)
6211 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006212 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006213 status = dict_add(retdict, di);
6214 if (status == FAIL)
6215 dictitem_free(di);
6216 }
6217 else
6218 status = FAIL;
6219 }
6220 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006221 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006222
6223 return status;
6224}
6225
6226/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006227 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006228 */
6229 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01006230qf_getprop_idx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006231{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006232 int curidx = qfl->qf_index;
6233 if (qf_list_empty(qfl))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006234 // For empty lists, current index is set to 0
6235 curidx = 0;
6236 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006237}
6238
6239/*
6240 * Return quickfix/location list details (title) as a
6241 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6242 * then current list is used. Otherwise the specified list is used.
6243 */
6244 int
6245qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6246{
6247 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006248 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006249 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006250 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006251 dictitem_T *di;
6252 int flags = QF_GETLIST_NONE;
6253
6254 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6255 return qf_get_list_from_lines(what, di, retdict);
6256
6257 if (wp != NULL)
6258 qi = GET_LOC_LIST(wp);
6259
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006260 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006261
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006262 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006263 qf_idx = qf_getprop_qfidx(qi, what);
6264
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006265 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006266 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006267 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006268
Bram Moolenaar0398e002019-03-21 21:12:49 +01006269 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006270
Bram Moolenaard823fa92016-08-12 16:29:27 +02006271 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006272 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006273 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006274 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006275 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006276 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006277 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006278 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006279 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006280 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006281 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006282 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006283 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar0398e002019-03-21 21:12:49 +01006284 status = qf_getprop_idx(qfl, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006285 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006286 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006287 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006288 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006289 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6290 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006291 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6292 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006293
Bram Moolenaard823fa92016-08-12 16:29:27 +02006294 return status;
6295}
6296
6297/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006298 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006299 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6300 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006301 */
6302 static int
6303qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01006304 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006305 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006306 int first_entry,
6307 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006308{
6309 static int did_bufnr_emsg;
6310 char_u *filename, *module, *pattern, *text, *type;
6311 int bufnum, valid, status, col, vcol, nr;
6312 long lnum;
6313
6314 if (first_entry)
6315 did_bufnr_emsg = FALSE;
6316
Bram Moolenaar8f667172018-12-14 15:38:31 +01006317 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6318 module = dict_get_string(d, (char_u *)"module", TRUE);
6319 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6320 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6321 col = (int)dict_get_number(d, (char_u *)"col");
6322 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6323 nr = (int)dict_get_number(d, (char_u *)"nr");
6324 type = dict_get_string(d, (char_u *)"type", TRUE);
6325 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6326 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006327 if (text == NULL)
6328 text = vim_strsave((char_u *)"");
6329
6330 valid = TRUE;
6331 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6332 valid = FALSE;
6333
6334 // Mark entries with non-existing buffer number as not valid. Give the
6335 // error message only once.
6336 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6337 {
6338 if (!did_bufnr_emsg)
6339 {
6340 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006341 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006342 }
6343 valid = FALSE;
6344 bufnum = 0;
6345 }
6346
6347 // If the 'valid' field is present it overrules the detected value.
6348 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006349 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006350
Bram Moolenaar0398e002019-03-21 21:12:49 +01006351 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006352 NULL, // dir
6353 filename,
6354 module,
6355 bufnum,
6356 text,
6357 lnum,
6358 col,
6359 vcol, // vis_col
6360 pattern, // search pattern
6361 nr,
6362 type == NULL ? NUL : *type,
6363 valid);
6364
6365 vim_free(filename);
6366 vim_free(module);
6367 vim_free(pattern);
6368 vim_free(text);
6369 vim_free(type);
6370
Bram Moolenaar9752c722018-12-22 16:49:34 +01006371 if (valid)
6372 *valid_entry = TRUE;
6373
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006374 return status;
6375}
6376
6377/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006378 * Add list of entries to quickfix/location list. Each list entry is
6379 * a dictionary with item information.
6380 */
6381 static int
6382qf_add_entries(
6383 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006384 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006385 list_T *list,
6386 char_u *title,
6387 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006388{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006389 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006390 listitem_T *li;
6391 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006392 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006393 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006394 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006395
Bram Moolenaara3921f42017-06-04 15:30:34 +02006396 if (action == ' ' || qf_idx == qi->qf_listcount)
6397 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006398 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006399 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006400 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006401 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006402 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01006403 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006404 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006405 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006406 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006407 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006408 qf_free_items(qfl);
6409 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006410 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006411
6412 for (li = list->lv_first; li != NULL; li = li->li_next)
6413 {
6414 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006415 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006416
6417 d = li->li_tv.vval.v_dict;
6418 if (d == NULL)
6419 continue;
6420
Bram Moolenaar0398e002019-03-21 21:12:49 +01006421 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006422 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02006423 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006424 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006425 }
6426
Bram Moolenaar9752c722018-12-22 16:49:34 +01006427 // Check if any valid error entries are added to the list.
6428 if (valid_entry)
6429 qfl->qf_nonevalid = FALSE;
6430 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006431 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006432 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006433
6434 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006435 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006436 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006437
6438 // Update the current error index if not appending to the list or if the
6439 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006440 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01006441 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006442
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006443 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006444 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006445
6446 return retval;
6447}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006448
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006449/*
6450 * Get the quickfix list index from 'nr' or 'id'
6451 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006452 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006453qf_setprop_get_qfidx(
6454 qf_info_T *qi,
6455 dict_T *what,
6456 int action,
6457 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006458{
6459 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006460 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006461
Bram Moolenaard823fa92016-08-12 16:29:27 +02006462 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6463 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006464 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006465 if (di->di_tv.v_type == VAR_NUMBER)
6466 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006467 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006468 if (di->di_tv.vval.v_number != 0)
6469 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006470
Bram Moolenaar55b69262017-08-13 13:42:01 +02006471 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6472 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006473 // When creating a new list, accept qf_idx pointing to the next
6474 // non-available list and add the new list at the end of the
6475 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006476 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006477 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006478 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006479 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006480 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006481 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006482 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006483 }
6484 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006485 && di->di_tv.vval.v_string != NULL
6486 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006487 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006488 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006489 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006490 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006491 qf_idx = 0;
6492 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006493 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006494 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006495 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006496 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006497 }
6498
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006499 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006500 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006501 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006502 if (di->di_tv.v_type != VAR_NUMBER)
6503 return INVALID_QFIDX;
6504
6505 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006506 }
6507
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006508 return qf_idx;
6509}
6510
6511/*
6512 * Set the quickfix list title.
6513 */
6514 static int
6515qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6516{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006517 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006518
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006519 if (di->di_tv.v_type != VAR_STRING)
6520 return FAIL;
6521
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006522 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006523 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006524 if (qf_idx == qi->qf_curlist)
6525 qf_update_win_titlevar(qi);
6526
6527 return OK;
6528}
6529
6530/*
6531 * Set quickfix list items/entries.
6532 */
6533 static int
6534qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6535{
6536 int retval = FAIL;
6537 char_u *title_save;
6538
6539 if (di->di_tv.v_type != VAR_LIST)
6540 return FAIL;
6541
6542 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6543 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6544 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006545 vim_free(title_save);
6546
6547 return retval;
6548}
6549
6550/*
6551 * Set quickfix list items/entries from a list of lines.
6552 */
6553 static int
6554qf_setprop_items_from_lines(
6555 qf_info_T *qi,
6556 int qf_idx,
6557 dict_T *what,
6558 dictitem_T *di,
6559 int action)
6560{
6561 char_u *errorformat = p_efm;
6562 dictitem_T *efm_di;
6563 int retval = FAIL;
6564
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006565 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006566 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6567 {
6568 if (efm_di->di_tv.v_type != VAR_STRING ||
6569 efm_di->di_tv.vval.v_string == NULL)
6570 return FAIL;
6571 errorformat = efm_di->di_tv.vval.v_string;
6572 }
6573
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006574 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006575 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6576 return FAIL;
6577
6578 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006579 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006580 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6581 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6582 retval = OK;
6583
6584 return retval;
6585}
6586
6587/*
6588 * Set quickfix list context.
6589 */
6590 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006591qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006592{
6593 typval_T *ctx;
6594
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006595 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006596 ctx = alloc_tv();
6597 if (ctx != NULL)
6598 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006599 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006600
6601 return OK;
6602}
6603
6604/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006605 * Set the current index in the specified quickfix list
6606 */
6607 static int
6608qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
6609{
6610 int denote = FALSE;
6611 int newidx;
6612 int old_qfidx;
6613 qfline_T *qf_ptr;
6614
6615 // If the specified index is '$', then use the last entry
6616 if (di->di_tv.v_type == VAR_STRING
6617 && di->di_tv.vval.v_string != NULL
6618 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
6619 newidx = qfl->qf_count;
6620 else
6621 {
6622 // Otherwise use the specified index
6623 newidx = tv_get_number_chk(&di->di_tv, &denote);
6624 if (denote)
6625 return FAIL;
6626 }
6627
6628 if (newidx < 1) // sanity check
6629 return FAIL;
6630 if (newidx > qfl->qf_count)
6631 newidx = qfl->qf_count;
6632
6633 old_qfidx = qfl->qf_index;
6634 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
6635 if (qf_ptr == NULL)
6636 return FAIL;
6637 qfl->qf_ptr = qf_ptr;
6638 qfl->qf_index = newidx;
6639
6640 // If the current list is modified and it is displayed in the quickfix
6641 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006642 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006643 qf_win_pos_update(qi, old_qfidx);
6644
6645 return OK;
6646}
6647
6648/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006649 * Set quickfix/location list properties (title, items, context).
6650 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006651 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006652 */
6653 static int
6654qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6655{
6656 dictitem_T *di;
6657 int retval = FAIL;
6658 int qf_idx;
6659 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006660 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006661
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006662 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006663 newlist = TRUE;
6664
6665 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006666 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006667 return FAIL;
6668
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006669 if (newlist)
6670 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006671 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006672 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006673 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006674 }
6675
Bram Moolenaar0398e002019-03-21 21:12:49 +01006676 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006677 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006678 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006679 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006680 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006681 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006682 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006683 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006684 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006685 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6686 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006687
Bram Moolenaarb254af32017-12-18 19:48:58 +01006688 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006689 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006690
Bram Moolenaard823fa92016-08-12 16:29:27 +02006691 return retval;
6692}
6693
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006694/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006695 * Free the entire quickfix/location list stack.
6696 * If the quickfix/location list window is open, then clear it.
6697 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006698 static void
6699qf_free_stack(win_T *wp, qf_info_T *qi)
6700{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006701 win_T *qfwin = qf_find_win(qi);
6702 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006703
6704 if (qfwin != NULL)
6705 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006706 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006707 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006708 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006709 qf_update_buffer(qi, NULL);
6710 }
6711
6712 if (wp != NULL && IS_LL_WINDOW(wp))
6713 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006714 // If in the location list window, then use the non-location list
6715 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006716 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006717 if (llwin != NULL)
6718 wp = llwin;
6719 }
6720
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006721 qf_free_all(wp);
6722 if (wp == NULL)
6723 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006724 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006725 qi->qf_curlist = 0;
6726 qi->qf_listcount = 0;
6727 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006728 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006729 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006730 // If the location list window is open, then create a new empty
6731 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006732 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006733
Bram Moolenaar95946f12019-03-31 15:31:59 +02006734 if (new_ll != NULL)
6735 {
6736 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006737
Bram Moolenaar95946f12019-03-31 15:31:59 +02006738 // first free the list reference in the location list window
6739 ll_free_all(&qfwin->w_llist_ref);
6740
6741 qfwin->w_llist_ref = new_ll;
6742 if (wp != qfwin)
6743 win_set_loclist(wp, new_ll);
6744 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006745 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006746}
6747
Bram Moolenaard823fa92016-08-12 16:29:27 +02006748/*
6749 * Populate the quickfix list with the items supplied in the list
6750 * of dictionaries. "title" will be copied to w:quickfix_title.
6751 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6752 */
6753 int
6754set_errorlist(
6755 win_T *wp,
6756 list_T *list,
6757 int action,
6758 char_u *title,
6759 dict_T *what)
6760{
6761 qf_info_T *qi = &ql_info;
6762 int retval = OK;
6763
6764 if (wp != NULL)
6765 {
6766 qi = ll_get_or_alloc_list(wp);
6767 if (qi == NULL)
6768 return FAIL;
6769 }
6770
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006771 if (action == 'f')
6772 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006773 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006774 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006775 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006776 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006777
6778 incr_quickfix_busy();
6779
6780 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006781 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006782 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006783 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006784 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006785 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006786 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01006787 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006788
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006789 decr_quickfix_busy();
6790
Bram Moolenaard823fa92016-08-12 16:29:27 +02006791 return retval;
6792}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006793
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006794/*
6795 * Mark the context as in use for all the lists in a quickfix stack.
6796 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006797 static int
6798mark_quickfix_ctx(qf_info_T *qi, int copyID)
6799{
6800 int i;
6801 int abort = FALSE;
6802 typval_T *ctx;
6803
6804 for (i = 0; i < LISTCOUNT && !abort; ++i)
6805 {
6806 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006807 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6808 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006809 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6810 }
6811
6812 return abort;
6813}
6814
6815/*
6816 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006817 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006818 */
6819 int
6820set_ref_in_quickfix(int copyID)
6821{
6822 int abort = FALSE;
6823 tabpage_T *tp;
6824 win_T *win;
6825
6826 abort = mark_quickfix_ctx(&ql_info, copyID);
6827 if (abort)
6828 return abort;
6829
6830 FOR_ALL_TAB_WINDOWS(tp, win)
6831 {
6832 if (win->w_llist != NULL)
6833 {
6834 abort = mark_quickfix_ctx(win->w_llist, copyID);
6835 if (abort)
6836 return abort;
6837 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006838 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6839 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006840 // In a location list window and none of the other windows is
6841 // referring to this location list. Mark the location list
6842 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006843 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6844 if (abort)
6845 return abort;
6846 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006847 }
6848
6849 return abort;
6850}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006851#endif
6852
Bram Moolenaar81695252004-12-29 20:58:21 +00006853/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01006854 * Return the autocmd name for the :cbuffer Ex commands
6855 */
6856 static char_u *
6857cbuffer_get_auname(cmdidx_T cmdidx)
6858{
6859 switch (cmdidx)
6860 {
6861 case CMD_cbuffer: return (char_u *)"cbuffer";
6862 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
6863 case CMD_caddbuffer: return (char_u *)"caddbuffer";
6864 case CMD_lbuffer: return (char_u *)"lbuffer";
6865 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
6866 case CMD_laddbuffer: return (char_u *)"laddbuffer";
6867 default: return NULL;
6868 }
6869}
6870
6871/*
6872 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
6873 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
6874 */
6875 static int
6876cbuffer_process_args(
6877 exarg_T *eap,
6878 buf_T **bufp,
6879 linenr_T *line1,
6880 linenr_T *line2)
6881{
6882 buf_T *buf = NULL;
6883
6884 if (*eap->arg == NUL)
6885 buf = curbuf;
6886 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6887 buf = buflist_findnr(atoi((char *)eap->arg));
6888
6889 if (buf == NULL)
6890 {
6891 emsg(_(e_invarg));
6892 return FAIL;
6893 }
6894
6895 if (buf->b_ml.ml_mfp == NULL)
6896 {
6897 emsg(_("E681: Buffer is not loaded"));
6898 return FAIL;
6899 }
6900
6901 if (eap->addr_count == 0)
6902 {
6903 eap->line1 = 1;
6904 eap->line2 = buf->b_ml.ml_line_count;
6905 }
6906
6907 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6908 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6909 {
6910 emsg(_(e_invrange));
6911 return FAIL;
6912 }
6913
6914 *line1 = eap->line1;
6915 *line2 = eap->line2;
6916 *bufp = buf;
6917
6918 return OK;
6919}
6920
6921/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006922 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006923 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006924 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006925 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006926 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006927 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006928 */
6929 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006930ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006931{
6932 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02006933 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006934 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006935 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006936 int_u save_qfid;
6937 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01006938 char_u *qf_title;
6939 linenr_T line1;
6940 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006941
Bram Moolenaara16123a2019-03-28 20:31:07 +01006942 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01006943 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01006944 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006945 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006946#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006947 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006948 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006949#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006950 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006951
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006952 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02006953 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6954 if (qi == NULL)
6955 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006956
Bram Moolenaara16123a2019-03-28 20:31:07 +01006957 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
6958 return;
6959
6960 qf_title = qf_cmdtitle(*eap->cmdlinep);
6961
6962 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006963 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01006964 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6965 (char *)qf_title, (char *)buf->b_sfname);
6966 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006967 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006968
6969 incr_quickfix_busy();
6970
6971 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
6972 (eap->cmdidx != CMD_caddbuffer
6973 && eap->cmdidx != CMD_laddbuffer),
6974 line1, line2,
6975 qf_title, NULL);
6976 if (qf_stack_empty(qi))
6977 {
6978 decr_quickfix_busy();
6979 return;
6980 }
6981 if (res >= 0)
6982 qf_list_changed(qf_get_curlist(qi));
6983
6984 // Remember the current quickfix list identifier, so that we can
6985 // check for autocommands changing the current quickfix list.
6986 save_qfid = qf_get_curlist(qi)->qf_id;
6987 if (au_name != NULL)
6988 {
6989 buf_T *curbuf_old = curbuf;
6990
6991 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
6992 TRUE, curbuf);
6993 if (curbuf != curbuf_old)
6994 // Autocommands changed buffer, don't jump now, "qi" may
6995 // be invalid.
6996 res = 0;
6997 }
6998 // Jump to the first error for a new list and if autocmds didn't
6999 // free the list.
7000 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7001 eap->cmdidx == CMD_lbuffer)
7002 && qflist_valid(wp, save_qfid))
7003 // display the first error
7004 qf_jump_first(qi, save_qfid, eap->forceit);
7005
7006 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007007}
7008
Bram Moolenaar1e015462005-09-25 22:16:38 +00007009#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007010/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007011 * Return the autocmd name for the :cexpr Ex commands.
7012 */
7013 static char_u *
7014cexpr_get_auname(cmdidx_T cmdidx)
7015{
7016 switch (cmdidx)
7017 {
7018 case CMD_cexpr: return (char_u *)"cexpr";
7019 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7020 case CMD_caddexpr: return (char_u *)"caddexpr";
7021 case CMD_lexpr: return (char_u *)"lexpr";
7022 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7023 case CMD_laddexpr: return (char_u *)"laddexpr";
7024 default: return NULL;
7025 }
7026}
7027
7028/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007029 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7030 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007031 */
7032 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007033ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007034{
7035 typval_T *tv;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007036 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007037 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007038 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007039 int_u save_qfid;
7040 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007041
Bram Moolenaara16123a2019-03-28 20:31:07 +01007042 au_name = cexpr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007043 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7044 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007045 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007046#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007047 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007048 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007049#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007050 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007051
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007052 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7053 if (qi == NULL)
7054 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007055
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007056 // Evaluate the expression. When the result is a string or a list we can
7057 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007058 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007059 if (tv != NULL)
7060 {
7061 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7062 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7063 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007064 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007065 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00007066 (eap->cmdidx != CMD_caddexpr
7067 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007068 (linenr_T)0, (linenr_T)0,
7069 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007070 if (qf_stack_empty(qi))
7071 {
7072 decr_quickfix_busy();
7073 goto cleanup;
7074 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01007075 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007076 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007077
7078 // Remember the current quickfix list identifier, so that we can
7079 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007080 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007081 if (au_name != NULL)
7082 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7083 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007084
7085 // Jump to the first error for a new list and if autocmds didn't
7086 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02007087 if (res > 0 && (eap->cmdidx == CMD_cexpr
7088 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007089 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02007090 // display the first error
7091 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007092 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00007093 }
7094 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007095 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007096cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00007097 free_tv(tv);
7098 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007099}
Bram Moolenaar1e015462005-09-25 22:16:38 +00007100#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007101
7102/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007103 * Get the location list for ":lhelpgrep"
7104 */
7105 static qf_info_T *
7106hgr_get_ll(int *new_ll)
7107{
7108 win_T *wp;
7109 qf_info_T *qi;
7110
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007111 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007112 if (bt_help(curwin->w_buffer))
7113 wp = curwin;
7114 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007115 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007116 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007117
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007118 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007119 qi = NULL;
7120 else
7121 qi = wp->w_llist;
7122
7123 if (qi == NULL)
7124 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007125 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007126 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007127 return NULL;
7128 *new_ll = TRUE;
7129 }
7130
7131 return qi;
7132}
7133
7134/*
7135 * Search for a pattern in a help file.
7136 */
7137 static void
7138hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007139 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007140 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007141 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007142 regmatch_T *p_regmatch)
7143{
7144 FILE *fd;
7145 long lnum;
7146
7147 fd = mch_fopen((char *)fname, "r");
7148 if (fd == NULL)
7149 return;
7150
7151 lnum = 1;
7152 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7153 {
7154 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007155
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007156 // Convert a line if 'encoding' is not utf-8 and
7157 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007158 if (p_vc->vc_type != CONV_NONE
7159 && has_non_ascii(IObuff))
7160 {
7161 line = string_convert(p_vc, IObuff, NULL);
7162 if (line == NULL)
7163 line = IObuff;
7164 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007165
7166 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7167 {
7168 int l = (int)STRLEN(line);
7169
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007170 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007171 while (l > 0 && line[l - 1] <= ' ')
7172 line[--l] = NUL;
7173
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007174 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007175 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007176 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007177 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007178 0,
7179 line,
7180 lnum,
7181 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007182 + 1, // col
7183 FALSE, // vis_col
7184 NULL, // search pattern
7185 0, // nr
7186 1, // type
7187 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02007188 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007189 {
7190 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007191 if (line != IObuff)
7192 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007193 break;
7194 }
7195 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007196 if (line != IObuff)
7197 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007198 ++lnum;
7199 line_breakcheck();
7200 }
7201 fclose(fd);
7202}
7203
7204/*
7205 * Search for a pattern in all the help files in the doc directory under
7206 * the given directory.
7207 */
7208 static void
7209hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007210 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007211 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007212 regmatch_T *p_regmatch,
7213 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007214#ifdef FEAT_MULTI_LANG
7215 , char_u *lang
7216#endif
7217 )
7218{
7219 int fcount;
7220 char_u **fnames;
7221 int fi;
7222
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007223 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007224 add_pathsep(dirname);
7225 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7226 if (gen_expand_wildcards(1, &dirname, &fcount,
7227 &fnames, EW_FILE|EW_SILENT) == OK
7228 && fcount > 0)
7229 {
7230 for (fi = 0; fi < fcount && !got_int; ++fi)
7231 {
7232#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007233 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007234 if (lang != NULL
7235 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007236 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007237 && !(STRNICMP(lang, "en", 2) == 0
7238 && STRNICMP("txt", fnames[fi]
7239 + STRLEN(fnames[fi]) - 3, 3) == 0))
7240 continue;
7241#endif
7242
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007243 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007244 }
7245 FreeWild(fcount, fnames);
7246 }
7247}
7248
7249/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007250 * Search for a pattern in all the help files in the 'runtimepath'
7251 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007252 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007253 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007254 */
7255 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007256hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007257{
7258 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007259
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007260 vimconv_T vc;
7261
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007262 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7263 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007264 vc.vc_type = CONV_NONE;
7265 if (!enc_utf8)
7266 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007267
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007268 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007269 p = p_rtp;
7270 while (*p != NUL && !got_int)
7271 {
7272 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7273
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007274 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007275#ifdef FEAT_MULTI_LANG
7276 , lang
7277#endif
7278 );
7279 }
7280
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007281 if (vc.vc_type != CONV_NONE)
7282 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007283}
7284
7285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 * ":helpgrep {pattern}"
7287 */
7288 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007289ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290{
7291 regmatch_T regmatch;
7292 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007293 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007294 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007295 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007296 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297
Bram Moolenaar73633f82012-01-20 13:39:07 +01007298 switch (eap->cmdidx)
7299 {
7300 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7301 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7302 default: break;
7303 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007304 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7305 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007306 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007307#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007308 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007309 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007310#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007311 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007312
Bram Moolenaar39665952018-08-15 20:59:48 +02007313 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007314 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007315 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007316 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007317 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007318 }
7319
Bram Moolenaara16123a2019-03-28 20:31:07 +01007320 // Make 'cpoptions' empty, the 'l' flag should not be used here.
7321 save_cpo = p_cpo;
7322 p_cpo = empty_option;
7323
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007324 incr_quickfix_busy();
7325
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007326#ifdef FEAT_MULTI_LANG
7327 // Check for a specified language
7328 lang = check_help_lang(eap->arg);
7329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7331 regmatch.rm_ic = FALSE;
7332 if (regmatch.regprog != NULL)
7333 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007334 qf_list_T *qfl;
7335
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007336 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007337 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007338 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007340 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007341
Bram Moolenaar473de612013-06-08 18:19:48 +02007342 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007344 qfl->qf_nonevalid = FALSE;
7345 qfl->qf_ptr = qfl->qf_start;
7346 qfl->qf_index = 1;
7347 qf_list_changed(qfl);
7348 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
7350
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007351 if (p_cpo == empty_option)
7352 p_cpo = save_cpo;
7353 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007354 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007355 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356
Bram Moolenaar73633f82012-01-20 13:39:07 +01007357 if (au_name != NULL)
7358 {
7359 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7360 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007361 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007362 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007363 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007364 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007365 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007366 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007367 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007368
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007369 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007370 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007371 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007372 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007373 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007374
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007375 decr_quickfix_busy();
7376
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007377 if (eap->cmdidx == CMD_lhelpgrep)
7378 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007379 // If the help window is not opened or if it already points to the
7380 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007381 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007382 {
7383 if (new_qi)
7384 ll_free_all(&qi);
7385 }
7386 else if (curwin->w_llist == NULL)
7387 curwin->w_llist = qi;
7388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389}
7390
7391#endif /* FEAT_QUICKFIX */