blob: 5ffa1aa0d752ed0b955b4b605cff8ba0e3a13207 [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 Moolenaar09037502018-09-25 22:08:14 +02002164 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2165 */
2166 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002167copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002168{
2169 int i;
2170 qfline_T *from_qfp;
2171 qfline_T *prevp;
2172
2173 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002174 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002175 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002176 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002177 NULL,
2178 NULL,
2179 from_qfp->qf_module,
2180 0,
2181 from_qfp->qf_text,
2182 from_qfp->qf_lnum,
2183 from_qfp->qf_col,
2184 from_qfp->qf_viscol,
2185 from_qfp->qf_pattern,
2186 from_qfp->qf_nr,
2187 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002188 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002189 return FAIL;
2190
2191 // qf_add_entry() will not set the qf_num field, as the
2192 // directory and file names are not supplied. So the qf_fnum
2193 // field is copied here.
2194 prevp = to_qfl->qf_last;
2195 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2196 prevp->qf_type = from_qfp->qf_type; // error type
2197 if (from_qfl->qf_ptr == from_qfp)
2198 to_qfl->qf_ptr = prevp; // current location
2199 }
2200
2201 return OK;
2202}
2203
2204/*
2205 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2206 */
2207 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002208copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002209{
2210 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002211 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002212 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2213 to_qfl->qf_count = 0;
2214 to_qfl->qf_index = 0;
2215 to_qfl->qf_start = NULL;
2216 to_qfl->qf_last = NULL;
2217 to_qfl->qf_ptr = NULL;
2218 if (from_qfl->qf_title != NULL)
2219 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2220 else
2221 to_qfl->qf_title = NULL;
2222 if (from_qfl->qf_ctx != NULL)
2223 {
2224 to_qfl->qf_ctx = alloc_tv();
2225 if (to_qfl->qf_ctx != NULL)
2226 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2227 }
2228 else
2229 to_qfl->qf_ctx = NULL;
2230
2231 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002232 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002233 return FAIL;
2234
2235 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2236
2237 // Assign a new ID for the location list
2238 to_qfl->qf_id = ++last_qf_id;
2239 to_qfl->qf_changedtick = 0L;
2240
2241 // When no valid entries are present in the list, qf_ptr points to
2242 // the first item in the list
2243 if (to_qfl->qf_nonevalid)
2244 {
2245 to_qfl->qf_ptr = to_qfl->qf_start;
2246 to_qfl->qf_index = 1;
2247 }
2248
2249 return OK;
2250}
2251
2252/*
2253 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002254 */
2255 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002256copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002257{
2258 qf_info_T *qi;
2259 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002260
Bram Moolenaar09037502018-09-25 22:08:14 +02002261 // When copying from a location list window, copy the referenced
2262 // location list. For other windows, copy the location list for
2263 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002264 if (IS_LL_WINDOW(from))
2265 qi = from->w_llist_ref;
2266 else
2267 qi = from->w_llist;
2268
Bram Moolenaar09037502018-09-25 22:08:14 +02002269 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002270 return;
2271
Bram Moolenaar09037502018-09-25 22:08:14 +02002272 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002273 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002274 return;
2275
2276 to->w_llist->qf_listcount = qi->qf_listcount;
2277
Bram Moolenaar09037502018-09-25 22:08:14 +02002278 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002279 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002280 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002281 to->w_llist->qf_curlist = idx;
2282
Bram Moolenaar0398e002019-03-21 21:12:49 +01002283 if (copy_loclist(qf_get_list(qi, idx),
2284 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002285 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002286 qf_free_all(to);
2287 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002288 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002289 }
2290
Bram Moolenaar09037502018-09-25 22:08:14 +02002291 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002292}
2293
2294/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002295 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002296 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 */
2298 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002299qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300{
Bram Moolenaar82404332016-07-10 17:00:38 +02002301 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002302 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002303 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002304
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002305 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
Bram Moolenaare60acc12011-05-10 16:41:25 +02002308#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002309 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002310#endif
2311#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002312 if (directory != NULL)
2313 slash_adjust(directory);
2314 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002315#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002316 if (directory != NULL && !vim_isAbsName(fname)
2317 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2318 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002319 // Here we check if the file really exists.
2320 // This should normally be true, but if make works without
2321 // "leaving directory"-messages we might have missed a
2322 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002323 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002326 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002327 if (directory)
2328 ptr = concat_fnames(directory, fname, TRUE);
2329 else
2330 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002332 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002333 bufname = ptr;
2334 }
2335 else
2336 bufname = fname;
2337
2338 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002339 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002340 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002341 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002342 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002344 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002345 {
2346 vim_free(qf_last_bufname);
2347 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2348 if (bufname == ptr)
2349 qf_last_bufname = bufname;
2350 else
2351 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002352 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002353 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002354 if (buf == NULL)
2355 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002356
Bram Moolenaarc1542742016-07-20 21:44:37 +02002357 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002358 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002359 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360}
2361
2362/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002363 * Push dirbuf onto the directory stack and return pointer to actual dir or
2364 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 */
2366 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002367qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368{
2369 struct dir_stack_T *ds_new;
2370 struct dir_stack_T *ds_ptr;
2371
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002372 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2374 if (ds_new == NULL)
2375 return NULL;
2376
2377 ds_new->next = *stackptr;
2378 *stackptr = ds_new;
2379
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002380 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 if (vim_isAbsName(dirbuf)
2382 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002383 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 (*stackptr)->dirname = vim_strsave(dirbuf);
2385 else
2386 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002387 // Okay we don't have an absolute path.
2388 // dirbuf must be a subdir of one of the directories on the stack.
2389 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 ds_new = (*stackptr)->next;
2391 (*stackptr)->dirname = NULL;
2392 while (ds_new)
2393 {
2394 vim_free((*stackptr)->dirname);
2395 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2396 TRUE);
2397 if (mch_isdir((*stackptr)->dirname) == TRUE)
2398 break;
2399
2400 ds_new = ds_new->next;
2401 }
2402
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002403 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 while ((*stackptr)->next != ds_new)
2405 {
2406 ds_ptr = (*stackptr)->next;
2407 (*stackptr)->next = (*stackptr)->next->next;
2408 vim_free(ds_ptr->dirname);
2409 vim_free(ds_ptr);
2410 }
2411
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002412 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 if (ds_new == NULL)
2414 {
2415 vim_free((*stackptr)->dirname);
2416 (*stackptr)->dirname = vim_strsave(dirbuf);
2417 }
2418 }
2419
2420 if ((*stackptr)->dirname != NULL)
2421 return (*stackptr)->dirname;
2422 else
2423 {
2424 ds_ptr = *stackptr;
2425 *stackptr = (*stackptr)->next;
2426 vim_free(ds_ptr);
2427 return NULL;
2428 }
2429}
2430
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431/*
2432 * pop dirbuf from the directory stack and return previous directory or NULL if
2433 * stack is empty
2434 */
2435 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002436qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437{
2438 struct dir_stack_T *ds_ptr;
2439
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002440 // TODO: Should we check if dirbuf is the directory on top of the stack?
2441 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002443 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 if (*stackptr != NULL)
2445 {
2446 ds_ptr = *stackptr;
2447 *stackptr = (*stackptr)->next;
2448 vim_free(ds_ptr->dirname);
2449 vim_free(ds_ptr);
2450 }
2451
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002452 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 return *stackptr ? (*stackptr)->dirname : NULL;
2454}
2455
2456/*
2457 * clean up directory stack
2458 */
2459 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002460qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461{
2462 struct dir_stack_T *ds_ptr;
2463
2464 while ((ds_ptr = *stackptr) != NULL)
2465 {
2466 *stackptr = (*stackptr)->next;
2467 vim_free(ds_ptr->dirname);
2468 vim_free(ds_ptr);
2469 }
2470}
2471
2472/*
2473 * Check in which directory of the directory stack the given file can be
2474 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002475 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 * Cleans up intermediate directory entries.
2477 *
2478 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002479 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 * ./
2481 * ./aa
2482 * ./aa/bb
2483 * ./bb
2484 * ./bb/x.c
2485 * and make says:
2486 * making all in aa
2487 * making all in bb
2488 * x.c:9: Error
2489 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2490 * qf_guess_filepath will return NULL.
2491 */
2492 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002493qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494{
2495 struct dir_stack_T *ds_ptr;
2496 struct dir_stack_T *ds_tmp;
2497 char_u *fullname;
2498
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002499 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002500 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 return NULL;
2502
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002503 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 fullname = NULL;
2505 while (ds_ptr)
2506 {
2507 vim_free(fullname);
2508 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2509
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002510 // If concat_fnames failed, just go on. The worst thing that can happen
2511 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2513 break;
2514
2515 ds_ptr = ds_ptr->next;
2516 }
2517
2518 vim_free(fullname);
2519
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002520 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002521 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002523 ds_tmp = qfl->qf_dir_stack->next;
2524 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 vim_free(ds_tmp->dirname);
2526 vim_free(ds_tmp);
2527 }
2528
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002529 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530}
2531
2532/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002533 * Returns TRUE if a quickfix/location list with the given identifier exists.
2534 */
2535 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002536qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002537{
2538 qf_info_T *qi = &ql_info;
2539 int i;
2540
2541 if (wp != NULL)
2542 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002543 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002544 if (qi == NULL)
2545 return FALSE;
2546 }
2547
2548 for (i = 0; i < qi->qf_listcount; ++i)
2549 if (qi->qf_lists[i].qf_id == qf_id)
2550 return TRUE;
2551
2552 return FALSE;
2553}
2554
2555/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002556 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002557 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002558 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002559 * Similar to location list.
2560 */
2561 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002562is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002563{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002564 qfline_T *qfp;
2565 int i;
2566
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002567 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002568 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2569 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002570 break;
2571
Bram Moolenaar95946f12019-03-31 15:31:59 +02002572 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002573 return FALSE;
2574
2575 return TRUE;
2576}
2577
2578/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002579 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002580 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002581 */
2582 static qfline_T *
2583get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002584 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002585 qfline_T *qf_ptr,
2586 int *qf_index,
2587 int dir)
2588{
2589 int idx;
2590 int old_qf_fnum;
2591
2592 idx = *qf_index;
2593 old_qf_fnum = qf_ptr->qf_fnum;
2594
2595 do
2596 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002597 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002598 return NULL;
2599 ++idx;
2600 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002601 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002602 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2603
2604 *qf_index = idx;
2605 return qf_ptr;
2606}
2607
2608/*
2609 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002610 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002611 */
2612 static qfline_T *
2613get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002614 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002615 qfline_T *qf_ptr,
2616 int *qf_index,
2617 int dir)
2618{
2619 int idx;
2620 int old_qf_fnum;
2621
2622 idx = *qf_index;
2623 old_qf_fnum = qf_ptr->qf_fnum;
2624
2625 do
2626 {
2627 if (idx == 1 || qf_ptr->qf_prev == NULL)
2628 return NULL;
2629 --idx;
2630 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002631 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002632 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2633
2634 *qf_index = idx;
2635 return qf_ptr;
2636}
2637
2638/*
2639 * Get the n'th (errornr) previous/next valid entry from the current entry in
2640 * the quickfix list.
2641 * dir == FORWARD or FORWARD_FILE: next valid entry
2642 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2643 */
2644 static qfline_T *
2645get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002646 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002647 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002648 int dir,
2649 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002650{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002651 qfline_T *qf_ptr = qfl->qf_ptr;
2652 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002653 qfline_T *prev_qf_ptr;
2654 int prev_index;
2655 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2656 char_u *err = e_no_more_items;
2657
2658 while (errornr--)
2659 {
2660 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002661 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002662
2663 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002664 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002665 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002666 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002667 if (qf_ptr == NULL)
2668 {
2669 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002670 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002671 if (err != NULL)
2672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002673 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002674 return NULL;
2675 }
2676 break;
2677 }
2678
2679 err = NULL;
2680 }
2681
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002682 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002683 return qf_ptr;
2684}
2685
2686/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002687 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2688 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002689 */
2690 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002691get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002692{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002693 qfline_T *qf_ptr = qfl->qf_ptr;
2694 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002695
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002696 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002697 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2698 {
2699 --qf_idx;
2700 qf_ptr = qf_ptr->qf_prev;
2701 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002702 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002703 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2704 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002705 {
2706 ++qf_idx;
2707 qf_ptr = qf_ptr->qf_next;
2708 }
2709
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002710 *new_qfidx = qf_idx;
2711 return qf_ptr;
2712}
2713
2714/*
2715 * Get a entry specied by 'errornr' and 'dir' from the current
2716 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2717 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2718 * Returns a pointer to the entry and the index of the new entry is stored in
2719 * 'new_qfidx'.
2720 */
2721 static qfline_T *
2722qf_get_entry(
2723 qf_list_T *qfl,
2724 int errornr,
2725 int dir,
2726 int *new_qfidx)
2727{
2728 qfline_T *qf_ptr = qfl->qf_ptr;
2729 int qfidx = qfl->qf_index;
2730
2731 if (dir != 0) // next/prev valid entry
2732 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2733 else if (errornr != 0) // go to specified number
2734 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2735
2736 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002737 return qf_ptr;
2738}
2739
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002740/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002741 * Find a window displaying a Vim help file.
2742 */
2743 static win_T *
2744qf_find_help_win(void)
2745{
2746 win_T *wp;
2747
2748 FOR_ALL_WINDOWS(wp)
2749 if (bt_help(wp->w_buffer))
2750 return wp;
2751
2752 return NULL;
2753}
2754
2755/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002756 * Set the location list for the specified window to 'qi'.
2757 */
2758 static void
2759win_set_loclist(win_T *wp, qf_info_T *qi)
2760{
2761 wp->w_llist = qi;
2762 qi->qf_refcount++;
2763}
2764
2765/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002766 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2767 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002768 */
2769 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002770jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002771{
2772 win_T *wp;
2773 int flags;
2774
Bram Moolenaarb2443732018-11-11 22:50:27 +01002775 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002776 wp = NULL;
2777 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002778 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002779 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2780 win_enter(wp, TRUE);
2781 else
2782 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002783 // Split off help window; put it at far top if no position
2784 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002785 flags = WSP_HELP;
2786 if (cmdmod.split == 0 && curwin->w_width != Columns
2787 && curwin->w_width < 80)
2788 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002789 // If the user asks to open a new window, then copy the location list.
2790 // Otherwise, don't copy the location list.
2791 if (IS_LL_STACK(qi) && !newwin)
2792 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002793
2794 if (win_split(0, flags) == FAIL)
2795 return FAIL;
2796
2797 *opened_window = TRUE;
2798
2799 if (curwin->w_height < p_hh)
2800 win_setheight((int)p_hh);
2801
Bram Moolenaarb2443732018-11-11 22:50:27 +01002802 // When using location list, the new window should use the supplied
2803 // location list. If the user asks to open a new window, then the new
2804 // window will get a copy of the location list.
2805 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002806 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002807 }
2808
2809 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002810 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002811
2812 return OK;
2813}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002814
2815/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002816 * Find a non-quickfix window in the current tabpage using the given location
2817 * list stack.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002818 * Returns NULL if a matching window is not found.
2819 */
2820 static win_T *
2821qf_find_win_with_loclist(qf_info_T *ll)
2822{
2823 win_T *wp;
2824
2825 FOR_ALL_WINDOWS(wp)
2826 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2827 return wp;
2828
2829 return NULL;
2830}
2831
2832/*
2833 * Find a window containing a normal buffer
2834 */
2835 static win_T *
2836qf_find_win_with_normal_buf(void)
2837{
2838 win_T *wp;
2839
2840 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002841 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002842 return wp;
2843
2844 return NULL;
2845}
2846
2847/*
2848 * Go to a window in any tabpage containing the specified file. Returns TRUE
2849 * if successfully jumped to the window. Otherwise returns FALSE.
2850 */
2851 static int
2852qf_goto_tabwin_with_file(int fnum)
2853{
2854 tabpage_T *tp;
2855 win_T *wp;
2856
2857 FOR_ALL_TAB_WINDOWS(tp, wp)
2858 if (wp->w_buffer->b_fnum == fnum)
2859 {
2860 goto_tabpage_win(tp, wp);
2861 return TRUE;
2862 }
2863
2864 return FALSE;
2865}
2866
2867/*
2868 * Create a new window to show a file above the quickfix window. Called when
2869 * only the quickfix window is present.
2870 */
2871 static int
2872qf_open_new_file_win(qf_info_T *ll_ref)
2873{
2874 int flags;
2875
2876 flags = WSP_ABOVE;
2877 if (ll_ref != NULL)
2878 flags |= WSP_NEWLOC;
2879 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002880 return FAIL; // not enough room for window
2881 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002882 swb_flags = 0;
2883 RESET_BINDING(curwin);
2884 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002885 // The new window should use the location list from the
2886 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002887 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002888 return OK;
2889}
2890
2891/*
2892 * Go to a window that shows the right buffer. If the window is not found, go
2893 * to the window just above the location list window. This is used for opening
2894 * a file from a location window and not from a quickfix window. If some usable
2895 * window is previously found, then it is supplied in 'use_win'.
2896 */
2897 static void
2898qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2899{
2900 win_T *win = use_win;
2901
2902 if (win == NULL)
2903 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002904 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002905 FOR_ALL_WINDOWS(win)
2906 if (win->w_buffer->b_fnum == qf_fnum)
2907 break;
2908 if (win == NULL)
2909 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002910 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002911 win = curwin;
2912 do
2913 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002914 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002915 break;
2916 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002917 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002918 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002919 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002920 } while (win != curwin);
2921 }
2922 }
2923 win_goto(win);
2924
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002925 // If the location list for the window is not set, then set it
2926 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01002927 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002928 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002929}
2930
2931/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002932 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2933 * not found, then go to the window just above the quickfix window. This is
2934 * used for opening a file from a quickfix window and not from a location
2935 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002936 */
2937 static void
2938qf_goto_win_with_qfl_file(int qf_fnum)
2939{
2940 win_T *win;
2941 win_T *altwin;
2942
2943 win = curwin;
2944 altwin = NULL;
2945 for (;;)
2946 {
2947 if (win->w_buffer->b_fnum == qf_fnum)
2948 break;
2949 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002950 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002951 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002952 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002953
2954 if (IS_QF_WINDOW(win))
2955 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002956 // Didn't find it, go to the window before the quickfix
2957 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002958 if (altwin != NULL)
2959 win = altwin;
2960 else if (curwin->w_prev != NULL)
2961 win = curwin->w_prev;
2962 else
2963 win = curwin->w_next;
2964 break;
2965 }
2966
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002967 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02002968 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002969 altwin = win;
2970 }
2971
2972 win_goto(win);
2973}
2974
2975/*
2976 * Find a suitable window for opening a file (qf_fnum) from the
2977 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01002978 * window, jump to it. Otherwise open a new window to display the file. If
2979 * 'newwin' is TRUE, then always open a new window. This is called from either
2980 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002981 */
2982 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002983qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002984{
2985 win_T *usable_win_ptr = NULL;
2986 int usable_win;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002987 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002988 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002989
2990 usable_win = 0;
2991
Bram Moolenaarb2443732018-11-11 22:50:27 +01002992 // If opening a new window, then don't use the location list referred by
2993 // the current window. Otherwise two windows will refer to the same
2994 // location list.
2995 if (!newwin)
2996 ll_ref = curwin->w_llist_ref;
2997
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002998 if (ll_ref != NULL)
2999 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003000 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003001 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
3002 if (usable_win_ptr != NULL)
3003 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003004 }
3005
3006 if (!usable_win)
3007 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003008 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003009 win = qf_find_win_with_normal_buf();
3010 if (win != NULL)
3011 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003012 }
3013
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003014 // If no usable window is found and 'switchbuf' contains "usetab"
3015 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003016 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003017 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003018
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003019 // If there is only one window and it is the quickfix window, create a
3020 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003021 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003022 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003023 if (qf_open_new_file_win(ll_ref) != OK)
3024 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003025 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003026 }
3027 else
3028 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003029 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003030 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003031 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003032 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003033 }
3034
3035 return OK;
3036}
3037
3038/*
3039 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003040 * Returns OK if successfully edited the file, FAIL on failing to open the
3041 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3042 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003043 */
3044 static int
3045qf_jump_edit_buffer(
3046 qf_info_T *qi,
3047 qfline_T *qf_ptr,
3048 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003049 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003050 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003051{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003052 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003053 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003054 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003055 int old_qf_curlist = qi->qf_curlist;
3056 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003057
3058 if (qf_ptr->qf_type == 1)
3059 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003060 // Open help file (do_ecmd() will set b_help flag, readfile() will
3061 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003062 if (!can_abandon(curbuf, forceit))
3063 {
3064 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003065 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003066 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003067
3068 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3069 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003070 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003071 }
3072 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003073 retval = buflist_getfile(qf_ptr->qf_fnum,
3074 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003075
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003076 // If a location list, check whether the associated window is still
3077 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003078 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003079 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003080 win_T *wp = win_id2wp(prev_winid);
3081 if (wp == NULL && curwin->w_llist != qi)
3082 {
3083 emsg(_("E924: Current window was closed"));
3084 *opened_window = FALSE;
3085 return NOTDONE;
3086 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003087 }
3088
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003089 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003090 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003091 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003092 return NOTDONE;
3093 }
3094
3095 if (old_qf_curlist != qi->qf_curlist
3096 || !is_qf_entry_present(qfl, qf_ptr))
3097 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003098 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003099 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003100 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003101 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003102 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003103 }
3104
3105 return retval;
3106}
3107
3108/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003109 * Go to the error line in the current file using either line/column number or
3110 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003111 */
3112 static void
3113qf_jump_goto_line(
3114 linenr_T qf_lnum,
3115 int qf_col,
3116 char_u qf_viscol,
3117 char_u *qf_pattern)
3118{
3119 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003120
3121 if (qf_pattern == NULL)
3122 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003123 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003124 i = qf_lnum;
3125 if (i > 0)
3126 {
3127 if (i > curbuf->b_ml.ml_line_count)
3128 i = curbuf->b_ml.ml_line_count;
3129 curwin->w_cursor.lnum = i;
3130 }
3131 if (qf_col > 0)
3132 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003133 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003134 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003135 coladvance(qf_col - 1);
3136 else
3137 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003138 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003139 check_cursor();
3140 }
3141 else
3142 beginline(BL_WHITE | BL_FIX);
3143 }
3144 else
3145 {
3146 pos_T save_cursor;
3147
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003148 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003149 save_cursor = curwin->w_cursor;
3150 curwin->w_cursor.lnum = 0;
3151 if (!do_search(NULL, '/', qf_pattern, (long)1,
3152 SEARCH_KEEP, NULL, NULL))
3153 curwin->w_cursor = save_cursor;
3154 }
3155}
3156
3157/*
3158 * Display quickfix list index and size message
3159 */
3160 static void
3161qf_jump_print_msg(
3162 qf_info_T *qi,
3163 int qf_index,
3164 qfline_T *qf_ptr,
3165 buf_T *old_curbuf,
3166 linenr_T old_lnum)
3167{
3168 linenr_T i;
3169 int len;
3170
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003171 // Update the screen before showing the message, unless the screen
3172 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003173 if (!msg_scrolled)
3174 update_topline_redraw();
3175 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003176 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003177 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3178 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003179 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003180 len = (int)STRLEN(IObuff);
3181 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3182
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003183 // Output the message. Overwrite to avoid scrolling when the 'O'
3184 // flag is present in 'shortmess'; But when not jumping, print the
3185 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003186 i = msg_scroll;
3187 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3188 msg_scroll = TRUE;
3189 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3190 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003191 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003192 msg_scroll = i;
3193}
3194
3195/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003196 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003197 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3198 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003199 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3200 * able to jump/open a window. Returns NOTDONE if a file is not associated
3201 * with the entry.
3202 */
3203 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003204qf_jump_open_window(
3205 qf_info_T *qi,
3206 qfline_T *qf_ptr,
3207 int newwin,
3208 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003209{
3210 // For ":helpgrep" find a help window or open one.
3211 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003212 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003213 return FAIL;
3214
3215 // If currently in the quickfix window, find another window to show the
3216 // file in.
3217 if (bt_quickfix(curbuf) && !*opened_window)
3218 {
3219 // If there is no file specified, we don't know where to go.
3220 // But do advance, otherwise ":cn" gets stuck.
3221 if (qf_ptr->qf_fnum == 0)
3222 return NOTDONE;
3223
Bram Moolenaarb2443732018-11-11 22:50:27 +01003224 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3225 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003226 return FAIL;
3227 }
3228
3229 return OK;
3230}
3231
3232/*
3233 * Edit a selected file from the quickfix/location list and jump to a
3234 * particular line/column, adjust the folds and display a message about the
3235 * jump.
3236 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3237 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3238 * the file.
3239 */
3240 static int
3241qf_jump_to_buffer(
3242 qf_info_T *qi,
3243 int qf_index,
3244 qfline_T *qf_ptr,
3245 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003246 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003247 int *opened_window,
3248 int openfold,
3249 int print_message)
3250{
3251 buf_T *old_curbuf;
3252 linenr_T old_lnum;
3253 int retval = OK;
3254
3255 // If there is a file name, read the wanted file if needed, and check
3256 // autowrite etc.
3257 old_curbuf = curbuf;
3258 old_lnum = curwin->w_cursor.lnum;
3259
3260 if (qf_ptr->qf_fnum != 0)
3261 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003262 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003263 opened_window);
3264 if (retval != OK)
3265 return retval;
3266 }
3267
3268 // When not switched to another buffer, still need to set pc mark
3269 if (curbuf == old_curbuf)
3270 setpcmark();
3271
3272 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3273 qf_ptr->qf_pattern);
3274
3275#ifdef FEAT_FOLDING
3276 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3277 foldOpenCursor();
3278#endif
3279 if (print_message)
3280 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3281
3282 return retval;
3283}
3284
3285/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003286 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 */
3288 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003289qf_jump(qf_info_T *qi,
3290 int dir,
3291 int errornr,
3292 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003294 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3295}
3296
3297/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003298 * Jump to a quickfix line.
3299 * If dir == 0 go to entry "errornr".
3300 * If dir == FORWARD go "errornr" valid entries forward.
3301 * If dir == BACKWARD go "errornr" valid entries backward.
3302 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3303 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3304 * else if "errornr" is zero, redisplay the same line
3305 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003306 * If 'newwin' is TRUE, then open the file in a new window.
3307 */
3308 void
3309qf_jump_newwin(qf_info_T *qi,
3310 int dir,
3311 int errornr,
3312 int forceit,
3313 int newwin)
3314{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003315 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003316 qfline_T *qf_ptr;
3317 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003320 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003321 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003322 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003326 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003328 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003330 if (qi == NULL)
3331 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003332
Bram Moolenaar0398e002019-03-21 21:12:49 +01003333 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003335 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 return;
3337 }
3338
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003339 incr_quickfix_busy();
3340
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003341 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003342
3343 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003345 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003347
3348 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3349 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003351 qf_ptr = old_qf_ptr;
3352 qf_index = old_qf_index;
3353 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003356 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003357 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003358 // No need to print the error message if it's visible in the error
3359 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 print_message = FALSE;
3361
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003362 prev_winid = curwin->w_id;
3363
Bram Moolenaarb2443732018-11-11 22:50:27 +01003364 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003365 if (retval == FAIL)
3366 goto failed;
3367 if (retval == NOTDONE)
3368 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003369
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003370 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003371 &opened_window, old_KeyTyped, print_message);
3372 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003374 // Quickfix/location list is freed by an autocmd
3375 qi = NULL;
3376 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003379 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003382 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003383 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003385 // Couldn't open file, so put index back where it was. This could
3386 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 qf_ptr = old_qf_ptr;
3389 qf_index = old_qf_index;
3390 }
3391 }
3392theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003393 if (qi != NULL)
3394 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003395 qfl->qf_ptr = qf_ptr;
3396 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003397 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003398 if (p_swb != old_swb)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003400 // Restore old 'switchbuf' value, but not when an autocommand or
3401 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003405 swb_flags = old_swb_flags;
3406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 else
3408 free_string_option(old_swb);
3409 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003410 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411}
3412
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003413// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003414static int qfFileAttr;
3415static int qfSepAttr;
3416static int qfLineAttr;
3417
3418/*
3419 * Display information about a single entry from the quickfix/location list.
3420 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003421 * 'cursel' will be set to TRUE for the currently selected entry in the
3422 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003423 */
3424 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003425qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003426{
3427 char_u *fname;
3428 buf_T *buf;
3429 int filter_entry;
3430
3431 fname = NULL;
3432 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3433 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3434 (char *)qfp->qf_module);
3435 else {
3436 if (qfp->qf_fnum != 0
3437 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3438 {
3439 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003440 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003441 fname = gettail(fname);
3442 }
3443 if (fname == NULL)
3444 sprintf((char *)IObuff, "%2d", qf_idx);
3445 else
3446 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3447 qf_idx, (char *)fname);
3448 }
3449
3450 // Support for filtering entries using :filter /pat/ clist
3451 // Match against the module name, file name, search pattern and
3452 // text of the entry.
3453 filter_entry = TRUE;
3454 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3455 filter_entry &= message_filtered(qfp->qf_module);
3456 if (filter_entry && fname != NULL)
3457 filter_entry &= message_filtered(fname);
3458 if (filter_entry && qfp->qf_pattern != NULL)
3459 filter_entry &= message_filtered(qfp->qf_pattern);
3460 if (filter_entry)
3461 filter_entry &= message_filtered(qfp->qf_text);
3462 if (filter_entry)
3463 return;
3464
3465 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003466 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003467
3468 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003469 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003470 if (qfp->qf_lnum == 0)
3471 IObuff[0] = NUL;
3472 else if (qfp->qf_col == 0)
3473 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3474 else
3475 sprintf((char *)IObuff, "%ld col %d",
3476 qfp->qf_lnum, qfp->qf_col);
3477 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3478 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003479 msg_puts_attr((char *)IObuff, qfLineAttr);
3480 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003481 if (qfp->qf_pattern != NULL)
3482 {
3483 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003484 msg_puts((char *)IObuff);
3485 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003486 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003487 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003488
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003489 // Remove newlines and leading whitespace from the text. For an
3490 // unrecognized line keep the indent, the compiler may mark a word
3491 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003492 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3493 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3494 IObuff, IOSIZE);
3495 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003496 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003497}
3498
3499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003501 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 */
3503 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003504qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003506 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003507 qfline_T *qfp;
3508 int i;
3509 int idx1 = 1;
3510 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003511 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003512 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003513 int all = eap->forceit; // if not :cl!, only show
3514 // recognised errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003515 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
Bram Moolenaar39665952018-08-15 20:59:48 +02003517 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003518 {
3519 qi = GET_LOC_LIST(curwin);
3520 if (qi == NULL)
3521 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003522 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003523 return;
3524 }
3525 }
3526
Bram Moolenaar0398e002019-03-21 21:12:49 +01003527 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003529 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 return;
3531 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003532 if (*arg == '+')
3533 {
3534 ++arg;
3535 plus = TRUE;
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003539 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 return;
3541 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003542 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003543 if (plus)
3544 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003545 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003546 idx2 = i + idx1;
3547 idx1 = i;
3548 }
3549 else
3550 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003551 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003552 if (idx1 < 0)
3553 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3554 if (idx2 < 0)
3555 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003558 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003559 shorten_fnames(FALSE);
3560
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003561 // Get the attributes for the different quickfix highlight items. Note
3562 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003563 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3564 if (qfFileAttr == 0)
3565 qfFileAttr = HL_ATTR(HLF_D);
3566 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3567 if (qfSepAttr == 0)
3568 qfSepAttr = HL_ATTR(HLF_D);
3569 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3570 if (qfLineAttr == 0)
3571 qfLineAttr = HL_ATTR(HLF_N);
3572
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003573 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003575 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
3577 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003578 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003579
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 ui_breakcheck();
3581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582}
3583
3584/*
3585 * Remove newlines and leading whitespace from an error message.
3586 * Put the result in "buf[bufsize]".
3587 */
3588 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003589qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590{
3591 int i;
3592 char_u *p = text;
3593
3594 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3595 {
3596 if (*p == '\n')
3597 {
3598 buf[i] = ' ';
3599 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003600 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 break;
3602 }
3603 else
3604 buf[i] = *p++;
3605 }
3606 buf[i] = NUL;
3607}
3608
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003609/*
3610 * Display information (list number, list size and the title) about a
3611 * quickfix/location list.
3612 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003613 static void
3614qf_msg(qf_info_T *qi, int which, char *lead)
3615{
3616 char *title = (char *)qi->qf_lists[which].qf_title;
3617 int count = qi->qf_lists[which].qf_count;
3618 char_u buf[IOSIZE];
3619
3620 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3621 lead,
3622 which + 1,
3623 qi->qf_listcount,
3624 count);
3625
3626 if (title != NULL)
3627 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003628 size_t len = STRLEN(buf);
3629
3630 if (len < 34)
3631 {
3632 vim_memset(buf + len, ' ', 34 - len);
3633 buf[34] = NUL;
3634 }
3635 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003636 }
3637 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003638 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003639}
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641/*
3642 * ":colder [count]": Up in the quickfix stack.
3643 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003644 * ":lolder [count]": Up in the location list stack.
3645 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 */
3647 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003648qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003650 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 int count;
3652
Bram Moolenaar39665952018-08-15 20:59:48 +02003653 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003654 {
3655 qi = GET_LOC_LIST(curwin);
3656 if (qi == NULL)
3657 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003658 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003659 return;
3660 }
3661 }
3662
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (eap->addr_count != 0)
3664 count = eap->line2;
3665 else
3666 count = 1;
3667 while (count--)
3668 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003669 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003671 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003673 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003674 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003676 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678 else
3679 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003680 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003682 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003683 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003685 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
3687 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003688 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003689 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690}
3691
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003692/*
3693 * Display the information about all the quickfix/location lists in the stack
3694 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003695 void
3696qf_history(exarg_T *eap)
3697{
3698 qf_info_T *qi = &ql_info;
3699 int i;
3700
Bram Moolenaar39665952018-08-15 20:59:48 +02003701 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003702 qi = GET_LOC_LIST(curwin);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003703 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003704 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003705 else
3706 for (i = 0; i < qi->qf_listcount; ++i)
3707 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3708}
3709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003711 * Free all the entries in the error list "idx". Note that other information
3712 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 */
3714 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003715qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003717 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003718 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003719 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003721 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003723 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003724 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003725 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003726 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003727 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003728 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003729 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003730 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003731 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003732 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003733 // Somehow qf_count may have an incorrect value, set it to 1
3734 // to avoid crashing when it's wrong.
3735 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003736 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003737 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003738 qfl->qf_start = qfpnext;
3739 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003741
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003742 qfl->qf_index = 0;
3743 qfl->qf_start = NULL;
3744 qfl->qf_last = NULL;
3745 qfl->qf_ptr = NULL;
3746 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003747
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003748 qf_clean_dir_stack(&qfl->qf_dir_stack);
3749 qfl->qf_directory = NULL;
3750 qf_clean_dir_stack(&qfl->qf_file_stack);
3751 qfl->qf_currfile = NULL;
3752 qfl->qf_multiline = FALSE;
3753 qfl->qf_multiignore = FALSE;
3754 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755}
3756
3757/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003758 * Free error list "idx". Frees all the entries in the quickfix list,
3759 * associated context information and the title.
3760 */
3761 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003762qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003763{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003764 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003765
Bram Moolenaard23a8232018-02-10 18:45:26 +01003766 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003767 free_tv(qfl->qf_ctx);
3768 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003769 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003770 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003771}
3772
3773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 * qf_mark_adjust: adjust marks
3775 */
3776 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003777qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003778 win_T *wp,
3779 linenr_T line1,
3780 linenr_T line2,
3781 long amount,
3782 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003784 int i;
3785 qfline_T *qfp;
3786 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003787 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003788 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003789 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
Bram Moolenaarc1542742016-07-20 21:44:37 +02003791 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003792 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003793 if (wp != NULL)
3794 {
3795 if (wp->w_llist == NULL)
3796 return;
3797 qi = wp->w_llist;
3798 }
3799
3800 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003801 {
3802 qf_list_T *qfl = qf_get_list(qi, idx);
3803
3804 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003805 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (qfp->qf_fnum == curbuf->b_fnum)
3807 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003808 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3810 {
3811 if (amount == MAXLNUM)
3812 qfp->qf_cleared = TRUE;
3813 else
3814 qfp->qf_lnum += amount;
3815 }
3816 else if (amount_after && qfp->qf_lnum > line2)
3817 qfp->qf_lnum += amount_after;
3818 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003819 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003820
3821 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003822 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823}
3824
3825/*
3826 * Make a nice message out of the error character and the error number:
3827 * char number message
3828 * e or E 0 " error"
3829 * w or W 0 " warning"
3830 * i or I 0 " info"
3831 * 0 0 ""
3832 * other 0 " c"
3833 * e or E n " error n"
3834 * w or W n " warning n"
3835 * i or I n " info n"
3836 * 0 n " error n"
3837 * other n " c n"
3838 * 1 x "" :helpgrep
3839 */
3840 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003841qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842{
3843 static char_u buf[20];
3844 static char_u cc[3];
3845 char_u *p;
3846
3847 if (c == 'W' || c == 'w')
3848 p = (char_u *)" warning";
3849 else if (c == 'I' || c == 'i')
3850 p = (char_u *)" info";
3851 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3852 p = (char_u *)" error";
3853 else if (c == 0 || c == 1)
3854 p = (char_u *)"";
3855 else
3856 {
3857 cc[0] = ' ';
3858 cc[1] = c;
3859 cc[2] = NUL;
3860 p = cc;
3861 }
3862
3863 if (nr <= 0)
3864 return p;
3865
3866 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3867 return buf;
3868}
3869
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003871 * When "split" is FALSE: Open the entry/result under the cursor.
3872 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3873 */
3874 void
3875qf_view_result(int split)
3876{
3877 qf_info_T *qi = &ql_info;
3878
3879 if (!bt_quickfix(curbuf))
3880 return;
3881
3882 if (IS_LL_WINDOW(curwin))
3883 qi = GET_LOC_LIST(curwin);
3884
Bram Moolenaar0398e002019-03-21 21:12:49 +01003885 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003887 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003888 return;
3889 }
3890
3891 if (split)
3892 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003893 // Open the selected entry in a new window
3894 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3895 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003896 return;
3897 }
3898
3899 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3900}
3901
3902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 * ":cwindow": open the quickfix window if we have errors to display,
3904 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003905 * ":lwindow": open the location list window if we have locations to display,
3906 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 */
3908 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003909ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003911 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003912 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 win_T *win;
3914
Bram Moolenaar39665952018-08-15 20:59:48 +02003915 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003916 {
3917 qi = GET_LOC_LIST(curwin);
3918 if (qi == NULL)
3919 return;
3920 }
3921
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003922 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003923
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003924 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003925 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003927 // If a quickfix window is open but we have no errors to display,
3928 // close the window. If a quickfix window is not open, then open
3929 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003930 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003931 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01003932 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 {
3934 if (win != NULL)
3935 ex_cclose(eap);
3936 }
3937 else if (win == NULL)
3938 ex_copen(eap);
3939}
3940
3941/*
3942 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003943 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003946ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003948 win_T *win = NULL;
3949 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
Bram Moolenaar39665952018-08-15 20:59:48 +02003951 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003952 {
3953 qi = GET_LOC_LIST(curwin);
3954 if (qi == NULL)
3955 return;
3956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003958 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003959 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (win != NULL)
3961 win_close(win, FALSE);
3962}
3963
3964/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003965 * Set "w:quickfix_title" if "qi" has a title.
3966 */
3967 static void
3968qf_set_title_var(qf_list_T *qfl)
3969{
3970 if (qfl->qf_title != NULL)
3971 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3972}
3973
3974/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003975 * Goto a quickfix or location list window (if present).
3976 * Returns OK if the window is found, FAIL otherwise.
3977 */
3978 static int
3979qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3980{
3981 win_T *win;
3982
3983 win = qf_find_win(qi);
3984 if (win == NULL)
3985 return FAIL;
3986
3987 win_goto(win);
3988 if (resize)
3989 {
3990 if (vertsplit)
3991 {
3992 if (sz != win->w_width)
3993 win_setwidth(sz);
3994 }
3995 else if (sz != win->w_height)
3996 win_setheight(sz);
3997 }
3998
3999 return OK;
4000}
4001
4002/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004003 * Set options for the buffer in the quickfix or location list window.
4004 */
4005 static void
4006qf_set_cwindow_options(void)
4007{
4008 // switch off 'swapfile'
4009 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4010 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4011 OPT_LOCAL);
4012 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4013 RESET_BINDING(curwin);
4014#ifdef FEAT_DIFF
4015 curwin->w_p_diff = FALSE;
4016#endif
4017#ifdef FEAT_FOLDING
4018 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4019 OPT_LOCAL);
4020#endif
4021}
4022
4023/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004024 * Open a new quickfix or location list window, load the quickfix buffer and
4025 * set the appropriate options for the window.
4026 * Returns FAIL if the window could not be opened.
4027 */
4028 static int
4029qf_open_new_cwindow(qf_info_T *qi, int height)
4030{
4031 buf_T *qf_buf;
4032 win_T *oldwin = curwin;
4033 tabpage_T *prevtab = curtab;
4034 int flags = 0;
4035 win_T *win;
4036
4037 qf_buf = qf_find_buf(qi);
4038
4039 // The current window becomes the previous window afterwards.
4040 win = curwin;
4041
4042 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4043 // Create the new quickfix window at the very bottom, except when
4044 // :belowright or :aboveleft is used.
4045 win_goto(lastwin);
4046 // Default is to open the window below the current window
4047 if (cmdmod.split == 0)
4048 flags = WSP_BELOW;
4049 flags |= WSP_NEWLOC;
4050 if (win_split(height, flags) == FAIL)
4051 return FAIL; // not enough room for window
4052 RESET_BINDING(curwin);
4053
4054 if (IS_LL_STACK(qi))
4055 {
4056 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004057 // location list stack from the window 'win'.
4058 curwin->w_llist_ref = qi;
4059 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004060 }
4061
4062 if (oldwin != curwin)
4063 oldwin = NULL; // don't store info when in another window
4064 if (qf_buf != NULL)
4065 {
4066 // Use the existing quickfix buffer
4067 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4068 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4069 }
4070 else
4071 {
4072 // Create a new quickfix buffer
4073 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4074
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004075 // save the number of the new buffer
4076 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004077 }
4078
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004079 // Set the options for the quickfix buffer/window (if not already done)
4080 // Do this even if the quickfix buffer was already present, as an autocmd
4081 // might have previously deleted (:bdelete) the quickfix buffer.
4082 if (curbuf->b_p_bt[0] != 'q')
4083 qf_set_cwindow_options();
4084
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004085 // Only set the height when still in the same tab page and there is no
4086 // window to the side.
4087 if (curtab == prevtab && curwin->w_width == Columns)
4088 win_setheight(height);
4089 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4090 if (win_valid(win))
4091 prevwin = win;
4092
4093 return OK;
4094}
4095
4096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004098 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 */
4100 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004101ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004103 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004104 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004106 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004107 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108
Bram Moolenaar39665952018-08-15 20:59:48 +02004109 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004110 {
4111 qi = GET_LOC_LIST(curwin);
4112 if (qi == NULL)
4113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004114 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004115 return;
4116 }
4117 }
4118
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004119 incr_quickfix_busy();
4120
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 if (eap->addr_count != 0)
4122 height = eap->line2;
4123 else
4124 height = QF_WINHEIGHT;
4125
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004126 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127#ifdef FEAT_GUI
4128 need_mouse_correct = TRUE;
4129#endif
4130
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004131 // Find an existing quickfix window, or open a new one.
4132 if (cmdmod.tab == 0)
4133 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4134 cmdmod.split & WSP_VERT);
4135 if (status == FAIL)
4136 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004137 {
4138 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004139 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004142 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004143 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004144 // Save the current index here, as updating the quickfix buffer may free
4145 // the quickfix list
4146 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004147
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004148 // Fill the buffer with the quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004149 qf_fill_buffer(qfl, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004151 decr_quickfix_busy();
4152
4153 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 curwin->w_cursor.col = 0;
4155 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004156 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157}
4158
4159/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004160 * Move the cursor in the quickfix window to "lnum".
4161 */
4162 static void
4163qf_win_goto(win_T *win, linenr_T lnum)
4164{
4165 win_T *old_curwin = curwin;
4166
4167 curwin = win;
4168 curbuf = win->w_buffer;
4169 curwin->w_cursor.lnum = lnum;
4170 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004171 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004172 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004173 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004174 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004175 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004176 curwin = old_curwin;
4177 curbuf = curwin->w_buffer;
4178}
4179
4180/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004181 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004182 */
4183 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004184ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004185{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004186 qf_info_T *qi = &ql_info;
4187 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004188
Bram Moolenaar39665952018-08-15 20:59:48 +02004189 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004190 {
4191 qi = GET_LOC_LIST(curwin);
4192 if (qi == NULL)
4193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004194 emsg(_(e_loclist));
Bram Moolenaar537ef082016-07-09 17:56:19 +02004195 return;
4196 }
4197 }
4198
4199 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004200 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4201 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4202}
4203
4204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 * Return the number of the current entry (line number in the quickfix
4206 * window).
4207 */
4208 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004209qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004211 qf_info_T *qi = &ql_info;
4212
4213 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004214 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004215 qi = wp->w_llist_ref;
4216
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004217 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218}
4219
4220/*
4221 * Update the cursor position in the quickfix window to the current error.
4222 * Return TRUE if there is a quickfix window.
4223 */
4224 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004225qf_win_pos_update(
4226 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004227 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228{
4229 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004230 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004232 // Put the cursor on the current error in the quickfix window, so that
4233 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004234 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 if (win != NULL
4236 && qf_index <= win->w_buffer->b_ml.ml_line_count
4237 && old_qf_index != qf_index)
4238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 if (qf_index > old_qf_index)
4240 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004241 win->w_redraw_top = old_qf_index;
4242 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 }
4244 else
4245 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004246 win->w_redraw_top = qf_index;
4247 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004249 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251 return win != NULL;
4252}
4253
4254/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004255 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004256 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004257 */
4258 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004259is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004260{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004261 // A window displaying the quickfix buffer will have the w_llist_ref field
4262 // set to NULL.
4263 // A window displaying a location list buffer will have the w_llist_ref
4264 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004265 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004266 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4267 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004268 return TRUE;
4269
4270 return FALSE;
4271}
4272
4273/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004274 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004275 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004276 */
4277 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004278qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004279{
4280 win_T *win;
4281
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004282 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004283 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004284 return win;
4285 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004286}
4287
4288/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004289 * Find a quickfix buffer.
4290 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 */
4292 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004293qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004295 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004296 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004298 if (qi->qf_bufnr != INVALID_QFBUFNR)
4299 {
4300 buf_T *qfbuf;
4301 qfbuf = buflist_findnr(qi->qf_bufnr);
4302 if (qfbuf != NULL)
4303 return qfbuf;
4304 // buffer is no longer present
4305 qi->qf_bufnr = INVALID_QFBUFNR;
4306 }
4307
Bram Moolenaar9c102382006-05-03 21:26:49 +00004308 FOR_ALL_TAB_WINDOWS(tp, win)
4309 if (is_qf_win(win, qi))
4310 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004311
Bram Moolenaar9c102382006-05-03 21:26:49 +00004312 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313}
4314
4315/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004316 * Update the w:quickfix_title variable in the quickfix/location list window
4317 */
4318 static void
4319qf_update_win_titlevar(qf_info_T *qi)
4320{
4321 win_T *win;
4322 win_T *curwin_save;
4323
4324 if ((win = qf_find_win(qi)) != NULL)
4325 {
4326 curwin_save = curwin;
4327 curwin = win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004328 qf_set_title_var(qf_get_curlist(qi));
Bram Moolenaard823fa92016-08-12 16:29:27 +02004329 curwin = curwin_save;
4330 }
4331}
4332
4333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 * Find the quickfix buffer. If it exists, update the contents.
4335 */
4336 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004337qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338{
4339 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004340 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004343 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004344 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 if (buf != NULL)
4346 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004347 linenr_T old_line_count = buf->b_ml.ml_line_count;
4348
4349 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004350 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004351 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352
Bram Moolenaard823fa92016-08-12 16:29:27 +02004353 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004354
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004355 qf_fill_buffer(qf_get_curlist(qi), buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004356 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004357
Bram Moolenaar864293a2016-06-02 13:40:04 +02004358 if (old_last == NULL)
4359 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004360 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004361
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004362 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004363 aucmd_restbuf(&aco);
4364 }
4365
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004366 // Only redraw when added lines are visible. This avoids flickering
4367 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004368 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4369 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
4371}
4372
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004373/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004374 * Add an error line to the quickfix buffer.
4375 */
4376 static int
4377qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4378{
4379 int len;
4380 buf_T *errbuf;
4381
4382 if (qfp->qf_module != NULL)
4383 {
4384 STRCPY(IObuff, qfp->qf_module);
4385 len = (int)STRLEN(IObuff);
4386 }
4387 else if (qfp->qf_fnum != 0
4388 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4389 && errbuf->b_fname != NULL)
4390 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004391 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004392 STRCPY(IObuff, gettail(errbuf->b_fname));
4393 else
4394 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004395 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004396 if (errbuf->b_sfname == NULL
4397 || mch_isFullName(errbuf->b_sfname))
4398 {
4399 if (*dirname == NUL)
4400 mch_dirname(dirname, MAXPATHL);
4401 shorten_buf_fname(errbuf, dirname, FALSE);
4402 }
4403 STRCPY(IObuff, errbuf->b_fname);
4404 }
4405 len = (int)STRLEN(IObuff);
4406 }
4407 else
4408 len = 0;
4409 IObuff[len++] = '|';
4410
4411 if (qfp->qf_lnum > 0)
4412 {
4413 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4414 len += (int)STRLEN(IObuff + len);
4415
4416 if (qfp->qf_col > 0)
4417 {
4418 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4419 len += (int)STRLEN(IObuff + len);
4420 }
4421
4422 sprintf((char *)IObuff + len, "%s",
4423 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4424 len += (int)STRLEN(IObuff + len);
4425 }
4426 else if (qfp->qf_pattern != NULL)
4427 {
4428 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4429 len += (int)STRLEN(IObuff + len);
4430 }
4431 IObuff[len++] = '|';
4432 IObuff[len++] = ' ';
4433
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004434 // Remove newlines and leading whitespace from the text.
4435 // For an unrecognized line keep the indent, the compiler may
4436 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004437 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4438 IObuff + len, IOSIZE - len);
4439
4440 if (ml_append_buf(buf, lnum, IObuff,
4441 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4442 return FAIL;
4443
4444 return OK;
4445}
4446
4447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 * Fill current buffer with quickfix errors, replacing any previous contents.
4449 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004450 * If "old_last" is not NULL append the items after this one.
4451 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4452 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 */
4454 static void
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004455qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004457 linenr_T lnum;
4458 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004459 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
Bram Moolenaar864293a2016-06-02 13:40:04 +02004461 if (old_last == NULL)
4462 {
4463 if (buf != curbuf)
4464 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004465 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004466 return;
4467 }
4468
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004469 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004470 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4471 (void)ml_delete((linenr_T)1, FALSE);
4472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004474 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004475 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004477 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004478
4479 *dirname = NUL;
4480
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004481 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004482 if (old_last == NULL)
4483 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004484 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004485 lnum = 0;
4486 }
4487 else
4488 {
4489 qfp = old_last->qf_next;
4490 lnum = buf->b_ml.ml_line_count;
4491 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004492 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004494 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004496
Bram Moolenaar864293a2016-06-02 13:40:04 +02004497 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004499 if (qfp == NULL)
4500 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004502
4503 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004504 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004505 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004508 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 check_lnums(TRUE);
4510
Bram Moolenaar864293a2016-06-02 13:40:04 +02004511 if (old_last == NULL)
4512 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004513 // Set the 'filetype' to "qf" each time after filling the buffer.
4514 // This resembles reading a file into a buffer, it's more logical when
4515 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004516 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004517 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4518 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004520 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004521 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004523 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004525 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004526 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004527
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004528 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004529 redraw_curbuf_later(NOT_VALID);
4530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004532 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 KeyTyped = old_KeyTyped;
4534}
4535
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004536/*
4537 * For every change made to the quickfix list, update the changed tick.
4538 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004539 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004540qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004541{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004542 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004543}
4544
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004546 * Return the quickfix/location list number with the given identifier.
4547 * Returns -1 if list is not found.
4548 */
4549 static int
4550qf_id2nr(qf_info_T *qi, int_u qfid)
4551{
4552 int qf_idx;
4553
4554 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4555 if (qi->qf_lists[qf_idx].qf_id == qfid)
4556 return qf_idx;
4557 return INVALID_QFIDX;
4558}
4559
4560/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004561 * If the current list is not "save_qfid" and we can find the list with that ID
4562 * then make it the current list.
4563 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004564 * Returns OK if successfully restored the list. Returns FAIL if the list with
4565 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004566 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004567 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004568qf_restore_list(qf_info_T *qi, int_u save_qfid)
4569{
4570 int curlist;
4571
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004572 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004573 {
4574 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004575 if (curlist < 0)
4576 // list is not present
4577 return FAIL;
4578 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004579 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004580 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004581}
4582
4583/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004584 * Jump to the first entry if there is one.
4585 */
4586 static void
4587qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4588{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004589 if (qf_restore_list(qi, save_qfid) == FAIL)
4590 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004591
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004592 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004593 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004594 qf_jump(qi, 0, 0, forceit);
4595}
4596
4597/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004598 * Return TRUE when using ":vimgrep" for ":grep".
4599 */
4600 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004601grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004602{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004603 return ((cmdidx == CMD_grep
4604 || cmdidx == CMD_lgrep
4605 || cmdidx == CMD_grepadd
4606 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004607 && STRCMP("internal",
4608 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4609}
4610
4611/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004612 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004614 static char_u *
4615make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004617 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004618 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004619 case CMD_make: return (char_u *)"make";
4620 case CMD_lmake: return (char_u *)"lmake";
4621 case CMD_grep: return (char_u *)"grep";
4622 case CMD_lgrep: return (char_u *)"lgrep";
4623 case CMD_grepadd: return (char_u *)"grepadd";
4624 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4625 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627}
4628
4629/*
4630 * Return the name for the errorfile, in allocated memory.
4631 * Find a new unique name when 'makeef' contains "##".
4632 * Returns NULL for error.
4633 */
4634 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004635get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636{
4637 char_u *p;
4638 char_u *name;
4639 static int start = -1;
4640 static int off = 0;
4641#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004642 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#endif
4644
4645 if (*p_mef == NUL)
4646 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004647 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004649 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 return name;
4651 }
4652
4653 for (p = p_mef; *p; ++p)
4654 if (p[0] == '#' && p[1] == '#')
4655 break;
4656
4657 if (*p == NUL)
4658 return vim_strsave(p_mef);
4659
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004660 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 for (;;)
4662 {
4663 if (start == -1)
4664 start = mch_get_pid();
4665 else
4666 off += 19;
4667
4668 name = alloc((unsigned)STRLEN(p_mef) + 30);
4669 if (name == NULL)
4670 break;
4671 STRCPY(name, p_mef);
4672 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4673 STRCAT(name, p + 2);
4674 if (mch_getperm(name) < 0
4675#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004676 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 && mch_lstat((char *)name, &sb) < 0
4678#endif
4679 )
4680 break;
4681 vim_free(name);
4682 }
4683 return name;
4684}
4685
4686/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004687 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4688 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4689 */
4690 static char_u *
4691make_get_fullcmd(char_u *makecmd, char_u *fname)
4692{
4693 char_u *cmd;
4694 unsigned len;
4695
4696 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4697 if (*p_sp != NUL)
4698 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4699 cmd = alloc(len);
4700 if (cmd == NULL)
4701 return NULL;
4702 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4703 (char *)p_shq);
4704
4705 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4706 if (*p_sp != NUL)
4707 append_redir(cmd, len, p_sp, fname);
4708
4709 // Display the fully formed command. Output a newline if there's something
4710 // else than the :make command that was typed (in which case the cursor is
4711 // in column 0).
4712 if (msg_col == 0)
4713 msg_didout = FALSE;
4714 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004715 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004716 msg_outtrans(cmd); // show what we are doing
4717
4718 return cmd;
4719}
4720
4721/*
4722 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4723 */
4724 void
4725ex_make(exarg_T *eap)
4726{
4727 char_u *fname;
4728 char_u *cmd;
4729 char_u *enc = NULL;
4730 win_T *wp = NULL;
4731 qf_info_T *qi = &ql_info;
4732 int res;
4733 char_u *au_name = NULL;
4734 int_u save_qfid;
4735
4736 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4737 if (grep_internal(eap->cmdidx))
4738 {
4739 ex_vimgrep(eap);
4740 return;
4741 }
4742
4743 au_name = make_get_auname(eap->cmdidx);
4744 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4745 curbuf->b_fname, TRUE, curbuf))
4746 {
4747#ifdef FEAT_EVAL
4748 if (aborting())
4749 return;
4750#endif
4751 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004752 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004753
4754 if (is_loclist_cmd(eap->cmdidx))
4755 wp = curwin;
4756
4757 autowrite_all();
4758 fname = get_mef_name();
4759 if (fname == NULL)
4760 return;
4761 mch_remove(fname); // in case it's not unique
4762
4763 cmd = make_get_fullcmd(eap->arg, fname);
4764 if (cmd == NULL)
4765 return;
4766
4767 // let the shell know if we are redirecting output or not
4768 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4769
4770#ifdef AMIGA
4771 out_flush();
4772 // read window status report and redraw before message
4773 (void)char_avail();
4774#endif
4775
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004776 incr_quickfix_busy();
4777
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004778 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4779 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4780 (eap->cmdidx != CMD_grepadd
4781 && eap->cmdidx != CMD_lgrepadd),
4782 qf_cmdtitle(*eap->cmdlinep), enc);
4783 if (wp != NULL)
4784 {
4785 qi = GET_LOC_LIST(wp);
4786 if (qi == NULL)
4787 goto cleanup;
4788 }
4789 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004790 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004791
4792 // Remember the current quickfix list identifier, so that we can
4793 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004794 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004795 if (au_name != NULL)
4796 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4797 curbuf->b_fname, TRUE, curbuf);
4798 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4799 // display the first error
4800 qf_jump_first(qi, save_qfid, FALSE);
4801
4802cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004803 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004804 mch_remove(fname);
4805 vim_free(fname);
4806 vim_free(cmd);
4807}
4808
4809/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004810 * Returns the number of valid entries in the current quickfix/location list.
4811 */
4812 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004813qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004814{
4815 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004816 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004817 qfline_T *qfp;
4818 int i, sz = 0;
4819 int prev_fnum = 0;
4820
Bram Moolenaar39665952018-08-15 20:59:48 +02004821 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004822 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004823 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004824 qi = GET_LOC_LIST(curwin);
4825 if (qi == NULL)
4826 return 0;
4827 }
4828
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004829 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01004830 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004831 {
4832 if (qfp->qf_valid)
4833 {
4834 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004835 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004836 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4837 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004838 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004839 sz++;
4840 prev_fnum = qfp->qf_fnum;
4841 }
4842 }
4843 }
4844
4845 return sz;
4846}
4847
4848/*
4849 * Returns the current index of the quickfix/location list.
4850 * Returns 0 if there is an error.
4851 */
4852 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004853qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004854{
4855 qf_info_T *qi = &ql_info;
4856
Bram Moolenaar39665952018-08-15 20:59:48 +02004857 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004858 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004859 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004860 qi = GET_LOC_LIST(curwin);
4861 if (qi == NULL)
4862 return 0;
4863 }
4864
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004865 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004866}
4867
4868/*
4869 * Returns the current index in the quickfix/location list (counting only valid
4870 * entries). If no valid entries are in the list, then returns 1.
4871 */
4872 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004873qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004874{
4875 qf_info_T *qi = &ql_info;
4876 qf_list_T *qfl;
4877 qfline_T *qfp;
4878 int i, eidx = 0;
4879 int prev_fnum = 0;
4880
Bram Moolenaar39665952018-08-15 20:59:48 +02004881 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004882 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004883 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004884 qi = GET_LOC_LIST(curwin);
4885 if (qi == NULL)
4886 return 1;
4887 }
4888
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004889 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004890 qfp = qfl->qf_start;
4891
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004892 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004893 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4894 return 1;
4895
4896 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4897 {
4898 if (qfp->qf_valid)
4899 {
4900 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4901 {
4902 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4903 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004904 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004905 eidx++;
4906 prev_fnum = qfp->qf_fnum;
4907 }
4908 }
4909 else
4910 eidx++;
4911 }
4912 }
4913
4914 return eidx ? eidx : 1;
4915}
4916
4917/*
4918 * Get the 'n'th valid error entry in the quickfix or location list.
4919 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4920 * For :cdo and :ldo returns the 'n'th valid error entry.
4921 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4922 */
4923 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004924qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004925{
Bram Moolenaar95946f12019-03-31 15:31:59 +02004926 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004927 int i, eidx;
4928 int prev_fnum = 0;
4929
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004930 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004931 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4932 return 1;
4933
Bram Moolenaar95946f12019-03-31 15:31:59 +02004934 eidx = 0;
4935 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004936 {
4937 if (qfp->qf_valid)
4938 {
4939 if (fdo)
4940 {
4941 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4942 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004943 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004944 eidx++;
4945 prev_fnum = qfp->qf_fnum;
4946 }
4947 }
4948 else
4949 eidx++;
4950 }
4951
4952 if (eidx == n)
4953 break;
4954 }
4955
4956 if (i <= qfl->qf_count)
4957 return i;
4958 else
4959 return 1;
4960}
4961
4962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004964 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004965 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 */
4967 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004968ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004970 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004971 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004972
Bram Moolenaar39665952018-08-15 20:59:48 +02004973 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004974 {
4975 qi = GET_LOC_LIST(curwin);
4976 if (qi == NULL)
4977 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004978 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004979 return;
4980 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004981 }
4982
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004983 if (eap->addr_count > 0)
4984 errornr = (int)eap->line2;
4985 else
4986 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004987 switch (eap->cmdidx)
4988 {
4989 case CMD_cc: case CMD_ll:
4990 errornr = 0;
4991 break;
4992 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4993 case CMD_lfirst:
4994 errornr = 1;
4995 break;
4996 default:
4997 errornr = 32767;
4998 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004999 }
5000
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005001 // For cdo and ldo commands, jump to the nth valid error.
5002 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005003 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5004 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005005 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005006 eap->addr_count > 0 ? (int)eap->line1 : 1,
5007 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5008
5009 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010}
5011
5012/*
5013 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005014 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005015 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 */
5017 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005018ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005020 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005021 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005022 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005023
Bram Moolenaar39665952018-08-15 20:59:48 +02005024 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005025 {
5026 qi = GET_LOC_LIST(curwin);
5027 if (qi == NULL)
5028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005029 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005030 return;
5031 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005032 }
5033
Bram Moolenaar55b69262017-08-13 13:42:01 +02005034 if (eap->addr_count > 0
5035 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5036 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005037 errornr = (int)eap->line2;
5038 else
5039 errornr = 1;
5040
Bram Moolenaar39665952018-08-15 20:59:48 +02005041 // Depending on the command jump to either next or previous entry/file.
5042 switch (eap->cmdidx)
5043 {
5044 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5045 dir = FORWARD;
5046 break;
5047 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5048 case CMD_lNext:
5049 dir = BACKWARD;
5050 break;
5051 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5052 dir = FORWARD_FILE;
5053 break;
5054 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5055 dir = BACKWARD_FILE;
5056 break;
5057 default:
5058 dir = FORWARD;
5059 break;
5060 }
5061
5062 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063}
5064
5065/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005066 * Return the autocmd name for the :cfile Ex commands
5067 */
5068 static char_u *
5069cfile_get_auname(cmdidx_T cmdidx)
5070{
5071 switch (cmdidx)
5072 {
5073 case CMD_cfile: return (char_u *)"cfile";
5074 case CMD_cgetfile: return (char_u *)"cgetfile";
5075 case CMD_caddfile: return (char_u *)"caddfile";
5076 case CMD_lfile: return (char_u *)"lfile";
5077 case CMD_lgetfile: return (char_u *)"lgetfile";
5078 case CMD_laddfile: return (char_u *)"laddfile";
5079 default: return NULL;
5080 }
5081}
5082
5083/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005084 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005085 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 */
5087 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005088ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005090 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005091 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005092 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005093 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005094 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005095 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005096
Bram Moolenaara16123a2019-03-28 20:31:07 +01005097 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005098 if (au_name != NULL)
5099 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005100
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005101 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005102#ifdef FEAT_BROWSE
5103 if (cmdmod.browse)
5104 {
5105 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005106 NULL, NULL,
5107 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005108 if (browse_file == NULL)
5109 return;
5110 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5111 vim_free(browse_file);
5112 }
5113 else
5114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005116 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005117
Bram Moolenaar39665952018-08-15 20:59:48 +02005118 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005119 wp = curwin;
5120
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005121 incr_quickfix_busy();
5122
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005123 // This function is used by the :cfile, :cgetfile and :caddfile
5124 // commands.
5125 // :cfile always creates a new quickfix list and jumps to the
5126 // first error.
5127 // :cgetfile creates a new quickfix list but doesn't jump to the
5128 // first error.
5129 // :caddfile adds to an existing quickfix list. If there is no
5130 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005131 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005132 && eap->cmdidx != CMD_laddfile),
5133 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005134 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005135 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005136 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005137 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005138 {
5139 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005140 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005141 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005142 }
5143 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005144 qf_list_changed(qf_get_curlist(qi));
5145 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005146 if (au_name != NULL)
5147 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005148
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005149 // Jump to the first error for a new list and if autocmds didn't
5150 // free the list.
5151 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5152 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005153 // display the first error
5154 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005155
5156 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005157}
5158
5159/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005160 * Return the vimgrep autocmd name.
5161 */
5162 static char_u *
5163vgr_get_auname(cmdidx_T cmdidx)
5164{
5165 switch (cmdidx)
5166 {
5167 case CMD_vimgrep: return (char_u *)"vimgrep";
5168 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5169 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5170 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5171 case CMD_grep: return (char_u *)"grep";
5172 case CMD_lgrep: return (char_u *)"lgrep";
5173 case CMD_grepadd: return (char_u *)"grepadd";
5174 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5175 default: return NULL;
5176 }
5177}
5178
5179/*
5180 * Initialize the regmatch used by vimgrep for pattern "s".
5181 */
5182 static void
5183vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5184{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005185 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005186 regmatch->regprog = NULL;
5187
5188 if (s == NULL || *s == NUL)
5189 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005190 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005191 if (last_search_pat() == NULL)
5192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005193 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005194 return;
5195 }
5196 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5197 }
5198 else
5199 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5200
5201 regmatch->rmm_ic = p_ic;
5202 regmatch->rmm_maxcol = 0;
5203}
5204
5205/*
5206 * Display a file name when vimgrep is running.
5207 */
5208 static void
5209vgr_display_fname(char_u *fname)
5210{
5211 char_u *p;
5212
5213 msg_start();
5214 p = msg_strtrunc(fname, TRUE);
5215 if (p == NULL)
5216 msg_outtrans(fname);
5217 else
5218 {
5219 msg_outtrans(p);
5220 vim_free(p);
5221 }
5222 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005223 msg_didout = FALSE; // overwrite this message
5224 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005225 msg_col = 0;
5226 out_flush();
5227}
5228
5229/*
5230 * Load a dummy buffer to search for a pattern using vimgrep.
5231 */
5232 static buf_T *
5233vgr_load_dummy_buf(
5234 char_u *fname,
5235 char_u *dirname_start,
5236 char_u *dirname_now)
5237{
5238 int save_mls;
5239#if defined(FEAT_SYN_HL)
5240 char_u *save_ei = NULL;
5241#endif
5242 buf_T *buf;
5243
5244#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005245 // Don't do Filetype autocommands to avoid loading syntax and
5246 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005247 save_ei = au_event_disable(",Filetype");
5248#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005249 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005250 save_mls = p_mls;
5251 p_mls = 0;
5252
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005253 // Load file into a buffer, so that 'fileencoding' is detected,
5254 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005255 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5256
5257 p_mls = save_mls;
5258#if defined(FEAT_SYN_HL)
5259 au_event_restore(save_ei);
5260#endif
5261
5262 return buf;
5263}
5264
5265/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005266 * Check whether a quickfix/location list is valid. Autocmds may remove or
5267 * change a quickfix list when vimgrep is running. If the list is not found,
5268 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005269 */
5270 static int
5271vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005272 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005273 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005274 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005275 char_u *title)
5276{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005277 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005278 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005279 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005280 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005281 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005282 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005283 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005284 return FALSE;
5285 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005286 else
5287 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005288 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005289 qf_new_list(qi, title);
5290 return TRUE;
5291 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005292 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005293
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005294 if (qf_restore_list(qi, qfid) == FAIL)
5295 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005296
5297 return TRUE;
5298}
5299
5300/*
5301 * Search for a pattern in all the lines in a buffer and add the matching lines
5302 * to a quickfix list.
5303 */
5304 static int
5305vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005306 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005307 char_u *fname,
5308 buf_T *buf,
5309 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005310 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005311 int duplicate_name,
5312 int flags)
5313{
5314 int found_match = FALSE;
5315 long lnum;
5316 colnr_T col;
5317
Bram Moolenaar1c299432018-10-28 14:36:09 +01005318 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005319 {
5320 col = 0;
5321 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5322 col, NULL, NULL) > 0)
5323 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005324 // Pass the buffer number so that it gets used even for a
5325 // dummy buffer, unless duplicate_name is set, then the
5326 // buffer will be wiped out below.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005327 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005328 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005329 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005330 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005331 duplicate_name ? 0 : buf->b_fnum,
5332 ml_get_buf(buf,
5333 regmatch->startpos[0].lnum + lnum, FALSE),
5334 regmatch->startpos[0].lnum + lnum,
5335 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005336 FALSE, // vis_col
5337 NULL, // search pattern
5338 0, // nr
5339 0, // type
5340 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02005341 ) == QF_FAIL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005342 {
5343 got_int = TRUE;
5344 break;
5345 }
5346 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005347 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005348 break;
5349 if ((flags & VGR_GLOBAL) == 0
5350 || regmatch->endpos[0].lnum > 0)
5351 break;
5352 col = regmatch->endpos[0].col
5353 + (col == regmatch->endpos[0].col);
5354 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5355 break;
5356 }
5357 line_breakcheck();
5358 if (got_int)
5359 break;
5360 }
5361
5362 return found_match;
5363}
5364
5365/*
5366 * Jump to the first match and update the directory.
5367 */
5368 static void
5369vgr_jump_to_match(
5370 qf_info_T *qi,
5371 int forceit,
5372 int *redraw_for_dummy,
5373 buf_T *first_match_buf,
5374 char_u *target_dir)
5375{
5376 buf_T *buf;
5377
5378 buf = curbuf;
5379 qf_jump(qi, 0, 0, forceit);
5380 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005381 // If we jumped to another buffer redrawing will already be
5382 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005383 *redraw_for_dummy = FALSE;
5384
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005385 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005386 if (curbuf == first_match_buf && target_dir != NULL)
5387 {
5388 exarg_T ea;
5389
5390 ea.arg = target_dir;
5391 ea.cmdidx = CMD_lcd;
5392 ex_cd(&ea);
5393 }
5394}
5395
5396/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005397 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005398 * ":vimgrepadd {pattern} file(s)"
5399 * ":lvimgrep {pattern} file(s)"
5400 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005401 */
5402 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005403ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005404{
Bram Moolenaar81695252004-12-29 20:58:21 +00005405 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005406 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005407 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005408 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005409 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005410 char_u *s;
5411 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005412 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005413 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005414 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005415 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005416 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005417 buf_T *buf;
5418 int duplicate_name = FALSE;
5419 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005420 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005421 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005422 buf_T *first_match_buf = NULL;
5423 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005424 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005425 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005426 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005427 char_u *dirname_start = NULL;
5428 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005429 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005430 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005431
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005432 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005433 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5434 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005435 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005436#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005437 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005438 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005439#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005440 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005441
Bram Moolenaar39665952018-08-15 20:59:48 +02005442 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005443 {
5444 qi = ll_get_or_alloc_list(curwin);
5445 if (qi == NULL)
5446 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005447 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005448 }
5449
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005450 if (eap->addr_count > 0)
5451 tomatch = eap->line2;
5452 else
5453 tomatch = MAXLNUM;
5454
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005455 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005456 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005457 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005458 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005459 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005461 emsg(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005462 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005463 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005464
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005465 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005466 if (regmatch.regprog == NULL)
5467 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005468
5469 p = skipwhite(p);
5470 if (*p == NUL)
5471 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005472 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005473 goto theend;
5474 }
5475
Bram Moolenaar55b69262017-08-13 13:42:01 +02005476 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005477 && eap->cmdidx != CMD_vimgrepadd
5478 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005479 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005480 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005481 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005482
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005483 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005484 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005485 goto theend;
5486 if (fcount == 0)
5487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005488 emsg(_(e_nomatch));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005489 goto theend;
5490 }
5491
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005492 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5493 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005494 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005495 {
5496 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005497 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005498 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005499
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005500 // Remember the current directory, because a BufRead autocommand that does
5501 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005502 mch_dirname(dirname_start, MAXPATHL);
5503
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005504 incr_quickfix_busy();
5505
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005506 // Remember the current quickfix list identifier, so that we can check for
5507 // autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005508 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005509
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005510 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005511 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005512 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005513 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005514 if (time(NULL) > seconds)
5515 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005516 // Display the file name every second or so, show the user we are
5517 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005518 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005519 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005520 }
5521
Bram Moolenaar81695252004-12-29 20:58:21 +00005522 buf = buflist_findname_exp(fnames[fi]);
5523 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5524 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005525 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005526 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005527 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005528 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005529
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005530 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005531 }
5532 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005533 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005534 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005535
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005536 // Check whether the quickfix list is still valid. When loading a
5537 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005538 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005539 {
5540 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005541 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005542 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005543 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005544 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005545
Bram Moolenaar81695252004-12-29 20:58:21 +00005546 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005547 {
5548 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005549 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005550 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005551 else
5552 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005553 // Try for a match in all lines of the buffer.
5554 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005555 found_match = vgr_match_buflines(qf_get_curlist(qi),
5556 fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005557 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005558
Bram Moolenaar81695252004-12-29 20:58:21 +00005559 if (using_dummy)
5560 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005561 if (found_match && first_match_buf == NULL)
5562 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005563 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005564 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005565 // Never keep a dummy buffer if there is another buffer
5566 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005567 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005568 buf = NULL;
5569 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005570 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005571 || buf->b_p_bh[0] == 'u' // "unload"
5572 || buf->b_p_bh[0] == 'w' // "wipe"
5573 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005574 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005575 // When no match was found we don't need to remember the
5576 // buffer, wipe it out. If there was a match and it
5577 // wasn't the first one or we won't jump there: only
5578 // unload the buffer.
5579 // Ignore 'hidden' here, because it may lead to having too
5580 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005581 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005582 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005583 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005584 buf = NULL;
5585 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005586 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005587 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005588 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005589 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005590 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005591 buf = NULL;
5592 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005593 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005594
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005595 if (buf != NULL)
5596 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005597 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005598 buf->b_flags &= ~BF_DUMMY;
5599
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005600 // If the buffer is still loaded we need to use the
5601 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005602 if (buf == first_match_buf
5603 && target_dir == NULL
5604 && STRCMP(dirname_start, dirname_now) != 0)
5605 target_dir = vim_strsave(dirname_now);
5606
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005607 // The buffer is still loaded, the Filetype autocommands
5608 // need to be done now, in that buffer. And the modelines
5609 // need to be done (again). But not the window-local
5610 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005611 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005612#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005613 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5614 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005615#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005616 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005617 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005618 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005619 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005620 }
5621 }
5622
5623 FreeWild(fcount, fnames);
5624
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005625 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005626 qfl->qf_nonevalid = FALSE;
5627 qfl->qf_ptr = qfl->qf_start;
5628 qfl->qf_index = 1;
5629 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005630
Bram Moolenaar864293a2016-06-02 13:40:04 +02005631 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005632
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005633 if (au_name != NULL)
5634 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5635 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005636 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5637 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005638 if (!qflist_valid(wp, save_qfid)
5639 || qf_restore_list(qi, save_qfid) == FAIL)
5640 {
5641 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005642 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005643 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005644
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005645 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01005646 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005647 {
5648 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005649 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5650 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005651 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005652 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005653 semsg(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005654
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005655 decr_quickfix_busy();
5656
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005657 // If we loaded a dummy buffer into the current window, the autocommands
5658 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005659 if (redraw_for_dummy)
5660 {
5661#ifdef FEAT_FOLDING
5662 foldUpdateAll(curwin);
5663#else
5664 redraw_later(NOT_VALID);
5665#endif
5666 }
5667
Bram Moolenaar86b68352004-12-27 21:59:20 +00005668theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005669 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005670 vim_free(dirname_now);
5671 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005672 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005673 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005674}
5675
5676/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005677 * Restore current working directory to "dirname_start" if they differ, taking
5678 * into account whether it is set locally or globally.
5679 */
5680 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005681restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005682{
5683 char_u *dirname_now = alloc(MAXPATHL);
5684
5685 if (NULL != dirname_now)
5686 {
5687 mch_dirname(dirname_now, MAXPATHL);
5688 if (STRCMP(dirname_start, dirname_now) != 0)
5689 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005690 // If the directory has changed, change it back by building up an
5691 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005692 exarg_T ea;
5693
5694 ea.arg = dirname_start;
5695 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5696 ex_cd(&ea);
5697 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005698 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005699 }
5700}
5701
5702/*
5703 * Load file "fname" into a dummy buffer and return the buffer pointer,
5704 * placing the directory resulting from the buffer load into the
5705 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5706 * prior to calling this function. Restores directory to "dirname_start" prior
5707 * to returning, if autocmds or the 'autochdir' option have changed it.
5708 *
5709 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5710 * or wipe_dummy_buffer() later!
5711 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005712 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005713 */
5714 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005715load_dummy_buffer(
5716 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005717 char_u *dirname_start, // in: old directory
5718 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005719{
5720 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005721 bufref_T newbufref;
5722 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005723 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005724 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005725 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005726
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005727 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005728 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5729 if (newbuf == NULL)
5730 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005731 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005732
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005733 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005734 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5735
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005736 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005737 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005738 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005739 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005740 ++newbuf->b_locked;
5741
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005742 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005743 aucmd_prepbuf(&aco, newbuf);
5744
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005745 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005746 (void)setfname(curbuf, fname, NULL, FALSE);
5747
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005748 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005749 check_need_swap(TRUE);
5750
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005751 // Remove the "dummy" flag, otherwise autocommands may not
5752 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005753 curbuf->b_flags &= ~BF_DUMMY;
5754
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005755 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005756 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005757 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005758 NULL, READ_NEW | READ_DUMMY);
5759 --newbuf->b_locked;
5760 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005761 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005762 && !(curbuf->b_flags & BF_NEW))
5763 {
5764 failed = FALSE;
5765 if (curbuf != newbuf)
5766 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005767 // Bloody autocommands changed the buffer! Can happen when
5768 // using netrw and editing a remote file. Use the current
5769 // buffer instead, delete the dummy one after restoring the
5770 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005771 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005772 newbuf = curbuf;
5773 }
5774 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005775
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005776 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005777 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005778 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5779 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005780
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005781 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5782 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005783 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005784 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005785
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005786 // When autocommands/'autochdir' option changed directory: go back.
5787 // Let the caller know what the resulting dir was first, in case it is
5788 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005789 mch_dirname(resulting_dir, MAXPATHL);
5790 restore_start_dir(dirname_start);
5791
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005792 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005793 return NULL;
5794 if (failed)
5795 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005796 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005797 return NULL;
5798 }
5799 return newbuf;
5800}
5801
5802/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005803 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5804 * directory to "dirname_start" prior to returning, if autocmds or the
5805 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005806 */
5807 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005808wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005809{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005810 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005811 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005812#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005813 cleanup_T cs;
5814
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005815 // Reset the error/interrupt/exception state here so that aborting()
5816 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5817 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005818 enter_cleanup(&cs);
5819#endif
5820
Bram Moolenaar81695252004-12-29 20:58:21 +00005821 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005822
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005823#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005824 // Restore the error/interrupt/exception state if not discarded by a
5825 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005826 leave_cleanup(&cs);
5827#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005828 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005829 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005830 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005831}
5832
5833/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005834 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5835 * directory to "dirname_start" prior to returning, if autocmds or the
5836 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005837 */
5838 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005839unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005840{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005841 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005842 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005843 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005844
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005845 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005846 restore_start_dir(dirname_start);
5847 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005848}
5849
Bram Moolenaar05159a02005-02-26 23:04:13 +00005850#if defined(FEAT_EVAL) || defined(PROTO)
5851/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005852 * Copy the specified quickfix entry items into a new dict and appened the dict
5853 * to 'list'. Returns OK on success.
5854 */
5855 static int
5856get_qfline_items(qfline_T *qfp, list_T *list)
5857{
5858 int bufnum;
5859 dict_T *dict;
5860 char_u buf[2];
5861
5862 // Handle entries with a non-existing buffer number.
5863 bufnum = qfp->qf_fnum;
5864 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5865 bufnum = 0;
5866
5867 if ((dict = dict_alloc()) == NULL)
5868 return FAIL;
5869 if (list_append_dict(list, dict) == FAIL)
5870 return FAIL;
5871
5872 buf[0] = qfp->qf_type;
5873 buf[1] = NUL;
5874 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5875 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5876 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5877 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5878 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5879 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5880 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5881 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5882 || dict_add_string(dict, "type", buf) == FAIL
5883 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
5884 return FAIL;
5885
5886 return OK;
5887}
5888
5889/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005890 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005891 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005892 */
5893 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005894get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005895{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005896 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01005897 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005898 qfline_T *qfp;
5899 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005900
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005901 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005902 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005903 qi = &ql_info;
5904 if (wp != NULL)
5905 {
5906 qi = GET_LOC_LIST(wp);
5907 if (qi == NULL)
5908 return FAIL;
5909 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005910 }
5911
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005912 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005913 qf_idx = qi->qf_curlist;
5914
Bram Moolenaar0398e002019-03-21 21:12:49 +01005915 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005916 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005917
Bram Moolenaar0398e002019-03-21 21:12:49 +01005918 qfl = qf_get_list(qi, qf_idx);
5919 if (qf_list_empty(qfl))
5920 return FAIL;
5921
Bram Moolenaara16123a2019-03-28 20:31:07 +01005922 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005923 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01005924 if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005925 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005926 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005927
Bram Moolenaar05159a02005-02-26 23:04:13 +00005928 return OK;
5929}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005930
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005931// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005932enum {
5933 QF_GETLIST_NONE = 0x0,
5934 QF_GETLIST_TITLE = 0x1,
5935 QF_GETLIST_ITEMS = 0x2,
5936 QF_GETLIST_NR = 0x4,
5937 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005938 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005939 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005940 QF_GETLIST_IDX = 0x40,
5941 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005942 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005943 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005944 QF_GETLIST_QFBUFNR = 0x400,
5945 QF_GETLIST_ALL = 0x7FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005946};
5947
5948/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005949 * Parse text from 'di' and return the quickfix list items.
5950 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005951 */
5952 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005953qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005954{
5955 int status = FAIL;
5956 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005957 char_u *errorformat = p_efm;
5958 dictitem_T *efm_di;
5959 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005960
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005961 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005962 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005963 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005964 // If errorformat is supplied then use it, otherwise use the 'efm'
5965 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005966 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5967 {
5968 if (efm_di->di_tv.v_type != VAR_STRING ||
5969 efm_di->di_tv.vval.v_string == NULL)
5970 return FAIL;
5971 errorformat = efm_di->di_tv.vval.v_string;
5972 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005973
Bram Moolenaar36538222017-09-02 19:51:44 +02005974 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005975 if (l == NULL)
5976 return FAIL;
5977
Bram Moolenaar2d67d302018-11-16 18:46:02 +01005978 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005979 if (qi != NULL)
5980 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005981 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005982 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5983 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005984 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005985 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005986 }
5987 free(qi);
5988 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005989 dict_add_list(retdict, "items", l);
5990 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005991 }
5992
5993 return status;
5994}
5995
5996/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005997 * Return the quickfix/location list window identifier in the current tabpage.
5998 */
5999 static int
6000qf_winid(qf_info_T *qi)
6001{
6002 win_T *win;
6003
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006004 // The quickfix window can be opened even if the quickfix list is not set
6005 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006006 if (qi == NULL)
6007 return 0;
6008 win = qf_find_win(qi);
6009 if (win != NULL)
6010 return win->w_id;
6011 return 0;
6012}
6013
6014/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006015 * Returns the number of the buffer displayed in the quickfix/location list
6016 * window. If there is no buffer associated with the list, then returns 0.
6017 */
6018 static int
6019qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6020{
6021 return dict_add_number(retdict, "qfbufnr",
6022 (qi == NULL) ? 0 : qi->qf_bufnr);
6023}
6024
6025/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006026 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006027 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006028 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006029qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006030{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006031 int flags = QF_GETLIST_NONE;
6032
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006033 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006034 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006035 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006036 if (!loclist)
6037 // File window ID is applicable only to location list windows
6038 flags &= ~ QF_GETLIST_FILEWINID;
6039 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006040
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006041 if (dict_find(what, (char_u *)"title", -1) != NULL)
6042 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006043
Bram Moolenaara6d48492017-12-12 22:45:31 +01006044 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6045 flags |= QF_GETLIST_NR;
6046
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006047 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6048 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006049
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006050 if (dict_find(what, (char_u *)"context", -1) != NULL)
6051 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006052
Bram Moolenaara6d48492017-12-12 22:45:31 +01006053 if (dict_find(what, (char_u *)"id", -1) != NULL)
6054 flags |= QF_GETLIST_ID;
6055
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006056 if (dict_find(what, (char_u *)"items", -1) != NULL)
6057 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006058
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006059 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6060 flags |= QF_GETLIST_IDX;
6061
6062 if (dict_find(what, (char_u *)"size", -1) != NULL)
6063 flags |= QF_GETLIST_SIZE;
6064
Bram Moolenaarb254af32017-12-18 19:48:58 +01006065 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6066 flags |= QF_GETLIST_TICK;
6067
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006068 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6069 flags |= QF_GETLIST_FILEWINID;
6070
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006071 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6072 flags |= QF_GETLIST_QFBUFNR;
6073
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006074 return flags;
6075}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006076
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006077/*
6078 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6079 * If 'nr' and 'id' are not present in 'what' then return the current
6080 * quickfix list index.
6081 * If 'nr' is zero then return the current quickfix list index.
6082 * If 'nr' is '$' then return the last quickfix list index.
6083 * If 'id' is present then return the index of the quickfix list with that id.
6084 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6085 * Return -1, if quickfix list is not present or if the stack is empty.
6086 */
6087 static int
6088qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6089{
6090 int qf_idx;
6091 dictitem_T *di;
6092
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006093 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006094 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6095 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006096 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006097 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006098 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006099 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006100 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006101 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006102 qf_idx = di->di_tv.vval.v_number - 1;
6103 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006104 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006105 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006106 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006107 else if (di->di_tv.v_type == VAR_STRING
6108 && di->di_tv.vval.v_string != NULL
6109 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006110 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006111 qf_idx = qi->qf_listcount - 1;
6112 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006113 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006114 }
6115
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006116 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6117 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006118 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006119 if (di->di_tv.v_type == VAR_NUMBER)
6120 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006121 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006122 if (di->di_tv.vval.v_number != 0)
6123 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6124 }
6125 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006126 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006127 }
6128
6129 return qf_idx;
6130}
6131
6132/*
6133 * Return default values for quickfix list properties in retdict.
6134 */
6135 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006136qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006137{
6138 int status = OK;
6139
6140 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006141 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006142 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6143 {
6144 list_T *l = list_alloc();
6145 if (l != NULL)
6146 status = dict_add_list(retdict, "items", l);
6147 else
6148 status = FAIL;
6149 }
6150 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006151 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006152 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006153 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006154 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006155 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006156 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006157 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006158 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006159 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006160 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006161 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006162 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006163 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006164 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006165 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006166 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6167 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006168
6169 return status;
6170}
6171
6172/*
6173 * Return the quickfix list title as 'title' in retdict
6174 */
6175 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006176qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006177{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006178 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006179}
6180
6181/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006182 * Returns the identifier of the window used to display files from a location
6183 * list. If there is no associated window, then returns 0. Useful only when
6184 * called from a location list window.
6185 */
6186 static int
6187qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6188{
6189 int winid = 0;
6190
6191 if (wp != NULL && IS_LL_WINDOW(wp))
6192 {
6193 win_T *ll_wp = qf_find_win_with_loclist(qi);
6194 if (ll_wp != NULL)
6195 winid = ll_wp->w_id;
6196 }
6197
6198 return dict_add_number(retdict, "filewinid", winid);
6199}
6200
6201/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006202 * Return the quickfix list items/entries as 'items' in retdict
6203 */
6204 static int
6205qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6206{
6207 int status = OK;
6208 list_T *l = list_alloc();
6209 if (l != NULL)
6210 {
6211 (void)get_errorlist(qi, NULL, qf_idx, l);
6212 dict_add_list(retdict, "items", l);
6213 }
6214 else
6215 status = FAIL;
6216
6217 return status;
6218}
6219
6220/*
6221 * Return the quickfix list context (if any) as 'context' in retdict.
6222 */
6223 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006224qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006225{
6226 int status;
6227 dictitem_T *di;
6228
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006229 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006230 {
6231 di = dictitem_alloc((char_u *)"context");
6232 if (di != NULL)
6233 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006234 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006235 status = dict_add(retdict, di);
6236 if (status == FAIL)
6237 dictitem_free(di);
6238 }
6239 else
6240 status = FAIL;
6241 }
6242 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006243 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006244
6245 return status;
6246}
6247
6248/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006249 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006250 */
6251 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01006252qf_getprop_idx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006253{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006254 int curidx = qfl->qf_index;
6255 if (qf_list_empty(qfl))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006256 // For empty lists, current index is set to 0
6257 curidx = 0;
6258 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006259}
6260
6261/*
6262 * Return quickfix/location list details (title) as a
6263 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6264 * then current list is used. Otherwise the specified list is used.
6265 */
6266 int
6267qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6268{
6269 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006270 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006271 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006272 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006273 dictitem_T *di;
6274 int flags = QF_GETLIST_NONE;
6275
6276 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6277 return qf_get_list_from_lines(what, di, retdict);
6278
6279 if (wp != NULL)
6280 qi = GET_LOC_LIST(wp);
6281
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006282 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006283
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006284 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006285 qf_idx = qf_getprop_qfidx(qi, what);
6286
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006287 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006288 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006289 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006290
Bram Moolenaar0398e002019-03-21 21:12:49 +01006291 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006292
Bram Moolenaard823fa92016-08-12 16:29:27 +02006293 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006294 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006295 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006296 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006297 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006298 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006299 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006300 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006301 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006302 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006303 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006304 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006305 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar0398e002019-03-21 21:12:49 +01006306 status = qf_getprop_idx(qfl, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006307 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006308 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006309 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006310 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006311 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6312 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006313 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6314 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006315
Bram Moolenaard823fa92016-08-12 16:29:27 +02006316 return status;
6317}
6318
6319/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006320 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006321 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6322 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006323 */
6324 static int
6325qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01006326 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006327 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006328 int first_entry,
6329 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006330{
6331 static int did_bufnr_emsg;
6332 char_u *filename, *module, *pattern, *text, *type;
6333 int bufnum, valid, status, col, vcol, nr;
6334 long lnum;
6335
6336 if (first_entry)
6337 did_bufnr_emsg = FALSE;
6338
Bram Moolenaar8f667172018-12-14 15:38:31 +01006339 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6340 module = dict_get_string(d, (char_u *)"module", TRUE);
6341 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6342 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6343 col = (int)dict_get_number(d, (char_u *)"col");
6344 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6345 nr = (int)dict_get_number(d, (char_u *)"nr");
6346 type = dict_get_string(d, (char_u *)"type", TRUE);
6347 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6348 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006349 if (text == NULL)
6350 text = vim_strsave((char_u *)"");
6351
6352 valid = TRUE;
6353 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6354 valid = FALSE;
6355
6356 // Mark entries with non-existing buffer number as not valid. Give the
6357 // error message only once.
6358 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6359 {
6360 if (!did_bufnr_emsg)
6361 {
6362 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006363 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006364 }
6365 valid = FALSE;
6366 bufnum = 0;
6367 }
6368
6369 // If the 'valid' field is present it overrules the detected value.
6370 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006371 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006372
Bram Moolenaar0398e002019-03-21 21:12:49 +01006373 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006374 NULL, // dir
6375 filename,
6376 module,
6377 bufnum,
6378 text,
6379 lnum,
6380 col,
6381 vcol, // vis_col
6382 pattern, // search pattern
6383 nr,
6384 type == NULL ? NUL : *type,
6385 valid);
6386
6387 vim_free(filename);
6388 vim_free(module);
6389 vim_free(pattern);
6390 vim_free(text);
6391 vim_free(type);
6392
Bram Moolenaar9752c722018-12-22 16:49:34 +01006393 if (valid)
6394 *valid_entry = TRUE;
6395
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006396 return status;
6397}
6398
6399/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006400 * Add list of entries to quickfix/location list. Each list entry is
6401 * a dictionary with item information.
6402 */
6403 static int
6404qf_add_entries(
6405 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006406 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006407 list_T *list,
6408 char_u *title,
6409 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006410{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006411 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006412 listitem_T *li;
6413 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006414 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006415 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006416 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006417
Bram Moolenaara3921f42017-06-04 15:30:34 +02006418 if (action == ' ' || qf_idx == qi->qf_listcount)
6419 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006420 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006421 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006422 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006423 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006424 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01006425 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006426 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006427 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006428 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006429 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006430 qf_free_items(qfl);
6431 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006432 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006433
6434 for (li = list->lv_first; li != NULL; li = li->li_next)
6435 {
6436 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006437 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006438
6439 d = li->li_tv.vval.v_dict;
6440 if (d == NULL)
6441 continue;
6442
Bram Moolenaar0398e002019-03-21 21:12:49 +01006443 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006444 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02006445 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006446 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006447 }
6448
Bram Moolenaar9752c722018-12-22 16:49:34 +01006449 // Check if any valid error entries are added to the list.
6450 if (valid_entry)
6451 qfl->qf_nonevalid = FALSE;
6452 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006453 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006454 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006455
6456 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006457 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006458 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006459
6460 // Update the current error index if not appending to the list or if the
6461 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006462 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01006463 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006464
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006465 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006466 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006467
6468 return retval;
6469}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006470
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006471/*
6472 * Get the quickfix list index from 'nr' or 'id'
6473 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006474 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006475qf_setprop_get_qfidx(
6476 qf_info_T *qi,
6477 dict_T *what,
6478 int action,
6479 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006480{
6481 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006482 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006483
Bram Moolenaard823fa92016-08-12 16:29:27 +02006484 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6485 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006486 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006487 if (di->di_tv.v_type == VAR_NUMBER)
6488 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006489 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006490 if (di->di_tv.vval.v_number != 0)
6491 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006492
Bram Moolenaar55b69262017-08-13 13:42:01 +02006493 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6494 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006495 // When creating a new list, accept qf_idx pointing to the next
6496 // non-available list and add the new list at the end of the
6497 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006498 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006499 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006500 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006501 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006502 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006503 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006504 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006505 }
6506 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006507 && di->di_tv.vval.v_string != NULL
6508 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006509 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006510 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006511 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006512 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006513 qf_idx = 0;
6514 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006515 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006516 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006517 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006518 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006519 }
6520
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006521 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006522 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006523 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006524 if (di->di_tv.v_type != VAR_NUMBER)
6525 return INVALID_QFIDX;
6526
6527 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006528 }
6529
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006530 return qf_idx;
6531}
6532
6533/*
6534 * Set the quickfix list title.
6535 */
6536 static int
6537qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6538{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006539 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006540
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006541 if (di->di_tv.v_type != VAR_STRING)
6542 return FAIL;
6543
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006544 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006545 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006546 if (qf_idx == qi->qf_curlist)
6547 qf_update_win_titlevar(qi);
6548
6549 return OK;
6550}
6551
6552/*
6553 * Set quickfix list items/entries.
6554 */
6555 static int
6556qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6557{
6558 int retval = FAIL;
6559 char_u *title_save;
6560
6561 if (di->di_tv.v_type != VAR_LIST)
6562 return FAIL;
6563
6564 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6565 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6566 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006567 vim_free(title_save);
6568
6569 return retval;
6570}
6571
6572/*
6573 * Set quickfix list items/entries from a list of lines.
6574 */
6575 static int
6576qf_setprop_items_from_lines(
6577 qf_info_T *qi,
6578 int qf_idx,
6579 dict_T *what,
6580 dictitem_T *di,
6581 int action)
6582{
6583 char_u *errorformat = p_efm;
6584 dictitem_T *efm_di;
6585 int retval = FAIL;
6586
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006587 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006588 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6589 {
6590 if (efm_di->di_tv.v_type != VAR_STRING ||
6591 efm_di->di_tv.vval.v_string == NULL)
6592 return FAIL;
6593 errorformat = efm_di->di_tv.vval.v_string;
6594 }
6595
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006596 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006597 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6598 return FAIL;
6599
6600 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006601 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006602 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6603 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6604 retval = OK;
6605
6606 return retval;
6607}
6608
6609/*
6610 * Set quickfix list context.
6611 */
6612 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006613qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006614{
6615 typval_T *ctx;
6616
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006617 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006618 ctx = alloc_tv();
6619 if (ctx != NULL)
6620 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006621 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006622
6623 return OK;
6624}
6625
6626/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006627 * Set the current index in the specified quickfix list
6628 */
6629 static int
6630qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
6631{
6632 int denote = FALSE;
6633 int newidx;
6634 int old_qfidx;
6635 qfline_T *qf_ptr;
6636
6637 // If the specified index is '$', then use the last entry
6638 if (di->di_tv.v_type == VAR_STRING
6639 && di->di_tv.vval.v_string != NULL
6640 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
6641 newidx = qfl->qf_count;
6642 else
6643 {
6644 // Otherwise use the specified index
6645 newidx = tv_get_number_chk(&di->di_tv, &denote);
6646 if (denote)
6647 return FAIL;
6648 }
6649
6650 if (newidx < 1) // sanity check
6651 return FAIL;
6652 if (newidx > qfl->qf_count)
6653 newidx = qfl->qf_count;
6654
6655 old_qfidx = qfl->qf_index;
6656 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
6657 if (qf_ptr == NULL)
6658 return FAIL;
6659 qfl->qf_ptr = qf_ptr;
6660 qfl->qf_index = newidx;
6661
6662 // If the current list is modified and it is displayed in the quickfix
6663 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006664 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006665 qf_win_pos_update(qi, old_qfidx);
6666
6667 return OK;
6668}
6669
6670/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006671 * Set quickfix/location list properties (title, items, context).
6672 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006673 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006674 */
6675 static int
6676qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6677{
6678 dictitem_T *di;
6679 int retval = FAIL;
6680 int qf_idx;
6681 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006682 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006683
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006684 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006685 newlist = TRUE;
6686
6687 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006688 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006689 return FAIL;
6690
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006691 if (newlist)
6692 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006693 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006694 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006695 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006696 }
6697
Bram Moolenaar0398e002019-03-21 21:12:49 +01006698 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006699 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006700 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006701 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006702 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006703 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006704 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006705 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006706 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006707 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6708 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006709
Bram Moolenaarb254af32017-12-18 19:48:58 +01006710 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006711 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006712
Bram Moolenaard823fa92016-08-12 16:29:27 +02006713 return retval;
6714}
6715
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006716/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006717 * Free the entire quickfix/location list stack.
6718 * If the quickfix/location list window is open, then clear it.
6719 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006720 static void
6721qf_free_stack(win_T *wp, qf_info_T *qi)
6722{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006723 win_T *qfwin = qf_find_win(qi);
6724 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006725
6726 if (qfwin != NULL)
6727 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006728 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006729 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006730 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006731 qf_update_buffer(qi, NULL);
6732 }
6733
6734 if (wp != NULL && IS_LL_WINDOW(wp))
6735 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006736 // If in the location list window, then use the non-location list
6737 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006738 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006739 if (llwin != NULL)
6740 wp = llwin;
6741 }
6742
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006743 qf_free_all(wp);
6744 if (wp == NULL)
6745 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006746 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006747 qi->qf_curlist = 0;
6748 qi->qf_listcount = 0;
6749 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006750 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006751 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006752 // If the location list window is open, then create a new empty
6753 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006754 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006755
Bram Moolenaar95946f12019-03-31 15:31:59 +02006756 if (new_ll != NULL)
6757 {
6758 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006759
Bram Moolenaar95946f12019-03-31 15:31:59 +02006760 // first free the list reference in the location list window
6761 ll_free_all(&qfwin->w_llist_ref);
6762
6763 qfwin->w_llist_ref = new_ll;
6764 if (wp != qfwin)
6765 win_set_loclist(wp, new_ll);
6766 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006767 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006768}
6769
Bram Moolenaard823fa92016-08-12 16:29:27 +02006770/*
6771 * Populate the quickfix list with the items supplied in the list
6772 * of dictionaries. "title" will be copied to w:quickfix_title.
6773 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6774 */
6775 int
6776set_errorlist(
6777 win_T *wp,
6778 list_T *list,
6779 int action,
6780 char_u *title,
6781 dict_T *what)
6782{
6783 qf_info_T *qi = &ql_info;
6784 int retval = OK;
6785
6786 if (wp != NULL)
6787 {
6788 qi = ll_get_or_alloc_list(wp);
6789 if (qi == NULL)
6790 return FAIL;
6791 }
6792
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006793 if (action == 'f')
6794 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006795 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006796 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006797 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006798 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006799
6800 incr_quickfix_busy();
6801
6802 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006803 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006804 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006805 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006806 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006807 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006808 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01006809 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006810
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006811 decr_quickfix_busy();
6812
Bram Moolenaard823fa92016-08-12 16:29:27 +02006813 return retval;
6814}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006815
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006816/*
6817 * Mark the context as in use for all the lists in a quickfix stack.
6818 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006819 static int
6820mark_quickfix_ctx(qf_info_T *qi, int copyID)
6821{
6822 int i;
6823 int abort = FALSE;
6824 typval_T *ctx;
6825
6826 for (i = 0; i < LISTCOUNT && !abort; ++i)
6827 {
6828 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006829 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6830 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006831 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6832 }
6833
6834 return abort;
6835}
6836
6837/*
6838 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006839 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006840 */
6841 int
6842set_ref_in_quickfix(int copyID)
6843{
6844 int abort = FALSE;
6845 tabpage_T *tp;
6846 win_T *win;
6847
6848 abort = mark_quickfix_ctx(&ql_info, copyID);
6849 if (abort)
6850 return abort;
6851
6852 FOR_ALL_TAB_WINDOWS(tp, win)
6853 {
6854 if (win->w_llist != NULL)
6855 {
6856 abort = mark_quickfix_ctx(win->w_llist, copyID);
6857 if (abort)
6858 return abort;
6859 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006860 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6861 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006862 // In a location list window and none of the other windows is
6863 // referring to this location list. Mark the location list
6864 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006865 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6866 if (abort)
6867 return abort;
6868 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006869 }
6870
6871 return abort;
6872}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006873#endif
6874
Bram Moolenaar81695252004-12-29 20:58:21 +00006875/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01006876 * Return the autocmd name for the :cbuffer Ex commands
6877 */
6878 static char_u *
6879cbuffer_get_auname(cmdidx_T cmdidx)
6880{
6881 switch (cmdidx)
6882 {
6883 case CMD_cbuffer: return (char_u *)"cbuffer";
6884 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
6885 case CMD_caddbuffer: return (char_u *)"caddbuffer";
6886 case CMD_lbuffer: return (char_u *)"lbuffer";
6887 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
6888 case CMD_laddbuffer: return (char_u *)"laddbuffer";
6889 default: return NULL;
6890 }
6891}
6892
6893/*
6894 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
6895 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
6896 */
6897 static int
6898cbuffer_process_args(
6899 exarg_T *eap,
6900 buf_T **bufp,
6901 linenr_T *line1,
6902 linenr_T *line2)
6903{
6904 buf_T *buf = NULL;
6905
6906 if (*eap->arg == NUL)
6907 buf = curbuf;
6908 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6909 buf = buflist_findnr(atoi((char *)eap->arg));
6910
6911 if (buf == NULL)
6912 {
6913 emsg(_(e_invarg));
6914 return FAIL;
6915 }
6916
6917 if (buf->b_ml.ml_mfp == NULL)
6918 {
6919 emsg(_("E681: Buffer is not loaded"));
6920 return FAIL;
6921 }
6922
6923 if (eap->addr_count == 0)
6924 {
6925 eap->line1 = 1;
6926 eap->line2 = buf->b_ml.ml_line_count;
6927 }
6928
6929 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6930 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6931 {
6932 emsg(_(e_invrange));
6933 return FAIL;
6934 }
6935
6936 *line1 = eap->line1;
6937 *line2 = eap->line2;
6938 *bufp = buf;
6939
6940 return OK;
6941}
6942
6943/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006944 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006945 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006946 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006947 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006948 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006949 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006950 */
6951 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006952ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006953{
6954 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006955 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006956 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006957 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006958 int_u save_qfid;
6959 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01006960 char_u *qf_title;
6961 linenr_T line1;
6962 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006963
Bram Moolenaara16123a2019-03-28 20:31:07 +01006964 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01006965 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01006966 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006967 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006968#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006969 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006970 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006971#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006972 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006973
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006974 // Must come after autocommands.
Bram Moolenaar39665952018-08-15 20:59:48 +02006975 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006976 {
6977 qi = ll_get_or_alloc_list(curwin);
6978 if (qi == NULL)
6979 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006980 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006981 }
6982
Bram Moolenaara16123a2019-03-28 20:31:07 +01006983 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
6984 return;
6985
6986 qf_title = qf_cmdtitle(*eap->cmdlinep);
6987
6988 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006989 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01006990 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6991 (char *)qf_title, (char *)buf->b_sfname);
6992 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006993 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006994
6995 incr_quickfix_busy();
6996
6997 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
6998 (eap->cmdidx != CMD_caddbuffer
6999 && eap->cmdidx != CMD_laddbuffer),
7000 line1, line2,
7001 qf_title, NULL);
7002 if (qf_stack_empty(qi))
7003 {
7004 decr_quickfix_busy();
7005 return;
7006 }
7007 if (res >= 0)
7008 qf_list_changed(qf_get_curlist(qi));
7009
7010 // Remember the current quickfix list identifier, so that we can
7011 // check for autocommands changing the current quickfix list.
7012 save_qfid = qf_get_curlist(qi)->qf_id;
7013 if (au_name != NULL)
7014 {
7015 buf_T *curbuf_old = curbuf;
7016
7017 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7018 TRUE, curbuf);
7019 if (curbuf != curbuf_old)
7020 // Autocommands changed buffer, don't jump now, "qi" may
7021 // be invalid.
7022 res = 0;
7023 }
7024 // Jump to the first error for a new list and if autocmds didn't
7025 // free the list.
7026 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7027 eap->cmdidx == CMD_lbuffer)
7028 && qflist_valid(wp, save_qfid))
7029 // display the first error
7030 qf_jump_first(qi, save_qfid, eap->forceit);
7031
7032 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007033}
7034
Bram Moolenaar1e015462005-09-25 22:16:38 +00007035#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007036/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007037 * Return the autocmd name for the :cexpr Ex commands.
7038 */
7039 static char_u *
7040cexpr_get_auname(cmdidx_T cmdidx)
7041{
7042 switch (cmdidx)
7043 {
7044 case CMD_cexpr: return (char_u *)"cexpr";
7045 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7046 case CMD_caddexpr: return (char_u *)"caddexpr";
7047 case CMD_lexpr: return (char_u *)"lexpr";
7048 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7049 case CMD_laddexpr: return (char_u *)"laddexpr";
7050 default: return NULL;
7051 }
7052}
7053
7054/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007055 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7056 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007057 */
7058 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007059ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007060{
7061 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007062 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007063 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007064 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007065 int_u save_qfid;
7066 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007067
Bram Moolenaara16123a2019-03-28 20:31:07 +01007068 au_name = cexpr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007069 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7070 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007071 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007072#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007073 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007074 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007075#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007076 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007077
Bram Moolenaar39665952018-08-15 20:59:48 +02007078 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01007079 {
7080 qi = ll_get_or_alloc_list(curwin);
7081 if (qi == NULL)
7082 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007083 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007084 }
7085
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007086 // Evaluate the expression. When the result is a string or a list we can
7087 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007088 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007089 if (tv != NULL)
7090 {
7091 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7092 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7093 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007094 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007095 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00007096 (eap->cmdidx != CMD_caddexpr
7097 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007098 (linenr_T)0, (linenr_T)0,
7099 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007100 if (qf_stack_empty(qi))
7101 {
7102 decr_quickfix_busy();
7103 goto cleanup;
7104 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01007105 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007106 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007107
7108 // Remember the current quickfix list identifier, so that we can
7109 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007110 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007111 if (au_name != NULL)
7112 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7113 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007114
7115 // Jump to the first error for a new list and if autocmds didn't
7116 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02007117 if (res > 0 && (eap->cmdidx == CMD_cexpr
7118 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007119 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02007120 // display the first error
7121 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007122 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00007123 }
7124 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007125 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007126cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00007127 free_tv(tv);
7128 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007129}
Bram Moolenaar1e015462005-09-25 22:16:38 +00007130#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007131
7132/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007133 * Get the location list for ":lhelpgrep"
7134 */
7135 static qf_info_T *
7136hgr_get_ll(int *new_ll)
7137{
7138 win_T *wp;
7139 qf_info_T *qi;
7140
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007141 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007142 if (bt_help(curwin->w_buffer))
7143 wp = curwin;
7144 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007145 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007146 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007147
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007148 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007149 qi = NULL;
7150 else
7151 qi = wp->w_llist;
7152
7153 if (qi == NULL)
7154 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007155 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007156 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007157 return NULL;
7158 *new_ll = TRUE;
7159 }
7160
7161 return qi;
7162}
7163
7164/*
7165 * Search for a pattern in a help file.
7166 */
7167 static void
7168hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007169 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007170 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007171 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007172 regmatch_T *p_regmatch)
7173{
7174 FILE *fd;
7175 long lnum;
7176
7177 fd = mch_fopen((char *)fname, "r");
7178 if (fd == NULL)
7179 return;
7180
7181 lnum = 1;
7182 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7183 {
7184 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007185
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007186 // Convert a line if 'encoding' is not utf-8 and
7187 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007188 if (p_vc->vc_type != CONV_NONE
7189 && has_non_ascii(IObuff))
7190 {
7191 line = string_convert(p_vc, IObuff, NULL);
7192 if (line == NULL)
7193 line = IObuff;
7194 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007195
7196 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7197 {
7198 int l = (int)STRLEN(line);
7199
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007200 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007201 while (l > 0 && line[l - 1] <= ' ')
7202 line[--l] = NUL;
7203
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007204 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007205 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007206 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007207 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007208 0,
7209 line,
7210 lnum,
7211 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007212 + 1, // col
7213 FALSE, // vis_col
7214 NULL, // search pattern
7215 0, // nr
7216 1, // type
7217 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02007218 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007219 {
7220 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007221 if (line != IObuff)
7222 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007223 break;
7224 }
7225 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007226 if (line != IObuff)
7227 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007228 ++lnum;
7229 line_breakcheck();
7230 }
7231 fclose(fd);
7232}
7233
7234/*
7235 * Search for a pattern in all the help files in the doc directory under
7236 * the given directory.
7237 */
7238 static void
7239hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007240 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007241 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007242 regmatch_T *p_regmatch,
7243 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007244#ifdef FEAT_MULTI_LANG
7245 , char_u *lang
7246#endif
7247 )
7248{
7249 int fcount;
7250 char_u **fnames;
7251 int fi;
7252
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007253 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007254 add_pathsep(dirname);
7255 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7256 if (gen_expand_wildcards(1, &dirname, &fcount,
7257 &fnames, EW_FILE|EW_SILENT) == OK
7258 && fcount > 0)
7259 {
7260 for (fi = 0; fi < fcount && !got_int; ++fi)
7261 {
7262#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007263 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007264 if (lang != NULL
7265 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007266 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007267 && !(STRNICMP(lang, "en", 2) == 0
7268 && STRNICMP("txt", fnames[fi]
7269 + STRLEN(fnames[fi]) - 3, 3) == 0))
7270 continue;
7271#endif
7272
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007273 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007274 }
7275 FreeWild(fcount, fnames);
7276 }
7277}
7278
7279/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007280 * Search for a pattern in all the help files in the 'runtimepath'
7281 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007282 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007283 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007284 */
7285 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007286hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007287{
7288 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007289
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007290 vimconv_T vc;
7291
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007292 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7293 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007294 vc.vc_type = CONV_NONE;
7295 if (!enc_utf8)
7296 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007297
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007298 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007299 p = p_rtp;
7300 while (*p != NUL && !got_int)
7301 {
7302 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7303
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007304 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007305#ifdef FEAT_MULTI_LANG
7306 , lang
7307#endif
7308 );
7309 }
7310
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007311 if (vc.vc_type != CONV_NONE)
7312 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007313}
7314
7315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 * ":helpgrep {pattern}"
7317 */
7318 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007319ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321 regmatch_T regmatch;
7322 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007323 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007324 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007325 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007326 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327
Bram Moolenaar73633f82012-01-20 13:39:07 +01007328 switch (eap->cmdidx)
7329 {
7330 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7331 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7332 default: break;
7333 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007334 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7335 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007336 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007337#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007338 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007339 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007340#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007341 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007342
Bram Moolenaar39665952018-08-15 20:59:48 +02007343 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007344 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007345 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007346 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007347 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007348 }
7349
Bram Moolenaara16123a2019-03-28 20:31:07 +01007350 // Make 'cpoptions' empty, the 'l' flag should not be used here.
7351 save_cpo = p_cpo;
7352 p_cpo = empty_option;
7353
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007354 incr_quickfix_busy();
7355
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007356#ifdef FEAT_MULTI_LANG
7357 // Check for a specified language
7358 lang = check_help_lang(eap->arg);
7359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7361 regmatch.rm_ic = FALSE;
7362 if (regmatch.regprog != NULL)
7363 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007364 qf_list_T *qfl;
7365
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007366 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007367 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007368 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007370 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007371
Bram Moolenaar473de612013-06-08 18:19:48 +02007372 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007374 qfl->qf_nonevalid = FALSE;
7375 qfl->qf_ptr = qfl->qf_start;
7376 qfl->qf_index = 1;
7377 qf_list_changed(qfl);
7378 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 }
7380
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007381 if (p_cpo == empty_option)
7382 p_cpo = save_cpo;
7383 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007384 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007385 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386
Bram Moolenaar73633f82012-01-20 13:39:07 +01007387 if (au_name != NULL)
7388 {
7389 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7390 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007391 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007392 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007393 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007394 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007395 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007396 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007397 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007398
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007399 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007400 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007401 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007402 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007403 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007404
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007405 decr_quickfix_busy();
7406
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007407 if (eap->cmdidx == CMD_lhelpgrep)
7408 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007409 // If the help window is not opened or if it already points to the
7410 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007411 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007412 {
7413 if (new_qi)
7414 ll_free_all(&qi);
7415 }
7416 else if (curwin->w_llist == NULL)
7417 curwin->w_llist = qi;
7418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419}
7420
7421#endif /* FEAT_QUICKFIX */