blob: e8c7829616ab2d130b71dda7c8dd7b838fa658c1 [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 Moolenaar3ff33112019-05-03 21:56:35 +0200180static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200182// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000183#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200184// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000185#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200186
187// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100188#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
189#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
190#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
191#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200192
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000193/*
194 * Return location list for window 'wp'
195 * For location list window, return the referenced location list
196 */
197#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
198
Bram Moolenaar95946f12019-03-31 15:31:59 +0200199// Macro to loop through all the items in a quickfix list
200// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100201#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
Bram Moolenaar95946f12019-03-31 15:31:59 +0200202 for (i = 1, qfp = qfl->qf_start; \
203 !got_int && i <= qfl->qf_count && qfp != NULL; \
Bram Moolenaara16123a2019-03-28 20:31:07 +0100204 ++i, qfp = qfp->qf_next)
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200207 * Looking up a buffer can be slow if there are many. Remember the last one
208 * to make this a lot faster if there are multiple matches in the same file.
209 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200210static char_u *qf_last_bufname = NULL;
211static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200212
Bram Moolenaar3c097222017-12-21 20:54:49 +0100213static char *e_loc_list_changed =
214 N_("E926: Current location list was changed");
215
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200216/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200217 * Maximum number of bytes allowed per line while reading a errorfile.
218 */
219#define LINE_MAXLEN 4096
220
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200221static struct fmtpattern
222{
223 char_u convchar;
224 char *pattern;
225} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200226 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200227 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200228 {'n', "\\d\\+"},
229 {'l', "\\d\\+"},
230 {'c', "\\d\\+"},
231 {'t', "."},
232 {'m', ".\\+"},
233 {'r', ".*"},
234 {'p', "[- .]*"},
235 {'v', "\\d\\+"},
236 {'s', ".\\+"},
237 {'o', ".\\+"}
238 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200239
240/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200241 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200242 * See fmt_pat definition above for the list of supported patterns. The
243 * pattern specifier is supplied in "efmpat". The converted pattern is stored
244 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200245 */
246 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200247efmpat_to_regpat(
248 char_u *efmpat,
249 char_u *regpat,
250 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200251 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100252 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200253{
254 char_u *srcptr;
255
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200256 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200257 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200258 // Each errorformat pattern can occur only once
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100259 semsg(_("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200260 return NULL;
261 }
262 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200263 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200264 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200265 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100267 semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200268 return NULL;
269 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200270 efminfo->addr[idx] = (char_u)++round;
271 *regpat++ = '\\';
272 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200273#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200274 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200275 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200276 // Also match "c:" in the file name, even when
277 // checking for a colon next: "%f:".
278 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200279 STRCPY(regpat, "\\%(\\a:\\)\\=");
280 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281 }
282#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200283 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200284 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200285 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200286 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200287 // A file name may contain spaces, but this isn't
288 // in "\f". For "%f:%l:%m" there may be a ":" in
289 // the file name. Use ".\{-1,}x" instead (x is
290 // the next character), the requirement that :999:
291 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200292 STRCPY(regpat, ".\\{-1,}");
293 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200294 }
295 else
296 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200297 // File name followed by '\\' or '%': include as
298 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200299 STRCPY(regpat, "\\f\\+");
300 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200301 }
302 }
303 else
304 {
305 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200306 while ((*regpat = *srcptr++) != NUL)
307 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200308 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200309 *regpat++ = '\\';
310 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200311
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200312 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200313}
314
315/*
316 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200317 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200318 */
319 static char_u *
320scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200321 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200322 char_u *efm,
323 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100324 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200325{
326 char_u *efmp = *pefmp;
327
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200328 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200329 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200330 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200331 {
332 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200333 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200334 if (efmp < efm + len)
335 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200336 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200337 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200338 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200339 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200340 if (efmp == efm + len)
341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100342 emsg(_("E374: Missing ] in format string"));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200343 return NULL;
344 }
345 }
346 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200347 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200348 *regpat++ = *++efmp;
349 *regpat++ = '\\';
350 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200351 }
352 else
353 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200354 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100355 semsg(_("E375: Unsupported %%%c in format string"), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200356 return NULL;
357 }
358
359 *pefmp = efmp;
360
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200361 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200362}
363
364/*
365 * Analyze/parse an errorformat prefix.
366 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200367 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100368efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200369{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200370 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200371 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200372 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200373 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200374 else
375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100376 semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200377 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200378 }
379
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200380 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200381}
382
383/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200384 * Converts a 'errorformat' string part in 'efm' to a regular expression
385 * pattern. The resulting regex pattern is returned in "regpat". Additional
386 * information about the 'erroformat' pattern is returned in "fmt_ptr".
387 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200388 */
389 static int
390efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200391 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200392 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200393 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100394 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200395{
396 char_u *ptr;
397 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200398 int round;
399 int idx = 0;
400
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200401 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200402 ptr = regpat;
403 *ptr++ = '^';
404 round = 0;
405 for (efmp = efm; efmp < efm + len; ++efmp)
406 {
407 if (*efmp == '%')
408 {
409 ++efmp;
410 for (idx = 0; idx < FMT_PATTERNS; ++idx)
411 if (fmt_pat[idx].convchar == *efmp)
412 break;
413 if (idx < FMT_PATTERNS)
414 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100415 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200416 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200417 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200418 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200419 }
420 else if (*efmp == '*')
421 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200422 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100423 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200424 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200425 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200426 }
427 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200428 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200429 else if (*efmp == '#')
430 *ptr++ = '*';
431 else if (*efmp == '>')
432 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200433 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200434 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200435 // prefix is allowed only at the beginning of the errorformat
436 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100437 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200438 if (efmp == NULL)
439 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200440 }
441 else
442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100443 semsg(_("E377: Invalid %%%c in format string"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200444 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200445 }
446 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200447 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200448 {
449 if (*efmp == '\\' && efmp + 1 < efm + len)
450 ++efmp;
451 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200452 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453 if (*efmp)
454 *ptr++ = *efmp;
455 }
456 }
457 *ptr++ = '$';
458 *ptr = NUL;
459
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200460 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200461}
462
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200463/*
464 * Free the 'errorformat' information list
465 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200466 static void
467free_efm_list(efm_T **efm_first)
468{
469 efm_T *efm_ptr;
470
471 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
472 {
473 *efm_first = efm_ptr->next;
474 vim_regfree(efm_ptr->prog);
475 vim_free(efm_ptr);
476 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100477 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200478}
479
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200480/*
481 * Compute the size of the buffer used to convert a 'errorformat' pattern into
482 * a regular expression pattern.
483 */
484 static int
485efm_regpat_bufsz(char_u *efm)
486{
487 int sz;
488 int i;
489
490 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
491 for (i = FMT_PATTERNS; i > 0; )
492 sz += (int)STRLEN(fmt_pat[--i].pattern);
493#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200494 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200495#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200496 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200497#endif
498
499 return sz;
500}
501
502/*
503 * Return the length of a 'errorformat' option part (separated by ",").
504 */
505 static int
506efm_option_part_len(char_u *efm)
507{
508 int len;
509
510 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
511 if (efm[len] == '\\' && efm[len + 1] != NUL)
512 ++len;
513
514 return len;
515}
516
517/*
518 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
519 * are parsed and converted to regular expressions. Returns information about
520 * the parsed 'errorformat' option.
521 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200522 static efm_T *
523parse_efm_option(char_u *efm)
524{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200525 efm_T *fmt_ptr = NULL;
526 efm_T *fmt_first = NULL;
527 efm_T *fmt_last = NULL;
528 char_u *fmtstr = NULL;
529 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200530 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200531
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200532 // Each part of the format string is copied and modified from errorformat
533 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200534
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200535 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200536 sz = efm_regpat_bufsz(efm);
537 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200538 goto parse_efm_error;
539
540 while (efm[0] != NUL)
541 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200542 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200543 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
544 if (fmt_ptr == NULL)
545 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200546 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200547 fmt_first = fmt_ptr;
548 else
549 fmt_last->next = fmt_ptr;
550 fmt_last = fmt_ptr;
551
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200552 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200553 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200554
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100555 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200556 goto parse_efm_error;
557 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
558 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200559 // Advance to next part
560 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200561 }
562
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200563 if (fmt_first == NULL) // nothing found
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100564 emsg(_("E378: 'errorformat' contains no pattern"));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200565
566 goto parse_efm_end;
567
568parse_efm_error:
569 free_efm_list(&fmt_first);
570
571parse_efm_end:
572 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200573
574 return fmt_first;
575}
576
Bram Moolenaare0d37972016-07-15 22:36:01 +0200577enum {
578 QF_FAIL = 0,
579 QF_OK = 1,
580 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200581 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200582 QF_IGNORE_LINE = 4,
583 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200584};
585
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200586/*
587 * State information used to parse lines and add entries to a quickfix/location
588 * list.
589 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200590typedef struct {
591 char_u *linebuf;
592 int linelen;
593 char_u *growbuf;
594 int growbufsiz;
595 FILE *fd;
596 typval_T *tv;
597 char_u *p_str;
598 listitem_T *p_li;
599 buf_T *buf;
600 linenr_T buflnum;
601 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100602 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200603} qfstate_T;
604
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200605/*
606 * Allocate more memory for the line buffer used for parsing lines.
607 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200608 static char_u *
609qf_grow_linebuf(qfstate_T *state, int newsz)
610{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200611 char_u *p;
612
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200613 // If the line exceeds LINE_MAXLEN exclude the last
614 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200615 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
616 if (state->growbuf == NULL)
617 {
618 state->growbuf = alloc(state->linelen + 1);
619 if (state->growbuf == NULL)
620 return NULL;
621 state->growbufsiz = state->linelen;
622 }
623 else if (state->linelen > state->growbufsiz)
624 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200625 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200626 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200627 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200628 state->growbufsiz = state->linelen;
629 }
630 return state->growbuf;
631}
632
633/*
634 * Get the next string (separated by newline) from state->p_str.
635 */
636 static int
637qf_get_next_str_line(qfstate_T *state)
638{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200639 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200640 char_u *p_str = state->p_str;
641 char_u *p;
642 int len;
643
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200644 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200645 return QF_END_OF_INPUT;
646
647 p = vim_strchr(p_str, '\n');
648 if (p != NULL)
649 len = (int)(p - p_str) + 1;
650 else
651 len = (int)STRLEN(p_str);
652
653 if (len > IOSIZE - 2)
654 {
655 state->linebuf = qf_grow_linebuf(state, len);
656 if (state->linebuf == NULL)
657 return QF_NOMEM;
658 }
659 else
660 {
661 state->linebuf = IObuff;
662 state->linelen = len;
663 }
664 vim_strncpy(state->linebuf, p_str, state->linelen);
665
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200666 // Increment using len in order to discard the rest of the
667 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200668 p_str += len;
669 state->p_str = p_str;
670
671 return QF_OK;
672}
673
674/*
675 * Get the next string from state->p_Li.
676 */
677 static int
678qf_get_next_list_line(qfstate_T *state)
679{
680 listitem_T *p_li = state->p_li;
681 int len;
682
683 while (p_li != NULL
684 && (p_li->li_tv.v_type != VAR_STRING
685 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200686 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200687
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200688 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200689 {
690 state->p_li = NULL;
691 return QF_END_OF_INPUT;
692 }
693
694 len = (int)STRLEN(p_li->li_tv.vval.v_string);
695 if (len > IOSIZE - 2)
696 {
697 state->linebuf = qf_grow_linebuf(state, len);
698 if (state->linebuf == NULL)
699 return QF_NOMEM;
700 }
701 else
702 {
703 state->linebuf = IObuff;
704 state->linelen = len;
705 }
706
707 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
708
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200709 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200710 return QF_OK;
711}
712
713/*
714 * Get the next string from state->buf.
715 */
716 static int
717qf_get_next_buf_line(qfstate_T *state)
718{
719 char_u *p_buf = NULL;
720 int len;
721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200722 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200723 if (state->buflnum > state->lnumlast)
724 return QF_END_OF_INPUT;
725
726 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
727 state->buflnum += 1;
728
729 len = (int)STRLEN(p_buf);
730 if (len > IOSIZE - 2)
731 {
732 state->linebuf = qf_grow_linebuf(state, len);
733 if (state->linebuf == NULL)
734 return QF_NOMEM;
735 }
736 else
737 {
738 state->linebuf = IObuff;
739 state->linelen = len;
740 }
741 vim_strncpy(state->linebuf, p_buf, state->linelen);
742
743 return QF_OK;
744}
745
746/*
747 * Get the next string from file state->fd.
748 */
749 static int
750qf_get_next_file_line(qfstate_T *state)
751{
752 int discard;
753 int growbuflen;
754
755 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
756 return QF_END_OF_INPUT;
757
758 discard = FALSE;
759 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200760 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200761 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200762 // The current line exceeds IObuff, continue reading using
763 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200764 if (state->growbuf == NULL)
765 {
766 state->growbufsiz = 2 * (IOSIZE - 1);
767 state->growbuf = alloc(state->growbufsiz);
768 if (state->growbuf == NULL)
769 return QF_NOMEM;
770 }
771
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200772 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200773 memcpy(state->growbuf, IObuff, IOSIZE - 1);
774 growbuflen = state->linelen;
775
776 for (;;)
777 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200778 char_u *p;
779
Bram Moolenaare0d37972016-07-15 22:36:01 +0200780 if (fgets((char *)state->growbuf + growbuflen,
781 state->growbufsiz - growbuflen, state->fd) == NULL)
782 break;
783 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
784 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200785 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200786 break;
787 if (state->growbufsiz == LINE_MAXLEN)
788 {
789 discard = TRUE;
790 break;
791 }
792
793 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
794 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200795 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200796 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200797 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200798 }
799
800 while (discard)
801 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200802 // The current line is longer than LINE_MAXLEN, continue
803 // reading but discard everything until EOL or EOF is
804 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200805 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
806 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200807 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200808 break;
809 }
810
811 state->linebuf = state->growbuf;
812 state->linelen = growbuflen;
813 }
814 else
815 state->linebuf = IObuff;
816
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200817 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200818 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
819 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100820 char_u *line;
821
822 line = string_convert(&state->vc, state->linebuf, &state->linelen);
823 if (line != NULL)
824 {
825 if (state->linelen < IOSIZE)
826 {
827 STRCPY(state->linebuf, line);
828 vim_free(line);
829 }
830 else
831 {
832 vim_free(state->growbuf);
833 state->linebuf = state->growbuf = line;
834 state->growbufsiz = state->linelen < LINE_MAXLEN
835 ? state->linelen : LINE_MAXLEN;
836 }
837 }
838 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100839
Bram Moolenaare0d37972016-07-15 22:36:01 +0200840 return QF_OK;
841}
842
843/*
844 * Get the next string from a file/buffer/list/string.
845 */
846 static int
847qf_get_nextline(qfstate_T *state)
848{
849 int status = QF_FAIL;
850
851 if (state->fd == NULL)
852 {
853 if (state->tv != NULL)
854 {
855 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200856 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200857 status = qf_get_next_str_line(state);
858 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200859 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200860 status = qf_get_next_list_line(state);
861 }
862 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200863 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200864 status = qf_get_next_buf_line(state);
865 }
866 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200867 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200868 status = qf_get_next_file_line(state);
869
870 if (status != QF_OK)
871 return status;
872
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200873 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200874 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200875 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200876 state->linebuf[state->linelen - 1] = NUL;
877#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200878 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
879 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200880#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200881 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200882
Bram Moolenaare0d37972016-07-15 22:36:01 +0200883 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200884
885 return QF_OK;
886}
887
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200888typedef struct {
889 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200890 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200891 char_u *errmsg;
892 int errmsglen;
893 long lnum;
894 int col;
895 char_u use_viscol;
896 char_u *pattern;
897 int enr;
898 int type;
899 int valid;
900} qffields_T;
901
902/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200903 * Parse the match for filename ('%f') pattern in regmatch.
904 * Return the matched value in "fields->namebuf".
905 */
906 static int
907qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
908{
909 int c;
910
911 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
912 return QF_FAIL;
913
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200914 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200915 c = *rmp->endp[midx];
916 *rmp->endp[midx] = NUL;
917 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
918 *rmp->endp[midx] = c;
919
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200920 // For separate filename patterns (%O, %P and %Q), the specified file
921 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200922 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
923 && mch_getperm(fields->namebuf) == -1)
924 return QF_FAIL;
925
926 return QF_OK;
927}
928
929/*
930 * Parse the match for error number ('%n') pattern in regmatch.
931 * Return the matched value in "fields->enr".
932 */
933 static int
934qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
935{
936 if (rmp->startp[midx] == NULL)
937 return QF_FAIL;
938 fields->enr = (int)atol((char *)rmp->startp[midx]);
939 return QF_OK;
940}
941
942/*
943 * Parse the match for line number (%l') pattern in regmatch.
944 * Return the matched value in "fields->lnum".
945 */
946 static int
947qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
948{
949 if (rmp->startp[midx] == NULL)
950 return QF_FAIL;
951 fields->lnum = atol((char *)rmp->startp[midx]);
952 return QF_OK;
953}
954
955/*
956 * Parse the match for column number ('%c') pattern in regmatch.
957 * Return the matched value in "fields->col".
958 */
959 static int
960qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
961{
962 if (rmp->startp[midx] == NULL)
963 return QF_FAIL;
964 fields->col = (int)atol((char *)rmp->startp[midx]);
965 return QF_OK;
966}
967
968/*
969 * Parse the match for error type ('%t') pattern in regmatch.
970 * Return the matched value in "fields->type".
971 */
972 static int
973qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
974{
975 if (rmp->startp[midx] == NULL)
976 return QF_FAIL;
977 fields->type = *rmp->startp[midx];
978 return QF_OK;
979}
980
981/*
982 * Parse the match for '%+' format pattern. The whole matching line is included
983 * in the error string. Return the matched line in "fields->errmsg".
984 */
985 static int
986qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
987{
988 char_u *p;
989
990 if (linelen >= fields->errmsglen)
991 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200992 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200993 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
994 return QF_NOMEM;
995 fields->errmsg = p;
996 fields->errmsglen = linelen + 1;
997 }
998 vim_strncpy(fields->errmsg, linebuf, linelen);
999 return QF_OK;
1000}
1001
1002/*
1003 * Parse the match for error message ('%m') pattern in regmatch.
1004 * Return the matched value in "fields->errmsg".
1005 */
1006 static int
1007qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1008{
1009 char_u *p;
1010 int len;
1011
1012 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1013 return QF_FAIL;
1014 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1015 if (len >= fields->errmsglen)
1016 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001017 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001018 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1019 return QF_NOMEM;
1020 fields->errmsg = p;
1021 fields->errmsglen = len + 1;
1022 }
1023 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1024 return QF_OK;
1025}
1026
1027/*
1028 * Parse the match for rest of a single-line file message ('%r') pattern.
1029 * Return the matched value in "tail".
1030 */
1031 static int
1032qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1033{
1034 if (rmp->startp[midx] == NULL)
1035 return QF_FAIL;
1036 *tail = rmp->startp[midx];
1037 return QF_OK;
1038}
1039
1040/*
1041 * Parse the match for the pointer line ('%p') pattern in regmatch.
1042 * Return the matched value in "fields->col".
1043 */
1044 static int
1045qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1046{
1047 char_u *match_ptr;
1048
1049 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1050 return QF_FAIL;
1051 fields->col = 0;
1052 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1053 ++match_ptr)
1054 {
1055 ++fields->col;
1056 if (*match_ptr == TAB)
1057 {
1058 fields->col += 7;
1059 fields->col -= fields->col % 8;
1060 }
1061 }
1062 ++fields->col;
1063 fields->use_viscol = TRUE;
1064 return QF_OK;
1065}
1066
1067/*
1068 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1069 * Return the matched value in "fields->col".
1070 */
1071 static int
1072qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1073{
1074 if (rmp->startp[midx] == NULL)
1075 return QF_FAIL;
1076 fields->col = (int)atol((char *)rmp->startp[midx]);
1077 fields->use_viscol = TRUE;
1078 return QF_OK;
1079}
1080
1081/*
1082 * Parse the match for the search text ('%s') pattern in regmatch.
1083 * Return the matched value in "fields->pattern".
1084 */
1085 static int
1086qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1087{
1088 int len;
1089
1090 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1091 return QF_FAIL;
1092 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1093 if (len > CMDBUFFSIZE - 5)
1094 len = CMDBUFFSIZE - 5;
1095 STRCPY(fields->pattern, "^\\V");
1096 STRNCAT(fields->pattern, rmp->startp[midx], len);
1097 fields->pattern[len + 3] = '\\';
1098 fields->pattern[len + 4] = '$';
1099 fields->pattern[len + 5] = NUL;
1100 return QF_OK;
1101}
1102
1103/*
1104 * Parse the match for the module ('%o') pattern in regmatch.
1105 * Return the matched value in "fields->module".
1106 */
1107 static int
1108qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1109{
1110 int len;
1111
1112 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1113 return QF_FAIL;
1114 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1115 if (len > CMDBUFFSIZE)
1116 len = CMDBUFFSIZE;
1117 STRNCAT(fields->module, rmp->startp[midx], len);
1118 return QF_OK;
1119}
1120
1121/*
1122 * 'errorformat' format pattern parser functions.
1123 * The '%f' and '%r' formats are parsed differently from other formats.
1124 * See qf_parse_match() for details.
1125 */
1126static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1127{
1128 NULL,
1129 qf_parse_fmt_n,
1130 qf_parse_fmt_l,
1131 qf_parse_fmt_c,
1132 qf_parse_fmt_t,
1133 qf_parse_fmt_m,
1134 NULL,
1135 qf_parse_fmt_p,
1136 qf_parse_fmt_v,
1137 qf_parse_fmt_s,
1138 qf_parse_fmt_o
1139};
1140
1141/*
1142 * Parse the error format pattern matches in "regmatch" and set the values in
1143 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1144 * match. Returns QF_OK if all the matches are successfully parsed. On
1145 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001146 */
1147 static int
1148qf_parse_match(
1149 char_u *linebuf,
1150 int linelen,
1151 efm_T *fmt_ptr,
1152 regmatch_T *regmatch,
1153 qffields_T *fields,
1154 int qf_multiline,
1155 int qf_multiscan,
1156 char_u **tail)
1157{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001158 int idx = fmt_ptr->prefix;
1159 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001160 int midx;
1161 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001162
1163 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1164 return QF_FAIL;
1165 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1166 fields->type = idx;
1167 else
1168 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001169
1170 // Extract error message data from matched line.
1171 // We check for an actual submatch, because "\[" and "\]" in
1172 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001173 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001174 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001175 status = QF_OK;
1176 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001177 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001178 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1179 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001180 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001181 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001182 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001183 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001184 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001185 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001186 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001187 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001188 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001189 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001190
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001191 if (status != QF_OK)
1192 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001193 }
1194
1195 return QF_OK;
1196}
1197
1198/*
1199 * Parse an error line in 'linebuf' using a single error format string in
1200 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1201 * Returns QF_OK if the efm format matches completely and the fields are
1202 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1203 */
1204 static int
1205qf_parse_get_fields(
1206 char_u *linebuf,
1207 int linelen,
1208 efm_T *fmt_ptr,
1209 qffields_T *fields,
1210 int qf_multiline,
1211 int qf_multiscan,
1212 char_u **tail)
1213{
1214 regmatch_T regmatch;
1215 int status = QF_FAIL;
1216 int r;
1217
1218 if (qf_multiscan &&
1219 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1220 return QF_FAIL;
1221
1222 fields->namebuf[0] = NUL;
1223 fields->module[0] = NUL;
1224 fields->pattern[0] = NUL;
1225 if (!qf_multiscan)
1226 fields->errmsg[0] = NUL;
1227 fields->lnum = 0;
1228 fields->col = 0;
1229 fields->use_viscol = FALSE;
1230 fields->enr = -1;
1231 fields->type = 0;
1232 *tail = NULL;
1233
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001234 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001235 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001236 regmatch.regprog = fmt_ptr->prog;
1237 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1238 fmt_ptr->prog = regmatch.regprog;
1239 if (r)
1240 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1241 fields, qf_multiline, qf_multiscan, tail);
1242
1243 return status;
1244}
1245
1246/*
1247 * Parse directory error format prefixes (%D and %X).
1248 * Push and pop directories from the directory stack when scanning directory
1249 * names.
1250 */
1251 static int
1252qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1253{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001254 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001255 {
1256 if (*fields->namebuf == NUL)
1257 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001258 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001259 return QF_FAIL;
1260 }
1261 qfl->qf_directory =
1262 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1263 if (qfl->qf_directory == NULL)
1264 return QF_FAIL;
1265 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001266 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001267 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1268
1269 return QF_OK;
1270}
1271
1272/*
1273 * Parse global file name error format prefixes (%O, %P and %Q).
1274 */
1275 static int
1276qf_parse_file_pfx(
1277 int idx,
1278 qffields_T *fields,
1279 qf_list_T *qfl,
1280 char_u *tail)
1281{
1282 fields->valid = FALSE;
1283 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1284 {
1285 if (*fields->namebuf && idx == 'P')
1286 qfl->qf_currfile =
1287 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1288 else if (idx == 'Q')
1289 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1290 *fields->namebuf = NUL;
1291 if (tail && *tail)
1292 {
1293 STRMOVE(IObuff, skipwhite(tail));
1294 qfl->qf_multiscan = TRUE;
1295 return QF_MULTISCAN;
1296 }
1297 }
1298
1299 return QF_OK;
1300}
1301
1302/*
1303 * Parse a non-error line (a line which doesn't match any of the error
1304 * format in 'efm').
1305 */
1306 static int
1307qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1308{
1309 char_u *p;
1310
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001311 fields->namebuf[0] = NUL; // no match found, remove file name
1312 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001313 fields->valid = FALSE;
1314 if (linelen >= fields->errmsglen)
1315 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001316 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001317 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1318 return QF_NOMEM;
1319 fields->errmsg = p;
1320 fields->errmsglen = linelen + 1;
1321 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001322 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001323 vim_strncpy(fields->errmsg, linebuf, linelen);
1324
1325 return QF_OK;
1326}
1327
1328/*
1329 * Parse multi-line error format prefixes (%C and %Z)
1330 */
1331 static int
1332qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001333 int idx,
1334 qf_list_T *qfl,
1335 qffields_T *fields)
1336{
1337 char_u *ptr;
1338 int len;
1339
1340 if (!qfl->qf_multiignore)
1341 {
1342 qfline_T *qfprev = qfl->qf_last;
1343
1344 if (qfprev == NULL)
1345 return QF_FAIL;
1346 if (*fields->errmsg && !qfl->qf_multiignore)
1347 {
1348 len = (int)STRLEN(qfprev->qf_text);
1349 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1350 == NULL)
1351 return QF_FAIL;
1352 STRCPY(ptr, qfprev->qf_text);
1353 vim_free(qfprev->qf_text);
1354 qfprev->qf_text = ptr;
1355 *(ptr += len) = '\n';
1356 STRCPY(++ptr, fields->errmsg);
1357 }
1358 if (qfprev->qf_nr == -1)
1359 qfprev->qf_nr = fields->enr;
1360 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001361 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001362 qfprev->qf_type = fields->type;
1363
1364 if (!qfprev->qf_lnum)
1365 qfprev->qf_lnum = fields->lnum;
1366 if (!qfprev->qf_col)
1367 qfprev->qf_col = fields->col;
1368 qfprev->qf_viscol = fields->use_viscol;
1369 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001370 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001371 qfl->qf_directory,
1372 *fields->namebuf || qfl->qf_directory != NULL
1373 ? fields->namebuf
1374 : qfl->qf_currfile != NULL && fields->valid
1375 ? qfl->qf_currfile : 0);
1376 }
1377 if (idx == 'Z')
1378 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1379 line_breakcheck();
1380
1381 return QF_IGNORE_LINE;
1382}
1383
1384/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001385 * Parse a line and get the quickfix fields.
1386 * Return the QF_ status.
1387 */
1388 static int
1389qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001390 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001391 char_u *linebuf,
1392 int linelen,
1393 efm_T *fmt_first,
1394 qffields_T *fields)
1395{
1396 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001397 int idx = 0;
1398 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001399 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001400
Bram Moolenaare333e792018-04-08 13:27:39 +02001401restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001402 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001403 if (fmt_start == NULL)
1404 fmt_ptr = fmt_first;
1405 else
1406 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001407 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001408 fmt_ptr = fmt_start;
1409 fmt_start = NULL;
1410 }
1411
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001412 // Try to match each part of 'errorformat' until we find a complete
1413 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001414 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001415 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1416 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001418 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1419 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1420 if (status == QF_NOMEM)
1421 return status;
1422 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001423 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001424 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001425 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001426
1427 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1428 {
1429 if (fmt_ptr != NULL)
1430 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001431 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001432 status = qf_parse_dir_pfx(idx, fields, qfl);
1433 if (status != QF_OK)
1434 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001435 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001436
1437 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1438 if (status != QF_OK)
1439 return status;
1440
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001441 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001442 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001443 }
1444 else if (fmt_ptr != NULL)
1445 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001446 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001447 if (fmt_ptr->conthere)
1448 fmt_start = fmt_ptr;
1449
1450 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1451 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001452 qfl->qf_multiline = TRUE; // start of a multi-line message
1453 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001454 }
1455 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001456 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001457 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001458 if (status != QF_OK)
1459 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 }
1461 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001462 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001463 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1464 if (status == QF_MULTISCAN)
1465 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001467 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001468 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001469 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001470 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001471 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001472 return QF_IGNORE_LINE;
1473 }
1474 }
1475
1476 return QF_OK;
1477}
1478
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001479/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001480 * Returns TRUE if the specified quickfix/location stack is empty
1481 */
1482 static int
1483qf_stack_empty(qf_info_T *qi)
1484{
1485 return qi == NULL || qi->qf_listcount <= 0;
1486}
1487
1488/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001489 * Returns TRUE if the specified quickfix/location list is empty.
1490 */
1491 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001492qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001493{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001494 return qfl == NULL || qfl->qf_count <= 0;
1495}
1496
1497/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001498 * Returns TRUE if the specified quickfix/location list is not empty and
1499 * has valid entries.
1500 */
1501 static int
1502qf_list_has_valid_entries(qf_list_T *qfl)
1503{
1504 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1505}
1506
1507/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001508 * Return a pointer to a list in the specified quickfix stack
1509 */
1510 static qf_list_T *
1511qf_get_list(qf_info_T *qi, int idx)
1512{
1513 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001514}
1515
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001516/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001517 * Allocate the fields used for parsing lines and populating a quickfix list.
1518 */
1519 static int
1520qf_alloc_fields(qffields_T *pfields)
1521{
1522 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1523 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1524 pfields->errmsglen = CMDBUFFSIZE + 1;
1525 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1526 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1527 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1528 || pfields->pattern == NULL || pfields->module == NULL)
1529 return FAIL;
1530
1531 return OK;
1532}
1533
1534/*
1535 * Free the fields used for parsing lines and populating a quickfix list.
1536 */
1537 static void
1538qf_free_fields(qffields_T *pfields)
1539{
1540 vim_free(pfields->namebuf);
1541 vim_free(pfields->module);
1542 vim_free(pfields->errmsg);
1543 vim_free(pfields->pattern);
1544}
1545
1546/*
1547 * Setup the state information used for parsing lines and populating a
1548 * quickfix list.
1549 */
1550 static int
1551qf_setup_state(
1552 qfstate_T *pstate,
1553 char_u *enc,
1554 char_u *efile,
1555 typval_T *tv,
1556 buf_T *buf,
1557 linenr_T lnumfirst,
1558 linenr_T lnumlast)
1559{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001560 pstate->vc.vc_type = CONV_NONE;
1561 if (enc != NULL && *enc != NUL)
1562 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001563
1564 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001566 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001567 return FAIL;
1568 }
1569
1570 if (tv != NULL)
1571 {
1572 if (tv->v_type == VAR_STRING)
1573 pstate->p_str = tv->vval.v_string;
1574 else if (tv->v_type == VAR_LIST)
1575 pstate->p_li = tv->vval.v_list->lv_first;
1576 pstate->tv = tv;
1577 }
1578 pstate->buf = buf;
1579 pstate->buflnum = lnumfirst;
1580 pstate->lnumlast = lnumlast;
1581
1582 return OK;
1583}
1584
1585/*
1586 * Cleanup the state information used for parsing lines and populating a
1587 * quickfix list.
1588 */
1589 static void
1590qf_cleanup_state(qfstate_T *pstate)
1591{
1592 if (pstate->fd != NULL)
1593 fclose(pstate->fd);
1594
1595 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001596 if (pstate->vc.vc_type != CONV_NONE)
1597 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001598}
1599
1600/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001601 * Process the next line from a file/buffer/list/string and add it
1602 * to the quickfix list 'qfl'.
1603 */
1604 static int
1605qf_init_process_nextline(
1606 qf_list_T *qfl,
1607 efm_T *fmt_first,
1608 qfstate_T *state,
1609 qffields_T *fields)
1610{
1611 int status;
1612
1613 // Get the next line from a file/buffer/list/string
1614 status = qf_get_nextline(state);
1615 if (status != QF_OK)
1616 return status;
1617
1618 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1619 fmt_first, fields);
1620 if (status != QF_OK)
1621 return status;
1622
1623 return qf_add_entry(qfl,
1624 qfl->qf_directory,
1625 (*fields->namebuf || qfl->qf_directory != NULL)
1626 ? fields->namebuf
1627 : ((qfl->qf_currfile != NULL && fields->valid)
1628 ? qfl->qf_currfile : (char_u *)NULL),
1629 fields->module,
1630 0,
1631 fields->errmsg,
1632 fields->lnum,
1633 fields->col,
1634 fields->use_viscol,
1635 fields->pattern,
1636 fields->enr,
1637 fields->type,
1638 fields->valid);
1639}
1640
1641/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001642 * Read the errorfile "efile" into memory, line by line, building the error
1643 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001644 * Alternative: when "efile" is NULL read errors from buffer "buf".
1645 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001646 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001647 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1648 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001649 * Return -1 for error, number of errors for success.
1650 */
1651 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001652qf_init_ext(
1653 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001654 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001655 char_u *efile,
1656 buf_T *buf,
1657 typval_T *tv,
1658 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001659 int newlist, // TRUE: start a new error list
1660 linenr_T lnumfirst, // first line number to use
1661 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001662 char_u *qf_title,
1663 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001664{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001665 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001666 qfstate_T state;
1667 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001668 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001669 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001670 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001672 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001673 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001674 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001676 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001677 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001678
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001679 vim_memset(&state, 0, sizeof(state));
1680 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001681 if ((qf_alloc_fields(&fields) == FAIL) ||
1682 (qf_setup_state(&state, enc, efile, tv, buf,
1683 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 goto qf_init_end;
1685
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001686 if (newlist || qf_idx == qi->qf_listcount)
1687 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001688 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001689 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001690 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001691 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001692 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001693 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001694 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001695 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001696 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001697 qfl = qf_get_list(qi, qf_idx);
1698 if (!qf_list_empty(qfl))
1699 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001702 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001703 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001704 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 else
1706 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001708 // If the errorformat didn't change between calls, then reuse the
1709 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001710 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1711 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001712 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001713 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001714 free_efm_list(&fmt_first);
1715
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001716 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001717 fmt_first = parse_efm_option(efm);
1718 if (fmt_first != NULL)
1719 last_efm = vim_strsave(efm);
1720 }
1721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001722 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001725 // got_int is reset here, because it was probably set when killing the
1726 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 got_int = FALSE;
1728
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001729 // Read the lines in the error file one by one.
1730 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001731 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001733 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001734 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001735 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001736 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001737 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001738 if (status == QF_FAIL)
1739 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 line_breakcheck();
1742 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001743 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001745 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001747 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001748 qfl->qf_ptr = qfl->qf_start;
1749 qfl->qf_index = 1;
1750 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752 else
1753 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001754 qfl->qf_nonevalid = FALSE;
1755 if (qfl->qf_ptr == NULL)
1756 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001758 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001759 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001760 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001762 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001764 if (!adding)
1765 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001766 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001767 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001768 qi->qf_listcount--;
1769 if (qi->qf_curlist > 0)
1770 --qi->qf_curlist;
1771 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001772qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001773 if (qf_idx == qi->qf_curlist)
1774 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001775 qf_cleanup_state(&state);
1776 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 return retval;
1779}
1780
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001781/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001782 * Read the errorfile "efile" into memory, line by line, building the error
1783 * list. Set the error list's title to qf_title.
1784 * Return -1 for error, number of errors for success.
1785 */
1786 int
1787qf_init(win_T *wp,
1788 char_u *efile,
1789 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001790 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001791 char_u *qf_title,
1792 char_u *enc)
1793{
1794 qf_info_T *qi = &ql_info;
1795
1796 if (wp != NULL)
1797 {
1798 qi = ll_get_or_alloc_list(wp);
1799 if (qi == NULL)
1800 return FAIL;
1801 }
1802
1803 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1804 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1805}
1806
1807/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001808 * Set the title of the specified quickfix list. Frees the previous title.
1809 * Prepends ':' to the title.
1810 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001811 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001812qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001813{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001814 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001815
Bram Moolenaarfb604092014-07-23 15:55:00 +02001816 if (title != NULL)
1817 {
1818 char_u *p = alloc((int)STRLEN(title) + 2);
1819
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001820 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001821 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001822 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001823 }
1824}
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001827 * The title of a quickfix/location list is set, by default, to the command
1828 * that created the quickfix list with the ":" prefix.
1829 * Create a quickfix list title string by prepending ":" to a user command.
1830 * Returns a pointer to a static buffer with the title.
1831 */
1832 static char_u *
1833qf_cmdtitle(char_u *cmd)
1834{
1835 static char_u qftitle_str[IOSIZE];
1836
1837 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1838 return qftitle_str;
1839}
1840
1841/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001842 * Return a pointer to the current list in the specified quickfix stack
1843 */
1844 static qf_list_T *
1845qf_get_curlist(qf_info_T *qi)
1846{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001847 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001848}
1849
1850/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001851 * Prepare for adding a new quickfix list. If the current list is in the
1852 * middle of the stack, then all the following lists are freed and then
1853 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 */
1855 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001856qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
1858 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001859 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001861 // If the current entry is not the last entry, delete entries beyond
1862 // the current entry. This makes it possible to browse in a tree-like
1863 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001864 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001865 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001867 // When the stack is full, remove to oldest entry
1868 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001869 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001871 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001873 qi->qf_lists[i - 1] = qi->qf_lists[i];
1874 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001877 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001878 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001879 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1880 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001881 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001882 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883}
1884
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001885/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001886 * Queue location list stack delete request.
1887 */
1888 static void
1889locstack_queue_delreq(qf_info_T *qi)
1890{
1891 qf_delq_T *q;
1892
1893 q = (qf_delq_T *)alloc((unsigned)sizeof(qf_delq_T));
1894 if (q != NULL)
1895 {
1896 q->qi = qi;
1897 q->next = qf_delq_head;
1898 qf_delq_head = q;
1899 }
1900}
1901
1902/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001903 * Return the global quickfix stack window buffer number.
1904 */
1905 int
1906qf_stack_get_bufnr(void)
1907{
1908 return ql_info.qf_bufnr;
1909}
1910
1911/*
1912 * Wipe the quickfix window buffer (if present) for the specified
1913 * quickfix/location list.
1914 */
1915 static void
1916wipe_qf_buffer(qf_info_T *qi)
1917{
1918 buf_T *qfbuf;
1919
1920 if (qi->qf_bufnr != INVALID_QFBUFNR)
1921 {
1922 qfbuf = buflist_findnr(qi->qf_bufnr);
1923 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1924 {
1925 // If the quickfix buffer is not loaded in any window, then
1926 // wipe the buffer.
1927 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE);
1928 qi->qf_bufnr = INVALID_QFBUFNR;
1929 }
1930 }
1931}
1932
1933/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001934 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001935 */
1936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001937ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001938{
1939 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001940 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001941
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001942 qi = *pqi;
1943 if (qi == NULL)
1944 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001945 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001946
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001947 // If the location list is still in use, then queue the delete request
1948 // to be processed later.
1949 if (quickfix_busy > 0)
1950 {
1951 locstack_queue_delreq(qi);
1952 return;
1953 }
1954
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001955 qi->qf_refcount--;
1956 if (qi->qf_refcount < 1)
1957 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001958 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001959 // If the quickfix window buffer is loaded, then wipe it
1960 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001961
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001962 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001963 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001964 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001965 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001966}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001967
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001968/*
1969 * Free all the quickfix/location lists in the stack.
1970 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001971 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001972qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001973{
1974 int i;
1975 qf_info_T *qi = &ql_info;
1976
1977 if (wp != NULL)
1978 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001979 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980 ll_free_all(&wp->w_llist);
1981 ll_free_all(&wp->w_llist_ref);
1982 }
1983 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001984 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001985 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001986 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001987}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001988
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001990 * Delay freeing of location list stacks when the quickfix code is running.
1991 * Used to avoid problems with autocmds freeing location list stacks when the
1992 * quickfix code is still referencing the stack.
1993 * Must always call decr_quickfix_busy() exactly once after this.
1994 */
1995 static void
1996incr_quickfix_busy(void)
1997{
1998 quickfix_busy++;
1999}
2000
2001/*
2002 * Safe to free location list stacks. Process any delayed delete requests.
2003 */
2004 static void
2005decr_quickfix_busy(void)
2006{
2007 if (--quickfix_busy == 0)
2008 {
2009 // No longer referencing the location lists. Process all the pending
2010 // delete requests.
2011 while (qf_delq_head != NULL)
2012 {
2013 qf_delq_T *q = qf_delq_head;
2014
2015 qf_delq_head = q->next;
2016 ll_free_all(&q->qi);
2017 vim_free(q);
2018 }
2019 }
2020#ifdef ABORT_ON_INTERNAL_ERROR
2021 if (quickfix_busy < 0)
2022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002023 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002024 abort();
2025 }
2026#endif
2027}
2028
2029#if defined(EXITFREE) || defined(PROTO)
2030 void
2031check_quickfix_busy(void)
2032{
2033 if (quickfix_busy != 0)
2034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002035 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002036# ifdef ABORT_ON_INTERNAL_ERROR
2037 abort();
2038# endif
2039 }
2040}
2041#endif
2042
2043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 * Add an entry to the end of the list of errors.
Bram Moolenaar95946f12019-03-31 15:31:59 +02002045 * Returns QF_OK or QF_FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 */
2047 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002048qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002049 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002050 char_u *dir, // optional directory name
2051 char_u *fname, // file name or NULL
2052 char_u *module, // module name or NULL
2053 int bufnum, // buffer number or zero
2054 char_u *mesg, // message
2055 long lnum, // line number
2056 int col, // column
2057 int vis_col, // using visual column
2058 char_u *pattern, // search pattern
2059 int nr, // error number
2060 int type, // type character
2061 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002063 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002064 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002066 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002067 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002068 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002069 {
2070 buf_T *buf = buflist_findnr(bufnum);
2071
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002072 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002073 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002074 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002075 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002076 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002077 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002078 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2080 {
2081 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002082 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084 qfp->qf_lnum = lnum;
2085 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002086 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002087 if (pattern == NULL || *pattern == NUL)
2088 qfp->qf_pattern = NULL;
2089 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2090 {
2091 vim_free(qfp->qf_text);
2092 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002093 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002094 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002095 if (module == NULL || *module == NUL)
2096 qfp->qf_module = NULL;
2097 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2098 {
2099 vim_free(qfp->qf_text);
2100 vim_free(qfp->qf_pattern);
2101 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002102 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002105 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 type = 0;
2107 qfp->qf_type = type;
2108 qfp->qf_valid = valid;
2109
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002110 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002111 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002113 qfl->qf_start = qfp;
2114 qfl->qf_ptr = qfp;
2115 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002116 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
2118 else
2119 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002120 qfp->qf_prev = *lastp;
2121 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002123 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002125 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002126 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002127 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002129 qfl->qf_index = qfl->qf_count;
2130 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
2132
Bram Moolenaar95946f12019-03-31 15:31:59 +02002133 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134}
2135
2136/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002137 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002138 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002139 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002140qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002141{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002142 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002143
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002144 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002145 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002146 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002147 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002148 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002149 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002150 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002151 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002152}
2153
2154/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002155 * Return the location list stack for window 'wp'.
2156 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002157 */
2158 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002159ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002160{
2161 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002162 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002163 return wp->w_llist_ref;
2164
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002165 // For a non-location list window, w_llist_ref should not point to a
2166 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002167 ll_free_all(&wp->w_llist_ref);
2168
2169 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002170 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002171 return wp->w_llist;
2172}
2173
2174/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002175 * Get the quickfix/location list stack to use for the specified Ex command.
2176 * For a location list command, returns the stack for the current window. If
2177 * the location list is not found, then returns NULL and prints an error
2178 * message if 'print_emsg' is TRUE.
2179 */
2180 static qf_info_T *
2181qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2182{
2183 qf_info_T *qi = &ql_info;
2184
2185 if (is_loclist_cmd(eap->cmdidx))
2186 {
2187 qi = GET_LOC_LIST(curwin);
2188 if (qi == NULL)
2189 {
2190 if (print_emsg)
2191 emsg(_(e_loclist));
2192 return NULL;
2193 }
2194 }
2195
2196 return qi;
2197}
2198
2199/*
2200 * Get the quickfix/location list stack to use for the specified Ex command.
2201 * For a location list command, returns the stack for the current window.
2202 * If the location list is not present, then allocates a new one.
2203 * Returns NULL if the allocation fails. For a location list command, sets
2204 * 'pwinp' to curwin.
2205 */
2206 static qf_info_T *
2207qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2208{
2209 qf_info_T *qi = &ql_info;
2210
2211 if (is_loclist_cmd(eap->cmdidx))
2212 {
2213 qi = ll_get_or_alloc_list(curwin);
2214 if (qi == NULL)
2215 return NULL;
2216 *pwinp = curwin;
2217 }
2218
2219 return qi;
2220}
2221
2222/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002223 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2224 */
2225 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002226copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002227{
2228 int i;
2229 qfline_T *from_qfp;
2230 qfline_T *prevp;
2231
2232 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002233 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002234 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002235 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002236 NULL,
2237 NULL,
2238 from_qfp->qf_module,
2239 0,
2240 from_qfp->qf_text,
2241 from_qfp->qf_lnum,
2242 from_qfp->qf_col,
2243 from_qfp->qf_viscol,
2244 from_qfp->qf_pattern,
2245 from_qfp->qf_nr,
2246 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002247 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002248 return FAIL;
2249
2250 // qf_add_entry() will not set the qf_num field, as the
2251 // directory and file names are not supplied. So the qf_fnum
2252 // field is copied here.
2253 prevp = to_qfl->qf_last;
2254 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2255 prevp->qf_type = from_qfp->qf_type; // error type
2256 if (from_qfl->qf_ptr == from_qfp)
2257 to_qfl->qf_ptr = prevp; // current location
2258 }
2259
2260 return OK;
2261}
2262
2263/*
2264 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2265 */
2266 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002267copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002268{
2269 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002270 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002271 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2272 to_qfl->qf_count = 0;
2273 to_qfl->qf_index = 0;
2274 to_qfl->qf_start = NULL;
2275 to_qfl->qf_last = NULL;
2276 to_qfl->qf_ptr = NULL;
2277 if (from_qfl->qf_title != NULL)
2278 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2279 else
2280 to_qfl->qf_title = NULL;
2281 if (from_qfl->qf_ctx != NULL)
2282 {
2283 to_qfl->qf_ctx = alloc_tv();
2284 if (to_qfl->qf_ctx != NULL)
2285 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2286 }
2287 else
2288 to_qfl->qf_ctx = NULL;
2289
2290 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002291 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002292 return FAIL;
2293
2294 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2295
2296 // Assign a new ID for the location list
2297 to_qfl->qf_id = ++last_qf_id;
2298 to_qfl->qf_changedtick = 0L;
2299
2300 // When no valid entries are present in the list, qf_ptr points to
2301 // the first item in the list
2302 if (to_qfl->qf_nonevalid)
2303 {
2304 to_qfl->qf_ptr = to_qfl->qf_start;
2305 to_qfl->qf_index = 1;
2306 }
2307
2308 return OK;
2309}
2310
2311/*
2312 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002313 */
2314 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002315copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002316{
2317 qf_info_T *qi;
2318 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002319
Bram Moolenaar09037502018-09-25 22:08:14 +02002320 // When copying from a location list window, copy the referenced
2321 // location list. For other windows, copy the location list for
2322 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002323 if (IS_LL_WINDOW(from))
2324 qi = from->w_llist_ref;
2325 else
2326 qi = from->w_llist;
2327
Bram Moolenaar09037502018-09-25 22:08:14 +02002328 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002329 return;
2330
Bram Moolenaar09037502018-09-25 22:08:14 +02002331 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002332 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002333 return;
2334
2335 to->w_llist->qf_listcount = qi->qf_listcount;
2336
Bram Moolenaar09037502018-09-25 22:08:14 +02002337 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002338 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002339 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002340 to->w_llist->qf_curlist = idx;
2341
Bram Moolenaar0398e002019-03-21 21:12:49 +01002342 if (copy_loclist(qf_get_list(qi, idx),
2343 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002344 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002345 qf_free_all(to);
2346 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002347 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002348 }
2349
Bram Moolenaar09037502018-09-25 22:08:14 +02002350 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002351}
2352
2353/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002354 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002355 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 */
2357 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002358qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
Bram Moolenaar82404332016-07-10 17:00:38 +02002360 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002361 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002362 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002363
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002364 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
Bram Moolenaare60acc12011-05-10 16:41:25 +02002367#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002368 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002369#endif
2370#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002371 if (directory != NULL)
2372 slash_adjust(directory);
2373 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002374#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002375 if (directory != NULL && !vim_isAbsName(fname)
2376 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2377 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002378 // Here we check if the file really exists.
2379 // This should normally be true, but if make works without
2380 // "leaving directory"-messages we might have missed a
2381 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002382 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002385 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002386 if (directory)
2387 ptr = concat_fnames(directory, fname, TRUE);
2388 else
2389 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002391 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002392 bufname = ptr;
2393 }
2394 else
2395 bufname = fname;
2396
2397 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002398 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002399 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002400 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002401 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002403 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002404 {
2405 vim_free(qf_last_bufname);
2406 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2407 if (bufname == ptr)
2408 qf_last_bufname = bufname;
2409 else
2410 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002411 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002412 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002413 if (buf == NULL)
2414 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002415
Bram Moolenaarc1542742016-07-20 21:44:37 +02002416 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002417 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002418 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419}
2420
2421/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002422 * Push dirbuf onto the directory stack and return pointer to actual dir or
2423 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 */
2425 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002426qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
2428 struct dir_stack_T *ds_new;
2429 struct dir_stack_T *ds_ptr;
2430
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002431 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2433 if (ds_new == NULL)
2434 return NULL;
2435
2436 ds_new->next = *stackptr;
2437 *stackptr = ds_new;
2438
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002439 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 if (vim_isAbsName(dirbuf)
2441 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002442 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 (*stackptr)->dirname = vim_strsave(dirbuf);
2444 else
2445 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002446 // Okay we don't have an absolute path.
2447 // dirbuf must be a subdir of one of the directories on the stack.
2448 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 ds_new = (*stackptr)->next;
2450 (*stackptr)->dirname = NULL;
2451 while (ds_new)
2452 {
2453 vim_free((*stackptr)->dirname);
2454 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2455 TRUE);
2456 if (mch_isdir((*stackptr)->dirname) == TRUE)
2457 break;
2458
2459 ds_new = ds_new->next;
2460 }
2461
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002462 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 while ((*stackptr)->next != ds_new)
2464 {
2465 ds_ptr = (*stackptr)->next;
2466 (*stackptr)->next = (*stackptr)->next->next;
2467 vim_free(ds_ptr->dirname);
2468 vim_free(ds_ptr);
2469 }
2470
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002471 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 if (ds_new == NULL)
2473 {
2474 vim_free((*stackptr)->dirname);
2475 (*stackptr)->dirname = vim_strsave(dirbuf);
2476 }
2477 }
2478
2479 if ((*stackptr)->dirname != NULL)
2480 return (*stackptr)->dirname;
2481 else
2482 {
2483 ds_ptr = *stackptr;
2484 *stackptr = (*stackptr)->next;
2485 vim_free(ds_ptr);
2486 return NULL;
2487 }
2488}
2489
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490/*
2491 * pop dirbuf from the directory stack and return previous directory or NULL if
2492 * stack is empty
2493 */
2494 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002495qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 struct dir_stack_T *ds_ptr;
2498
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002499 // TODO: Should we check if dirbuf is the directory on top of the stack?
2500 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002502 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 if (*stackptr != NULL)
2504 {
2505 ds_ptr = *stackptr;
2506 *stackptr = (*stackptr)->next;
2507 vim_free(ds_ptr->dirname);
2508 vim_free(ds_ptr);
2509 }
2510
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002511 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 return *stackptr ? (*stackptr)->dirname : NULL;
2513}
2514
2515/*
2516 * clean up directory stack
2517 */
2518 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002519qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
2521 struct dir_stack_T *ds_ptr;
2522
2523 while ((ds_ptr = *stackptr) != NULL)
2524 {
2525 *stackptr = (*stackptr)->next;
2526 vim_free(ds_ptr->dirname);
2527 vim_free(ds_ptr);
2528 }
2529}
2530
2531/*
2532 * Check in which directory of the directory stack the given file can be
2533 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002534 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 * Cleans up intermediate directory entries.
2536 *
2537 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002538 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 * ./
2540 * ./aa
2541 * ./aa/bb
2542 * ./bb
2543 * ./bb/x.c
2544 * and make says:
2545 * making all in aa
2546 * making all in bb
2547 * x.c:9: Error
2548 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2549 * qf_guess_filepath will return NULL.
2550 */
2551 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002552qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553{
2554 struct dir_stack_T *ds_ptr;
2555 struct dir_stack_T *ds_tmp;
2556 char_u *fullname;
2557
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002558 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002559 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 return NULL;
2561
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002562 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 fullname = NULL;
2564 while (ds_ptr)
2565 {
2566 vim_free(fullname);
2567 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2568
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002569 // If concat_fnames failed, just go on. The worst thing that can happen
2570 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2572 break;
2573
2574 ds_ptr = ds_ptr->next;
2575 }
2576
2577 vim_free(fullname);
2578
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002579 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002580 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002582 ds_tmp = qfl->qf_dir_stack->next;
2583 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 vim_free(ds_tmp->dirname);
2585 vim_free(ds_tmp);
2586 }
2587
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002588 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589}
2590
2591/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002592 * Returns TRUE if a quickfix/location list with the given identifier exists.
2593 */
2594 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002595qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002596{
2597 qf_info_T *qi = &ql_info;
2598 int i;
2599
2600 if (wp != NULL)
2601 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002602 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002603 if (qi == NULL)
2604 return FALSE;
2605 }
2606
2607 for (i = 0; i < qi->qf_listcount; ++i)
2608 if (qi->qf_lists[i].qf_id == qf_id)
2609 return TRUE;
2610
2611 return FALSE;
2612}
2613
2614/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002615 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002616 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002617 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002618 * Similar to location list.
2619 */
2620 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002621is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002622{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002623 qfline_T *qfp;
2624 int i;
2625
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002626 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002627 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2628 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002629 break;
2630
Bram Moolenaar95946f12019-03-31 15:31:59 +02002631 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002632 return FALSE;
2633
2634 return TRUE;
2635}
2636
2637/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002638 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002639 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002640 */
2641 static qfline_T *
2642get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002643 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002644 qfline_T *qf_ptr,
2645 int *qf_index,
2646 int dir)
2647{
2648 int idx;
2649 int old_qf_fnum;
2650
2651 idx = *qf_index;
2652 old_qf_fnum = qf_ptr->qf_fnum;
2653
2654 do
2655 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002656 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002657 return NULL;
2658 ++idx;
2659 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002660 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002661 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2662
2663 *qf_index = idx;
2664 return qf_ptr;
2665}
2666
2667/*
2668 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002669 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002670 */
2671 static qfline_T *
2672get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002673 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002674 qfline_T *qf_ptr,
2675 int *qf_index,
2676 int dir)
2677{
2678 int idx;
2679 int old_qf_fnum;
2680
2681 idx = *qf_index;
2682 old_qf_fnum = qf_ptr->qf_fnum;
2683
2684 do
2685 {
2686 if (idx == 1 || qf_ptr->qf_prev == NULL)
2687 return NULL;
2688 --idx;
2689 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002690 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002691 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2692
2693 *qf_index = idx;
2694 return qf_ptr;
2695}
2696
2697/*
2698 * Get the n'th (errornr) previous/next valid entry from the current entry in
2699 * the quickfix list.
2700 * dir == FORWARD or FORWARD_FILE: next valid entry
2701 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2702 */
2703 static qfline_T *
2704get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002705 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002706 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002707 int dir,
2708 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002709{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002710 qfline_T *qf_ptr = qfl->qf_ptr;
2711 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002712 qfline_T *prev_qf_ptr;
2713 int prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002714 char_u *err = e_no_more_items;
2715
2716 while (errornr--)
2717 {
2718 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002719 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002720
2721 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002722 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002723 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002724 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002725 if (qf_ptr == NULL)
2726 {
2727 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002728 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002729 if (err != NULL)
2730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002731 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002732 return NULL;
2733 }
2734 break;
2735 }
2736
2737 err = NULL;
2738 }
2739
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002740 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002741 return qf_ptr;
2742}
2743
2744/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002745 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2746 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002747 */
2748 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002749get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002750{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002751 qfline_T *qf_ptr = qfl->qf_ptr;
2752 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002753
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002754 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002755 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2756 {
2757 --qf_idx;
2758 qf_ptr = qf_ptr->qf_prev;
2759 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002760 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002761 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2762 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002763 {
2764 ++qf_idx;
2765 qf_ptr = qf_ptr->qf_next;
2766 }
2767
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002768 *new_qfidx = qf_idx;
2769 return qf_ptr;
2770}
2771
2772/*
2773 * Get a entry specied by 'errornr' and 'dir' from the current
2774 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2775 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2776 * Returns a pointer to the entry and the index of the new entry is stored in
2777 * 'new_qfidx'.
2778 */
2779 static qfline_T *
2780qf_get_entry(
2781 qf_list_T *qfl,
2782 int errornr,
2783 int dir,
2784 int *new_qfidx)
2785{
2786 qfline_T *qf_ptr = qfl->qf_ptr;
2787 int qfidx = qfl->qf_index;
2788
2789 if (dir != 0) // next/prev valid entry
2790 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2791 else if (errornr != 0) // go to specified number
2792 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2793
2794 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002795 return qf_ptr;
2796}
2797
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002798/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002799 * Find a window displaying a Vim help file.
2800 */
2801 static win_T *
2802qf_find_help_win(void)
2803{
2804 win_T *wp;
2805
2806 FOR_ALL_WINDOWS(wp)
2807 if (bt_help(wp->w_buffer))
2808 return wp;
2809
2810 return NULL;
2811}
2812
2813/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002814 * Set the location list for the specified window to 'qi'.
2815 */
2816 static void
2817win_set_loclist(win_T *wp, qf_info_T *qi)
2818{
2819 wp->w_llist = qi;
2820 qi->qf_refcount++;
2821}
2822
2823/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002824 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2825 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002826 */
2827 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002828jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002829{
2830 win_T *wp;
2831 int flags;
2832
Bram Moolenaarb2443732018-11-11 22:50:27 +01002833 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002834 wp = NULL;
2835 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002836 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002837 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2838 win_enter(wp, TRUE);
2839 else
2840 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002841 // Split off help window; put it at far top if no position
2842 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002843 flags = WSP_HELP;
2844 if (cmdmod.split == 0 && curwin->w_width != Columns
2845 && curwin->w_width < 80)
2846 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002847 // If the user asks to open a new window, then copy the location list.
2848 // Otherwise, don't copy the location list.
2849 if (IS_LL_STACK(qi) && !newwin)
2850 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002851
2852 if (win_split(0, flags) == FAIL)
2853 return FAIL;
2854
2855 *opened_window = TRUE;
2856
2857 if (curwin->w_height < p_hh)
2858 win_setheight((int)p_hh);
2859
Bram Moolenaarb2443732018-11-11 22:50:27 +01002860 // When using location list, the new window should use the supplied
2861 // location list. If the user asks to open a new window, then the new
2862 // window will get a copy of the location list.
2863 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002864 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002865 }
2866
2867 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002868 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002869
2870 return OK;
2871}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002872
2873/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002874 * Find a non-quickfix window in the current tabpage using the given location
2875 * list stack.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002876 * Returns NULL if a matching window is not found.
2877 */
2878 static win_T *
2879qf_find_win_with_loclist(qf_info_T *ll)
2880{
2881 win_T *wp;
2882
2883 FOR_ALL_WINDOWS(wp)
2884 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2885 return wp;
2886
2887 return NULL;
2888}
2889
2890/*
2891 * Find a window containing a normal buffer
2892 */
2893 static win_T *
2894qf_find_win_with_normal_buf(void)
2895{
2896 win_T *wp;
2897
2898 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002899 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002900 return wp;
2901
2902 return NULL;
2903}
2904
2905/*
2906 * Go to a window in any tabpage containing the specified file. Returns TRUE
2907 * if successfully jumped to the window. Otherwise returns FALSE.
2908 */
2909 static int
2910qf_goto_tabwin_with_file(int fnum)
2911{
2912 tabpage_T *tp;
2913 win_T *wp;
2914
2915 FOR_ALL_TAB_WINDOWS(tp, wp)
2916 if (wp->w_buffer->b_fnum == fnum)
2917 {
2918 goto_tabpage_win(tp, wp);
2919 return TRUE;
2920 }
2921
2922 return FALSE;
2923}
2924
2925/*
2926 * Create a new window to show a file above the quickfix window. Called when
2927 * only the quickfix window is present.
2928 */
2929 static int
2930qf_open_new_file_win(qf_info_T *ll_ref)
2931{
2932 int flags;
2933
2934 flags = WSP_ABOVE;
2935 if (ll_ref != NULL)
2936 flags |= WSP_NEWLOC;
2937 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002938 return FAIL; // not enough room for window
2939 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002940 swb_flags = 0;
2941 RESET_BINDING(curwin);
2942 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002943 // The new window should use the location list from the
2944 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002945 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002946 return OK;
2947}
2948
2949/*
2950 * Go to a window that shows the right buffer. If the window is not found, go
2951 * to the window just above the location list window. This is used for opening
2952 * a file from a location window and not from a quickfix window. If some usable
2953 * window is previously found, then it is supplied in 'use_win'.
2954 */
2955 static void
2956qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2957{
2958 win_T *win = use_win;
2959
2960 if (win == NULL)
2961 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002962 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002963 FOR_ALL_WINDOWS(win)
2964 if (win->w_buffer->b_fnum == qf_fnum)
2965 break;
2966 if (win == NULL)
2967 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002968 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002969 win = curwin;
2970 do
2971 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002972 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002973 break;
2974 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002975 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002976 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002977 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002978 } while (win != curwin);
2979 }
2980 }
2981 win_goto(win);
2982
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002983 // If the location list for the window is not set, then set it
2984 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01002985 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002986 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002987}
2988
2989/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002990 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2991 * not found, then go to the window just above the quickfix window. This is
2992 * used for opening a file from a quickfix window and not from a location
2993 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002994 */
2995 static void
2996qf_goto_win_with_qfl_file(int qf_fnum)
2997{
2998 win_T *win;
2999 win_T *altwin;
3000
3001 win = curwin;
3002 altwin = NULL;
3003 for (;;)
3004 {
3005 if (win->w_buffer->b_fnum == qf_fnum)
3006 break;
3007 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003008 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003009 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003010 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003011
3012 if (IS_QF_WINDOW(win))
3013 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003014 // Didn't find it, go to the window before the quickfix
3015 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003016 if (altwin != NULL)
3017 win = altwin;
3018 else if (curwin->w_prev != NULL)
3019 win = curwin->w_prev;
3020 else
3021 win = curwin->w_next;
3022 break;
3023 }
3024
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003025 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003026 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003027 altwin = win;
3028 }
3029
3030 win_goto(win);
3031}
3032
3033/*
3034 * Find a suitable window for opening a file (qf_fnum) from the
3035 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003036 * window, jump to it. Otherwise open a new window to display the file. If
3037 * 'newwin' is TRUE, then always open a new window. This is called from either
3038 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003039 */
3040 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003041qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003042{
3043 win_T *usable_win_ptr = NULL;
3044 int usable_win;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003045 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003046 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003047
3048 usable_win = 0;
3049
Bram Moolenaarb2443732018-11-11 22:50:27 +01003050 // If opening a new window, then don't use the location list referred by
3051 // the current window. Otherwise two windows will refer to the same
3052 // location list.
3053 if (!newwin)
3054 ll_ref = curwin->w_llist_ref;
3055
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003056 if (ll_ref != NULL)
3057 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003058 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003059 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
3060 if (usable_win_ptr != NULL)
3061 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003062 }
3063
3064 if (!usable_win)
3065 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003066 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003067 win = qf_find_win_with_normal_buf();
3068 if (win != NULL)
3069 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003070 }
3071
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003072 // If no usable window is found and 'switchbuf' contains "usetab"
3073 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003074 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003075 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003076
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003077 // If there is only one window and it is the quickfix window, create a
3078 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003079 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003080 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003081 if (qf_open_new_file_win(ll_ref) != OK)
3082 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003083 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003084 }
3085 else
3086 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003087 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003088 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003089 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003090 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003091 }
3092
3093 return OK;
3094}
3095
3096/*
3097 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003098 * Returns OK if successfully edited the file, FAIL on failing to open the
3099 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3100 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003101 */
3102 static int
3103qf_jump_edit_buffer(
3104 qf_info_T *qi,
3105 qfline_T *qf_ptr,
3106 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003107 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003108 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003109{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003110 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003111 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003112 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003113 int old_qf_curlist = qi->qf_curlist;
3114 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003115
3116 if (qf_ptr->qf_type == 1)
3117 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003118 // Open help file (do_ecmd() will set b_help flag, readfile() will
3119 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003120 if (!can_abandon(curbuf, forceit))
3121 {
3122 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003123 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003124 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003125
3126 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3127 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003128 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003129 }
3130 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003131 retval = buflist_getfile(qf_ptr->qf_fnum,
3132 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003133
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003134 // If a location list, check whether the associated window is still
3135 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003136 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003137 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003138 win_T *wp = win_id2wp(prev_winid);
3139 if (wp == NULL && curwin->w_llist != qi)
3140 {
3141 emsg(_("E924: Current window was closed"));
3142 *opened_window = FALSE;
3143 return NOTDONE;
3144 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003145 }
3146
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003147 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003149 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003150 return NOTDONE;
3151 }
3152
3153 if (old_qf_curlist != qi->qf_curlist
3154 || !is_qf_entry_present(qfl, qf_ptr))
3155 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003156 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003157 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003158 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003159 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003160 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003161 }
3162
3163 return retval;
3164}
3165
3166/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003167 * Go to the error line in the current file using either line/column number or
3168 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003169 */
3170 static void
3171qf_jump_goto_line(
3172 linenr_T qf_lnum,
3173 int qf_col,
3174 char_u qf_viscol,
3175 char_u *qf_pattern)
3176{
3177 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003178
3179 if (qf_pattern == NULL)
3180 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003181 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003182 i = qf_lnum;
3183 if (i > 0)
3184 {
3185 if (i > curbuf->b_ml.ml_line_count)
3186 i = curbuf->b_ml.ml_line_count;
3187 curwin->w_cursor.lnum = i;
3188 }
3189 if (qf_col > 0)
3190 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003191 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003192 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003193 coladvance(qf_col - 1);
3194 else
3195 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003196 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003197 check_cursor();
3198 }
3199 else
3200 beginline(BL_WHITE | BL_FIX);
3201 }
3202 else
3203 {
3204 pos_T save_cursor;
3205
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003206 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003207 save_cursor = curwin->w_cursor;
3208 curwin->w_cursor.lnum = 0;
3209 if (!do_search(NULL, '/', qf_pattern, (long)1,
3210 SEARCH_KEEP, NULL, NULL))
3211 curwin->w_cursor = save_cursor;
3212 }
3213}
3214
3215/*
3216 * Display quickfix list index and size message
3217 */
3218 static void
3219qf_jump_print_msg(
3220 qf_info_T *qi,
3221 int qf_index,
3222 qfline_T *qf_ptr,
3223 buf_T *old_curbuf,
3224 linenr_T old_lnum)
3225{
3226 linenr_T i;
3227 int len;
3228
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003229 // Update the screen before showing the message, unless the screen
3230 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003231 if (!msg_scrolled)
3232 update_topline_redraw();
3233 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003234 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003235 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3236 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003237 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003238 len = (int)STRLEN(IObuff);
3239 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3240
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003241 // Output the message. Overwrite to avoid scrolling when the 'O'
3242 // flag is present in 'shortmess'; But when not jumping, print the
3243 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003244 i = msg_scroll;
3245 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3246 msg_scroll = TRUE;
3247 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3248 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003249 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003250 msg_scroll = i;
3251}
3252
3253/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003254 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003255 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3256 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003257 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3258 * able to jump/open a window. Returns NOTDONE if a file is not associated
3259 * with the entry.
3260 */
3261 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003262qf_jump_open_window(
3263 qf_info_T *qi,
3264 qfline_T *qf_ptr,
3265 int newwin,
3266 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003267{
3268 // For ":helpgrep" find a help window or open one.
3269 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003270 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003271 return FAIL;
3272
3273 // If currently in the quickfix window, find another window to show the
3274 // file in.
3275 if (bt_quickfix(curbuf) && !*opened_window)
3276 {
3277 // If there is no file specified, we don't know where to go.
3278 // But do advance, otherwise ":cn" gets stuck.
3279 if (qf_ptr->qf_fnum == 0)
3280 return NOTDONE;
3281
Bram Moolenaarb2443732018-11-11 22:50:27 +01003282 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3283 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003284 return FAIL;
3285 }
3286
3287 return OK;
3288}
3289
3290/*
3291 * Edit a selected file from the quickfix/location list and jump to a
3292 * particular line/column, adjust the folds and display a message about the
3293 * jump.
3294 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3295 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3296 * the file.
3297 */
3298 static int
3299qf_jump_to_buffer(
3300 qf_info_T *qi,
3301 int qf_index,
3302 qfline_T *qf_ptr,
3303 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003304 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003305 int *opened_window,
3306 int openfold,
3307 int print_message)
3308{
3309 buf_T *old_curbuf;
3310 linenr_T old_lnum;
3311 int retval = OK;
3312
3313 // If there is a file name, read the wanted file if needed, and check
3314 // autowrite etc.
3315 old_curbuf = curbuf;
3316 old_lnum = curwin->w_cursor.lnum;
3317
3318 if (qf_ptr->qf_fnum != 0)
3319 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003320 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003321 opened_window);
3322 if (retval != OK)
3323 return retval;
3324 }
3325
3326 // When not switched to another buffer, still need to set pc mark
3327 if (curbuf == old_curbuf)
3328 setpcmark();
3329
3330 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3331 qf_ptr->qf_pattern);
3332
3333#ifdef FEAT_FOLDING
3334 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3335 foldOpenCursor();
3336#endif
3337 if (print_message)
3338 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3339
3340 return retval;
3341}
3342
3343/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003344 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 */
3346 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003347qf_jump(qf_info_T *qi,
3348 int dir,
3349 int errornr,
3350 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003352 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3353}
3354
3355/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003356 * Jump to a quickfix line.
3357 * If dir == 0 go to entry "errornr".
3358 * If dir == FORWARD go "errornr" valid entries forward.
3359 * If dir == BACKWARD go "errornr" valid entries backward.
3360 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3361 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3362 * else if "errornr" is zero, redisplay the same line
3363 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003364 * If 'newwin' is TRUE, then open the file in a new window.
3365 */
3366 void
3367qf_jump_newwin(qf_info_T *qi,
3368 int dir,
3369 int errornr,
3370 int forceit,
3371 int newwin)
3372{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003373 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003374 qfline_T *qf_ptr;
3375 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003378 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003379 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003380 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003384 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003386 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003388 if (qi == NULL)
3389 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003390
Bram Moolenaar0398e002019-03-21 21:12:49 +01003391 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003393 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 return;
3395 }
3396
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003397 incr_quickfix_busy();
3398
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003399 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003400
3401 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003403 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003405
3406 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3407 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003409 qf_ptr = old_qf_ptr;
3410 qf_index = old_qf_index;
3411 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003414 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003415 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003416 // No need to print the error message if it's visible in the error
3417 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 print_message = FALSE;
3419
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003420 prev_winid = curwin->w_id;
3421
Bram Moolenaarb2443732018-11-11 22:50:27 +01003422 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003423 if (retval == FAIL)
3424 goto failed;
3425 if (retval == NOTDONE)
3426 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003427
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003428 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003429 &opened_window, old_KeyTyped, print_message);
3430 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003432 // Quickfix/location list is freed by an autocmd
3433 qi = NULL;
3434 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003437 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003440 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003441 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003443 // Couldn't open file, so put index back where it was. This could
3444 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 qf_ptr = old_qf_ptr;
3447 qf_index = old_qf_index;
3448 }
3449 }
3450theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003451 if (qi != NULL)
3452 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003453 qfl->qf_ptr = qf_ptr;
3454 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003455 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003456 if (p_swb != old_swb)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003458 // Restore old 'switchbuf' value, but not when an autocommand or
3459 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003461 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003463 swb_flags = old_swb_flags;
3464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 else
3466 free_string_option(old_swb);
3467 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003468 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469}
3470
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003471// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003472static int qfFileAttr;
3473static int qfSepAttr;
3474static int qfLineAttr;
3475
3476/*
3477 * Display information about a single entry from the quickfix/location list.
3478 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003479 * 'cursel' will be set to TRUE for the currently selected entry in the
3480 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003481 */
3482 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003483qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003484{
3485 char_u *fname;
3486 buf_T *buf;
3487 int filter_entry;
3488
3489 fname = NULL;
3490 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3491 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3492 (char *)qfp->qf_module);
3493 else {
3494 if (qfp->qf_fnum != 0
3495 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3496 {
3497 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003498 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003499 fname = gettail(fname);
3500 }
3501 if (fname == NULL)
3502 sprintf((char *)IObuff, "%2d", qf_idx);
3503 else
3504 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3505 qf_idx, (char *)fname);
3506 }
3507
3508 // Support for filtering entries using :filter /pat/ clist
3509 // Match against the module name, file name, search pattern and
3510 // text of the entry.
3511 filter_entry = TRUE;
3512 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3513 filter_entry &= message_filtered(qfp->qf_module);
3514 if (filter_entry && fname != NULL)
3515 filter_entry &= message_filtered(fname);
3516 if (filter_entry && qfp->qf_pattern != NULL)
3517 filter_entry &= message_filtered(qfp->qf_pattern);
3518 if (filter_entry)
3519 filter_entry &= message_filtered(qfp->qf_text);
3520 if (filter_entry)
3521 return;
3522
3523 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003524 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003525
3526 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003527 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003528 if (qfp->qf_lnum == 0)
3529 IObuff[0] = NUL;
3530 else if (qfp->qf_col == 0)
3531 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3532 else
3533 sprintf((char *)IObuff, "%ld col %d",
3534 qfp->qf_lnum, qfp->qf_col);
3535 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3536 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003537 msg_puts_attr((char *)IObuff, qfLineAttr);
3538 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003539 if (qfp->qf_pattern != NULL)
3540 {
3541 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003542 msg_puts((char *)IObuff);
3543 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003544 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003545 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003546
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003547 // Remove newlines and leading whitespace from the text. For an
3548 // unrecognized line keep the indent, the compiler may mark a word
3549 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003550 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3551 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3552 IObuff, IOSIZE);
3553 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003554 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003555}
3556
3557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003559 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 */
3561 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003562qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003564 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003565 qfline_T *qfp;
3566 int i;
3567 int idx1 = 1;
3568 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003569 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003570 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003571 int all = eap->forceit; // if not :cl!, only show
3572 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003573 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003575 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3576 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003577
Bram Moolenaar0398e002019-03-21 21:12:49 +01003578 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003580 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 return;
3582 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003583 if (*arg == '+')
3584 {
3585 ++arg;
3586 plus = TRUE;
3587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003590 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 return;
3592 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003593 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003594 if (plus)
3595 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003596 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003597 idx2 = i + idx1;
3598 idx1 = i;
3599 }
3600 else
3601 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003602 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003603 if (idx1 < 0)
3604 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3605 if (idx2 < 0)
3606 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003609 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003610 shorten_fnames(FALSE);
3611
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003612 // Get the attributes for the different quickfix highlight items. Note
3613 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003614 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3615 if (qfFileAttr == 0)
3616 qfFileAttr = HL_ATTR(HLF_D);
3617 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3618 if (qfSepAttr == 0)
3619 qfSepAttr = HL_ATTR(HLF_D);
3620 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3621 if (qfLineAttr == 0)
3622 qfLineAttr = HL_ATTR(HLF_N);
3623
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003624 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003626 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
3628 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003629 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003630
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 ui_breakcheck();
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633}
3634
3635/*
3636 * Remove newlines and leading whitespace from an error message.
3637 * Put the result in "buf[bufsize]".
3638 */
3639 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003640qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
3642 int i;
3643 char_u *p = text;
3644
3645 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3646 {
3647 if (*p == '\n')
3648 {
3649 buf[i] = ' ';
3650 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003651 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 break;
3653 }
3654 else
3655 buf[i] = *p++;
3656 }
3657 buf[i] = NUL;
3658}
3659
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003660/*
3661 * Display information (list number, list size and the title) about a
3662 * quickfix/location list.
3663 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003664 static void
3665qf_msg(qf_info_T *qi, int which, char *lead)
3666{
3667 char *title = (char *)qi->qf_lists[which].qf_title;
3668 int count = qi->qf_lists[which].qf_count;
3669 char_u buf[IOSIZE];
3670
3671 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3672 lead,
3673 which + 1,
3674 qi->qf_listcount,
3675 count);
3676
3677 if (title != NULL)
3678 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003679 size_t len = STRLEN(buf);
3680
3681 if (len < 34)
3682 {
3683 vim_memset(buf + len, ' ', 34 - len);
3684 buf[34] = NUL;
3685 }
3686 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003687 }
3688 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003689 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003690}
3691
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692/*
3693 * ":colder [count]": Up in the quickfix stack.
3694 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003695 * ":lolder [count]": Up in the location list stack.
3696 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 */
3698 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003699qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003701 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 int count;
3703
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003704 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3705 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 if (eap->addr_count != 0)
3708 count = eap->line2;
3709 else
3710 count = 1;
3711 while (count--)
3712 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003713 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003715 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003717 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003718 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003720 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722 else
3723 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003724 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003726 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003727 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003729 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
3731 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003732 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003733 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734}
3735
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003736/*
3737 * Display the information about all the quickfix/location lists in the stack
3738 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003739 void
3740qf_history(exarg_T *eap)
3741{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003742 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003743 int i;
3744
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003745 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003746 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003747 else
3748 for (i = 0; i < qi->qf_listcount; ++i)
3749 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3750}
3751
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003753 * Free all the entries in the error list "idx". Note that other information
3754 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 */
3756 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003757qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003759 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003760 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003761 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003763 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003765 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003766 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003767 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003768 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003769 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003770 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003771 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003772 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003773 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003774 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003775 // Somehow qf_count may have an incorrect value, set it to 1
3776 // to avoid crashing when it's wrong.
3777 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003778 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003779 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003780 qfl->qf_start = qfpnext;
3781 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003783
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003784 qfl->qf_index = 0;
3785 qfl->qf_start = NULL;
3786 qfl->qf_last = NULL;
3787 qfl->qf_ptr = NULL;
3788 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003789
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003790 qf_clean_dir_stack(&qfl->qf_dir_stack);
3791 qfl->qf_directory = NULL;
3792 qf_clean_dir_stack(&qfl->qf_file_stack);
3793 qfl->qf_currfile = NULL;
3794 qfl->qf_multiline = FALSE;
3795 qfl->qf_multiignore = FALSE;
3796 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797}
3798
3799/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003800 * Free error list "idx". Frees all the entries in the quickfix list,
3801 * associated context information and the title.
3802 */
3803 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003804qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003805{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003806 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003807
Bram Moolenaard23a8232018-02-10 18:45:26 +01003808 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003809 free_tv(qfl->qf_ctx);
3810 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003811 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003812 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003813}
3814
3815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 * qf_mark_adjust: adjust marks
3817 */
3818 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003819qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003820 win_T *wp,
3821 linenr_T line1,
3822 linenr_T line2,
3823 long amount,
3824 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003826 int i;
3827 qfline_T *qfp;
3828 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003829 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003830 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003831 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
Bram Moolenaarc1542742016-07-20 21:44:37 +02003833 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003834 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003835 if (wp != NULL)
3836 {
3837 if (wp->w_llist == NULL)
3838 return;
3839 qi = wp->w_llist;
3840 }
3841
3842 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003843 {
3844 qf_list_T *qfl = qf_get_list(qi, idx);
3845
3846 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003847 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 if (qfp->qf_fnum == curbuf->b_fnum)
3849 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003850 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3852 {
3853 if (amount == MAXLNUM)
3854 qfp->qf_cleared = TRUE;
3855 else
3856 qfp->qf_lnum += amount;
3857 }
3858 else if (amount_after && qfp->qf_lnum > line2)
3859 qfp->qf_lnum += amount_after;
3860 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003861 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003862
3863 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003864 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865}
3866
3867/*
3868 * Make a nice message out of the error character and the error number:
3869 * char number message
3870 * e or E 0 " error"
3871 * w or W 0 " warning"
3872 * i or I 0 " info"
3873 * 0 0 ""
3874 * other 0 " c"
3875 * e or E n " error n"
3876 * w or W n " warning n"
3877 * i or I n " info n"
3878 * 0 n " error n"
3879 * other n " c n"
3880 * 1 x "" :helpgrep
3881 */
3882 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003883qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884{
3885 static char_u buf[20];
3886 static char_u cc[3];
3887 char_u *p;
3888
3889 if (c == 'W' || c == 'w')
3890 p = (char_u *)" warning";
3891 else if (c == 'I' || c == 'i')
3892 p = (char_u *)" info";
3893 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3894 p = (char_u *)" error";
3895 else if (c == 0 || c == 1)
3896 p = (char_u *)"";
3897 else
3898 {
3899 cc[0] = ' ';
3900 cc[1] = c;
3901 cc[2] = NUL;
3902 p = cc;
3903 }
3904
3905 if (nr <= 0)
3906 return p;
3907
3908 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3909 return buf;
3910}
3911
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003913 * When "split" is FALSE: Open the entry/result under the cursor.
3914 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3915 */
3916 void
3917qf_view_result(int split)
3918{
3919 qf_info_T *qi = &ql_info;
3920
3921 if (!bt_quickfix(curbuf))
3922 return;
3923
3924 if (IS_LL_WINDOW(curwin))
3925 qi = GET_LOC_LIST(curwin);
3926
Bram Moolenaar0398e002019-03-21 21:12:49 +01003927 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003928 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003929 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003930 return;
3931 }
3932
3933 if (split)
3934 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003935 // Open the selected entry in a new window
3936 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3937 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003938 return;
3939 }
3940
3941 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3942}
3943
3944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 * ":cwindow": open the quickfix window if we have errors to display,
3946 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003947 * ":lwindow": open the location list window if we have locations to display,
3948 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 */
3950 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003951ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003953 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003954 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 win_T *win;
3956
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003957 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3958 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003959
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003960 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003961
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003962 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003963 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003965 // If a quickfix window is open but we have no errors to display,
3966 // close the window. If a quickfix window is not open, then open
3967 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003968 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003969 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01003970 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
3972 if (win != NULL)
3973 ex_cclose(eap);
3974 }
3975 else if (win == NULL)
3976 ex_copen(eap);
3977}
3978
3979/*
3980 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003981 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003984ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003986 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003987 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003989 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
3990 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003992 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003993 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (win != NULL)
3995 win_close(win, FALSE);
3996}
3997
3998/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003999 * Set "w:quickfix_title" if "qi" has a title.
4000 */
4001 static void
4002qf_set_title_var(qf_list_T *qfl)
4003{
4004 if (qfl->qf_title != NULL)
4005 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4006}
4007
4008/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004009 * Goto a quickfix or location list window (if present).
4010 * Returns OK if the window is found, FAIL otherwise.
4011 */
4012 static int
4013qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4014{
4015 win_T *win;
4016
4017 win = qf_find_win(qi);
4018 if (win == NULL)
4019 return FAIL;
4020
4021 win_goto(win);
4022 if (resize)
4023 {
4024 if (vertsplit)
4025 {
4026 if (sz != win->w_width)
4027 win_setwidth(sz);
4028 }
Bram Moolenaar36d50222019-05-02 20:17:40 +02004029 else if (sz != win->w_height
4030 && win->w_height + win->w_status_height < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004031 win_setheight(sz);
4032 }
4033
4034 return OK;
4035}
4036
4037/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004038 * Set options for the buffer in the quickfix or location list window.
4039 */
4040 static void
4041qf_set_cwindow_options(void)
4042{
4043 // switch off 'swapfile'
4044 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4045 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4046 OPT_LOCAL);
4047 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4048 RESET_BINDING(curwin);
4049#ifdef FEAT_DIFF
4050 curwin->w_p_diff = FALSE;
4051#endif
4052#ifdef FEAT_FOLDING
4053 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4054 OPT_LOCAL);
4055#endif
4056}
4057
4058/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004059 * Open a new quickfix or location list window, load the quickfix buffer and
4060 * set the appropriate options for the window.
4061 * Returns FAIL if the window could not be opened.
4062 */
4063 static int
4064qf_open_new_cwindow(qf_info_T *qi, int height)
4065{
4066 buf_T *qf_buf;
4067 win_T *oldwin = curwin;
4068 tabpage_T *prevtab = curtab;
4069 int flags = 0;
4070 win_T *win;
4071
4072 qf_buf = qf_find_buf(qi);
4073
4074 // The current window becomes the previous window afterwards.
4075 win = curwin;
4076
4077 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4078 // Create the new quickfix window at the very bottom, except when
4079 // :belowright or :aboveleft is used.
4080 win_goto(lastwin);
4081 // Default is to open the window below the current window
4082 if (cmdmod.split == 0)
4083 flags = WSP_BELOW;
4084 flags |= WSP_NEWLOC;
4085 if (win_split(height, flags) == FAIL)
4086 return FAIL; // not enough room for window
4087 RESET_BINDING(curwin);
4088
4089 if (IS_LL_STACK(qi))
4090 {
4091 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004092 // location list stack from the window 'win'.
4093 curwin->w_llist_ref = qi;
4094 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004095 }
4096
4097 if (oldwin != curwin)
4098 oldwin = NULL; // don't store info when in another window
4099 if (qf_buf != NULL)
4100 {
4101 // Use the existing quickfix buffer
4102 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4103 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4104 }
4105 else
4106 {
4107 // Create a new quickfix buffer
4108 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4109
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004110 // save the number of the new buffer
4111 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004112 }
4113
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004114 // Set the options for the quickfix buffer/window (if not already done)
4115 // Do this even if the quickfix buffer was already present, as an autocmd
4116 // might have previously deleted (:bdelete) the quickfix buffer.
4117 if (curbuf->b_p_bt[0] != 'q')
4118 qf_set_cwindow_options();
4119
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004120 // Only set the height when still in the same tab page and there is no
4121 // window to the side.
4122 if (curtab == prevtab && curwin->w_width == Columns)
4123 win_setheight(height);
4124 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4125 if (win_valid(win))
4126 prevwin = win;
4127
4128 return OK;
4129}
4130
4131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004133 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 */
4135 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004136ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004138 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004139 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004141 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004142 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004144 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4145 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004146
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004147 incr_quickfix_busy();
4148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 if (eap->addr_count != 0)
4150 height = eap->line2;
4151 else
4152 height = QF_WINHEIGHT;
4153
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004154 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155#ifdef FEAT_GUI
4156 need_mouse_correct = TRUE;
4157#endif
4158
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004159 // Find an existing quickfix window, or open a new one.
4160 if (cmdmod.tab == 0)
4161 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4162 cmdmod.split & WSP_VERT);
4163 if (status == FAIL)
4164 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004165 {
4166 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004167 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004170 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004171 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004172 // Save the current index here, as updating the quickfix buffer may free
4173 // the quickfix list
4174 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004175
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004176 // Fill the buffer with the quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004177 qf_fill_buffer(qfl, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004179 decr_quickfix_busy();
4180
4181 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 curwin->w_cursor.col = 0;
4183 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004184 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185}
4186
4187/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004188 * Move the cursor in the quickfix window to "lnum".
4189 */
4190 static void
4191qf_win_goto(win_T *win, linenr_T lnum)
4192{
4193 win_T *old_curwin = curwin;
4194
4195 curwin = win;
4196 curbuf = win->w_buffer;
4197 curwin->w_cursor.lnum = lnum;
4198 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004199 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004200 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004201 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004202 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004203 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004204 curwin = old_curwin;
4205 curbuf = curwin->w_buffer;
4206}
4207
4208/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004209 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004210 */
4211 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004212ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004213{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004214 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004215 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004216
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004217 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4218 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004219
4220 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004221 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4222 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4223}
4224
4225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 * Return the number of the current entry (line number in the quickfix
4227 * window).
4228 */
4229 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004230qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004232 qf_info_T *qi = &ql_info;
4233
4234 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004235 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004236 qi = wp->w_llist_ref;
4237
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004238 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239}
4240
4241/*
4242 * Update the cursor position in the quickfix window to the current error.
4243 * Return TRUE if there is a quickfix window.
4244 */
4245 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004246qf_win_pos_update(
4247 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004248 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249{
4250 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004251 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004253 // Put the cursor on the current error in the quickfix window, so that
4254 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004255 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 if (win != NULL
4257 && qf_index <= win->w_buffer->b_ml.ml_line_count
4258 && old_qf_index != qf_index)
4259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (qf_index > old_qf_index)
4261 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004262 win->w_redraw_top = old_qf_index;
4263 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 else
4266 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004267 win->w_redraw_top = qf_index;
4268 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004270 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272 return win != NULL;
4273}
4274
4275/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004276 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004277 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004278 */
4279 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004280is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004281{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004282 // A window displaying the quickfix buffer will have the w_llist_ref field
4283 // set to NULL.
4284 // A window displaying a location list buffer will have the w_llist_ref
4285 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004286 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004287 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4288 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004289 return TRUE;
4290
4291 return FALSE;
4292}
4293
4294/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004295 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004296 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004297 */
4298 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004299qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004300{
4301 win_T *win;
4302
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004303 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004304 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004305 return win;
4306 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004307}
4308
4309/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004310 * Find a quickfix buffer.
4311 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 */
4313 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004314qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004316 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004317 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004319 if (qi->qf_bufnr != INVALID_QFBUFNR)
4320 {
4321 buf_T *qfbuf;
4322 qfbuf = buflist_findnr(qi->qf_bufnr);
4323 if (qfbuf != NULL)
4324 return qfbuf;
4325 // buffer is no longer present
4326 qi->qf_bufnr = INVALID_QFBUFNR;
4327 }
4328
Bram Moolenaar9c102382006-05-03 21:26:49 +00004329 FOR_ALL_TAB_WINDOWS(tp, win)
4330 if (is_qf_win(win, qi))
4331 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004332
Bram Moolenaar9c102382006-05-03 21:26:49 +00004333 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334}
4335
4336/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004337 * Update the w:quickfix_title variable in the quickfix/location list window
4338 */
4339 static void
4340qf_update_win_titlevar(qf_info_T *qi)
4341{
4342 win_T *win;
4343 win_T *curwin_save;
4344
4345 if ((win = qf_find_win(qi)) != NULL)
4346 {
4347 curwin_save = curwin;
4348 curwin = win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004349 qf_set_title_var(qf_get_curlist(qi));
Bram Moolenaard823fa92016-08-12 16:29:27 +02004350 curwin = curwin_save;
4351 }
4352}
4353
4354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 * Find the quickfix buffer. If it exists, update the contents.
4356 */
4357 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004358qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359{
4360 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004361 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004364 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004365 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 if (buf != NULL)
4367 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004368 linenr_T old_line_count = buf->b_ml.ml_line_count;
4369
4370 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004371 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004372 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
Bram Moolenaard823fa92016-08-12 16:29:27 +02004374 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004375
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004376 qf_fill_buffer(qf_get_curlist(qi), buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004377 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004378
Bram Moolenaar864293a2016-06-02 13:40:04 +02004379 if (old_last == NULL)
4380 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004381 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004382
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004383 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004384 aucmd_restbuf(&aco);
4385 }
4386
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004387 // Only redraw when added lines are visible. This avoids flickering
4388 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004389 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4390 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 }
4392}
4393
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004394/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004395 * Add an error line to the quickfix buffer.
4396 */
4397 static int
4398qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4399{
4400 int len;
4401 buf_T *errbuf;
4402
4403 if (qfp->qf_module != NULL)
4404 {
4405 STRCPY(IObuff, qfp->qf_module);
4406 len = (int)STRLEN(IObuff);
4407 }
4408 else if (qfp->qf_fnum != 0
4409 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4410 && errbuf->b_fname != NULL)
4411 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004412 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004413 STRCPY(IObuff, gettail(errbuf->b_fname));
4414 else
4415 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004416 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004417 if (errbuf->b_sfname == NULL
4418 || mch_isFullName(errbuf->b_sfname))
4419 {
4420 if (*dirname == NUL)
4421 mch_dirname(dirname, MAXPATHL);
4422 shorten_buf_fname(errbuf, dirname, FALSE);
4423 }
4424 STRCPY(IObuff, errbuf->b_fname);
4425 }
4426 len = (int)STRLEN(IObuff);
4427 }
4428 else
4429 len = 0;
4430 IObuff[len++] = '|';
4431
4432 if (qfp->qf_lnum > 0)
4433 {
4434 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4435 len += (int)STRLEN(IObuff + len);
4436
4437 if (qfp->qf_col > 0)
4438 {
4439 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4440 len += (int)STRLEN(IObuff + len);
4441 }
4442
4443 sprintf((char *)IObuff + len, "%s",
4444 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4445 len += (int)STRLEN(IObuff + len);
4446 }
4447 else if (qfp->qf_pattern != NULL)
4448 {
4449 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4450 len += (int)STRLEN(IObuff + len);
4451 }
4452 IObuff[len++] = '|';
4453 IObuff[len++] = ' ';
4454
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004455 // Remove newlines and leading whitespace from the text.
4456 // For an unrecognized line keep the indent, the compiler may
4457 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004458 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4459 IObuff + len, IOSIZE - len);
4460
4461 if (ml_append_buf(buf, lnum, IObuff,
4462 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4463 return FAIL;
4464
4465 return OK;
4466}
4467
4468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 * Fill current buffer with quickfix errors, replacing any previous contents.
4470 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004471 * If "old_last" is not NULL append the items after this one.
4472 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4473 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 */
4475 static void
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004476qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004478 linenr_T lnum;
4479 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004480 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
Bram Moolenaar864293a2016-06-02 13:40:04 +02004482 if (old_last == NULL)
4483 {
4484 if (buf != curbuf)
4485 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004486 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004487 return;
4488 }
4489
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004490 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004491 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4492 (void)ml_delete((linenr_T)1, FALSE);
4493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004495 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004496 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004498 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004499
4500 *dirname = NUL;
4501
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004502 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004503 if (old_last == NULL)
4504 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004505 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004506 lnum = 0;
4507 }
4508 else
4509 {
4510 qfp = old_last->qf_next;
4511 lnum = buf->b_ml.ml_line_count;
4512 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004513 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004515 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004517
Bram Moolenaar864293a2016-06-02 13:40:04 +02004518 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004520 if (qfp == NULL)
4521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004523
4524 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004525 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004526 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004529 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 check_lnums(TRUE);
4531
Bram Moolenaar864293a2016-06-02 13:40:04 +02004532 if (old_last == NULL)
4533 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004534 // Set the 'filetype' to "qf" each time after filling the buffer.
4535 // This resembles reading a file into a buffer, it's more logical when
4536 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004537 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004538 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4539 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004541 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004542 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004544 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004546 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004547 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004548
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004549 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004550 redraw_curbuf_later(NOT_VALID);
4551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004553 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 KeyTyped = old_KeyTyped;
4555}
4556
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004557/*
4558 * For every change made to the quickfix list, update the changed tick.
4559 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004560 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004561qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004562{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004563 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004564}
4565
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004567 * Return the quickfix/location list number with the given identifier.
4568 * Returns -1 if list is not found.
4569 */
4570 static int
4571qf_id2nr(qf_info_T *qi, int_u qfid)
4572{
4573 int qf_idx;
4574
4575 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4576 if (qi->qf_lists[qf_idx].qf_id == qfid)
4577 return qf_idx;
4578 return INVALID_QFIDX;
4579}
4580
4581/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004582 * If the current list is not "save_qfid" and we can find the list with that ID
4583 * then make it the current list.
4584 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004585 * Returns OK if successfully restored the list. Returns FAIL if the list with
4586 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004587 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004588 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004589qf_restore_list(qf_info_T *qi, int_u save_qfid)
4590{
4591 int curlist;
4592
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004593 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004594 {
4595 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004596 if (curlist < 0)
4597 // list is not present
4598 return FAIL;
4599 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004600 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004601 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004602}
4603
4604/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004605 * Jump to the first entry if there is one.
4606 */
4607 static void
4608qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4609{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004610 if (qf_restore_list(qi, save_qfid) == FAIL)
4611 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004612
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004613 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004614 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004615 qf_jump(qi, 0, 0, forceit);
4616}
4617
4618/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004619 * Return TRUE when using ":vimgrep" for ":grep".
4620 */
4621 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004622grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004623{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004624 return ((cmdidx == CMD_grep
4625 || cmdidx == CMD_lgrep
4626 || cmdidx == CMD_grepadd
4627 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004628 && STRCMP("internal",
4629 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4630}
4631
4632/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004633 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004635 static char_u *
4636make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004638 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004639 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004640 case CMD_make: return (char_u *)"make";
4641 case CMD_lmake: return (char_u *)"lmake";
4642 case CMD_grep: return (char_u *)"grep";
4643 case CMD_lgrep: return (char_u *)"lgrep";
4644 case CMD_grepadd: return (char_u *)"grepadd";
4645 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4646 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648}
4649
4650/*
4651 * Return the name for the errorfile, in allocated memory.
4652 * Find a new unique name when 'makeef' contains "##".
4653 * Returns NULL for error.
4654 */
4655 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004656get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
4658 char_u *p;
4659 char_u *name;
4660 static int start = -1;
4661 static int off = 0;
4662#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004663 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664#endif
4665
4666 if (*p_mef == NUL)
4667 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004668 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004670 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 return name;
4672 }
4673
4674 for (p = p_mef; *p; ++p)
4675 if (p[0] == '#' && p[1] == '#')
4676 break;
4677
4678 if (*p == NUL)
4679 return vim_strsave(p_mef);
4680
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004681 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 for (;;)
4683 {
4684 if (start == -1)
4685 start = mch_get_pid();
4686 else
4687 off += 19;
4688
4689 name = alloc((unsigned)STRLEN(p_mef) + 30);
4690 if (name == NULL)
4691 break;
4692 STRCPY(name, p_mef);
4693 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4694 STRCAT(name, p + 2);
4695 if (mch_getperm(name) < 0
4696#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004697 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 && mch_lstat((char *)name, &sb) < 0
4699#endif
4700 )
4701 break;
4702 vim_free(name);
4703 }
4704 return name;
4705}
4706
4707/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004708 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4709 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4710 */
4711 static char_u *
4712make_get_fullcmd(char_u *makecmd, char_u *fname)
4713{
4714 char_u *cmd;
4715 unsigned len;
4716
4717 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4718 if (*p_sp != NUL)
4719 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4720 cmd = alloc(len);
4721 if (cmd == NULL)
4722 return NULL;
4723 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4724 (char *)p_shq);
4725
4726 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4727 if (*p_sp != NUL)
4728 append_redir(cmd, len, p_sp, fname);
4729
4730 // Display the fully formed command. Output a newline if there's something
4731 // else than the :make command that was typed (in which case the cursor is
4732 // in column 0).
4733 if (msg_col == 0)
4734 msg_didout = FALSE;
4735 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004736 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004737 msg_outtrans(cmd); // show what we are doing
4738
4739 return cmd;
4740}
4741
4742/*
4743 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4744 */
4745 void
4746ex_make(exarg_T *eap)
4747{
4748 char_u *fname;
4749 char_u *cmd;
4750 char_u *enc = NULL;
4751 win_T *wp = NULL;
4752 qf_info_T *qi = &ql_info;
4753 int res;
4754 char_u *au_name = NULL;
4755 int_u save_qfid;
4756
4757 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4758 if (grep_internal(eap->cmdidx))
4759 {
4760 ex_vimgrep(eap);
4761 return;
4762 }
4763
4764 au_name = make_get_auname(eap->cmdidx);
4765 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4766 curbuf->b_fname, TRUE, curbuf))
4767 {
4768#ifdef FEAT_EVAL
4769 if (aborting())
4770 return;
4771#endif
4772 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004773 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004774
4775 if (is_loclist_cmd(eap->cmdidx))
4776 wp = curwin;
4777
4778 autowrite_all();
4779 fname = get_mef_name();
4780 if (fname == NULL)
4781 return;
4782 mch_remove(fname); // in case it's not unique
4783
4784 cmd = make_get_fullcmd(eap->arg, fname);
4785 if (cmd == NULL)
4786 return;
4787
4788 // let the shell know if we are redirecting output or not
4789 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4790
4791#ifdef AMIGA
4792 out_flush();
4793 // read window status report and redraw before message
4794 (void)char_avail();
4795#endif
4796
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004797 incr_quickfix_busy();
4798
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004799 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4800 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4801 (eap->cmdidx != CMD_grepadd
4802 && eap->cmdidx != CMD_lgrepadd),
4803 qf_cmdtitle(*eap->cmdlinep), enc);
4804 if (wp != NULL)
4805 {
4806 qi = GET_LOC_LIST(wp);
4807 if (qi == NULL)
4808 goto cleanup;
4809 }
4810 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004811 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004812
4813 // Remember the current quickfix list identifier, so that we can
4814 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004815 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004816 if (au_name != NULL)
4817 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4818 curbuf->b_fname, TRUE, curbuf);
4819 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4820 // display the first error
4821 qf_jump_first(qi, save_qfid, FALSE);
4822
4823cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004824 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004825 mch_remove(fname);
4826 vim_free(fname);
4827 vim_free(cmd);
4828}
4829
4830/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004831 * Returns the number of valid entries in the current quickfix/location list.
4832 */
4833 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004834qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004835{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004836 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004837 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004838 qfline_T *qfp;
4839 int i, sz = 0;
4840 int prev_fnum = 0;
4841
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004842 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4843 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004844
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004845 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01004846 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004847 {
4848 if (qfp->qf_valid)
4849 {
4850 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004851 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004852 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4853 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004854 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004855 sz++;
4856 prev_fnum = qfp->qf_fnum;
4857 }
4858 }
4859 }
4860
4861 return sz;
4862}
4863
4864/*
4865 * Returns the current index of the quickfix/location list.
4866 * Returns 0 if there is an error.
4867 */
4868 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004869qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004870{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004871 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004872
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004873 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4874 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004875
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004876 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004877}
4878
4879/*
4880 * Returns the current index in the quickfix/location list (counting only valid
4881 * entries). If no valid entries are in the list, then returns 1.
4882 */
4883 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004884qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004885{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004886 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004887 qf_list_T *qfl;
4888 qfline_T *qfp;
4889 int i, eidx = 0;
4890 int prev_fnum = 0;
4891
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004892 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4893 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004894
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004895 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004896 qfp = qfl->qf_start;
4897
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004898 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004899 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004900 return 1;
4901
4902 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4903 {
4904 if (qfp->qf_valid)
4905 {
4906 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4907 {
4908 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4909 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004910 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004911 eidx++;
4912 prev_fnum = qfp->qf_fnum;
4913 }
4914 }
4915 else
4916 eidx++;
4917 }
4918 }
4919
4920 return eidx ? eidx : 1;
4921}
4922
4923/*
4924 * Get the 'n'th valid error entry in the quickfix or location list.
4925 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4926 * For :cdo and :ldo returns the 'n'th valid error entry.
4927 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4928 */
4929 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004930qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004931{
Bram Moolenaar95946f12019-03-31 15:31:59 +02004932 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004933 int i, eidx;
4934 int prev_fnum = 0;
4935
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004936 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004937 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004938 return 1;
4939
Bram Moolenaar95946f12019-03-31 15:31:59 +02004940 eidx = 0;
4941 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004942 {
4943 if (qfp->qf_valid)
4944 {
4945 if (fdo)
4946 {
4947 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4948 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004949 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004950 eidx++;
4951 prev_fnum = qfp->qf_fnum;
4952 }
4953 }
4954 else
4955 eidx++;
4956 }
4957
4958 if (eidx == n)
4959 break;
4960 }
4961
4962 if (i <= qfl->qf_count)
4963 return i;
4964 else
4965 return 1;
4966}
4967
4968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004970 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004971 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 */
4973 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004974ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004976 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004977 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004978
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004979 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4980 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004981
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004982 if (eap->addr_count > 0)
4983 errornr = (int)eap->line2;
4984 else
4985 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004986 switch (eap->cmdidx)
4987 {
4988 case CMD_cc: case CMD_ll:
4989 errornr = 0;
4990 break;
4991 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4992 case CMD_lfirst:
4993 errornr = 1;
4994 break;
4995 default:
4996 errornr = 32767;
4997 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004998 }
4999
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005000 // For cdo and ldo commands, jump to the nth valid error.
5001 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005002 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5003 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005004 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005005 eap->addr_count > 0 ? (int)eap->line1 : 1,
5006 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5007
5008 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009}
5010
5011/*
5012 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005013 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005014 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 */
5016 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005017ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005019 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005020 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005021 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005022
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005023 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5024 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005025
Bram Moolenaar55b69262017-08-13 13:42:01 +02005026 if (eap->addr_count > 0
5027 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5028 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005029 errornr = (int)eap->line2;
5030 else
5031 errornr = 1;
5032
Bram Moolenaar39665952018-08-15 20:59:48 +02005033 // Depending on the command jump to either next or previous entry/file.
5034 switch (eap->cmdidx)
5035 {
5036 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5037 dir = FORWARD;
5038 break;
5039 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5040 case CMD_lNext:
5041 dir = BACKWARD;
5042 break;
5043 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5044 dir = FORWARD_FILE;
5045 break;
5046 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5047 dir = BACKWARD_FILE;
5048 break;
5049 default:
5050 dir = FORWARD;
5051 break;
5052 }
5053
5054 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055}
5056
5057/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005058 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5059 * The index of the entry is stored in 'errornr'.
5060 * Returns NULL if an entry is not found.
5061 */
5062 static qfline_T *
5063qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5064{
5065 qfline_T *qfp = NULL;
5066 int idx = 0;
5067
5068 // Find the first entry in this file
5069 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5070 if (qfp->qf_fnum == bnr)
5071 break;
5072
5073 *errornr = idx;
5074 return qfp;
5075}
5076
5077/*
5078 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5079 * with the error number for the first entry. Assumes the entries are sorted in
5080 * the quickfix list by line number.
5081 */
5082 static qfline_T *
5083qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5084{
5085 while (!got_int
5086 && entry->qf_prev != NULL
5087 && entry->qf_fnum == entry->qf_prev->qf_fnum
5088 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5089 {
5090 entry = entry->qf_prev;
5091 --*errornr;
5092 }
5093
5094 return entry;
5095}
5096
5097/*
5098 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5099 * with the error number for the last entry. Assumes the entries are sorted in
5100 * the quickfix list by line number.
5101 */
5102 static qfline_T *
5103qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5104{
5105 while (!got_int &&
5106 entry->qf_next != NULL
5107 && entry->qf_fnum == entry->qf_next->qf_fnum
5108 && entry->qf_lnum == entry->qf_next->qf_lnum)
5109 {
5110 entry = entry->qf_next;
5111 ++*errornr;
5112 }
5113
5114 return entry;
5115}
5116
5117/*
5118 * Find the first quickfix entry below line 'lnum' in buffer 'bnr'.
5119 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5120 * index of the very first entry in the quickfix list.
5121 * Returns NULL if an entry is not found after 'lnum'.
5122 */
5123 static qfline_T *
5124qf_find_entry_on_next_line(
5125 int bnr,
5126 linenr_T lnum,
5127 qfline_T *qfp,
5128 int *errornr)
5129{
5130 if (qfp->qf_lnum > lnum)
5131 // First entry is after line 'lnum'
5132 return qfp;
5133
5134 // Find the entry just before or at the line 'lnum'
5135 while (qfp->qf_next != NULL
5136 && qfp->qf_next->qf_fnum == bnr
5137 && qfp->qf_next->qf_lnum <= lnum)
5138 {
5139 qfp = qfp->qf_next;
5140 ++*errornr;
5141 }
5142
5143 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
5144 // No entries found after 'lnum'
5145 return NULL;
5146
5147 // Use the entry just after line 'lnum'
5148 qfp = qfp->qf_next;
5149 ++*errornr;
5150
5151 return qfp;
5152}
5153
5154/*
5155 * Find the first quickfix entry before line 'lnum' in buffer 'bnr'.
5156 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5157 * index of the very first entry in the quickfix list.
5158 * Returns NULL if an entry is not found before 'lnum'.
5159 */
5160 static qfline_T *
5161qf_find_entry_on_prev_line(
5162 int bnr,
5163 linenr_T lnum,
5164 qfline_T *qfp,
5165 int *errornr)
5166{
5167 // Find the entry just before the line 'lnum'
5168 while (qfp->qf_next != NULL
5169 && qfp->qf_next->qf_fnum == bnr
5170 && qfp->qf_next->qf_lnum < lnum)
5171 {
5172 qfp = qfp->qf_next;
5173 ++*errornr;
5174 }
5175
5176 if (qfp->qf_lnum >= lnum) // entry is after 'lnum'
5177 return NULL;
5178
5179 // If multiple entries are on the same line, then use the first entry
5180 qfp = qf_find_first_entry_on_line(qfp, errornr);
5181
5182 return qfp;
5183}
5184
5185/*
5186 * Find a quickfix entry in 'qfl' closest to line 'lnum' in buffer 'bnr' in
5187 * the direction 'dir'.
5188 */
5189 static qfline_T *
5190qf_find_closest_entry(
5191 qf_list_T *qfl,
5192 int bnr,
5193 linenr_T lnum,
5194 int dir,
5195 int *errornr)
5196{
5197 qfline_T *qfp;
5198
5199 *errornr = 0;
5200
5201 // Find the first entry in this file
5202 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5203 if (qfp == NULL)
5204 return NULL; // no entry in this file
5205
5206 if (dir == FORWARD)
5207 qfp = qf_find_entry_on_next_line(bnr, lnum, qfp, errornr);
5208 else
5209 qfp = qf_find_entry_on_prev_line(bnr, lnum, qfp, errornr);
5210
5211 return qfp;
5212}
5213
5214/*
5215 * Get the nth quickfix entry below the specified entry treating multiple
5216 * entries on a single line as one. Searches forward in the list.
5217 */
5218 static qfline_T *
5219qf_get_nth_below_entry(qfline_T *entry, int *errornr, int n)
5220{
5221 while (n-- > 0 && !got_int)
5222 {
5223 qfline_T *first_entry = entry;
5224 int first_errornr = *errornr;
5225
5226 // Treat all the entries on the same line in this file as one
5227 entry = qf_find_last_entry_on_line(entry, errornr);
5228
5229 if (entry->qf_next == NULL
5230 || entry->qf_next->qf_fnum != entry->qf_fnum)
5231 {
5232 // If multiple entries are on the same line, then use the first
5233 // entry
5234 entry = first_entry;
5235 *errornr = first_errornr;
5236 break;
5237 }
5238
5239 entry = entry->qf_next;
5240 ++*errornr;
5241 }
5242
5243 return entry;
5244}
5245
5246/*
5247 * Get the nth quickfix entry above the specified entry treating multiple
5248 * entries on a single line as one. Searches backwards in the list.
5249 */
5250 static qfline_T *
5251qf_get_nth_above_entry(qfline_T *entry, int *errornr, int n)
5252{
5253 while (n-- > 0 && !got_int)
5254 {
5255 if (entry->qf_prev == NULL
5256 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5257 break;
5258
5259 entry = entry->qf_prev;
5260 --*errornr;
5261
5262 // If multiple entries are on the same line, then use the first entry
5263 entry = qf_find_first_entry_on_line(entry, errornr);
5264 }
5265
5266 return entry;
5267}
5268
5269/*
5270 * Find the n'th quickfix entry adjacent to line 'lnum' in buffer 'bnr' in the
5271 * specified direction.
5272 * Returns the error number in the quickfix list or 0 if an entry is not found.
5273 */
5274 static int
5275qf_find_nth_adj_entry(qf_list_T *qfl, int bnr, linenr_T lnum, int n, int dir)
5276{
5277 qfline_T *adj_entry;
5278 int errornr;
5279
5280 // Find an entry closest to the specified line
5281 adj_entry = qf_find_closest_entry(qfl, bnr, lnum, dir, &errornr);
5282 if (adj_entry == NULL)
5283 return 0;
5284
5285 if (--n > 0)
5286 {
5287 // Go to the n'th entry in the current buffer
5288 if (dir == FORWARD)
5289 adj_entry = qf_get_nth_below_entry(adj_entry, &errornr, n);
5290 else
5291 adj_entry = qf_get_nth_above_entry(adj_entry, &errornr, n);
5292 }
5293
5294 return errornr;
5295}
5296
5297/*
5298 * Jump to a quickfix entry in the current file nearest to the current line.
5299 * ":cabove", ":cbelow", ":labove" and ":lbelow" commands
5300 */
5301 void
5302ex_cbelow(exarg_T *eap)
5303{
5304 qf_info_T *qi;
5305 qf_list_T *qfl;
5306 int dir;
5307 int buf_has_flag;
5308 int errornr = 0;
5309
5310 if (eap->addr_count > 0 && eap->line2 <= 0)
5311 {
5312 emsg(_(e_invrange));
5313 return;
5314 }
5315
5316 // Check whether the current buffer has any quickfix entries
5317 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow)
5318 buf_has_flag = BUF_HAS_QF_ENTRY;
5319 else
5320 buf_has_flag = BUF_HAS_LL_ENTRY;
5321 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5322 {
5323 emsg(_(e_quickfix));
5324 return;
5325 }
5326
5327 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5328 return;
5329
5330 qfl = qf_get_curlist(qi);
5331 // check if the list has valid errors
5332 if (!qf_list_has_valid_entries(qfl))
5333 {
5334 emsg(_(e_quickfix));
5335 return;
5336 }
5337
5338 if (eap->cmdidx == CMD_cbelow || eap->cmdidx == CMD_lbelow)
5339 dir = FORWARD;
5340 else
5341 dir = BACKWARD;
5342
5343 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, curwin->w_cursor.lnum,
5344 eap->addr_count > 0 ? eap->line2 : 0, dir);
5345
5346 if (errornr > 0)
5347 qf_jump(qi, 0, errornr, FALSE);
5348 else
5349 emsg(_(e_no_more_items));
5350}
5351
5352/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005353 * Return the autocmd name for the :cfile Ex commands
5354 */
5355 static char_u *
5356cfile_get_auname(cmdidx_T cmdidx)
5357{
5358 switch (cmdidx)
5359 {
5360 case CMD_cfile: return (char_u *)"cfile";
5361 case CMD_cgetfile: return (char_u *)"cgetfile";
5362 case CMD_caddfile: return (char_u *)"caddfile";
5363 case CMD_lfile: return (char_u *)"lfile";
5364 case CMD_lgetfile: return (char_u *)"lgetfile";
5365 case CMD_laddfile: return (char_u *)"laddfile";
5366 default: return NULL;
5367 }
5368}
5369
5370/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005371 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005372 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 */
5374 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005375ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005377 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005378 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005379 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005380 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005381 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005382 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005383
Bram Moolenaara16123a2019-03-28 20:31:07 +01005384 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005385 if (au_name != NULL)
5386 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005387
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005388 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005389#ifdef FEAT_BROWSE
5390 if (cmdmod.browse)
5391 {
5392 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005393 NULL, NULL,
5394 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005395 if (browse_file == NULL)
5396 return;
5397 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5398 vim_free(browse_file);
5399 }
5400 else
5401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005403 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005404
Bram Moolenaar39665952018-08-15 20:59:48 +02005405 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005406 wp = curwin;
5407
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005408 incr_quickfix_busy();
5409
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005410 // This function is used by the :cfile, :cgetfile and :caddfile
5411 // commands.
5412 // :cfile always creates a new quickfix list and jumps to the
5413 // first error.
5414 // :cgetfile creates a new quickfix list but doesn't jump to the
5415 // first error.
5416 // :caddfile adds to an existing quickfix list. If there is no
5417 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005418 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005419 && eap->cmdidx != CMD_laddfile),
5420 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005421 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005422 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005423 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005424 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005425 {
5426 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005427 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005428 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005429 }
5430 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005431 qf_list_changed(qf_get_curlist(qi));
5432 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005433 if (au_name != NULL)
5434 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005435
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005436 // Jump to the first error for a new list and if autocmds didn't
5437 // free the list.
5438 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5439 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005440 // display the first error
5441 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005442
5443 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005444}
5445
5446/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005447 * Return the vimgrep autocmd name.
5448 */
5449 static char_u *
5450vgr_get_auname(cmdidx_T cmdidx)
5451{
5452 switch (cmdidx)
5453 {
5454 case CMD_vimgrep: return (char_u *)"vimgrep";
5455 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5456 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5457 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5458 case CMD_grep: return (char_u *)"grep";
5459 case CMD_lgrep: return (char_u *)"lgrep";
5460 case CMD_grepadd: return (char_u *)"grepadd";
5461 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5462 default: return NULL;
5463 }
5464}
5465
5466/*
5467 * Initialize the regmatch used by vimgrep for pattern "s".
5468 */
5469 static void
5470vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5471{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005472 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005473 regmatch->regprog = NULL;
5474
5475 if (s == NULL || *s == NUL)
5476 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005477 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005478 if (last_search_pat() == NULL)
5479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005480 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005481 return;
5482 }
5483 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5484 }
5485 else
5486 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5487
5488 regmatch->rmm_ic = p_ic;
5489 regmatch->rmm_maxcol = 0;
5490}
5491
5492/*
5493 * Display a file name when vimgrep is running.
5494 */
5495 static void
5496vgr_display_fname(char_u *fname)
5497{
5498 char_u *p;
5499
5500 msg_start();
5501 p = msg_strtrunc(fname, TRUE);
5502 if (p == NULL)
5503 msg_outtrans(fname);
5504 else
5505 {
5506 msg_outtrans(p);
5507 vim_free(p);
5508 }
5509 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005510 msg_didout = FALSE; // overwrite this message
5511 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005512 msg_col = 0;
5513 out_flush();
5514}
5515
5516/*
5517 * Load a dummy buffer to search for a pattern using vimgrep.
5518 */
5519 static buf_T *
5520vgr_load_dummy_buf(
5521 char_u *fname,
5522 char_u *dirname_start,
5523 char_u *dirname_now)
5524{
5525 int save_mls;
5526#if defined(FEAT_SYN_HL)
5527 char_u *save_ei = NULL;
5528#endif
5529 buf_T *buf;
5530
5531#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005532 // Don't do Filetype autocommands to avoid loading syntax and
5533 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005534 save_ei = au_event_disable(",Filetype");
5535#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005536 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005537 save_mls = p_mls;
5538 p_mls = 0;
5539
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005540 // Load file into a buffer, so that 'fileencoding' is detected,
5541 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005542 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5543
5544 p_mls = save_mls;
5545#if defined(FEAT_SYN_HL)
5546 au_event_restore(save_ei);
5547#endif
5548
5549 return buf;
5550}
5551
5552/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005553 * Check whether a quickfix/location list is valid. Autocmds may remove or
5554 * change a quickfix list when vimgrep is running. If the list is not found,
5555 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005556 */
5557 static int
5558vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005559 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005560 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005561 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005562 char_u *title)
5563{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005564 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005565 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005566 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005567 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005568 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005569 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005570 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005571 return FALSE;
5572 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005573 else
5574 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005575 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005576 qf_new_list(qi, title);
5577 return TRUE;
5578 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005579 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005580
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005581 if (qf_restore_list(qi, qfid) == FAIL)
5582 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005583
5584 return TRUE;
5585}
5586
5587/*
5588 * Search for a pattern in all the lines in a buffer and add the matching lines
5589 * to a quickfix list.
5590 */
5591 static int
5592vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005593 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005594 char_u *fname,
5595 buf_T *buf,
5596 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005597 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005598 int duplicate_name,
5599 int flags)
5600{
5601 int found_match = FALSE;
5602 long lnum;
5603 colnr_T col;
5604
Bram Moolenaar1c299432018-10-28 14:36:09 +01005605 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005606 {
5607 col = 0;
5608 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5609 col, NULL, NULL) > 0)
5610 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005611 // Pass the buffer number so that it gets used even for a
5612 // dummy buffer, unless duplicate_name is set, then the
5613 // buffer will be wiped out below.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005614 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005615 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005616 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005617 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005618 duplicate_name ? 0 : buf->b_fnum,
5619 ml_get_buf(buf,
5620 regmatch->startpos[0].lnum + lnum, FALSE),
5621 regmatch->startpos[0].lnum + lnum,
5622 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005623 FALSE, // vis_col
5624 NULL, // search pattern
5625 0, // nr
5626 0, // type
5627 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02005628 ) == QF_FAIL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005629 {
5630 got_int = TRUE;
5631 break;
5632 }
5633 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005634 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005635 break;
5636 if ((flags & VGR_GLOBAL) == 0
5637 || regmatch->endpos[0].lnum > 0)
5638 break;
5639 col = regmatch->endpos[0].col
5640 + (col == regmatch->endpos[0].col);
5641 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5642 break;
5643 }
5644 line_breakcheck();
5645 if (got_int)
5646 break;
5647 }
5648
5649 return found_match;
5650}
5651
5652/*
5653 * Jump to the first match and update the directory.
5654 */
5655 static void
5656vgr_jump_to_match(
5657 qf_info_T *qi,
5658 int forceit,
5659 int *redraw_for_dummy,
5660 buf_T *first_match_buf,
5661 char_u *target_dir)
5662{
5663 buf_T *buf;
5664
5665 buf = curbuf;
5666 qf_jump(qi, 0, 0, forceit);
5667 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005668 // If we jumped to another buffer redrawing will already be
5669 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005670 *redraw_for_dummy = FALSE;
5671
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005672 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005673 if (curbuf == first_match_buf && target_dir != NULL)
5674 {
5675 exarg_T ea;
5676
5677 ea.arg = target_dir;
5678 ea.cmdidx = CMD_lcd;
5679 ex_cd(&ea);
5680 }
5681}
5682
5683/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005684 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005685 * ":vimgrepadd {pattern} file(s)"
5686 * ":lvimgrep {pattern} file(s)"
5687 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005688 */
5689 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005690ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005691{
Bram Moolenaar81695252004-12-29 20:58:21 +00005692 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005693 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005694 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005695 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005696 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005697 char_u *s;
5698 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005699 int fi;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005700 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005701 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005702 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005703 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005704 buf_T *buf;
5705 int duplicate_name = FALSE;
5706 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005707 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005708 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005709 buf_T *first_match_buf = NULL;
5710 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005711 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005712 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005713 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005714 char_u *dirname_start = NULL;
5715 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005716 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005717 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005718
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005719 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005720 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5721 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005722 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005723#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005724 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005725 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005726#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005727 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005728
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005729 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
5730 if (qi == NULL)
5731 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00005732
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005733 if (eap->addr_count > 0)
5734 tomatch = eap->line2;
5735 else
5736 tomatch = MAXLNUM;
5737
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005738 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005739 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005740 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005741 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005742 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005744 emsg(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005745 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005746 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005747
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005748 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005749 if (regmatch.regprog == NULL)
5750 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005751
5752 p = skipwhite(p);
5753 if (*p == NUL)
5754 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005755 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005756 goto theend;
5757 }
5758
Bram Moolenaar55b69262017-08-13 13:42:01 +02005759 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005760 && eap->cmdidx != CMD_vimgrepadd
5761 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005762 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005763 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005764 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005765
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005766 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005767 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005768 goto theend;
5769 if (fcount == 0)
5770 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005771 emsg(_(e_nomatch));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005772 goto theend;
5773 }
5774
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005775 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5776 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005777 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005778 {
5779 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005780 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005781 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005782
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005783 // Remember the current directory, because a BufRead autocommand that does
5784 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005785 mch_dirname(dirname_start, MAXPATHL);
5786
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005787 incr_quickfix_busy();
5788
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005789 // Remember the current quickfix list identifier, so that we can check for
5790 // autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005791 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005792
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005793 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005794 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005795 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005796 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005797 if (time(NULL) > seconds)
5798 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005799 // Display the file name every second or so, show the user we are
5800 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005801 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005802 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005803 }
5804
Bram Moolenaar81695252004-12-29 20:58:21 +00005805 buf = buflist_findname_exp(fnames[fi]);
5806 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5807 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005808 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005809 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005810 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005811 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005812
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005813 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005814 }
5815 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005816 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005817 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005818
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005819 // Check whether the quickfix list is still valid. When loading a
5820 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005821 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005822 {
5823 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005824 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005825 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005826 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005827 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005828
Bram Moolenaar81695252004-12-29 20:58:21 +00005829 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005830 {
5831 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005832 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005833 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005834 else
5835 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005836 // Try for a match in all lines of the buffer.
5837 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005838 found_match = vgr_match_buflines(qf_get_curlist(qi),
5839 fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005840 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005841
Bram Moolenaar81695252004-12-29 20:58:21 +00005842 if (using_dummy)
5843 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005844 if (found_match && first_match_buf == NULL)
5845 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005846 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005847 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005848 // Never keep a dummy buffer if there is another buffer
5849 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005850 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005851 buf = NULL;
5852 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005853 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005854 || buf->b_p_bh[0] == 'u' // "unload"
5855 || buf->b_p_bh[0] == 'w' // "wipe"
5856 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005857 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005858 // When no match was found we don't need to remember the
5859 // buffer, wipe it out. If there was a match and it
5860 // wasn't the first one or we won't jump there: only
5861 // unload the buffer.
5862 // Ignore 'hidden' here, because it may lead to having too
5863 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005864 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005865 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005866 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005867 buf = NULL;
5868 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005869 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005870 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005871 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005872 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005873 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005874 buf = NULL;
5875 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005876 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005877
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005878 if (buf != NULL)
5879 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005880 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005881 buf->b_flags &= ~BF_DUMMY;
5882
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005883 // If the buffer is still loaded we need to use the
5884 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005885 if (buf == first_match_buf
5886 && target_dir == NULL
5887 && STRCMP(dirname_start, dirname_now) != 0)
5888 target_dir = vim_strsave(dirname_now);
5889
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005890 // The buffer is still loaded, the Filetype autocommands
5891 // need to be done now, in that buffer. And the modelines
5892 // need to be done (again). But not the window-local
5893 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005894 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005895#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005896 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5897 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005898#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005899 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005900 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005901 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005902 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005903 }
5904 }
5905
5906 FreeWild(fcount, fnames);
5907
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005908 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005909 qfl->qf_nonevalid = FALSE;
5910 qfl->qf_ptr = qfl->qf_start;
5911 qfl->qf_index = 1;
5912 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005913
Bram Moolenaar864293a2016-06-02 13:40:04 +02005914 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005915
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005916 if (au_name != NULL)
5917 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5918 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005919 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5920 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005921 if (!qflist_valid(wp, save_qfid)
5922 || qf_restore_list(qi, save_qfid) == FAIL)
5923 {
5924 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005925 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005926 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005927
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005928 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01005929 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005930 {
5931 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005932 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5933 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005934 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005935 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005936 semsg(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005937
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005938 decr_quickfix_busy();
5939
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005940 // If we loaded a dummy buffer into the current window, the autocommands
5941 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005942 if (redraw_for_dummy)
5943 {
5944#ifdef FEAT_FOLDING
5945 foldUpdateAll(curwin);
5946#else
5947 redraw_later(NOT_VALID);
5948#endif
5949 }
5950
Bram Moolenaar86b68352004-12-27 21:59:20 +00005951theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005952 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005953 vim_free(dirname_now);
5954 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005955 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005956 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005957}
5958
5959/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005960 * Restore current working directory to "dirname_start" if they differ, taking
5961 * into account whether it is set locally or globally.
5962 */
5963 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005964restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005965{
5966 char_u *dirname_now = alloc(MAXPATHL);
5967
5968 if (NULL != dirname_now)
5969 {
5970 mch_dirname(dirname_now, MAXPATHL);
5971 if (STRCMP(dirname_start, dirname_now) != 0)
5972 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005973 // If the directory has changed, change it back by building up an
5974 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005975 exarg_T ea;
5976
5977 ea.arg = dirname_start;
5978 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5979 ex_cd(&ea);
5980 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005981 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005982 }
5983}
5984
5985/*
5986 * Load file "fname" into a dummy buffer and return the buffer pointer,
5987 * placing the directory resulting from the buffer load into the
5988 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5989 * prior to calling this function. Restores directory to "dirname_start" prior
5990 * to returning, if autocmds or the 'autochdir' option have changed it.
5991 *
5992 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5993 * or wipe_dummy_buffer() later!
5994 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005995 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005996 */
5997 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005998load_dummy_buffer(
5999 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006000 char_u *dirname_start, // in: old directory
6001 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006002{
6003 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006004 bufref_T newbufref;
6005 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006006 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006007 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006008 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006009
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006010 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006011 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6012 if (newbuf == NULL)
6013 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006014 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006015
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006016 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006017 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6018
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006019 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006020 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006021 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006022 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006023 ++newbuf->b_locked;
6024
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006025 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006026 aucmd_prepbuf(&aco, newbuf);
6027
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006028 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006029 (void)setfname(curbuf, fname, NULL, FALSE);
6030
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006031 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006032 check_need_swap(TRUE);
6033
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006034 // Remove the "dummy" flag, otherwise autocommands may not
6035 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006036 curbuf->b_flags &= ~BF_DUMMY;
6037
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006038 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006039 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006040 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006041 NULL, READ_NEW | READ_DUMMY);
6042 --newbuf->b_locked;
6043 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006044 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006045 && !(curbuf->b_flags & BF_NEW))
6046 {
6047 failed = FALSE;
6048 if (curbuf != newbuf)
6049 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006050 // Bloody autocommands changed the buffer! Can happen when
6051 // using netrw and editing a remote file. Use the current
6052 // buffer instead, delete the dummy one after restoring the
6053 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006054 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006055 newbuf = curbuf;
6056 }
6057 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006058
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006059 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006060 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006061 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6062 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006063
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006064 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6065 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006066 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006067 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006068
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006069 // When autocommands/'autochdir' option changed directory: go back.
6070 // Let the caller know what the resulting dir was first, in case it is
6071 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006072 mch_dirname(resulting_dir, MAXPATHL);
6073 restore_start_dir(dirname_start);
6074
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006075 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006076 return NULL;
6077 if (failed)
6078 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006079 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006080 return NULL;
6081 }
6082 return newbuf;
6083}
6084
6085/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006086 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6087 * directory to "dirname_start" prior to returning, if autocmds or the
6088 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006089 */
6090 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006091wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006092{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006093 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006094 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006095#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006096 cleanup_T cs;
6097
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006098 // Reset the error/interrupt/exception state here so that aborting()
6099 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6100 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006101 enter_cleanup(&cs);
6102#endif
6103
Bram Moolenaar81695252004-12-29 20:58:21 +00006104 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006105
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006106#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006107 // Restore the error/interrupt/exception state if not discarded by a
6108 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006109 leave_cleanup(&cs);
6110#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006111 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006112 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006113 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006114}
6115
6116/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006117 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6118 * directory to "dirname_start" prior to returning, if autocmds or the
6119 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006120 */
6121 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006122unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006123{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006124 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006125 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006126 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006127
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006128 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006129 restore_start_dir(dirname_start);
6130 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006131}
6132
Bram Moolenaar05159a02005-02-26 23:04:13 +00006133#if defined(FEAT_EVAL) || defined(PROTO)
6134/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01006135 * Copy the specified quickfix entry items into a new dict and appened the dict
6136 * to 'list'. Returns OK on success.
6137 */
6138 static int
6139get_qfline_items(qfline_T *qfp, list_T *list)
6140{
6141 int bufnum;
6142 dict_T *dict;
6143 char_u buf[2];
6144
6145 // Handle entries with a non-existing buffer number.
6146 bufnum = qfp->qf_fnum;
6147 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6148 bufnum = 0;
6149
6150 if ((dict = dict_alloc()) == NULL)
6151 return FAIL;
6152 if (list_append_dict(list, dict) == FAIL)
6153 return FAIL;
6154
6155 buf[0] = qfp->qf_type;
6156 buf[1] = NUL;
6157 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
6158 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6159 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6160 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6161 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
6162 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6163 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6164 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6165 || dict_add_string(dict, "type", buf) == FAIL
6166 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6167 return FAIL;
6168
6169 return OK;
6170}
6171
6172/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006173 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006174 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006175 */
6176 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006177get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006178{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006179 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006180 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006181 qfline_T *qfp;
6182 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006183
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006184 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006185 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006186 qi = &ql_info;
6187 if (wp != NULL)
6188 {
6189 qi = GET_LOC_LIST(wp);
6190 if (qi == NULL)
6191 return FAIL;
6192 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006193 }
6194
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006195 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006196 qf_idx = qi->qf_curlist;
6197
Bram Moolenaar0398e002019-03-21 21:12:49 +01006198 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006199 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006200
Bram Moolenaar0398e002019-03-21 21:12:49 +01006201 qfl = qf_get_list(qi, qf_idx);
6202 if (qf_list_empty(qfl))
6203 return FAIL;
6204
Bram Moolenaara16123a2019-03-28 20:31:07 +01006205 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006206 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01006207 if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006208 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006209 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006210
Bram Moolenaar05159a02005-02-26 23:04:13 +00006211 return OK;
6212}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006213
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006214// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006215enum {
6216 QF_GETLIST_NONE = 0x0,
6217 QF_GETLIST_TITLE = 0x1,
6218 QF_GETLIST_ITEMS = 0x2,
6219 QF_GETLIST_NR = 0x4,
6220 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006221 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006222 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006223 QF_GETLIST_IDX = 0x40,
6224 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006225 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006226 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006227 QF_GETLIST_QFBUFNR = 0x400,
6228 QF_GETLIST_ALL = 0x7FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006229};
6230
6231/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006232 * Parse text from 'di' and return the quickfix list items.
6233 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006234 */
6235 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006236qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006237{
6238 int status = FAIL;
6239 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006240 char_u *errorformat = p_efm;
6241 dictitem_T *efm_di;
6242 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006243
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006244 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006245 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006246 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006247 // If errorformat is supplied then use it, otherwise use the 'efm'
6248 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006249 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6250 {
6251 if (efm_di->di_tv.v_type != VAR_STRING ||
6252 efm_di->di_tv.vval.v_string == NULL)
6253 return FAIL;
6254 errorformat = efm_di->di_tv.vval.v_string;
6255 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006256
Bram Moolenaar36538222017-09-02 19:51:44 +02006257 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006258 if (l == NULL)
6259 return FAIL;
6260
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006261 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006262 if (qi != NULL)
6263 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006264 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006265 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6266 {
Bram Moolenaarda732532017-08-31 20:58:02 +02006267 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006268 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006269 }
6270 free(qi);
6271 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006272 dict_add_list(retdict, "items", l);
6273 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006274 }
6275
6276 return status;
6277}
6278
6279/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006280 * Return the quickfix/location list window identifier in the current tabpage.
6281 */
6282 static int
6283qf_winid(qf_info_T *qi)
6284{
6285 win_T *win;
6286
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006287 // The quickfix window can be opened even if the quickfix list is not set
6288 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006289 if (qi == NULL)
6290 return 0;
6291 win = qf_find_win(qi);
6292 if (win != NULL)
6293 return win->w_id;
6294 return 0;
6295}
6296
6297/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006298 * Returns the number of the buffer displayed in the quickfix/location list
6299 * window. If there is no buffer associated with the list, then returns 0.
6300 */
6301 static int
6302qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6303{
6304 return dict_add_number(retdict, "qfbufnr",
6305 (qi == NULL) ? 0 : qi->qf_bufnr);
6306}
6307
6308/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006309 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006310 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006311 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006312qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006313{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006314 int flags = QF_GETLIST_NONE;
6315
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006316 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006317 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006318 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006319 if (!loclist)
6320 // File window ID is applicable only to location list windows
6321 flags &= ~ QF_GETLIST_FILEWINID;
6322 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006323
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006324 if (dict_find(what, (char_u *)"title", -1) != NULL)
6325 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006326
Bram Moolenaara6d48492017-12-12 22:45:31 +01006327 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6328 flags |= QF_GETLIST_NR;
6329
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006330 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6331 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006332
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006333 if (dict_find(what, (char_u *)"context", -1) != NULL)
6334 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006335
Bram Moolenaara6d48492017-12-12 22:45:31 +01006336 if (dict_find(what, (char_u *)"id", -1) != NULL)
6337 flags |= QF_GETLIST_ID;
6338
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006339 if (dict_find(what, (char_u *)"items", -1) != NULL)
6340 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006341
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006342 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6343 flags |= QF_GETLIST_IDX;
6344
6345 if (dict_find(what, (char_u *)"size", -1) != NULL)
6346 flags |= QF_GETLIST_SIZE;
6347
Bram Moolenaarb254af32017-12-18 19:48:58 +01006348 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6349 flags |= QF_GETLIST_TICK;
6350
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006351 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6352 flags |= QF_GETLIST_FILEWINID;
6353
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006354 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6355 flags |= QF_GETLIST_QFBUFNR;
6356
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006357 return flags;
6358}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006359
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006360/*
6361 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6362 * If 'nr' and 'id' are not present in 'what' then return the current
6363 * quickfix list index.
6364 * If 'nr' is zero then return the current quickfix list index.
6365 * If 'nr' is '$' then return the last quickfix list index.
6366 * If 'id' is present then return the index of the quickfix list with that id.
6367 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6368 * Return -1, if quickfix list is not present or if the stack is empty.
6369 */
6370 static int
6371qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6372{
6373 int qf_idx;
6374 dictitem_T *di;
6375
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006376 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006377 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6378 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006379 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006380 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006381 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006382 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006383 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006384 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006385 qf_idx = di->di_tv.vval.v_number - 1;
6386 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006387 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006388 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006389 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006390 else if (di->di_tv.v_type == VAR_STRING
6391 && di->di_tv.vval.v_string != NULL
6392 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006393 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006394 qf_idx = qi->qf_listcount - 1;
6395 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006396 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006397 }
6398
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006399 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6400 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006401 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006402 if (di->di_tv.v_type == VAR_NUMBER)
6403 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006404 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006405 if (di->di_tv.vval.v_number != 0)
6406 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6407 }
6408 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006409 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006410 }
6411
6412 return qf_idx;
6413}
6414
6415/*
6416 * Return default values for quickfix list properties in retdict.
6417 */
6418 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006419qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006420{
6421 int status = OK;
6422
6423 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006424 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006425 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6426 {
6427 list_T *l = list_alloc();
6428 if (l != NULL)
6429 status = dict_add_list(retdict, "items", l);
6430 else
6431 status = FAIL;
6432 }
6433 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006434 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006435 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006436 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006437 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006438 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006439 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006440 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006441 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006442 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006443 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006444 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006445 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006446 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006447 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006448 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006449 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6450 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006451
6452 return status;
6453}
6454
6455/*
6456 * Return the quickfix list title as 'title' in retdict
6457 */
6458 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006459qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006460{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006461 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006462}
6463
6464/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006465 * Returns the identifier of the window used to display files from a location
6466 * list. If there is no associated window, then returns 0. Useful only when
6467 * called from a location list window.
6468 */
6469 static int
6470qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6471{
6472 int winid = 0;
6473
6474 if (wp != NULL && IS_LL_WINDOW(wp))
6475 {
6476 win_T *ll_wp = qf_find_win_with_loclist(qi);
6477 if (ll_wp != NULL)
6478 winid = ll_wp->w_id;
6479 }
6480
6481 return dict_add_number(retdict, "filewinid", winid);
6482}
6483
6484/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006485 * Return the quickfix list items/entries as 'items' in retdict
6486 */
6487 static int
6488qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6489{
6490 int status = OK;
6491 list_T *l = list_alloc();
6492 if (l != NULL)
6493 {
6494 (void)get_errorlist(qi, NULL, qf_idx, l);
6495 dict_add_list(retdict, "items", l);
6496 }
6497 else
6498 status = FAIL;
6499
6500 return status;
6501}
6502
6503/*
6504 * Return the quickfix list context (if any) as 'context' in retdict.
6505 */
6506 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006507qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006508{
6509 int status;
6510 dictitem_T *di;
6511
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006512 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006513 {
6514 di = dictitem_alloc((char_u *)"context");
6515 if (di != NULL)
6516 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006517 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006518 status = dict_add(retdict, di);
6519 if (status == FAIL)
6520 dictitem_free(di);
6521 }
6522 else
6523 status = FAIL;
6524 }
6525 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006526 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006527
6528 return status;
6529}
6530
6531/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006532 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006533 */
6534 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01006535qf_getprop_idx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006536{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006537 int curidx = qfl->qf_index;
6538 if (qf_list_empty(qfl))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006539 // For empty lists, current index is set to 0
6540 curidx = 0;
6541 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006542}
6543
6544/*
6545 * Return quickfix/location list details (title) as a
6546 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6547 * then current list is used. Otherwise the specified list is used.
6548 */
6549 int
6550qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6551{
6552 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006553 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006554 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006555 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006556 dictitem_T *di;
6557 int flags = QF_GETLIST_NONE;
6558
6559 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6560 return qf_get_list_from_lines(what, di, retdict);
6561
6562 if (wp != NULL)
6563 qi = GET_LOC_LIST(wp);
6564
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006565 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006566
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006567 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006568 qf_idx = qf_getprop_qfidx(qi, what);
6569
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006570 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006571 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006572 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006573
Bram Moolenaar0398e002019-03-21 21:12:49 +01006574 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006575
Bram Moolenaard823fa92016-08-12 16:29:27 +02006576 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006577 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006578 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006579 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006580 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006581 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006582 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006583 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006584 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006585 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006586 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006587 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006588 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar0398e002019-03-21 21:12:49 +01006589 status = qf_getprop_idx(qfl, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006590 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006591 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006592 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006593 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006594 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6595 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006596 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6597 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006598
Bram Moolenaard823fa92016-08-12 16:29:27 +02006599 return status;
6600}
6601
6602/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006603 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006604 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6605 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006606 */
6607 static int
6608qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01006609 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006610 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006611 int first_entry,
6612 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006613{
6614 static int did_bufnr_emsg;
6615 char_u *filename, *module, *pattern, *text, *type;
6616 int bufnum, valid, status, col, vcol, nr;
6617 long lnum;
6618
6619 if (first_entry)
6620 did_bufnr_emsg = FALSE;
6621
Bram Moolenaar8f667172018-12-14 15:38:31 +01006622 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6623 module = dict_get_string(d, (char_u *)"module", TRUE);
6624 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6625 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6626 col = (int)dict_get_number(d, (char_u *)"col");
6627 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6628 nr = (int)dict_get_number(d, (char_u *)"nr");
6629 type = dict_get_string(d, (char_u *)"type", TRUE);
6630 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6631 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006632 if (text == NULL)
6633 text = vim_strsave((char_u *)"");
6634
6635 valid = TRUE;
6636 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6637 valid = FALSE;
6638
6639 // Mark entries with non-existing buffer number as not valid. Give the
6640 // error message only once.
6641 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6642 {
6643 if (!did_bufnr_emsg)
6644 {
6645 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006646 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006647 }
6648 valid = FALSE;
6649 bufnum = 0;
6650 }
6651
6652 // If the 'valid' field is present it overrules the detected value.
6653 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006654 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006655
Bram Moolenaar0398e002019-03-21 21:12:49 +01006656 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006657 NULL, // dir
6658 filename,
6659 module,
6660 bufnum,
6661 text,
6662 lnum,
6663 col,
6664 vcol, // vis_col
6665 pattern, // search pattern
6666 nr,
6667 type == NULL ? NUL : *type,
6668 valid);
6669
6670 vim_free(filename);
6671 vim_free(module);
6672 vim_free(pattern);
6673 vim_free(text);
6674 vim_free(type);
6675
Bram Moolenaar9752c722018-12-22 16:49:34 +01006676 if (valid)
6677 *valid_entry = TRUE;
6678
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006679 return status;
6680}
6681
6682/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006683 * Add list of entries to quickfix/location list. Each list entry is
6684 * a dictionary with item information.
6685 */
6686 static int
6687qf_add_entries(
6688 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006689 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006690 list_T *list,
6691 char_u *title,
6692 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006693{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006694 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006695 listitem_T *li;
6696 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006697 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006698 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006699 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006700
Bram Moolenaara3921f42017-06-04 15:30:34 +02006701 if (action == ' ' || qf_idx == qi->qf_listcount)
6702 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006703 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006704 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006705 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006706 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006707 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01006708 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006709 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006710 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006711 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006712 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006713 qf_free_items(qfl);
6714 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006715 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006716
6717 for (li = list->lv_first; li != NULL; li = li->li_next)
6718 {
6719 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006720 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006721
6722 d = li->li_tv.vval.v_dict;
6723 if (d == NULL)
6724 continue;
6725
Bram Moolenaar0398e002019-03-21 21:12:49 +01006726 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006727 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02006728 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006729 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006730 }
6731
Bram Moolenaar9752c722018-12-22 16:49:34 +01006732 // Check if any valid error entries are added to the list.
6733 if (valid_entry)
6734 qfl->qf_nonevalid = FALSE;
6735 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006736 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006737 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006738
6739 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006740 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006741 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006742
6743 // Update the current error index if not appending to the list or if the
6744 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006745 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01006746 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006747
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006748 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006749 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006750
6751 return retval;
6752}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006753
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006754/*
6755 * Get the quickfix list index from 'nr' or 'id'
6756 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006757 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006758qf_setprop_get_qfidx(
6759 qf_info_T *qi,
6760 dict_T *what,
6761 int action,
6762 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006763{
6764 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006765 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006766
Bram Moolenaard823fa92016-08-12 16:29:27 +02006767 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6768 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006769 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006770 if (di->di_tv.v_type == VAR_NUMBER)
6771 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006772 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006773 if (di->di_tv.vval.v_number != 0)
6774 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006775
Bram Moolenaar55b69262017-08-13 13:42:01 +02006776 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6777 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006778 // When creating a new list, accept qf_idx pointing to the next
6779 // non-available list and add the new list at the end of the
6780 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006781 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006782 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006783 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006784 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006785 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006786 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006787 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006788 }
6789 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006790 && di->di_tv.vval.v_string != NULL
6791 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006792 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006793 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006794 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006795 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006796 qf_idx = 0;
6797 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006798 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006799 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006800 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006801 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006802 }
6803
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006804 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006805 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006806 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006807 if (di->di_tv.v_type != VAR_NUMBER)
6808 return INVALID_QFIDX;
6809
6810 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006811 }
6812
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006813 return qf_idx;
6814}
6815
6816/*
6817 * Set the quickfix list title.
6818 */
6819 static int
6820qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6821{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006822 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006823
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006824 if (di->di_tv.v_type != VAR_STRING)
6825 return FAIL;
6826
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006827 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006828 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006829 if (qf_idx == qi->qf_curlist)
6830 qf_update_win_titlevar(qi);
6831
6832 return OK;
6833}
6834
6835/*
6836 * Set quickfix list items/entries.
6837 */
6838 static int
6839qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6840{
6841 int retval = FAIL;
6842 char_u *title_save;
6843
6844 if (di->di_tv.v_type != VAR_LIST)
6845 return FAIL;
6846
6847 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6848 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6849 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006850 vim_free(title_save);
6851
6852 return retval;
6853}
6854
6855/*
6856 * Set quickfix list items/entries from a list of lines.
6857 */
6858 static int
6859qf_setprop_items_from_lines(
6860 qf_info_T *qi,
6861 int qf_idx,
6862 dict_T *what,
6863 dictitem_T *di,
6864 int action)
6865{
6866 char_u *errorformat = p_efm;
6867 dictitem_T *efm_di;
6868 int retval = FAIL;
6869
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006870 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006871 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6872 {
6873 if (efm_di->di_tv.v_type != VAR_STRING ||
6874 efm_di->di_tv.vval.v_string == NULL)
6875 return FAIL;
6876 errorformat = efm_di->di_tv.vval.v_string;
6877 }
6878
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006879 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006880 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6881 return FAIL;
6882
6883 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006884 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006885 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6886 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6887 retval = OK;
6888
6889 return retval;
6890}
6891
6892/*
6893 * Set quickfix list context.
6894 */
6895 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006896qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006897{
6898 typval_T *ctx;
6899
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006900 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006901 ctx = alloc_tv();
6902 if (ctx != NULL)
6903 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006904 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006905
6906 return OK;
6907}
6908
6909/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006910 * Set the current index in the specified quickfix list
6911 */
6912 static int
6913qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
6914{
6915 int denote = FALSE;
6916 int newidx;
6917 int old_qfidx;
6918 qfline_T *qf_ptr;
6919
6920 // If the specified index is '$', then use the last entry
6921 if (di->di_tv.v_type == VAR_STRING
6922 && di->di_tv.vval.v_string != NULL
6923 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
6924 newidx = qfl->qf_count;
6925 else
6926 {
6927 // Otherwise use the specified index
6928 newidx = tv_get_number_chk(&di->di_tv, &denote);
6929 if (denote)
6930 return FAIL;
6931 }
6932
6933 if (newidx < 1) // sanity check
6934 return FAIL;
6935 if (newidx > qfl->qf_count)
6936 newidx = qfl->qf_count;
6937
6938 old_qfidx = qfl->qf_index;
6939 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
6940 if (qf_ptr == NULL)
6941 return FAIL;
6942 qfl->qf_ptr = qf_ptr;
6943 qfl->qf_index = newidx;
6944
6945 // If the current list is modified and it is displayed in the quickfix
6946 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006947 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006948 qf_win_pos_update(qi, old_qfidx);
6949
6950 return OK;
6951}
6952
6953/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006954 * Set quickfix/location list properties (title, items, context).
6955 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006956 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006957 */
6958 static int
6959qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6960{
6961 dictitem_T *di;
6962 int retval = FAIL;
6963 int qf_idx;
6964 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006965 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006966
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006967 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006968 newlist = TRUE;
6969
6970 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006971 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006972 return FAIL;
6973
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006974 if (newlist)
6975 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006976 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006977 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006978 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006979 }
6980
Bram Moolenaar0398e002019-03-21 21:12:49 +01006981 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006982 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006983 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006984 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006985 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006986 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006987 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006988 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006989 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006990 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6991 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006992
Bram Moolenaarb254af32017-12-18 19:48:58 +01006993 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006994 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006995
Bram Moolenaard823fa92016-08-12 16:29:27 +02006996 return retval;
6997}
6998
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006999/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007000 * Free the entire quickfix/location list stack.
7001 * If the quickfix/location list window is open, then clear it.
7002 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007003 static void
7004qf_free_stack(win_T *wp, qf_info_T *qi)
7005{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007006 win_T *qfwin = qf_find_win(qi);
7007 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007008
7009 if (qfwin != NULL)
7010 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007011 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007012 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007013 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007014 qf_update_buffer(qi, NULL);
7015 }
7016
7017 if (wp != NULL && IS_LL_WINDOW(wp))
7018 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007019 // If in the location list window, then use the non-location list
7020 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007021 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007022 if (llwin != NULL)
7023 wp = llwin;
7024 }
7025
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007026 qf_free_all(wp);
7027 if (wp == NULL)
7028 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007029 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007030 qi->qf_curlist = 0;
7031 qi->qf_listcount = 0;
7032 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007033 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007034 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007035 // If the location list window is open, then create a new empty
7036 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007037 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007038
Bram Moolenaar95946f12019-03-31 15:31:59 +02007039 if (new_ll != NULL)
7040 {
7041 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007042
Bram Moolenaar95946f12019-03-31 15:31:59 +02007043 // first free the list reference in the location list window
7044 ll_free_all(&qfwin->w_llist_ref);
7045
7046 qfwin->w_llist_ref = new_ll;
7047 if (wp != qfwin)
7048 win_set_loclist(wp, new_ll);
7049 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007050 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007051}
7052
Bram Moolenaard823fa92016-08-12 16:29:27 +02007053/*
7054 * Populate the quickfix list with the items supplied in the list
7055 * of dictionaries. "title" will be copied to w:quickfix_title.
7056 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
7057 */
7058 int
7059set_errorlist(
7060 win_T *wp,
7061 list_T *list,
7062 int action,
7063 char_u *title,
7064 dict_T *what)
7065{
7066 qf_info_T *qi = &ql_info;
7067 int retval = OK;
7068
7069 if (wp != NULL)
7070 {
7071 qi = ll_get_or_alloc_list(wp);
7072 if (qi == NULL)
7073 return FAIL;
7074 }
7075
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007076 if (action == 'f')
7077 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007078 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007079 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007080 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007081 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007082
7083 incr_quickfix_busy();
7084
7085 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007086 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007087 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007088 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007089 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007090 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007091 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007092 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007093
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007094 decr_quickfix_busy();
7095
Bram Moolenaard823fa92016-08-12 16:29:27 +02007096 return retval;
7097}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007098
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007099/*
7100 * Mark the context as in use for all the lists in a quickfix stack.
7101 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007102 static int
7103mark_quickfix_ctx(qf_info_T *qi, int copyID)
7104{
7105 int i;
7106 int abort = FALSE;
7107 typval_T *ctx;
7108
7109 for (i = 0; i < LISTCOUNT && !abort; ++i)
7110 {
7111 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007112 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7113 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007114 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
7115 }
7116
7117 return abort;
7118}
7119
7120/*
7121 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007122 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007123 */
7124 int
7125set_ref_in_quickfix(int copyID)
7126{
7127 int abort = FALSE;
7128 tabpage_T *tp;
7129 win_T *win;
7130
7131 abort = mark_quickfix_ctx(&ql_info, copyID);
7132 if (abort)
7133 return abort;
7134
7135 FOR_ALL_TAB_WINDOWS(tp, win)
7136 {
7137 if (win->w_llist != NULL)
7138 {
7139 abort = mark_quickfix_ctx(win->w_llist, copyID);
7140 if (abort)
7141 return abort;
7142 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007143 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7144 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007145 // In a location list window and none of the other windows is
7146 // referring to this location list. Mark the location list
7147 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007148 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7149 if (abort)
7150 return abort;
7151 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007152 }
7153
7154 return abort;
7155}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007156#endif
7157
Bram Moolenaar81695252004-12-29 20:58:21 +00007158/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007159 * Return the autocmd name for the :cbuffer Ex commands
7160 */
7161 static char_u *
7162cbuffer_get_auname(cmdidx_T cmdidx)
7163{
7164 switch (cmdidx)
7165 {
7166 case CMD_cbuffer: return (char_u *)"cbuffer";
7167 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7168 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7169 case CMD_lbuffer: return (char_u *)"lbuffer";
7170 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7171 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7172 default: return NULL;
7173 }
7174}
7175
7176/*
7177 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7178 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7179 */
7180 static int
7181cbuffer_process_args(
7182 exarg_T *eap,
7183 buf_T **bufp,
7184 linenr_T *line1,
7185 linenr_T *line2)
7186{
7187 buf_T *buf = NULL;
7188
7189 if (*eap->arg == NUL)
7190 buf = curbuf;
7191 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7192 buf = buflist_findnr(atoi((char *)eap->arg));
7193
7194 if (buf == NULL)
7195 {
7196 emsg(_(e_invarg));
7197 return FAIL;
7198 }
7199
7200 if (buf->b_ml.ml_mfp == NULL)
7201 {
7202 emsg(_("E681: Buffer is not loaded"));
7203 return FAIL;
7204 }
7205
7206 if (eap->addr_count == 0)
7207 {
7208 eap->line1 = 1;
7209 eap->line2 = buf->b_ml.ml_line_count;
7210 }
7211
7212 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7213 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7214 {
7215 emsg(_(e_invrange));
7216 return FAIL;
7217 }
7218
7219 *line1 = eap->line1;
7220 *line2 = eap->line2;
7221 *bufp = buf;
7222
7223 return OK;
7224}
7225
7226/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007227 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007228 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007229 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007230 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007231 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007232 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007233 */
7234 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007235ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007236{
7237 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007238 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007239 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007240 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007241 int_u save_qfid;
7242 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007243 char_u *qf_title;
7244 linenr_T line1;
7245 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007246
Bram Moolenaara16123a2019-03-28 20:31:07 +01007247 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007248 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007249 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007250 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007251#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007252 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007253 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007254#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007255 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007256
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007257 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007258 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7259 if (qi == NULL)
7260 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007261
Bram Moolenaara16123a2019-03-28 20:31:07 +01007262 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7263 return;
7264
7265 qf_title = qf_cmdtitle(*eap->cmdlinep);
7266
7267 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007268 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007269 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7270 (char *)qf_title, (char *)buf->b_sfname);
7271 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007272 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007273
7274 incr_quickfix_busy();
7275
7276 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7277 (eap->cmdidx != CMD_caddbuffer
7278 && eap->cmdidx != CMD_laddbuffer),
7279 line1, line2,
7280 qf_title, NULL);
7281 if (qf_stack_empty(qi))
7282 {
7283 decr_quickfix_busy();
7284 return;
7285 }
7286 if (res >= 0)
7287 qf_list_changed(qf_get_curlist(qi));
7288
7289 // Remember the current quickfix list identifier, so that we can
7290 // check for autocommands changing the current quickfix list.
7291 save_qfid = qf_get_curlist(qi)->qf_id;
7292 if (au_name != NULL)
7293 {
7294 buf_T *curbuf_old = curbuf;
7295
7296 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7297 TRUE, curbuf);
7298 if (curbuf != curbuf_old)
7299 // Autocommands changed buffer, don't jump now, "qi" may
7300 // be invalid.
7301 res = 0;
7302 }
7303 // Jump to the first error for a new list and if autocmds didn't
7304 // free the list.
7305 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7306 eap->cmdidx == CMD_lbuffer)
7307 && qflist_valid(wp, save_qfid))
7308 // display the first error
7309 qf_jump_first(qi, save_qfid, eap->forceit);
7310
7311 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007312}
7313
Bram Moolenaar1e015462005-09-25 22:16:38 +00007314#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007315/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007316 * Return the autocmd name for the :cexpr Ex commands.
7317 */
7318 static char_u *
7319cexpr_get_auname(cmdidx_T cmdidx)
7320{
7321 switch (cmdidx)
7322 {
7323 case CMD_cexpr: return (char_u *)"cexpr";
7324 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7325 case CMD_caddexpr: return (char_u *)"caddexpr";
7326 case CMD_lexpr: return (char_u *)"lexpr";
7327 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7328 case CMD_laddexpr: return (char_u *)"laddexpr";
7329 default: return NULL;
7330 }
7331}
7332
7333/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007334 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7335 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007336 */
7337 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007338ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007339{
7340 typval_T *tv;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007341 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007342 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007343 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007344 int_u save_qfid;
7345 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007346
Bram Moolenaara16123a2019-03-28 20:31:07 +01007347 au_name = cexpr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007348 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7349 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007350 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007351#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007352 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007353 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007354#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007355 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007356
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007357 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7358 if (qi == NULL)
7359 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007360
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007361 // Evaluate the expression. When the result is a string or a list we can
7362 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007363 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007364 if (tv != NULL)
7365 {
7366 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7367 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7368 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007369 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007370 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00007371 (eap->cmdidx != CMD_caddexpr
7372 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007373 (linenr_T)0, (linenr_T)0,
7374 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007375 if (qf_stack_empty(qi))
7376 {
7377 decr_quickfix_busy();
7378 goto cleanup;
7379 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01007380 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007381 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007382
7383 // Remember the current quickfix list identifier, so that we can
7384 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007385 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007386 if (au_name != NULL)
7387 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7388 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007389
7390 // Jump to the first error for a new list and if autocmds didn't
7391 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02007392 if (res > 0 && (eap->cmdidx == CMD_cexpr
7393 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007394 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02007395 // display the first error
7396 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007397 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00007398 }
7399 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007400 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007401cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00007402 free_tv(tv);
7403 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007404}
Bram Moolenaar1e015462005-09-25 22:16:38 +00007405#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007406
7407/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007408 * Get the location list for ":lhelpgrep"
7409 */
7410 static qf_info_T *
7411hgr_get_ll(int *new_ll)
7412{
7413 win_T *wp;
7414 qf_info_T *qi;
7415
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007416 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007417 if (bt_help(curwin->w_buffer))
7418 wp = curwin;
7419 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007420 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007421 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007422
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007423 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007424 qi = NULL;
7425 else
7426 qi = wp->w_llist;
7427
7428 if (qi == NULL)
7429 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007430 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007431 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007432 return NULL;
7433 *new_ll = TRUE;
7434 }
7435
7436 return qi;
7437}
7438
7439/*
7440 * Search for a pattern in a help file.
7441 */
7442 static void
7443hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007444 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007445 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007446 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007447 regmatch_T *p_regmatch)
7448{
7449 FILE *fd;
7450 long lnum;
7451
7452 fd = mch_fopen((char *)fname, "r");
7453 if (fd == NULL)
7454 return;
7455
7456 lnum = 1;
7457 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7458 {
7459 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007460
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007461 // Convert a line if 'encoding' is not utf-8 and
7462 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007463 if (p_vc->vc_type != CONV_NONE
7464 && has_non_ascii(IObuff))
7465 {
7466 line = string_convert(p_vc, IObuff, NULL);
7467 if (line == NULL)
7468 line = IObuff;
7469 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007470
7471 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7472 {
7473 int l = (int)STRLEN(line);
7474
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007475 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007476 while (l > 0 && line[l - 1] <= ' ')
7477 line[--l] = NUL;
7478
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007479 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007480 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007481 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007482 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007483 0,
7484 line,
7485 lnum,
7486 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007487 + 1, // col
7488 FALSE, // vis_col
7489 NULL, // search pattern
7490 0, // nr
7491 1, // type
7492 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02007493 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007494 {
7495 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007496 if (line != IObuff)
7497 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007498 break;
7499 }
7500 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007501 if (line != IObuff)
7502 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007503 ++lnum;
7504 line_breakcheck();
7505 }
7506 fclose(fd);
7507}
7508
7509/*
7510 * Search for a pattern in all the help files in the doc directory under
7511 * the given directory.
7512 */
7513 static void
7514hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007515 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007516 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007517 regmatch_T *p_regmatch,
7518 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007519#ifdef FEAT_MULTI_LANG
7520 , char_u *lang
7521#endif
7522 )
7523{
7524 int fcount;
7525 char_u **fnames;
7526 int fi;
7527
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007528 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007529 add_pathsep(dirname);
7530 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7531 if (gen_expand_wildcards(1, &dirname, &fcount,
7532 &fnames, EW_FILE|EW_SILENT) == OK
7533 && fcount > 0)
7534 {
7535 for (fi = 0; fi < fcount && !got_int; ++fi)
7536 {
7537#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007538 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007539 if (lang != NULL
7540 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007541 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007542 && !(STRNICMP(lang, "en", 2) == 0
7543 && STRNICMP("txt", fnames[fi]
7544 + STRLEN(fnames[fi]) - 3, 3) == 0))
7545 continue;
7546#endif
7547
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007548 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007549 }
7550 FreeWild(fcount, fnames);
7551 }
7552}
7553
7554/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007555 * Search for a pattern in all the help files in the 'runtimepath'
7556 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007557 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007558 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007559 */
7560 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007561hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007562{
7563 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007564
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007565 vimconv_T vc;
7566
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007567 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7568 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007569 vc.vc_type = CONV_NONE;
7570 if (!enc_utf8)
7571 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007572
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007573 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007574 p = p_rtp;
7575 while (*p != NUL && !got_int)
7576 {
7577 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7578
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007579 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007580#ifdef FEAT_MULTI_LANG
7581 , lang
7582#endif
7583 );
7584 }
7585
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007586 if (vc.vc_type != CONV_NONE)
7587 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007588}
7589
7590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 * ":helpgrep {pattern}"
7592 */
7593 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007594ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595{
7596 regmatch_T regmatch;
7597 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007598 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007599 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007600 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007601 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602
Bram Moolenaar73633f82012-01-20 13:39:07 +01007603 switch (eap->cmdidx)
7604 {
7605 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7606 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7607 default: break;
7608 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007609 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7610 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007611 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007612#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007613 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007614 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007615#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007616 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007617
Bram Moolenaar39665952018-08-15 20:59:48 +02007618 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007619 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007620 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007621 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007622 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007623 }
7624
Bram Moolenaara16123a2019-03-28 20:31:07 +01007625 // Make 'cpoptions' empty, the 'l' flag should not be used here.
7626 save_cpo = p_cpo;
7627 p_cpo = empty_option;
7628
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007629 incr_quickfix_busy();
7630
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007631#ifdef FEAT_MULTI_LANG
7632 // Check for a specified language
7633 lang = check_help_lang(eap->arg);
7634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7636 regmatch.rm_ic = FALSE;
7637 if (regmatch.regprog != NULL)
7638 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007639 qf_list_T *qfl;
7640
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007641 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007642 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007643 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007645 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007646
Bram Moolenaar473de612013-06-08 18:19:48 +02007647 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007649 qfl->qf_nonevalid = FALSE;
7650 qfl->qf_ptr = qfl->qf_start;
7651 qfl->qf_index = 1;
7652 qf_list_changed(qfl);
7653 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
7655
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007656 if (p_cpo == empty_option)
7657 p_cpo = save_cpo;
7658 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007659 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007660 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661
Bram Moolenaar73633f82012-01-20 13:39:07 +01007662 if (au_name != NULL)
7663 {
7664 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7665 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007666 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007667 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007668 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007669 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007670 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007671 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007672 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007673
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007674 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007675 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007676 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007677 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007678 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007679
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007680 decr_quickfix_busy();
7681
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007682 if (eap->cmdidx == CMD_lhelpgrep)
7683 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007684 // If the help window is not opened or if it already points to the
7685 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007686 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007687 {
7688 if (new_qi)
7689 ll_free_all(&qi);
7690 }
7691 else if (curwin->w_llist == NULL)
7692 curwin->w_llist = qi;
7693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694}
7695
7696#endif /* FEAT_QUICKFIX */