blob: f82a187166087d021b00e3dad7968ee80b4e5072 [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 Moolenaar5843f5f2019-08-20 20:13:45 +0200170static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100172static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static win_T *qf_find_win(qf_info_T *qi);
174static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200175static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +0100176static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
178static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
179static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
180static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar3ff33112019-05-03 21:56:35 +0200181static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200183// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000184#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200185// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000186#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200187
188// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100189#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
190#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
191#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
192#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200193
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000194/*
195 * Return location list for window 'wp'
196 * For location list window, return the referenced location list
197 */
198#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
199
Bram Moolenaar95946f12019-03-31 15:31:59 +0200200// Macro to loop through all the items in a quickfix list
201// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100202#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
Bram Moolenaar95946f12019-03-31 15:31:59 +0200203 for (i = 1, qfp = qfl->qf_start; \
204 !got_int && i <= qfl->qf_count && qfp != NULL; \
Bram Moolenaara16123a2019-03-28 20:31:07 +0100205 ++i, qfp = qfp->qf_next)
206
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200208 * Looking up a buffer can be slow if there are many. Remember the last one
209 * to make this a lot faster if there are multiple matches in the same file.
210 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200211static char_u *qf_last_bufname = NULL;
212static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200213
Bram Moolenaar3c097222017-12-21 20:54:49 +0100214static char *e_loc_list_changed =
215 N_("E926: Current location list was changed");
216
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200217/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200218 * Maximum number of bytes allowed per line while reading a errorfile.
219 */
220#define LINE_MAXLEN 4096
221
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200222static struct fmtpattern
223{
224 char_u convchar;
225 char *pattern;
226} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200227 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200228 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200229 {'n', "\\d\\+"},
230 {'l', "\\d\\+"},
231 {'c', "\\d\\+"},
232 {'t', "."},
233 {'m', ".\\+"},
234 {'r', ".*"},
235 {'p', "[- .]*"},
236 {'v', "\\d\\+"},
237 {'s', ".\\+"},
238 {'o', ".\\+"}
239 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200240
241/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200242 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200243 * See fmt_pat definition above for the list of supported patterns. The
244 * pattern specifier is supplied in "efmpat". The converted pattern is stored
245 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200246 */
247 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200248efmpat_to_regpat(
249 char_u *efmpat,
250 char_u *regpat,
251 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200252 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100253 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254{
255 char_u *srcptr;
256
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200257 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200258 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200259 // Each errorformat pattern can occur only once
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100260 semsg(_("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200261 return NULL;
262 }
263 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200264 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200265 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200266 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100268 semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200269 return NULL;
270 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200271 efminfo->addr[idx] = (char_u)++round;
272 *regpat++ = '\\';
273 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200274#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200275 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200276 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200277 // Also match "c:" in the file name, even when
278 // checking for a colon next: "%f:".
279 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200280 STRCPY(regpat, "\\%(\\a:\\)\\=");
281 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200282 }
283#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200284 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200285 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200286 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200287 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200288 // A file name may contain spaces, but this isn't
289 // in "\f". For "%f:%l:%m" there may be a ":" in
290 // the file name. Use ".\{-1,}x" instead (x is
291 // the next character), the requirement that :999:
292 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200293 STRCPY(regpat, ".\\{-1,}");
294 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200295 }
296 else
297 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200298 // File name followed by '\\' or '%': include as
299 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200300 STRCPY(regpat, "\\f\\+");
301 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200302 }
303 }
304 else
305 {
306 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200307 while ((*regpat = *srcptr++) != NUL)
308 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200309 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200310 *regpat++ = '\\';
311 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200312
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200313 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200314}
315
316/*
317 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200318 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200319 */
320 static char_u *
321scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200322 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200323 char_u *efm,
324 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100325 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200326{
327 char_u *efmp = *pefmp;
328
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200329 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200330 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200331 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200332 {
333 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200334 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200335 if (efmp < efm + len)
336 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200337 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200338 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200339 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200340 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200341 if (efmp == efm + len)
342 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100343 emsg(_("E374: Missing ] in format string"));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200344 return NULL;
345 }
346 }
347 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200348 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200349 *regpat++ = *++efmp;
350 *regpat++ = '\\';
351 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200352 }
353 else
354 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200355 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100356 semsg(_("E375: Unsupported %%%c in format string"), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200357 return NULL;
358 }
359
360 *pefmp = efmp;
361
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200362 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200363}
364
365/*
366 * Analyze/parse an errorformat prefix.
367 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200368 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100369efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200370{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200371 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200372 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200373 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200374 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200375 else
376 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100377 semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200378 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200379 }
380
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200381 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200382}
383
384/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200385 * Converts a 'errorformat' string part in 'efm' to a regular expression
386 * pattern. The resulting regex pattern is returned in "regpat". Additional
387 * information about the 'erroformat' pattern is returned in "fmt_ptr".
388 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200389 */
390 static int
391efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200392 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200393 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200394 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100395 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200396{
397 char_u *ptr;
398 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200399 int round;
400 int idx = 0;
401
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200402 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200403 ptr = regpat;
404 *ptr++ = '^';
405 round = 0;
406 for (efmp = efm; efmp < efm + len; ++efmp)
407 {
408 if (*efmp == '%')
409 {
410 ++efmp;
411 for (idx = 0; idx < FMT_PATTERNS; ++idx)
412 if (fmt_pat[idx].convchar == *efmp)
413 break;
414 if (idx < FMT_PATTERNS)
415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100416 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200417 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200418 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200419 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200420 }
421 else if (*efmp == '*')
422 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200423 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100424 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200425 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200426 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200427 }
428 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200429 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200430 else if (*efmp == '#')
431 *ptr++ = '*';
432 else if (*efmp == '>')
433 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200434 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200435 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200436 // prefix is allowed only at the beginning of the errorformat
437 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100438 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200439 if (efmp == NULL)
440 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200441 }
442 else
443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100444 semsg(_("E377: Invalid %%%c in format string"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200445 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200446 }
447 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200448 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200449 {
450 if (*efmp == '\\' && efmp + 1 < efm + len)
451 ++efmp;
452 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200453 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200454 if (*efmp)
455 *ptr++ = *efmp;
456 }
457 }
458 *ptr++ = '$';
459 *ptr = NUL;
460
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200461 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200462}
463
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200464/*
465 * Free the 'errorformat' information list
466 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200467 static void
468free_efm_list(efm_T **efm_first)
469{
470 efm_T *efm_ptr;
471
472 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
473 {
474 *efm_first = efm_ptr->next;
475 vim_regfree(efm_ptr->prog);
476 vim_free(efm_ptr);
477 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100478 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200479}
480
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200481/*
482 * Compute the size of the buffer used to convert a 'errorformat' pattern into
483 * a regular expression pattern.
484 */
485 static int
486efm_regpat_bufsz(char_u *efm)
487{
488 int sz;
489 int i;
490
491 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
492 for (i = FMT_PATTERNS; i > 0; )
493 sz += (int)STRLEN(fmt_pat[--i].pattern);
494#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200495 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200496#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200497 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200498#endif
499
500 return sz;
501}
502
503/*
504 * Return the length of a 'errorformat' option part (separated by ",").
505 */
506 static int
507efm_option_part_len(char_u *efm)
508{
509 int len;
510
511 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
512 if (efm[len] == '\\' && efm[len + 1] != NUL)
513 ++len;
514
515 return len;
516}
517
518/*
519 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
520 * are parsed and converted to regular expressions. Returns information about
521 * the parsed 'errorformat' option.
522 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200523 static efm_T *
524parse_efm_option(char_u *efm)
525{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200526 efm_T *fmt_ptr = NULL;
527 efm_T *fmt_first = NULL;
528 efm_T *fmt_last = NULL;
529 char_u *fmtstr = NULL;
530 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200531 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200532
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200533 // Each part of the format string is copied and modified from errorformat
534 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200535
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200536 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200537 sz = efm_regpat_bufsz(efm);
538 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200539 goto parse_efm_error;
540
541 while (efm[0] != NUL)
542 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200543 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200544 fmt_ptr = ALLOC_CLEAR_ONE(efm_T);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200545 if (fmt_ptr == NULL)
546 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200547 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200548 fmt_first = fmt_ptr;
549 else
550 fmt_last->next = fmt_ptr;
551 fmt_last = fmt_ptr;
552
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200553 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200554 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200555
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100556 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200557 goto parse_efm_error;
558 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
559 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200560 // Advance to next part
561 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200562 }
563
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200564 if (fmt_first == NULL) // nothing found
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100565 emsg(_("E378: 'errorformat' contains no pattern"));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200566
567 goto parse_efm_end;
568
569parse_efm_error:
570 free_efm_list(&fmt_first);
571
572parse_efm_end:
573 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200574
575 return fmt_first;
576}
577
Bram Moolenaare0d37972016-07-15 22:36:01 +0200578enum {
579 QF_FAIL = 0,
580 QF_OK = 1,
581 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200582 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200583 QF_IGNORE_LINE = 4,
584 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200585};
586
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200587/*
588 * State information used to parse lines and add entries to a quickfix/location
589 * list.
590 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200591typedef struct {
592 char_u *linebuf;
593 int linelen;
594 char_u *growbuf;
595 int growbufsiz;
596 FILE *fd;
597 typval_T *tv;
598 char_u *p_str;
599 listitem_T *p_li;
600 buf_T *buf;
601 linenr_T buflnum;
602 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100603 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200604} qfstate_T;
605
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200606/*
607 * Allocate more memory for the line buffer used for parsing lines.
608 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200609 static char_u *
610qf_grow_linebuf(qfstate_T *state, int newsz)
611{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200612 char_u *p;
613
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200614 // If the line exceeds LINE_MAXLEN exclude the last
615 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200616 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
617 if (state->growbuf == NULL)
618 {
619 state->growbuf = alloc(state->linelen + 1);
620 if (state->growbuf == NULL)
621 return NULL;
622 state->growbufsiz = state->linelen;
623 }
624 else if (state->linelen > state->growbufsiz)
625 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200626 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200627 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200628 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200629 state->growbufsiz = state->linelen;
630 }
631 return state->growbuf;
632}
633
634/*
635 * Get the next string (separated by newline) from state->p_str.
636 */
637 static int
638qf_get_next_str_line(qfstate_T *state)
639{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200640 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200641 char_u *p_str = state->p_str;
642 char_u *p;
643 int len;
644
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200645 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200646 return QF_END_OF_INPUT;
647
648 p = vim_strchr(p_str, '\n');
649 if (p != NULL)
650 len = (int)(p - p_str) + 1;
651 else
652 len = (int)STRLEN(p_str);
653
654 if (len > IOSIZE - 2)
655 {
656 state->linebuf = qf_grow_linebuf(state, len);
657 if (state->linebuf == NULL)
658 return QF_NOMEM;
659 }
660 else
661 {
662 state->linebuf = IObuff;
663 state->linelen = len;
664 }
665 vim_strncpy(state->linebuf, p_str, state->linelen);
666
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200667 // Increment using len in order to discard the rest of the
668 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200669 p_str += len;
670 state->p_str = p_str;
671
672 return QF_OK;
673}
674
675/*
676 * Get the next string from state->p_Li.
677 */
678 static int
679qf_get_next_list_line(qfstate_T *state)
680{
681 listitem_T *p_li = state->p_li;
682 int len;
683
684 while (p_li != NULL
685 && (p_li->li_tv.v_type != VAR_STRING
686 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200687 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200688
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200689 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200690 {
691 state->p_li = NULL;
692 return QF_END_OF_INPUT;
693 }
694
695 len = (int)STRLEN(p_li->li_tv.vval.v_string);
696 if (len > IOSIZE - 2)
697 {
698 state->linebuf = qf_grow_linebuf(state, len);
699 if (state->linebuf == NULL)
700 return QF_NOMEM;
701 }
702 else
703 {
704 state->linebuf = IObuff;
705 state->linelen = len;
706 }
707
708 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
709
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200710 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200711 return QF_OK;
712}
713
714/*
715 * Get the next string from state->buf.
716 */
717 static int
718qf_get_next_buf_line(qfstate_T *state)
719{
720 char_u *p_buf = NULL;
721 int len;
722
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200723 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200724 if (state->buflnum > state->lnumlast)
725 return QF_END_OF_INPUT;
726
727 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
728 state->buflnum += 1;
729
730 len = (int)STRLEN(p_buf);
731 if (len > IOSIZE - 2)
732 {
733 state->linebuf = qf_grow_linebuf(state, len);
734 if (state->linebuf == NULL)
735 return QF_NOMEM;
736 }
737 else
738 {
739 state->linebuf = IObuff;
740 state->linelen = len;
741 }
742 vim_strncpy(state->linebuf, p_buf, state->linelen);
743
744 return QF_OK;
745}
746
747/*
748 * Get the next string from file state->fd.
749 */
750 static int
751qf_get_next_file_line(qfstate_T *state)
752{
753 int discard;
754 int growbuflen;
755
756 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
757 return QF_END_OF_INPUT;
758
759 discard = FALSE;
760 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200761 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200762 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200763 // The current line exceeds IObuff, continue reading using
764 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200765 if (state->growbuf == NULL)
766 {
767 state->growbufsiz = 2 * (IOSIZE - 1);
768 state->growbuf = alloc(state->growbufsiz);
769 if (state->growbuf == NULL)
770 return QF_NOMEM;
771 }
772
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200773 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200774 memcpy(state->growbuf, IObuff, IOSIZE - 1);
775 growbuflen = state->linelen;
776
777 for (;;)
778 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200779 char_u *p;
780
Bram Moolenaare0d37972016-07-15 22:36:01 +0200781 if (fgets((char *)state->growbuf + growbuflen,
782 state->growbufsiz - growbuflen, state->fd) == NULL)
783 break;
784 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
785 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200786 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200787 break;
788 if (state->growbufsiz == LINE_MAXLEN)
789 {
790 discard = TRUE;
791 break;
792 }
793
794 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
795 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200796 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200797 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200798 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200799 }
800
801 while (discard)
802 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200803 // The current line is longer than LINE_MAXLEN, continue
804 // reading but discard everything until EOL or EOF is
805 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200806 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
807 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200808 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200809 break;
810 }
811
812 state->linebuf = state->growbuf;
813 state->linelen = growbuflen;
814 }
815 else
816 state->linebuf = IObuff;
817
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200818 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200819 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
820 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100821 char_u *line;
822
823 line = string_convert(&state->vc, state->linebuf, &state->linelen);
824 if (line != NULL)
825 {
826 if (state->linelen < IOSIZE)
827 {
828 STRCPY(state->linebuf, line);
829 vim_free(line);
830 }
831 else
832 {
833 vim_free(state->growbuf);
834 state->linebuf = state->growbuf = line;
835 state->growbufsiz = state->linelen < LINE_MAXLEN
836 ? state->linelen : LINE_MAXLEN;
837 }
838 }
839 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100840
Bram Moolenaare0d37972016-07-15 22:36:01 +0200841 return QF_OK;
842}
843
844/*
845 * Get the next string from a file/buffer/list/string.
846 */
847 static int
848qf_get_nextline(qfstate_T *state)
849{
850 int status = QF_FAIL;
851
852 if (state->fd == NULL)
853 {
854 if (state->tv != NULL)
855 {
856 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200857 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200858 status = qf_get_next_str_line(state);
859 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200860 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200861 status = qf_get_next_list_line(state);
862 }
863 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200864 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200865 status = qf_get_next_buf_line(state);
866 }
867 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200868 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200869 status = qf_get_next_file_line(state);
870
871 if (status != QF_OK)
872 return status;
873
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200874 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200875 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200876 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200877 state->linebuf[state->linelen - 1] = NUL;
878#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200879 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
880 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200881#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200882 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200883
Bram Moolenaare0d37972016-07-15 22:36:01 +0200884 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200885
886 return QF_OK;
887}
888
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200889typedef struct {
890 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200891 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200892 char_u *errmsg;
893 int errmsglen;
894 long lnum;
895 int col;
896 char_u use_viscol;
897 char_u *pattern;
898 int enr;
899 int type;
900 int valid;
901} qffields_T;
902
903/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200904 * Parse the match for filename ('%f') pattern in regmatch.
905 * Return the matched value in "fields->namebuf".
906 */
907 static int
908qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
909{
910 int c;
911
912 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
913 return QF_FAIL;
914
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200915 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200916 c = *rmp->endp[midx];
917 *rmp->endp[midx] = NUL;
918 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
919 *rmp->endp[midx] = c;
920
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200921 // For separate filename patterns (%O, %P and %Q), the specified file
922 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200923 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
924 && mch_getperm(fields->namebuf) == -1)
925 return QF_FAIL;
926
927 return QF_OK;
928}
929
930/*
931 * Parse the match for error number ('%n') pattern in regmatch.
932 * Return the matched value in "fields->enr".
933 */
934 static int
935qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
936{
937 if (rmp->startp[midx] == NULL)
938 return QF_FAIL;
939 fields->enr = (int)atol((char *)rmp->startp[midx]);
940 return QF_OK;
941}
942
943/*
944 * Parse the match for line number (%l') pattern in regmatch.
945 * Return the matched value in "fields->lnum".
946 */
947 static int
948qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
949{
950 if (rmp->startp[midx] == NULL)
951 return QF_FAIL;
952 fields->lnum = atol((char *)rmp->startp[midx]);
953 return QF_OK;
954}
955
956/*
957 * Parse the match for column number ('%c') pattern in regmatch.
958 * Return the matched value in "fields->col".
959 */
960 static int
961qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
962{
963 if (rmp->startp[midx] == NULL)
964 return QF_FAIL;
965 fields->col = (int)atol((char *)rmp->startp[midx]);
966 return QF_OK;
967}
968
969/*
970 * Parse the match for error type ('%t') pattern in regmatch.
971 * Return the matched value in "fields->type".
972 */
973 static int
974qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
975{
976 if (rmp->startp[midx] == NULL)
977 return QF_FAIL;
978 fields->type = *rmp->startp[midx];
979 return QF_OK;
980}
981
982/*
Bram Moolenaarf4140482020-02-15 23:06:45 +0100983 * Copy a non-error line into the error string. Return the matched line in
984 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200985 */
986 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +0100987copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200988{
989 char_u *p;
990
991 if (linelen >= fields->errmsglen)
992 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200993 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200994 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
995 return QF_NOMEM;
996 fields->errmsg = p;
997 fields->errmsglen = linelen + 1;
998 }
Bram Moolenaarf4140482020-02-15 23:06:45 +0100999 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001000 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001001
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001002 return QF_OK;
1003}
1004
1005/*
1006 * Parse the match for error message ('%m') pattern in regmatch.
1007 * Return the matched value in "fields->errmsg".
1008 */
1009 static int
1010qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1011{
1012 char_u *p;
1013 int len;
1014
1015 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1016 return QF_FAIL;
1017 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1018 if (len >= fields->errmsglen)
1019 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001020 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001021 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1022 return QF_NOMEM;
1023 fields->errmsg = p;
1024 fields->errmsglen = len + 1;
1025 }
1026 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1027 return QF_OK;
1028}
1029
1030/*
1031 * Parse the match for rest of a single-line file message ('%r') pattern.
1032 * Return the matched value in "tail".
1033 */
1034 static int
1035qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1036{
1037 if (rmp->startp[midx] == NULL)
1038 return QF_FAIL;
1039 *tail = rmp->startp[midx];
1040 return QF_OK;
1041}
1042
1043/*
1044 * Parse the match for the pointer line ('%p') pattern in regmatch.
1045 * Return the matched value in "fields->col".
1046 */
1047 static int
1048qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1049{
1050 char_u *match_ptr;
1051
1052 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1053 return QF_FAIL;
1054 fields->col = 0;
1055 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1056 ++match_ptr)
1057 {
1058 ++fields->col;
1059 if (*match_ptr == TAB)
1060 {
1061 fields->col += 7;
1062 fields->col -= fields->col % 8;
1063 }
1064 }
1065 ++fields->col;
1066 fields->use_viscol = TRUE;
1067 return QF_OK;
1068}
1069
1070/*
1071 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1072 * Return the matched value in "fields->col".
1073 */
1074 static int
1075qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1076{
1077 if (rmp->startp[midx] == NULL)
1078 return QF_FAIL;
1079 fields->col = (int)atol((char *)rmp->startp[midx]);
1080 fields->use_viscol = TRUE;
1081 return QF_OK;
1082}
1083
1084/*
1085 * Parse the match for the search text ('%s') pattern in regmatch.
1086 * Return the matched value in "fields->pattern".
1087 */
1088 static int
1089qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1090{
1091 int len;
1092
1093 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1094 return QF_FAIL;
1095 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1096 if (len > CMDBUFFSIZE - 5)
1097 len = CMDBUFFSIZE - 5;
1098 STRCPY(fields->pattern, "^\\V");
1099 STRNCAT(fields->pattern, rmp->startp[midx], len);
1100 fields->pattern[len + 3] = '\\';
1101 fields->pattern[len + 4] = '$';
1102 fields->pattern[len + 5] = NUL;
1103 return QF_OK;
1104}
1105
1106/*
1107 * Parse the match for the module ('%o') pattern in regmatch.
1108 * Return the matched value in "fields->module".
1109 */
1110 static int
1111qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1112{
1113 int len;
1114
1115 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1116 return QF_FAIL;
1117 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1118 if (len > CMDBUFFSIZE)
1119 len = CMDBUFFSIZE;
1120 STRNCAT(fields->module, rmp->startp[midx], len);
1121 return QF_OK;
1122}
1123
1124/*
1125 * 'errorformat' format pattern parser functions.
1126 * The '%f' and '%r' formats are parsed differently from other formats.
1127 * See qf_parse_match() for details.
1128 */
1129static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1130{
1131 NULL,
1132 qf_parse_fmt_n,
1133 qf_parse_fmt_l,
1134 qf_parse_fmt_c,
1135 qf_parse_fmt_t,
1136 qf_parse_fmt_m,
1137 NULL,
1138 qf_parse_fmt_p,
1139 qf_parse_fmt_v,
1140 qf_parse_fmt_s,
1141 qf_parse_fmt_o
1142};
1143
1144/*
1145 * Parse the error format pattern matches in "regmatch" and set the values in
1146 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1147 * match. Returns QF_OK if all the matches are successfully parsed. On
1148 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001149 */
1150 static int
1151qf_parse_match(
1152 char_u *linebuf,
1153 int linelen,
1154 efm_T *fmt_ptr,
1155 regmatch_T *regmatch,
1156 qffields_T *fields,
1157 int qf_multiline,
1158 int qf_multiscan,
1159 char_u **tail)
1160{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001161 int idx = fmt_ptr->prefix;
1162 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001163 int midx;
1164 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001165
1166 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1167 return QF_FAIL;
1168 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1169 fields->type = idx;
1170 else
1171 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001172
1173 // Extract error message data from matched line.
1174 // We check for an actual submatch, because "\[" and "\]" in
1175 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001176 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001177 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001178 status = QF_OK;
1179 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001180 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001181 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1182 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001183 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001184 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001185 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001186 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001187 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001188 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001189 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001190 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001191 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001192 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001193
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001194 if (status != QF_OK)
1195 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001196 }
1197
1198 return QF_OK;
1199}
1200
1201/*
1202 * Parse an error line in 'linebuf' using a single error format string in
1203 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1204 * Returns QF_OK if the efm format matches completely and the fields are
1205 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1206 */
1207 static int
1208qf_parse_get_fields(
1209 char_u *linebuf,
1210 int linelen,
1211 efm_T *fmt_ptr,
1212 qffields_T *fields,
1213 int qf_multiline,
1214 int qf_multiscan,
1215 char_u **tail)
1216{
1217 regmatch_T regmatch;
1218 int status = QF_FAIL;
1219 int r;
1220
1221 if (qf_multiscan &&
1222 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1223 return QF_FAIL;
1224
1225 fields->namebuf[0] = NUL;
1226 fields->module[0] = NUL;
1227 fields->pattern[0] = NUL;
1228 if (!qf_multiscan)
1229 fields->errmsg[0] = NUL;
1230 fields->lnum = 0;
1231 fields->col = 0;
1232 fields->use_viscol = FALSE;
1233 fields->enr = -1;
1234 fields->type = 0;
1235 *tail = NULL;
1236
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001237 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001238 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001239 regmatch.regprog = fmt_ptr->prog;
1240 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1241 fmt_ptr->prog = regmatch.regprog;
1242 if (r)
1243 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1244 fields, qf_multiline, qf_multiscan, tail);
1245
1246 return status;
1247}
1248
1249/*
1250 * Parse directory error format prefixes (%D and %X).
1251 * Push and pop directories from the directory stack when scanning directory
1252 * names.
1253 */
1254 static int
1255qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1256{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001257 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001258 {
1259 if (*fields->namebuf == NUL)
1260 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001261 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001262 return QF_FAIL;
1263 }
1264 qfl->qf_directory =
1265 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1266 if (qfl->qf_directory == NULL)
1267 return QF_FAIL;
1268 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001269 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001270 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1271
1272 return QF_OK;
1273}
1274
1275/*
1276 * Parse global file name error format prefixes (%O, %P and %Q).
1277 */
1278 static int
1279qf_parse_file_pfx(
1280 int idx,
1281 qffields_T *fields,
1282 qf_list_T *qfl,
1283 char_u *tail)
1284{
1285 fields->valid = FALSE;
1286 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1287 {
1288 if (*fields->namebuf && idx == 'P')
1289 qfl->qf_currfile =
1290 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1291 else if (idx == 'Q')
1292 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1293 *fields->namebuf = NUL;
1294 if (tail && *tail)
1295 {
1296 STRMOVE(IObuff, skipwhite(tail));
1297 qfl->qf_multiscan = TRUE;
1298 return QF_MULTISCAN;
1299 }
1300 }
1301
1302 return QF_OK;
1303}
1304
1305/*
1306 * Parse a non-error line (a line which doesn't match any of the error
1307 * format in 'efm').
1308 */
1309 static int
1310qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1311{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001312 fields->namebuf[0] = NUL; // no match found, remove file name
1313 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001314 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001315
Bram Moolenaarf4140482020-02-15 23:06:45 +01001316 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001317}
1318
1319/*
1320 * Parse multi-line error format prefixes (%C and %Z)
1321 */
1322 static int
1323qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001324 int idx,
1325 qf_list_T *qfl,
1326 qffields_T *fields)
1327{
1328 char_u *ptr;
1329 int len;
1330
1331 if (!qfl->qf_multiignore)
1332 {
1333 qfline_T *qfprev = qfl->qf_last;
1334
1335 if (qfprev == NULL)
1336 return QF_FAIL;
1337 if (*fields->errmsg && !qfl->qf_multiignore)
1338 {
1339 len = (int)STRLEN(qfprev->qf_text);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001340 if ((ptr = alloc(len + STRLEN(fields->errmsg) + 2))
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001341 == NULL)
1342 return QF_FAIL;
1343 STRCPY(ptr, qfprev->qf_text);
1344 vim_free(qfprev->qf_text);
1345 qfprev->qf_text = ptr;
1346 *(ptr += len) = '\n';
1347 STRCPY(++ptr, fields->errmsg);
1348 }
1349 if (qfprev->qf_nr == -1)
1350 qfprev->qf_nr = fields->enr;
1351 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001352 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001353 qfprev->qf_type = fields->type;
1354
1355 if (!qfprev->qf_lnum)
1356 qfprev->qf_lnum = fields->lnum;
1357 if (!qfprev->qf_col)
1358 qfprev->qf_col = fields->col;
1359 qfprev->qf_viscol = fields->use_viscol;
1360 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001361 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001362 qfl->qf_directory,
1363 *fields->namebuf || qfl->qf_directory != NULL
1364 ? fields->namebuf
1365 : qfl->qf_currfile != NULL && fields->valid
1366 ? qfl->qf_currfile : 0);
1367 }
1368 if (idx == 'Z')
1369 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1370 line_breakcheck();
1371
1372 return QF_IGNORE_LINE;
1373}
1374
1375/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001376 * Parse a line and get the quickfix fields.
1377 * Return the QF_ status.
1378 */
1379 static int
1380qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001381 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001382 char_u *linebuf,
1383 int linelen,
1384 efm_T *fmt_first,
1385 qffields_T *fields)
1386{
1387 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001388 int idx = 0;
1389 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001390 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001391
Bram Moolenaare333e792018-04-08 13:27:39 +02001392restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001393 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001394 if (fmt_start == NULL)
1395 fmt_ptr = fmt_first;
1396 else
1397 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001398 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001399 fmt_ptr = fmt_start;
1400 fmt_start = NULL;
1401 }
1402
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001403 // Try to match each part of 'errorformat' until we find a complete
1404 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001405 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001406 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1407 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001408 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001409 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1410 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1411 if (status == QF_NOMEM)
1412 return status;
1413 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001414 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001415 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001416 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417
1418 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1419 {
1420 if (fmt_ptr != NULL)
1421 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001422 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001423 status = qf_parse_dir_pfx(idx, fields, qfl);
1424 if (status != QF_OK)
1425 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001426 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001427
1428 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1429 if (status != QF_OK)
1430 return status;
1431
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001432 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001433 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001434 }
1435 else if (fmt_ptr != NULL)
1436 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001437 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001438 if (fmt_ptr->conthere)
1439 fmt_start = fmt_ptr;
1440
1441 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1442 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001443 qfl->qf_multiline = TRUE; // start of a multi-line message
1444 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001445 }
1446 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001447 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001448 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001449 if (status != QF_OK)
1450 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001451 }
1452 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001453 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001454 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1455 if (status == QF_MULTISCAN)
1456 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001457 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001458 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001459 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001460 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001461 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001462 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001463 return QF_IGNORE_LINE;
1464 }
1465 }
1466
1467 return QF_OK;
1468}
1469
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001470/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001471 * Returns TRUE if the specified quickfix/location stack is empty
1472 */
1473 static int
1474qf_stack_empty(qf_info_T *qi)
1475{
1476 return qi == NULL || qi->qf_listcount <= 0;
1477}
1478
1479/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001480 * Returns TRUE if the specified quickfix/location list is empty.
1481 */
1482 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001483qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001484{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001485 return qfl == NULL || qfl->qf_count <= 0;
1486}
1487
1488/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001489 * Returns TRUE if the specified quickfix/location list is not empty and
1490 * has valid entries.
1491 */
1492 static int
1493qf_list_has_valid_entries(qf_list_T *qfl)
1494{
1495 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1496}
1497
1498/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001499 * Return a pointer to a list in the specified quickfix stack
1500 */
1501 static qf_list_T *
1502qf_get_list(qf_info_T *qi, int idx)
1503{
1504 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001505}
1506
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001507/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001508 * Allocate the fields used for parsing lines and populating a quickfix list.
1509 */
1510 static int
1511qf_alloc_fields(qffields_T *pfields)
1512{
1513 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1514 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1515 pfields->errmsglen = CMDBUFFSIZE + 1;
1516 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1517 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1518 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1519 || pfields->pattern == NULL || pfields->module == NULL)
1520 return FAIL;
1521
1522 return OK;
1523}
1524
1525/*
1526 * Free the fields used for parsing lines and populating a quickfix list.
1527 */
1528 static void
1529qf_free_fields(qffields_T *pfields)
1530{
1531 vim_free(pfields->namebuf);
1532 vim_free(pfields->module);
1533 vim_free(pfields->errmsg);
1534 vim_free(pfields->pattern);
1535}
1536
1537/*
1538 * Setup the state information used for parsing lines and populating a
1539 * quickfix list.
1540 */
1541 static int
1542qf_setup_state(
1543 qfstate_T *pstate,
1544 char_u *enc,
1545 char_u *efile,
1546 typval_T *tv,
1547 buf_T *buf,
1548 linenr_T lnumfirst,
1549 linenr_T lnumlast)
1550{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001551 pstate->vc.vc_type = CONV_NONE;
1552 if (enc != NULL && *enc != NUL)
1553 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001554
1555 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001557 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001558 return FAIL;
1559 }
1560
1561 if (tv != NULL)
1562 {
1563 if (tv->v_type == VAR_STRING)
1564 pstate->p_str = tv->vval.v_string;
1565 else if (tv->v_type == VAR_LIST)
1566 pstate->p_li = tv->vval.v_list->lv_first;
1567 pstate->tv = tv;
1568 }
1569 pstate->buf = buf;
1570 pstate->buflnum = lnumfirst;
1571 pstate->lnumlast = lnumlast;
1572
1573 return OK;
1574}
1575
1576/*
1577 * Cleanup the state information used for parsing lines and populating a
1578 * quickfix list.
1579 */
1580 static void
1581qf_cleanup_state(qfstate_T *pstate)
1582{
1583 if (pstate->fd != NULL)
1584 fclose(pstate->fd);
1585
1586 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001587 if (pstate->vc.vc_type != CONV_NONE)
1588 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001589}
1590
1591/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001592 * Process the next line from a file/buffer/list/string and add it
1593 * to the quickfix list 'qfl'.
1594 */
1595 static int
1596qf_init_process_nextline(
1597 qf_list_T *qfl,
1598 efm_T *fmt_first,
1599 qfstate_T *state,
1600 qffields_T *fields)
1601{
1602 int status;
1603
1604 // Get the next line from a file/buffer/list/string
1605 status = qf_get_nextline(state);
1606 if (status != QF_OK)
1607 return status;
1608
1609 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1610 fmt_first, fields);
1611 if (status != QF_OK)
1612 return status;
1613
1614 return qf_add_entry(qfl,
1615 qfl->qf_directory,
1616 (*fields->namebuf || qfl->qf_directory != NULL)
1617 ? fields->namebuf
1618 : ((qfl->qf_currfile != NULL && fields->valid)
1619 ? qfl->qf_currfile : (char_u *)NULL),
1620 fields->module,
1621 0,
1622 fields->errmsg,
1623 fields->lnum,
1624 fields->col,
1625 fields->use_viscol,
1626 fields->pattern,
1627 fields->enr,
1628 fields->type,
1629 fields->valid);
1630}
1631
1632/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001633 * Read the errorfile "efile" into memory, line by line, building the error
1634 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001635 * Alternative: when "efile" is NULL read errors from buffer "buf".
1636 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001637 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001638 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1639 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001640 * Return -1 for error, number of errors for success.
1641 */
1642 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001643qf_init_ext(
1644 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001645 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001646 char_u *efile,
1647 buf_T *buf,
1648 typval_T *tv,
1649 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001650 int newlist, // TRUE: start a new error list
1651 linenr_T lnumfirst, // first line number to use
1652 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001653 char_u *qf_title,
1654 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001655{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001656 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001657 qfstate_T state;
1658 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001659 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001660 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001661 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001663 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001664 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001665 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001667 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001668 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001669
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001670 vim_memset(&state, 0, sizeof(state));
1671 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001672 if ((qf_alloc_fields(&fields) == FAIL) ||
1673 (qf_setup_state(&state, enc, efile, tv, buf,
1674 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 goto qf_init_end;
1676
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001677 if (newlist || qf_idx == qi->qf_listcount)
1678 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001679 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001680 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001681 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001682 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001683 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001684 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001685 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001686 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001687 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001688 qfl = qf_get_list(qi, qf_idx);
1689 if (!qf_list_empty(qfl))
1690 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001693 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001694 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001695 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 else
1697 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001699 // If the errorformat didn't change between calls, then reuse the
1700 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001701 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1702 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001703 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001704 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001705 free_efm_list(&fmt_first);
1706
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001707 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001708 fmt_first = parse_efm_option(efm);
1709 if (fmt_first != NULL)
1710 last_efm = vim_strsave(efm);
1711 }
1712
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001713 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001716 // got_int is reset here, because it was probably set when killing the
1717 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 got_int = FALSE;
1719
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001720 // Read the lines in the error file one by one.
1721 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001722 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001724 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001725 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001726 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001727 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001728 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001729 if (status == QF_FAIL)
1730 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 line_breakcheck();
1733 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001734 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001736 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001738 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001739 qfl->qf_ptr = qfl->qf_start;
1740 qfl->qf_index = 1;
1741 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 }
1743 else
1744 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001745 qfl->qf_nonevalid = FALSE;
1746 if (qfl->qf_ptr == NULL)
1747 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001749 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001750 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001751 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001753 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001755 if (!adding)
1756 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001757 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001758 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001759 qi->qf_listcount--;
1760 if (qi->qf_curlist > 0)
1761 --qi->qf_curlist;
1762 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001763qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001764 if (qf_idx == qi->qf_curlist)
1765 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001766 qf_cleanup_state(&state);
1767 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769 return retval;
1770}
1771
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001772/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001773 * Read the errorfile "efile" into memory, line by line, building the error
1774 * list. Set the error list's title to qf_title.
1775 * Return -1 for error, number of errors for success.
1776 */
1777 int
1778qf_init(win_T *wp,
1779 char_u *efile,
1780 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001781 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001782 char_u *qf_title,
1783 char_u *enc)
1784{
1785 qf_info_T *qi = &ql_info;
1786
1787 if (wp != NULL)
1788 {
1789 qi = ll_get_or_alloc_list(wp);
1790 if (qi == NULL)
1791 return FAIL;
1792 }
1793
1794 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1795 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1796}
1797
1798/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001799 * Set the title of the specified quickfix list. Frees the previous title.
1800 * Prepends ':' to the title.
1801 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001802 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001803qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001804{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001805 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001806
Bram Moolenaarfb604092014-07-23 15:55:00 +02001807 if (title != NULL)
1808 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02001809 char_u *p = alloc(STRLEN(title) + 2);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001810
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001811 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001812 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001813 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001814 }
1815}
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001818 * The title of a quickfix/location list is set, by default, to the command
1819 * that created the quickfix list with the ":" prefix.
1820 * Create a quickfix list title string by prepending ":" to a user command.
1821 * Returns a pointer to a static buffer with the title.
1822 */
1823 static char_u *
1824qf_cmdtitle(char_u *cmd)
1825{
1826 static char_u qftitle_str[IOSIZE];
1827
1828 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1829 return qftitle_str;
1830}
1831
1832/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001833 * Return a pointer to the current list in the specified quickfix stack
1834 */
1835 static qf_list_T *
1836qf_get_curlist(qf_info_T *qi)
1837{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001838 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001839}
1840
1841/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001842 * Prepare for adding a new quickfix list. If the current list is in the
1843 * middle of the stack, then all the following lists are freed and then
1844 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 */
1846 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001847qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
1849 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001850 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001852 // If the current entry is not the last entry, delete entries beyond
1853 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001854 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001855 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001856 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001858 // When the stack is full, remove to oldest entry
1859 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001860 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001862 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001864 qi->qf_lists[i - 1] = qi->qf_lists[i];
1865 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 }
1867 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001868 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001869 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001870 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1871 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001872 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001873 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874}
1875
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001876/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001877 * Queue location list stack delete request.
1878 */
1879 static void
1880locstack_queue_delreq(qf_info_T *qi)
1881{
1882 qf_delq_T *q;
1883
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001884 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001885 if (q != NULL)
1886 {
1887 q->qi = qi;
1888 q->next = qf_delq_head;
1889 qf_delq_head = q;
1890 }
1891}
1892
1893/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001894 * Return the global quickfix stack window buffer number.
1895 */
1896 int
1897qf_stack_get_bufnr(void)
1898{
1899 return ql_info.qf_bufnr;
1900}
1901
1902/*
1903 * Wipe the quickfix window buffer (if present) for the specified
1904 * quickfix/location list.
1905 */
1906 static void
1907wipe_qf_buffer(qf_info_T *qi)
1908{
1909 buf_T *qfbuf;
1910
1911 if (qi->qf_bufnr != INVALID_QFBUFNR)
1912 {
1913 qfbuf = buflist_findnr(qi->qf_bufnr);
1914 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1915 {
1916 // If the quickfix buffer is not loaded in any window, then
1917 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001918 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001919 qi->qf_bufnr = INVALID_QFBUFNR;
1920 }
1921 }
1922}
1923
1924/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001925 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001926 */
1927 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001928ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001929{
1930 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001931 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001932
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001933 qi = *pqi;
1934 if (qi == NULL)
1935 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001936 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001937
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001938 // If the location list is still in use, then queue the delete request
1939 // to be processed later.
1940 if (quickfix_busy > 0)
1941 {
1942 locstack_queue_delreq(qi);
1943 return;
1944 }
1945
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001946 qi->qf_refcount--;
1947 if (qi->qf_refcount < 1)
1948 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001949 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001950 // If the quickfix window buffer is loaded, then wipe it
1951 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001952
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001953 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001954 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001955 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001956 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001957}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001958
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001959/*
1960 * Free all the quickfix/location lists in the stack.
1961 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001962 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001963qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001964{
1965 int i;
1966 qf_info_T *qi = &ql_info;
1967
1968 if (wp != NULL)
1969 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001970 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001971 ll_free_all(&wp->w_llist);
1972 ll_free_all(&wp->w_llist_ref);
1973 }
1974 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001975 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001976 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001977 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001978}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001981 * Delay freeing of location list stacks when the quickfix code is running.
1982 * Used to avoid problems with autocmds freeing location list stacks when the
1983 * quickfix code is still referencing the stack.
1984 * Must always call decr_quickfix_busy() exactly once after this.
1985 */
1986 static void
1987incr_quickfix_busy(void)
1988{
1989 quickfix_busy++;
1990}
1991
1992/*
1993 * Safe to free location list stacks. Process any delayed delete requests.
1994 */
1995 static void
1996decr_quickfix_busy(void)
1997{
1998 if (--quickfix_busy == 0)
1999 {
2000 // No longer referencing the location lists. Process all the pending
2001 // delete requests.
2002 while (qf_delq_head != NULL)
2003 {
2004 qf_delq_T *q = qf_delq_head;
2005
2006 qf_delq_head = q->next;
2007 ll_free_all(&q->qi);
2008 vim_free(q);
2009 }
2010 }
2011#ifdef ABORT_ON_INTERNAL_ERROR
2012 if (quickfix_busy < 0)
2013 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002014 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002015 abort();
2016 }
2017#endif
2018}
2019
2020#if defined(EXITFREE) || defined(PROTO)
2021 void
2022check_quickfix_busy(void)
2023{
2024 if (quickfix_busy != 0)
2025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002026 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002027# ifdef ABORT_ON_INTERNAL_ERROR
2028 abort();
2029# endif
2030 }
2031}
2032#endif
2033
2034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 * Add an entry to the end of the list of errors.
Bram Moolenaar95946f12019-03-31 15:31:59 +02002036 * Returns QF_OK or QF_FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 */
2038 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002039qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002040 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002041 char_u *dir, // optional directory name
2042 char_u *fname, // file name or NULL
2043 char_u *module, // module name or NULL
2044 int bufnum, // buffer number or zero
2045 char_u *mesg, // message
2046 long lnum, // line number
2047 int col, // column
2048 int vis_col, // using visual column
2049 char_u *pattern, // search pattern
2050 int nr, // error number
2051 int type, // type character
2052 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002054 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002055 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002057 if ((qfp = ALLOC_ONE(qfline_T)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002058 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002059 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002060 {
2061 buf_T *buf = buflist_findnr(bufnum);
2062
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002063 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002064 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002065 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002066 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002067 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002068 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002069 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2071 {
2072 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002073 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075 qfp->qf_lnum = lnum;
2076 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002077 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002078 if (pattern == NULL || *pattern == NUL)
2079 qfp->qf_pattern = NULL;
2080 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2081 {
2082 vim_free(qfp->qf_text);
2083 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002084 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002085 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002086 if (module == NULL || *module == NUL)
2087 qfp->qf_module = NULL;
2088 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2089 {
2090 vim_free(qfp->qf_text);
2091 vim_free(qfp->qf_pattern);
2092 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002093 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002096 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 type = 0;
2098 qfp->qf_type = type;
2099 qfp->qf_valid = valid;
2100
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002101 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002102 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002104 qfl->qf_start = qfp;
2105 qfl->qf_ptr = qfp;
2106 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002107 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 }
2109 else
2110 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002111 qfp->qf_prev = *lastp;
2112 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002114 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002116 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002117 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002118 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002120 qfl->qf_index = qfl->qf_count;
2121 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
2123
Bram Moolenaar95946f12019-03-31 15:31:59 +02002124 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125}
2126
2127/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002128 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002129 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002130 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002131qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002132{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002133 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002134
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002135 qi = ALLOC_CLEAR_ONE(qf_info_T);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002136 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002137 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002138 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002139 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002140 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002141 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002142 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002143}
2144
2145/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002146 * Return the location list stack for window 'wp'.
2147 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002148 */
2149 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002150ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002151{
2152 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002153 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002154 return wp->w_llist_ref;
2155
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002156 // For a non-location list window, w_llist_ref should not point to a
2157 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002158 ll_free_all(&wp->w_llist_ref);
2159
2160 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002161 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002162 return wp->w_llist;
2163}
2164
2165/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002166 * Get the quickfix/location list stack to use for the specified Ex command.
2167 * For a location list command, returns the stack for the current window. If
2168 * the location list is not found, then returns NULL and prints an error
2169 * message if 'print_emsg' is TRUE.
2170 */
2171 static qf_info_T *
2172qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2173{
2174 qf_info_T *qi = &ql_info;
2175
2176 if (is_loclist_cmd(eap->cmdidx))
2177 {
2178 qi = GET_LOC_LIST(curwin);
2179 if (qi == NULL)
2180 {
2181 if (print_emsg)
2182 emsg(_(e_loclist));
2183 return NULL;
2184 }
2185 }
2186
2187 return qi;
2188}
2189
2190/*
2191 * Get the quickfix/location list stack to use for the specified Ex command.
2192 * For a location list command, returns the stack for the current window.
2193 * If the location list is not present, then allocates a new one.
2194 * Returns NULL if the allocation fails. For a location list command, sets
2195 * 'pwinp' to curwin.
2196 */
2197 static qf_info_T *
2198qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2199{
2200 qf_info_T *qi = &ql_info;
2201
2202 if (is_loclist_cmd(eap->cmdidx))
2203 {
2204 qi = ll_get_or_alloc_list(curwin);
2205 if (qi == NULL)
2206 return NULL;
2207 *pwinp = curwin;
2208 }
2209
2210 return qi;
2211}
2212
2213/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002214 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2215 */
2216 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002217copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002218{
2219 int i;
2220 qfline_T *from_qfp;
2221 qfline_T *prevp;
2222
2223 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002224 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002225 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002226 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002227 NULL,
2228 NULL,
2229 from_qfp->qf_module,
2230 0,
2231 from_qfp->qf_text,
2232 from_qfp->qf_lnum,
2233 from_qfp->qf_col,
2234 from_qfp->qf_viscol,
2235 from_qfp->qf_pattern,
2236 from_qfp->qf_nr,
2237 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002238 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002239 return FAIL;
2240
2241 // qf_add_entry() will not set the qf_num field, as the
2242 // directory and file names are not supplied. So the qf_fnum
2243 // field is copied here.
2244 prevp = to_qfl->qf_last;
2245 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2246 prevp->qf_type = from_qfp->qf_type; // error type
2247 if (from_qfl->qf_ptr == from_qfp)
2248 to_qfl->qf_ptr = prevp; // current location
2249 }
2250
2251 return OK;
2252}
2253
2254/*
2255 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2256 */
2257 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002258copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002259{
2260 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002261 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002262 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2263 to_qfl->qf_count = 0;
2264 to_qfl->qf_index = 0;
2265 to_qfl->qf_start = NULL;
2266 to_qfl->qf_last = NULL;
2267 to_qfl->qf_ptr = NULL;
2268 if (from_qfl->qf_title != NULL)
2269 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2270 else
2271 to_qfl->qf_title = NULL;
2272 if (from_qfl->qf_ctx != NULL)
2273 {
2274 to_qfl->qf_ctx = alloc_tv();
2275 if (to_qfl->qf_ctx != NULL)
2276 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2277 }
2278 else
2279 to_qfl->qf_ctx = NULL;
2280
2281 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002282 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002283 return FAIL;
2284
2285 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2286
2287 // Assign a new ID for the location list
2288 to_qfl->qf_id = ++last_qf_id;
2289 to_qfl->qf_changedtick = 0L;
2290
2291 // When no valid entries are present in the list, qf_ptr points to
2292 // the first item in the list
2293 if (to_qfl->qf_nonevalid)
2294 {
2295 to_qfl->qf_ptr = to_qfl->qf_start;
2296 to_qfl->qf_index = 1;
2297 }
2298
2299 return OK;
2300}
2301
2302/*
2303 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002304 */
2305 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002306copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002307{
2308 qf_info_T *qi;
2309 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002310
Bram Moolenaar09037502018-09-25 22:08:14 +02002311 // When copying from a location list window, copy the referenced
2312 // location list. For other windows, copy the location list for
2313 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002314 if (IS_LL_WINDOW(from))
2315 qi = from->w_llist_ref;
2316 else
2317 qi = from->w_llist;
2318
Bram Moolenaar09037502018-09-25 22:08:14 +02002319 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002320 return;
2321
Bram Moolenaar09037502018-09-25 22:08:14 +02002322 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002323 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002324 return;
2325
2326 to->w_llist->qf_listcount = qi->qf_listcount;
2327
Bram Moolenaar09037502018-09-25 22:08:14 +02002328 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002329 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002330 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002331 to->w_llist->qf_curlist = idx;
2332
Bram Moolenaar0398e002019-03-21 21:12:49 +01002333 if (copy_loclist(qf_get_list(qi, idx),
2334 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002335 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002336 qf_free_all(to);
2337 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002338 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002339 }
2340
Bram Moolenaar09037502018-09-25 22:08:14 +02002341 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002342}
2343
2344/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002345 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002346 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 */
2348 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002349qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350{
Bram Moolenaar82404332016-07-10 17:00:38 +02002351 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002352 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002353 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002354
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002355 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
Bram Moolenaare60acc12011-05-10 16:41:25 +02002358#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002359 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002360#endif
2361#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002362 if (directory != NULL)
2363 slash_adjust(directory);
2364 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002365#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002366 if (directory != NULL && !vim_isAbsName(fname)
2367 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2368 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002369 // Here we check if the file really exists.
2370 // This should normally be true, but if make works without
2371 // "leaving directory"-messages we might have missed a
2372 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002373 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002376 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002377 if (directory)
2378 ptr = concat_fnames(directory, fname, TRUE);
2379 else
2380 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002382 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002383 bufname = ptr;
2384 }
2385 else
2386 bufname = fname;
2387
2388 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002389 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002390 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002391 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002392 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002394 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002395 {
2396 vim_free(qf_last_bufname);
2397 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2398 if (bufname == ptr)
2399 qf_last_bufname = bufname;
2400 else
2401 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002402 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002403 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002404 if (buf == NULL)
2405 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002406
Bram Moolenaarc1542742016-07-20 21:44:37 +02002407 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002408 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002409 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410}
2411
2412/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002413 * Push dirbuf onto the directory stack and return pointer to actual dir or
2414 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 */
2416 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002417qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419 struct dir_stack_T *ds_new;
2420 struct dir_stack_T *ds_ptr;
2421
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002422 // allocate new stack element and hook it in
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002423 ds_new = ALLOC_ONE(struct dir_stack_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 if (ds_new == NULL)
2425 return NULL;
2426
2427 ds_new->next = *stackptr;
2428 *stackptr = ds_new;
2429
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002430 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 if (vim_isAbsName(dirbuf)
2432 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002433 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 (*stackptr)->dirname = vim_strsave(dirbuf);
2435 else
2436 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002437 // Okay we don't have an absolute path.
2438 // dirbuf must be a subdir of one of the directories on the stack.
2439 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 ds_new = (*stackptr)->next;
2441 (*stackptr)->dirname = NULL;
2442 while (ds_new)
2443 {
2444 vim_free((*stackptr)->dirname);
2445 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2446 TRUE);
2447 if (mch_isdir((*stackptr)->dirname) == TRUE)
2448 break;
2449
2450 ds_new = ds_new->next;
2451 }
2452
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002453 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 while ((*stackptr)->next != ds_new)
2455 {
2456 ds_ptr = (*stackptr)->next;
2457 (*stackptr)->next = (*stackptr)->next->next;
2458 vim_free(ds_ptr->dirname);
2459 vim_free(ds_ptr);
2460 }
2461
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002462 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 if (ds_new == NULL)
2464 {
2465 vim_free((*stackptr)->dirname);
2466 (*stackptr)->dirname = vim_strsave(dirbuf);
2467 }
2468 }
2469
2470 if ((*stackptr)->dirname != NULL)
2471 return (*stackptr)->dirname;
2472 else
2473 {
2474 ds_ptr = *stackptr;
2475 *stackptr = (*stackptr)->next;
2476 vim_free(ds_ptr);
2477 return NULL;
2478 }
2479}
2480
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481/*
2482 * pop dirbuf from the directory stack and return previous directory or NULL if
2483 * stack is empty
2484 */
2485 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002486qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
2488 struct dir_stack_T *ds_ptr;
2489
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002490 // TODO: Should we check if dirbuf is the directory on top of the stack?
2491 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002493 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 if (*stackptr != NULL)
2495 {
2496 ds_ptr = *stackptr;
2497 *stackptr = (*stackptr)->next;
2498 vim_free(ds_ptr->dirname);
2499 vim_free(ds_ptr);
2500 }
2501
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002502 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 return *stackptr ? (*stackptr)->dirname : NULL;
2504}
2505
2506/*
2507 * clean up directory stack
2508 */
2509 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002510qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
2512 struct dir_stack_T *ds_ptr;
2513
2514 while ((ds_ptr = *stackptr) != NULL)
2515 {
2516 *stackptr = (*stackptr)->next;
2517 vim_free(ds_ptr->dirname);
2518 vim_free(ds_ptr);
2519 }
2520}
2521
2522/*
2523 * Check in which directory of the directory stack the given file can be
2524 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002525 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 * Cleans up intermediate directory entries.
2527 *
2528 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002529 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 * ./
2531 * ./aa
2532 * ./aa/bb
2533 * ./bb
2534 * ./bb/x.c
2535 * and make says:
2536 * making all in aa
2537 * making all in bb
2538 * x.c:9: Error
2539 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2540 * qf_guess_filepath will return NULL.
2541 */
2542 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002543qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545 struct dir_stack_T *ds_ptr;
2546 struct dir_stack_T *ds_tmp;
2547 char_u *fullname;
2548
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002549 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002550 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 return NULL;
2552
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002553 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 fullname = NULL;
2555 while (ds_ptr)
2556 {
2557 vim_free(fullname);
2558 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2559
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002560 // If concat_fnames failed, just go on. The worst thing that can happen
2561 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2563 break;
2564
2565 ds_ptr = ds_ptr->next;
2566 }
2567
2568 vim_free(fullname);
2569
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002570 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002571 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002573 ds_tmp = qfl->qf_dir_stack->next;
2574 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 vim_free(ds_tmp->dirname);
2576 vim_free(ds_tmp);
2577 }
2578
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002579 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580}
2581
2582/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002583 * Returns TRUE if a quickfix/location list with the given identifier exists.
2584 */
2585 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002586qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002587{
2588 qf_info_T *qi = &ql_info;
2589 int i;
2590
2591 if (wp != NULL)
2592 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002593 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002594 if (qi == NULL)
2595 return FALSE;
2596 }
2597
2598 for (i = 0; i < qi->qf_listcount; ++i)
2599 if (qi->qf_lists[i].qf_id == qf_id)
2600 return TRUE;
2601
2602 return FALSE;
2603}
2604
2605/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002606 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002607 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002608 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002609 * Similar to location list.
2610 */
2611 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002612is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002613{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002614 qfline_T *qfp;
2615 int i;
2616
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002617 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002618 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2619 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002620 break;
2621
Bram Moolenaar95946f12019-03-31 15:31:59 +02002622 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002623 return FALSE;
2624
2625 return TRUE;
2626}
2627
2628/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002629 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002630 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002631 */
2632 static qfline_T *
2633get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002634 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002635 qfline_T *qf_ptr,
2636 int *qf_index,
2637 int dir)
2638{
2639 int idx;
2640 int old_qf_fnum;
2641
2642 idx = *qf_index;
2643 old_qf_fnum = qf_ptr->qf_fnum;
2644
2645 do
2646 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002647 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002648 return NULL;
2649 ++idx;
2650 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002651 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002652 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2653
2654 *qf_index = idx;
2655 return qf_ptr;
2656}
2657
2658/*
2659 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002660 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002661 */
2662 static qfline_T *
2663get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002664 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002665 qfline_T *qf_ptr,
2666 int *qf_index,
2667 int dir)
2668{
2669 int idx;
2670 int old_qf_fnum;
2671
2672 idx = *qf_index;
2673 old_qf_fnum = qf_ptr->qf_fnum;
2674
2675 do
2676 {
2677 if (idx == 1 || qf_ptr->qf_prev == NULL)
2678 return NULL;
2679 --idx;
2680 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002681 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002682 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2683
2684 *qf_index = idx;
2685 return qf_ptr;
2686}
2687
2688/*
2689 * Get the n'th (errornr) previous/next valid entry from the current entry in
2690 * the quickfix list.
2691 * dir == FORWARD or FORWARD_FILE: next valid entry
2692 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2693 */
2694 static qfline_T *
2695get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002696 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002697 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002698 int dir,
2699 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002700{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002701 qfline_T *qf_ptr = qfl->qf_ptr;
2702 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002703 qfline_T *prev_qf_ptr;
2704 int prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002705 char_u *err = e_no_more_items;
2706
2707 while (errornr--)
2708 {
2709 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002710 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002711
2712 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002713 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002714 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002715 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002716 if (qf_ptr == NULL)
2717 {
2718 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002719 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002720 if (err != NULL)
2721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002722 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002723 return NULL;
2724 }
2725 break;
2726 }
2727
2728 err = NULL;
2729 }
2730
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002731 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002732 return qf_ptr;
2733}
2734
2735/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002736 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2737 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002738 */
2739 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002740get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002741{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002742 qfline_T *qf_ptr = qfl->qf_ptr;
2743 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002744
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002745 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002746 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2747 {
2748 --qf_idx;
2749 qf_ptr = qf_ptr->qf_prev;
2750 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002751 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002752 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2753 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002754 {
2755 ++qf_idx;
2756 qf_ptr = qf_ptr->qf_next;
2757 }
2758
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002759 *new_qfidx = qf_idx;
2760 return qf_ptr;
2761}
2762
2763/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002764 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002765 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2766 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2767 * Returns a pointer to the entry and the index of the new entry is stored in
2768 * 'new_qfidx'.
2769 */
2770 static qfline_T *
2771qf_get_entry(
2772 qf_list_T *qfl,
2773 int errornr,
2774 int dir,
2775 int *new_qfidx)
2776{
2777 qfline_T *qf_ptr = qfl->qf_ptr;
2778 int qfidx = qfl->qf_index;
2779
2780 if (dir != 0) // next/prev valid entry
2781 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2782 else if (errornr != 0) // go to specified number
2783 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2784
2785 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002786 return qf_ptr;
2787}
2788
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002789/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002790 * Find a window displaying a Vim help file.
2791 */
2792 static win_T *
2793qf_find_help_win(void)
2794{
2795 win_T *wp;
2796
2797 FOR_ALL_WINDOWS(wp)
2798 if (bt_help(wp->w_buffer))
2799 return wp;
2800
2801 return NULL;
2802}
2803
2804/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002805 * Set the location list for the specified window to 'qi'.
2806 */
2807 static void
2808win_set_loclist(win_T *wp, qf_info_T *qi)
2809{
2810 wp->w_llist = qi;
2811 qi->qf_refcount++;
2812}
2813
2814/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002815 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2816 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002817 */
2818 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002819jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002820{
2821 win_T *wp;
2822 int flags;
2823
Bram Moolenaarb2443732018-11-11 22:50:27 +01002824 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002825 wp = NULL;
2826 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002827 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002828 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2829 win_enter(wp, TRUE);
2830 else
2831 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002832 // Split off help window; put it at far top if no position
2833 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002834 flags = WSP_HELP;
2835 if (cmdmod.split == 0 && curwin->w_width != Columns
2836 && curwin->w_width < 80)
2837 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002838 // If the user asks to open a new window, then copy the location list.
2839 // Otherwise, don't copy the location list.
2840 if (IS_LL_STACK(qi) && !newwin)
2841 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002842
2843 if (win_split(0, flags) == FAIL)
2844 return FAIL;
2845
2846 *opened_window = TRUE;
2847
2848 if (curwin->w_height < p_hh)
2849 win_setheight((int)p_hh);
2850
Bram Moolenaarb2443732018-11-11 22:50:27 +01002851 // When using location list, the new window should use the supplied
2852 // location list. If the user asks to open a new window, then the new
2853 // window will get a copy of the location list.
2854 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002855 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002856 }
2857
2858 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002859 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002860
2861 return OK;
2862}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002863
2864/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002865 * Find a non-quickfix window in the current tabpage using the given location
2866 * list stack.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002867 * Returns NULL if a matching window is not found.
2868 */
2869 static win_T *
2870qf_find_win_with_loclist(qf_info_T *ll)
2871{
2872 win_T *wp;
2873
2874 FOR_ALL_WINDOWS(wp)
2875 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2876 return wp;
2877
2878 return NULL;
2879}
2880
2881/*
2882 * Find a window containing a normal buffer
2883 */
2884 static win_T *
2885qf_find_win_with_normal_buf(void)
2886{
2887 win_T *wp;
2888
2889 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002890 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002891 return wp;
2892
2893 return NULL;
2894}
2895
2896/*
2897 * Go to a window in any tabpage containing the specified file. Returns TRUE
2898 * if successfully jumped to the window. Otherwise returns FALSE.
2899 */
2900 static int
2901qf_goto_tabwin_with_file(int fnum)
2902{
2903 tabpage_T *tp;
2904 win_T *wp;
2905
2906 FOR_ALL_TAB_WINDOWS(tp, wp)
2907 if (wp->w_buffer->b_fnum == fnum)
2908 {
2909 goto_tabpage_win(tp, wp);
2910 return TRUE;
2911 }
2912
2913 return FALSE;
2914}
2915
2916/*
2917 * Create a new window to show a file above the quickfix window. Called when
2918 * only the quickfix window is present.
2919 */
2920 static int
2921qf_open_new_file_win(qf_info_T *ll_ref)
2922{
2923 int flags;
2924
2925 flags = WSP_ABOVE;
2926 if (ll_ref != NULL)
2927 flags |= WSP_NEWLOC;
2928 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002929 return FAIL; // not enough room for window
2930 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002931 swb_flags = 0;
2932 RESET_BINDING(curwin);
2933 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002934 // The new window should use the location list from the
2935 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002936 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002937 return OK;
2938}
2939
2940/*
2941 * Go to a window that shows the right buffer. If the window is not found, go
2942 * to the window just above the location list window. This is used for opening
2943 * a file from a location window and not from a quickfix window. If some usable
2944 * window is previously found, then it is supplied in 'use_win'.
2945 */
2946 static void
2947qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2948{
2949 win_T *win = use_win;
2950
2951 if (win == NULL)
2952 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002953 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002954 FOR_ALL_WINDOWS(win)
2955 if (win->w_buffer->b_fnum == qf_fnum)
2956 break;
2957 if (win == NULL)
2958 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002959 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002960 win = curwin;
2961 do
2962 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002963 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002964 break;
2965 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002966 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002967 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002968 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002969 } while (win != curwin);
2970 }
2971 }
2972 win_goto(win);
2973
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002974 // If the location list for the window is not set, then set it
2975 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01002976 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002977 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002978}
2979
2980/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002981 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2982 * not found, then go to the window just above the quickfix window. This is
2983 * used for opening a file from a quickfix window and not from a location
2984 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002985 */
2986 static void
2987qf_goto_win_with_qfl_file(int qf_fnum)
2988{
2989 win_T *win;
2990 win_T *altwin;
2991
2992 win = curwin;
2993 altwin = NULL;
2994 for (;;)
2995 {
2996 if (win->w_buffer->b_fnum == qf_fnum)
2997 break;
2998 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002999 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003000 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003001 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003002
3003 if (IS_QF_WINDOW(win))
3004 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003005 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003006 // window, unless 'switchbuf' contains 'uselast': in this case we
3007 // try to jump to the previously used window first.
3008 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3009 win = prevwin;
3010 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003011 win = altwin;
3012 else if (curwin->w_prev != NULL)
3013 win = curwin->w_prev;
3014 else
3015 win = curwin->w_next;
3016 break;
3017 }
3018
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003019 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003020 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003021 altwin = win;
3022 }
3023
3024 win_goto(win);
3025}
3026
3027/*
3028 * Find a suitable window for opening a file (qf_fnum) from the
3029 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003030 * window, jump to it. Otherwise open a new window to display the file. If
3031 * 'newwin' is TRUE, then always open a new window. This is called from either
3032 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003033 */
3034 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003035qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003036{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003037 win_T *usable_wp = NULL;
3038 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003039 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003040
Bram Moolenaarb2443732018-11-11 22:50:27 +01003041 // If opening a new window, then don't use the location list referred by
3042 // the current window. Otherwise two windows will refer to the same
3043 // location list.
3044 if (!newwin)
3045 ll_ref = curwin->w_llist_ref;
3046
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003047 if (ll_ref != NULL)
3048 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003049 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003050 usable_wp = qf_find_win_with_loclist(ll_ref);
3051 if (usable_wp != NULL)
3052 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003053 }
3054
3055 if (!usable_win)
3056 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003057 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003058 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003059 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003060 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003061 }
3062
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003063 // If no usable window is found and 'switchbuf' contains "usetab"
3064 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003065 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003066 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003067
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003068 // If there is only one window and it is the quickfix window, create a
3069 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003070 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003071 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003072 if (qf_open_new_file_win(ll_ref) != OK)
3073 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003074 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003075 }
3076 else
3077 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003078 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003079 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003080 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003081 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003082 }
3083
3084 return OK;
3085}
3086
3087/*
3088 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003089 * Returns OK if successfully edited the file, FAIL on failing to open the
3090 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3091 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003092 */
3093 static int
3094qf_jump_edit_buffer(
3095 qf_info_T *qi,
3096 qfline_T *qf_ptr,
3097 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003098 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003099 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003100{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003101 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003102 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003103 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003104 int old_qf_curlist = qi->qf_curlist;
3105 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003106
3107 if (qf_ptr->qf_type == 1)
3108 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003109 // Open help file (do_ecmd() will set b_help flag, readfile() will
3110 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003111 if (!can_abandon(curbuf, forceit))
3112 {
3113 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003114 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003115 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003116
3117 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3118 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003119 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003120 }
3121 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003122 retval = buflist_getfile(qf_ptr->qf_fnum,
3123 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003124
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003125 // If a location list, check whether the associated window is still
3126 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003127 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003128 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003129 win_T *wp = win_id2wp(prev_winid);
3130 if (wp == NULL && curwin->w_llist != qi)
3131 {
3132 emsg(_("E924: Current window was closed"));
3133 *opened_window = FALSE;
3134 return NOTDONE;
3135 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003136 }
3137
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003138 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003139 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003140 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003141 return NOTDONE;
3142 }
3143
3144 if (old_qf_curlist != qi->qf_curlist
3145 || !is_qf_entry_present(qfl, qf_ptr))
3146 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003147 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003148 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003149 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003150 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003151 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003152 }
3153
3154 return retval;
3155}
3156
3157/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003158 * Go to the error line in the current file using either line/column number or
3159 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003160 */
3161 static void
3162qf_jump_goto_line(
3163 linenr_T qf_lnum,
3164 int qf_col,
3165 char_u qf_viscol,
3166 char_u *qf_pattern)
3167{
3168 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003169
3170 if (qf_pattern == NULL)
3171 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003172 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003173 i = qf_lnum;
3174 if (i > 0)
3175 {
3176 if (i > curbuf->b_ml.ml_line_count)
3177 i = curbuf->b_ml.ml_line_count;
3178 curwin->w_cursor.lnum = i;
3179 }
3180 if (qf_col > 0)
3181 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003182 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003183 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003184 coladvance(qf_col - 1);
3185 else
3186 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003187 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003188 check_cursor();
3189 }
3190 else
3191 beginline(BL_WHITE | BL_FIX);
3192 }
3193 else
3194 {
3195 pos_T save_cursor;
3196
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003197 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003198 save_cursor = curwin->w_cursor;
3199 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003200 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003201 curwin->w_cursor = save_cursor;
3202 }
3203}
3204
3205/*
3206 * Display quickfix list index and size message
3207 */
3208 static void
3209qf_jump_print_msg(
3210 qf_info_T *qi,
3211 int qf_index,
3212 qfline_T *qf_ptr,
3213 buf_T *old_curbuf,
3214 linenr_T old_lnum)
3215{
3216 linenr_T i;
3217 int len;
3218
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003219 // Update the screen before showing the message, unless the screen
3220 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003221 if (!msg_scrolled)
3222 update_topline_redraw();
3223 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003224 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003225 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3226 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003227 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003228 len = (int)STRLEN(IObuff);
3229 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3230
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003231 // Output the message. Overwrite to avoid scrolling when the 'O'
3232 // flag is present in 'shortmess'; But when not jumping, print the
3233 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003234 i = msg_scroll;
3235 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3236 msg_scroll = TRUE;
3237 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3238 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003239 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003240 msg_scroll = i;
3241}
3242
3243/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003244 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003245 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3246 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003247 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3248 * able to jump/open a window. Returns NOTDONE if a file is not associated
3249 * with the entry.
3250 */
3251 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003252qf_jump_open_window(
3253 qf_info_T *qi,
3254 qfline_T *qf_ptr,
3255 int newwin,
3256 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003257{
3258 // For ":helpgrep" find a help window or open one.
3259 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003260 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003261 return FAIL;
3262
3263 // If currently in the quickfix window, find another window to show the
3264 // file in.
3265 if (bt_quickfix(curbuf) && !*opened_window)
3266 {
3267 // If there is no file specified, we don't know where to go.
3268 // But do advance, otherwise ":cn" gets stuck.
3269 if (qf_ptr->qf_fnum == 0)
3270 return NOTDONE;
3271
Bram Moolenaarb2443732018-11-11 22:50:27 +01003272 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3273 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003274 return FAIL;
3275 }
3276
3277 return OK;
3278}
3279
3280/*
3281 * Edit a selected file from the quickfix/location list and jump to a
3282 * particular line/column, adjust the folds and display a message about the
3283 * jump.
3284 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3285 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3286 * the file.
3287 */
3288 static int
3289qf_jump_to_buffer(
3290 qf_info_T *qi,
3291 int qf_index,
3292 qfline_T *qf_ptr,
3293 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003294 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003295 int *opened_window,
3296 int openfold,
3297 int print_message)
3298{
3299 buf_T *old_curbuf;
3300 linenr_T old_lnum;
3301 int retval = OK;
3302
3303 // If there is a file name, read the wanted file if needed, and check
3304 // autowrite etc.
3305 old_curbuf = curbuf;
3306 old_lnum = curwin->w_cursor.lnum;
3307
3308 if (qf_ptr->qf_fnum != 0)
3309 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003310 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003311 opened_window);
3312 if (retval != OK)
3313 return retval;
3314 }
3315
3316 // When not switched to another buffer, still need to set pc mark
3317 if (curbuf == old_curbuf)
3318 setpcmark();
3319
3320 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3321 qf_ptr->qf_pattern);
3322
3323#ifdef FEAT_FOLDING
3324 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3325 foldOpenCursor();
3326#endif
3327 if (print_message)
3328 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3329
3330 return retval;
3331}
3332
3333/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003334 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 */
3336 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003337qf_jump(qf_info_T *qi,
3338 int dir,
3339 int errornr,
3340 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003342 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3343}
3344
3345/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003346 * Jump to a quickfix line.
3347 * If dir == 0 go to entry "errornr".
3348 * If dir == FORWARD go "errornr" valid entries forward.
3349 * If dir == BACKWARD go "errornr" valid entries backward.
3350 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3351 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3352 * else if "errornr" is zero, redisplay the same line
3353 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003354 * If 'newwin' is TRUE, then open the file in a new window.
3355 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003356 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003357qf_jump_newwin(qf_info_T *qi,
3358 int dir,
3359 int errornr,
3360 int forceit,
3361 int newwin)
3362{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003363 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003364 qfline_T *qf_ptr;
3365 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003368 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003369 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003370 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003373 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003374 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003376 if (qi == NULL)
3377 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003378
Bram Moolenaar0398e002019-03-21 21:12:49 +01003379 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003381 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 return;
3383 }
3384
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003385 incr_quickfix_busy();
3386
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003387 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003388
3389 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003391 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003393
3394 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3395 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003397 qf_ptr = old_qf_ptr;
3398 qf_index = old_qf_index;
3399 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003402 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003403 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003404 // No need to print the error message if it's visible in the error
3405 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 print_message = FALSE;
3407
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003408 prev_winid = curwin->w_id;
3409
Bram Moolenaarb2443732018-11-11 22:50:27 +01003410 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003411 if (retval == FAIL)
3412 goto failed;
3413 if (retval == NOTDONE)
3414 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003415
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003416 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003417 &opened_window, old_KeyTyped, print_message);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003418 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003420 // Quickfix/location list is freed by an autocmd
3421 qi = NULL;
3422 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003425 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003428 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003429 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003431 // Couldn't open file, so put index back where it was. This could
3432 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 qf_ptr = old_qf_ptr;
3435 qf_index = old_qf_index;
3436 }
3437 }
3438theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003439 if (qi != NULL)
3440 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003441 qfl->qf_ptr = qf_ptr;
3442 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003443 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003444 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003446 // Restore old 'switchbuf' value, but not when an autocommand or
3447 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003448 p_swb = old_swb;
3449 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003451 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452}
3453
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003454// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003455static int qfFileAttr;
3456static int qfSepAttr;
3457static int qfLineAttr;
3458
3459/*
3460 * Display information about a single entry from the quickfix/location list.
3461 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003462 * 'cursel' will be set to TRUE for the currently selected entry in the
3463 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003464 */
3465 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003466qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003467{
3468 char_u *fname;
3469 buf_T *buf;
3470 int filter_entry;
3471
3472 fname = NULL;
3473 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3474 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3475 (char *)qfp->qf_module);
3476 else {
3477 if (qfp->qf_fnum != 0
3478 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3479 {
3480 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003481 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003482 fname = gettail(fname);
3483 }
3484 if (fname == NULL)
3485 sprintf((char *)IObuff, "%2d", qf_idx);
3486 else
3487 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3488 qf_idx, (char *)fname);
3489 }
3490
3491 // Support for filtering entries using :filter /pat/ clist
3492 // Match against the module name, file name, search pattern and
3493 // text of the entry.
3494 filter_entry = TRUE;
3495 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3496 filter_entry &= message_filtered(qfp->qf_module);
3497 if (filter_entry && fname != NULL)
3498 filter_entry &= message_filtered(fname);
3499 if (filter_entry && qfp->qf_pattern != NULL)
3500 filter_entry &= message_filtered(qfp->qf_pattern);
3501 if (filter_entry)
3502 filter_entry &= message_filtered(qfp->qf_text);
3503 if (filter_entry)
3504 return;
3505
3506 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003507 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003508
3509 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003510 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003511 if (qfp->qf_lnum == 0)
3512 IObuff[0] = NUL;
3513 else if (qfp->qf_col == 0)
3514 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3515 else
3516 sprintf((char *)IObuff, "%ld col %d",
3517 qfp->qf_lnum, qfp->qf_col);
3518 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3519 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003520 msg_puts_attr((char *)IObuff, qfLineAttr);
3521 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003522 if (qfp->qf_pattern != NULL)
3523 {
3524 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003525 msg_puts((char *)IObuff);
3526 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003527 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003528 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003529
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003530 // Remove newlines and leading whitespace from the text. For an
3531 // unrecognized line keep the indent, the compiler may mark a word
3532 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003533 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3534 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3535 IObuff, IOSIZE);
3536 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003537 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003538}
3539
3540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003542 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 */
3544 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003545qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003547 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003548 qfline_T *qfp;
3549 int i;
3550 int idx1 = 1;
3551 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003552 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003553 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003554 int all = eap->forceit; // if not :cl!, only show
3555 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003556 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003558 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3559 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003560
Bram Moolenaar0398e002019-03-21 21:12:49 +01003561 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003563 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 return;
3565 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003566 if (*arg == '+')
3567 {
3568 ++arg;
3569 plus = TRUE;
3570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3572 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003573 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 return;
3575 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003576 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003577 if (plus)
3578 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003579 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003580 idx2 = i + idx1;
3581 idx1 = i;
3582 }
3583 else
3584 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003585 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003586 if (idx1 < 0)
3587 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3588 if (idx2 < 0)
3589 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003592 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003593 shorten_fnames(FALSE);
3594
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003595 // Get the attributes for the different quickfix highlight items. Note
3596 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003597 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3598 if (qfFileAttr == 0)
3599 qfFileAttr = HL_ATTR(HLF_D);
3600 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3601 if (qfSepAttr == 0)
3602 qfSepAttr = HL_ATTR(HLF_D);
3603 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3604 if (qfLineAttr == 0)
3605 qfLineAttr = HL_ATTR(HLF_N);
3606
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003607 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003609 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
3611 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003612 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 ui_breakcheck();
3615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616}
3617
3618/*
3619 * Remove newlines and leading whitespace from an error message.
3620 * Put the result in "buf[bufsize]".
3621 */
3622 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003623qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624{
3625 int i;
3626 char_u *p = text;
3627
3628 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3629 {
3630 if (*p == '\n')
3631 {
3632 buf[i] = ' ';
3633 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003634 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 break;
3636 }
3637 else
3638 buf[i] = *p++;
3639 }
3640 buf[i] = NUL;
3641}
3642
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003643/*
3644 * Display information (list number, list size and the title) about a
3645 * quickfix/location list.
3646 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003647 static void
3648qf_msg(qf_info_T *qi, int which, char *lead)
3649{
3650 char *title = (char *)qi->qf_lists[which].qf_title;
3651 int count = qi->qf_lists[which].qf_count;
3652 char_u buf[IOSIZE];
3653
3654 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3655 lead,
3656 which + 1,
3657 qi->qf_listcount,
3658 count);
3659
3660 if (title != NULL)
3661 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003662 size_t len = STRLEN(buf);
3663
3664 if (len < 34)
3665 {
3666 vim_memset(buf + len, ' ', 34 - len);
3667 buf[34] = NUL;
3668 }
3669 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003670 }
3671 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003672 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003673}
3674
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675/*
3676 * ":colder [count]": Up in the quickfix stack.
3677 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003678 * ":lolder [count]": Up in the location list stack.
3679 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 */
3681 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003682qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003684 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 int count;
3686
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003687 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3688 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 if (eap->addr_count != 0)
3691 count = eap->line2;
3692 else
3693 count = 1;
3694 while (count--)
3695 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003696 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003698 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003700 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003701 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003703 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705 else
3706 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003707 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003709 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003710 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003712 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 }
3714 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003715 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003716 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717}
3718
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003719/*
3720 * Display the information about all the quickfix/location lists in the stack
3721 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003722 void
3723qf_history(exarg_T *eap)
3724{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003725 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003726 int i;
3727
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003728 if (eap->addr_count > 0)
3729 {
3730 if (qi == NULL)
3731 {
3732 emsg(_(e_loclist));
3733 return;
3734 }
3735
3736 // Jump to the specified quickfix list
3737 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3738 {
3739 qi->qf_curlist = eap->line2 - 1;
3740 qf_msg(qi, qi->qf_curlist, "");
3741 qf_update_buffer(qi, NULL);
3742 }
3743 else
3744 emsg(_(e_invrange));
3745
3746 return;
3747 }
3748
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003749 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003750 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003751 else
3752 for (i = 0; i < qi->qf_listcount; ++i)
3753 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3754}
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003757 * Free all the entries in the error list "idx". Note that other information
3758 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 */
3760 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003761qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003763 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003764 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003765 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003767 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003769 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003770 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003771 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003772 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003773 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003774 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003775 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003776 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003777 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003778 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003779 // Somehow qf_count may have an incorrect value, set it to 1
3780 // to avoid crashing when it's wrong.
3781 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003782 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003783 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003784 qfl->qf_start = qfpnext;
3785 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003787
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003788 qfl->qf_index = 0;
3789 qfl->qf_start = NULL;
3790 qfl->qf_last = NULL;
3791 qfl->qf_ptr = NULL;
3792 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003793
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003794 qf_clean_dir_stack(&qfl->qf_dir_stack);
3795 qfl->qf_directory = NULL;
3796 qf_clean_dir_stack(&qfl->qf_file_stack);
3797 qfl->qf_currfile = NULL;
3798 qfl->qf_multiline = FALSE;
3799 qfl->qf_multiignore = FALSE;
3800 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801}
3802
3803/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003804 * Free error list "idx". Frees all the entries in the quickfix list,
3805 * associated context information and the title.
3806 */
3807 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003808qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003809{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003810 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003811
Bram Moolenaard23a8232018-02-10 18:45:26 +01003812 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003813 free_tv(qfl->qf_ctx);
3814 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003815 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003816 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003817}
3818
3819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 * qf_mark_adjust: adjust marks
3821 */
3822 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003823qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003824 win_T *wp,
3825 linenr_T line1,
3826 linenr_T line2,
3827 long amount,
3828 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003830 int i;
3831 qfline_T *qfp;
3832 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003833 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003834 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003835 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
Bram Moolenaarc1542742016-07-20 21:44:37 +02003837 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003838 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003839 if (wp != NULL)
3840 {
3841 if (wp->w_llist == NULL)
3842 return;
3843 qi = wp->w_llist;
3844 }
3845
3846 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003847 {
3848 qf_list_T *qfl = qf_get_list(qi, idx);
3849
3850 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003851 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 if (qfp->qf_fnum == curbuf->b_fnum)
3853 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003854 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3856 {
3857 if (amount == MAXLNUM)
3858 qfp->qf_cleared = TRUE;
3859 else
3860 qfp->qf_lnum += amount;
3861 }
3862 else if (amount_after && qfp->qf_lnum > line2)
3863 qfp->qf_lnum += amount_after;
3864 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003865 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003866
3867 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003868 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869}
3870
3871/*
3872 * Make a nice message out of the error character and the error number:
3873 * char number message
3874 * e or E 0 " error"
3875 * w or W 0 " warning"
3876 * i or I 0 " info"
3877 * 0 0 ""
3878 * other 0 " c"
3879 * e or E n " error n"
3880 * w or W n " warning n"
3881 * i or I n " info n"
3882 * 0 n " error n"
3883 * other n " c n"
3884 * 1 x "" :helpgrep
3885 */
3886 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003887qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888{
3889 static char_u buf[20];
3890 static char_u cc[3];
3891 char_u *p;
3892
3893 if (c == 'W' || c == 'w')
3894 p = (char_u *)" warning";
3895 else if (c == 'I' || c == 'i')
3896 p = (char_u *)" info";
3897 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3898 p = (char_u *)" error";
3899 else if (c == 0 || c == 1)
3900 p = (char_u *)"";
3901 else
3902 {
3903 cc[0] = ' ';
3904 cc[1] = c;
3905 cc[2] = NUL;
3906 p = cc;
3907 }
3908
3909 if (nr <= 0)
3910 return p;
3911
3912 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3913 return buf;
3914}
3915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003917 * When "split" is FALSE: Open the entry/result under the cursor.
3918 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3919 */
3920 void
3921qf_view_result(int split)
3922{
3923 qf_info_T *qi = &ql_info;
3924
3925 if (!bt_quickfix(curbuf))
3926 return;
3927
3928 if (IS_LL_WINDOW(curwin))
3929 qi = GET_LOC_LIST(curwin);
3930
Bram Moolenaar0398e002019-03-21 21:12:49 +01003931 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003933 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003934 return;
3935 }
3936
3937 if (split)
3938 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003939 // Open the selected entry in a new window
3940 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3941 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003942 return;
3943 }
3944
3945 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3946}
3947
3948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 * ":cwindow": open the quickfix window if we have errors to display,
3950 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003951 * ":lwindow": open the location list window if we have locations to display,
3952 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 */
3954 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003955ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003957 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003958 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 win_T *win;
3960
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003961 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3962 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003963
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003964 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003965
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003966 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003967 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003969 // If a quickfix window is open but we have no errors to display,
3970 // close the window. If a quickfix window is not open, then open
3971 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003972 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003973 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01003974 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 {
3976 if (win != NULL)
3977 ex_cclose(eap);
3978 }
3979 else if (win == NULL)
3980 ex_copen(eap);
3981}
3982
3983/*
3984 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003985 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003988ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003990 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003991 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003993 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
3994 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003996 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003997 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (win != NULL)
3999 win_close(win, FALSE);
4000}
4001
4002/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004003 * Set "w:quickfix_title" if "qi" has a title.
4004 */
4005 static void
4006qf_set_title_var(qf_list_T *qfl)
4007{
4008 if (qfl->qf_title != NULL)
4009 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4010}
4011
4012/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004013 * Goto a quickfix or location list window (if present).
4014 * Returns OK if the window is found, FAIL otherwise.
4015 */
4016 static int
4017qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4018{
4019 win_T *win;
4020
4021 win = qf_find_win(qi);
4022 if (win == NULL)
4023 return FAIL;
4024
4025 win_goto(win);
4026 if (resize)
4027 {
4028 if (vertsplit)
4029 {
4030 if (sz != win->w_width)
4031 win_setwidth(sz);
4032 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004033 else if (sz != win->w_height && win->w_height
4034 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004035 win_setheight(sz);
4036 }
4037
4038 return OK;
4039}
4040
4041/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004042 * Set options for the buffer in the quickfix or location list window.
4043 */
4044 static void
4045qf_set_cwindow_options(void)
4046{
4047 // switch off 'swapfile'
4048 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4049 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4050 OPT_LOCAL);
4051 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4052 RESET_BINDING(curwin);
4053#ifdef FEAT_DIFF
4054 curwin->w_p_diff = FALSE;
4055#endif
4056#ifdef FEAT_FOLDING
4057 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4058 OPT_LOCAL);
4059#endif
4060}
4061
4062/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004063 * Open a new quickfix or location list window, load the quickfix buffer and
4064 * set the appropriate options for the window.
4065 * Returns FAIL if the window could not be opened.
4066 */
4067 static int
4068qf_open_new_cwindow(qf_info_T *qi, int height)
4069{
4070 buf_T *qf_buf;
4071 win_T *oldwin = curwin;
4072 tabpage_T *prevtab = curtab;
4073 int flags = 0;
4074 win_T *win;
4075
4076 qf_buf = qf_find_buf(qi);
4077
4078 // The current window becomes the previous window afterwards.
4079 win = curwin;
4080
4081 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4082 // Create the new quickfix window at the very bottom, except when
4083 // :belowright or :aboveleft is used.
4084 win_goto(lastwin);
4085 // Default is to open the window below the current window
4086 if (cmdmod.split == 0)
4087 flags = WSP_BELOW;
4088 flags |= WSP_NEWLOC;
4089 if (win_split(height, flags) == FAIL)
4090 return FAIL; // not enough room for window
4091 RESET_BINDING(curwin);
4092
4093 if (IS_LL_STACK(qi))
4094 {
4095 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004096 // location list stack from the window 'win'.
4097 curwin->w_llist_ref = qi;
4098 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004099 }
4100
4101 if (oldwin != curwin)
4102 oldwin = NULL; // don't store info when in another window
4103 if (qf_buf != NULL)
4104 {
4105 // Use the existing quickfix buffer
4106 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4107 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4108 }
4109 else
4110 {
4111 // Create a new quickfix buffer
4112 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4113
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004114 // save the number of the new buffer
4115 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004116 }
4117
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004118 // Set the options for the quickfix buffer/window (if not already done)
4119 // Do this even if the quickfix buffer was already present, as an autocmd
4120 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004121 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004122 qf_set_cwindow_options();
4123
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004124 // Only set the height when still in the same tab page and there is no
4125 // window to the side.
4126 if (curtab == prevtab && curwin->w_width == Columns)
4127 win_setheight(height);
4128 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4129 if (win_valid(win))
4130 prevwin = win;
4131
4132 return OK;
4133}
4134
4135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004137 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 */
4139 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004140ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004142 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004143 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004145 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004146 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004148 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4149 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004150
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004151 incr_quickfix_busy();
4152
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 if (eap->addr_count != 0)
4154 height = eap->line2;
4155 else
4156 height = QF_WINHEIGHT;
4157
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004158 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159#ifdef FEAT_GUI
4160 need_mouse_correct = TRUE;
4161#endif
4162
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004163 // Find an existing quickfix window, or open a new one.
4164 if (cmdmod.tab == 0)
4165 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4166 cmdmod.split & WSP_VERT);
4167 if (status == FAIL)
4168 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004169 {
4170 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004171 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004174 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004175 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004176 // Save the current index here, as updating the quickfix buffer may free
4177 // the quickfix list
4178 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004179
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004180 // Fill the buffer with the quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004181 qf_fill_buffer(qfl, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004183 decr_quickfix_busy();
4184
4185 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 curwin->w_cursor.col = 0;
4187 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004188 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189}
4190
4191/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004192 * Move the cursor in the quickfix window to "lnum".
4193 */
4194 static void
4195qf_win_goto(win_T *win, linenr_T lnum)
4196{
4197 win_T *old_curwin = curwin;
4198
4199 curwin = win;
4200 curbuf = win->w_buffer;
4201 curwin->w_cursor.lnum = lnum;
4202 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004203 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004204 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004205 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004206 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004207 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004208 curwin = old_curwin;
4209 curbuf = curwin->w_buffer;
4210}
4211
4212/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004213 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004214 */
4215 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004216ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004217{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004218 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004219 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004220
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004221 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4222 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004223
4224 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004225 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4226 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4227}
4228
4229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 * Return the number of the current entry (line number in the quickfix
4231 * window).
4232 */
4233 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004234qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004236 qf_info_T *qi = &ql_info;
4237
4238 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004239 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004240 qi = wp->w_llist_ref;
4241
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004242 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243}
4244
4245/*
4246 * Update the cursor position in the quickfix window to the current error.
4247 * Return TRUE if there is a quickfix window.
4248 */
4249 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004250qf_win_pos_update(
4251 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004252 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
4254 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004255 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004257 // Put the cursor on the current error in the quickfix window, so that
4258 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004259 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (win != NULL
4261 && qf_index <= win->w_buffer->b_ml.ml_line_count
4262 && old_qf_index != qf_index)
4263 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 if (qf_index > old_qf_index)
4265 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004266 win->w_redraw_top = old_qf_index;
4267 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 else
4270 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004271 win->w_redraw_top = qf_index;
4272 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004274 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 return win != NULL;
4277}
4278
4279/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004280 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004281 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004282 */
4283 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004284is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004285{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004286 // A window displaying the quickfix buffer will have the w_llist_ref field
4287 // set to NULL.
4288 // A window displaying a location list buffer will have the w_llist_ref
4289 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004290 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004291 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4292 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004293 return TRUE;
4294
4295 return FALSE;
4296}
4297
4298/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004299 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004300 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004301 */
4302 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004303qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004304{
4305 win_T *win;
4306
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004307 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004308 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004309 return win;
4310 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004311}
4312
4313/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004314 * Find a quickfix buffer.
4315 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 */
4317 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004318qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004320 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004321 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004323 if (qi->qf_bufnr != INVALID_QFBUFNR)
4324 {
4325 buf_T *qfbuf;
4326 qfbuf = buflist_findnr(qi->qf_bufnr);
4327 if (qfbuf != NULL)
4328 return qfbuf;
4329 // buffer is no longer present
4330 qi->qf_bufnr = INVALID_QFBUFNR;
4331 }
4332
Bram Moolenaar9c102382006-05-03 21:26:49 +00004333 FOR_ALL_TAB_WINDOWS(tp, win)
4334 if (is_qf_win(win, qi))
4335 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004336
Bram Moolenaar9c102382006-05-03 21:26:49 +00004337 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338}
4339
4340/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004341 * Update the w:quickfix_title variable in the quickfix/location list window
4342 */
4343 static void
4344qf_update_win_titlevar(qf_info_T *qi)
4345{
4346 win_T *win;
4347 win_T *curwin_save;
4348
4349 if ((win = qf_find_win(qi)) != NULL)
4350 {
4351 curwin_save = curwin;
4352 curwin = win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004353 qf_set_title_var(qf_get_curlist(qi));
Bram Moolenaard823fa92016-08-12 16:29:27 +02004354 curwin = curwin_save;
4355 }
4356}
4357
4358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 * Find the quickfix buffer. If it exists, update the contents.
4360 */
4361 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004362qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363{
4364 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004365 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004368 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004369 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (buf != NULL)
4371 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004372 linenr_T old_line_count = buf->b_ml.ml_line_count;
4373
4374 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004375 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004376 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
Bram Moolenaard823fa92016-08-12 16:29:27 +02004378 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004379
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004380 qf_fill_buffer(qf_get_curlist(qi), buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004381 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004382
Bram Moolenaar864293a2016-06-02 13:40:04 +02004383 if (old_last == NULL)
4384 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004385 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004386
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004387 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004388 aucmd_restbuf(&aco);
4389 }
4390
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004391 // Only redraw when added lines are visible. This avoids flickering
4392 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004393 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4394 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
4396}
4397
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004398/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004399 * Add an error line to the quickfix buffer.
4400 */
4401 static int
4402qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4403{
4404 int len;
4405 buf_T *errbuf;
4406
4407 if (qfp->qf_module != NULL)
4408 {
Bram Moolenaara106e6c2019-11-24 22:13:58 +01004409 vim_strncpy(IObuff, qfp->qf_module, IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004410 len = (int)STRLEN(IObuff);
4411 }
4412 else if (qfp->qf_fnum != 0
4413 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4414 && errbuf->b_fname != NULL)
4415 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004416 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaara106e6c2019-11-24 22:13:58 +01004417 vim_strncpy(IObuff, gettail(errbuf->b_fname), IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004418 else
4419 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004420 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004421 if (errbuf->b_sfname == NULL
4422 || mch_isFullName(errbuf->b_sfname))
4423 {
4424 if (*dirname == NUL)
4425 mch_dirname(dirname, MAXPATHL);
4426 shorten_buf_fname(errbuf, dirname, FALSE);
4427 }
Bram Moolenaara106e6c2019-11-24 22:13:58 +01004428 vim_strncpy(IObuff, errbuf->b_fname, IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004429 }
4430 len = (int)STRLEN(IObuff);
4431 }
4432 else
4433 len = 0;
Bram Moolenaara106e6c2019-11-24 22:13:58 +01004434
4435 if (len < IOSIZE - 1)
4436 IObuff[len++] = '|';
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004437
4438 if (qfp->qf_lnum > 0)
4439 {
Bram Moolenaara106e6c2019-11-24 22:13:58 +01004440 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%ld", qfp->qf_lnum);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004441 len += (int)STRLEN(IObuff + len);
4442
4443 if (qfp->qf_col > 0)
4444 {
Bram Moolenaara106e6c2019-11-24 22:13:58 +01004445 vim_snprintf((char *)IObuff + len, IOSIZE - len,
4446 " col %d", qfp->qf_col);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004447 len += (int)STRLEN(IObuff + len);
4448 }
4449
Bram Moolenaara106e6c2019-11-24 22:13:58 +01004450 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%s",
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004451 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4452 len += (int)STRLEN(IObuff + len);
4453 }
4454 else if (qfp->qf_pattern != NULL)
4455 {
4456 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4457 len += (int)STRLEN(IObuff + len);
4458 }
Bram Moolenaara106e6c2019-11-24 22:13:58 +01004459 if (len < IOSIZE - 2)
4460 {
4461 IObuff[len++] = '|';
4462 IObuff[len++] = ' ';
4463 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004464
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004465 // Remove newlines and leading whitespace from the text.
4466 // For an unrecognized line keep the indent, the compiler may
4467 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004468 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4469 IObuff + len, IOSIZE - len);
4470
4471 if (ml_append_buf(buf, lnum, IObuff,
4472 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4473 return FAIL;
4474
4475 return OK;
4476}
4477
4478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 * Fill current buffer with quickfix errors, replacing any previous contents.
4480 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004481 * If "old_last" is not NULL append the items after this one.
4482 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4483 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 */
4485 static void
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004486qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004488 linenr_T lnum;
4489 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004490 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
Bram Moolenaar864293a2016-06-02 13:40:04 +02004492 if (old_last == NULL)
4493 {
4494 if (buf != curbuf)
4495 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004496 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004497 return;
4498 }
4499
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004500 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004501 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4502 (void)ml_delete((linenr_T)1, FALSE);
4503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004505 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004506 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004508 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004509
4510 *dirname = NUL;
4511
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004512 // Add one line for each error
Bram Moolenaar99234f22020-02-10 22:56:54 +01004513 if (old_last == NULL || old_last->qf_next == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004514 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004515 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004516 lnum = 0;
4517 }
4518 else
4519 {
4520 qfp = old_last->qf_next;
4521 lnum = buf->b_ml.ml_line_count;
4522 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004523 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004525 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004527
Bram Moolenaar864293a2016-06-02 13:40:04 +02004528 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004530 if (qfp == NULL)
4531 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004533
4534 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004535 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004536 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004539 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 check_lnums(TRUE);
4541
Bram Moolenaar864293a2016-06-02 13:40:04 +02004542 if (old_last == NULL)
4543 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004544 // Set the 'filetype' to "qf" each time after filling the buffer.
4545 // This resembles reading a file into a buffer, it's more logical when
4546 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004547 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004548 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4549 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004551 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004552 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004554 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004556 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004557 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004558
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004559 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004560 redraw_curbuf_later(NOT_VALID);
4561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004563 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 KeyTyped = old_KeyTyped;
4565}
4566
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004567/*
4568 * For every change made to the quickfix list, update the changed tick.
4569 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004570 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004571qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004572{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004573 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004574}
4575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004577 * Return the quickfix/location list number with the given identifier.
4578 * Returns -1 if list is not found.
4579 */
4580 static int
4581qf_id2nr(qf_info_T *qi, int_u qfid)
4582{
4583 int qf_idx;
4584
4585 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4586 if (qi->qf_lists[qf_idx].qf_id == qfid)
4587 return qf_idx;
4588 return INVALID_QFIDX;
4589}
4590
4591/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004592 * If the current list is not "save_qfid" and we can find the list with that ID
4593 * then make it the current list.
4594 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004595 * Returns OK if successfully restored the list. Returns FAIL if the list with
4596 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004597 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004598 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004599qf_restore_list(qf_info_T *qi, int_u save_qfid)
4600{
4601 int curlist;
4602
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004603 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004604 {
4605 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004606 if (curlist < 0)
4607 // list is not present
4608 return FAIL;
4609 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004610 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004611 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004612}
4613
4614/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004615 * Jump to the first entry if there is one.
4616 */
4617 static void
4618qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4619{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004620 if (qf_restore_list(qi, save_qfid) == FAIL)
4621 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004622
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004623 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004624 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004625 qf_jump(qi, 0, 0, forceit);
4626}
4627
4628/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004629 * Return TRUE when using ":vimgrep" for ":grep".
4630 */
4631 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004632grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004633{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004634 return ((cmdidx == CMD_grep
4635 || cmdidx == CMD_lgrep
4636 || cmdidx == CMD_grepadd
4637 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004638 && STRCMP("internal",
4639 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4640}
4641
4642/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004643 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004645 static char_u *
4646make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004648 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004649 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004650 case CMD_make: return (char_u *)"make";
4651 case CMD_lmake: return (char_u *)"lmake";
4652 case CMD_grep: return (char_u *)"grep";
4653 case CMD_lgrep: return (char_u *)"lgrep";
4654 case CMD_grepadd: return (char_u *)"grepadd";
4655 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4656 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658}
4659
4660/*
4661 * Return the name for the errorfile, in allocated memory.
4662 * Find a new unique name when 'makeef' contains "##".
4663 * Returns NULL for error.
4664 */
4665 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004666get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667{
4668 char_u *p;
4669 char_u *name;
4670 static int start = -1;
4671 static int off = 0;
4672#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004673 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674#endif
4675
4676 if (*p_mef == NUL)
4677 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004678 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004680 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 return name;
4682 }
4683
4684 for (p = p_mef; *p; ++p)
4685 if (p[0] == '#' && p[1] == '#')
4686 break;
4687
4688 if (*p == NUL)
4689 return vim_strsave(p_mef);
4690
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004691 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 for (;;)
4693 {
4694 if (start == -1)
4695 start = mch_get_pid();
4696 else
4697 off += 19;
4698
Bram Moolenaar964b3742019-05-24 18:54:09 +02004699 name = alloc(STRLEN(p_mef) + 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 if (name == NULL)
4701 break;
4702 STRCPY(name, p_mef);
4703 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4704 STRCAT(name, p + 2);
4705 if (mch_getperm(name) < 0
4706#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004707 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 && mch_lstat((char *)name, &sb) < 0
4709#endif
4710 )
4711 break;
4712 vim_free(name);
4713 }
4714 return name;
4715}
4716
4717/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004718 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4719 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4720 */
4721 static char_u *
4722make_get_fullcmd(char_u *makecmd, char_u *fname)
4723{
4724 char_u *cmd;
4725 unsigned len;
4726
4727 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4728 if (*p_sp != NUL)
4729 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4730 cmd = alloc(len);
4731 if (cmd == NULL)
4732 return NULL;
4733 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4734 (char *)p_shq);
4735
4736 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4737 if (*p_sp != NUL)
4738 append_redir(cmd, len, p_sp, fname);
4739
4740 // Display the fully formed command. Output a newline if there's something
4741 // else than the :make command that was typed (in which case the cursor is
4742 // in column 0).
4743 if (msg_col == 0)
4744 msg_didout = FALSE;
4745 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004746 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004747 msg_outtrans(cmd); // show what we are doing
4748
4749 return cmd;
4750}
4751
4752/*
4753 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4754 */
4755 void
4756ex_make(exarg_T *eap)
4757{
4758 char_u *fname;
4759 char_u *cmd;
4760 char_u *enc = NULL;
4761 win_T *wp = NULL;
4762 qf_info_T *qi = &ql_info;
4763 int res;
4764 char_u *au_name = NULL;
4765 int_u save_qfid;
4766
4767 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4768 if (grep_internal(eap->cmdidx))
4769 {
4770 ex_vimgrep(eap);
4771 return;
4772 }
4773
4774 au_name = make_get_auname(eap->cmdidx);
4775 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4776 curbuf->b_fname, TRUE, curbuf))
4777 {
4778#ifdef FEAT_EVAL
4779 if (aborting())
4780 return;
4781#endif
4782 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004783 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004784
4785 if (is_loclist_cmd(eap->cmdidx))
4786 wp = curwin;
4787
4788 autowrite_all();
4789 fname = get_mef_name();
4790 if (fname == NULL)
4791 return;
4792 mch_remove(fname); // in case it's not unique
4793
4794 cmd = make_get_fullcmd(eap->arg, fname);
4795 if (cmd == NULL)
4796 return;
4797
4798 // let the shell know if we are redirecting output or not
4799 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4800
4801#ifdef AMIGA
4802 out_flush();
4803 // read window status report and redraw before message
4804 (void)char_avail();
4805#endif
4806
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004807 incr_quickfix_busy();
4808
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004809 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4810 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4811 (eap->cmdidx != CMD_grepadd
4812 && eap->cmdidx != CMD_lgrepadd),
4813 qf_cmdtitle(*eap->cmdlinep), enc);
4814 if (wp != NULL)
4815 {
4816 qi = GET_LOC_LIST(wp);
4817 if (qi == NULL)
4818 goto cleanup;
4819 }
4820 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004821 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004822
4823 // Remember the current quickfix list identifier, so that we can
4824 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004825 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004826 if (au_name != NULL)
4827 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4828 curbuf->b_fname, TRUE, curbuf);
4829 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4830 // display the first error
4831 qf_jump_first(qi, save_qfid, FALSE);
4832
4833cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004834 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004835 mch_remove(fname);
4836 vim_free(fname);
4837 vim_free(cmd);
4838}
4839
4840/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02004841 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004842 */
4843 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004844qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004845{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004846 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004847
4848 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4849 return 0;
4850 return qf_get_curlist(qi)->qf_count;
4851}
4852
4853/*
4854 * Returns the number of valid entries in the current quickfix/location list.
4855 */
4856 int
4857qf_get_valid_size(exarg_T *eap)
4858{
4859 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004860 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004861 qfline_T *qfp;
4862 int i, sz = 0;
4863 int prev_fnum = 0;
4864
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004865 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4866 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004867
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004868 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01004869 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004870 {
4871 if (qfp->qf_valid)
4872 {
4873 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004874 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004875 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4876 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004877 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004878 sz++;
4879 prev_fnum = qfp->qf_fnum;
4880 }
4881 }
4882 }
4883
4884 return sz;
4885}
4886
4887/*
4888 * Returns the current index of the quickfix/location list.
4889 * Returns 0 if there is an error.
4890 */
4891 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004892qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004893{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004894 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004895
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004896 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4897 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004898
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004899 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004900}
4901
4902/*
4903 * Returns the current index in the quickfix/location list (counting only valid
4904 * entries). If no valid entries are in the list, then returns 1.
4905 */
4906 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004907qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004908{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004909 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004910 qf_list_T *qfl;
4911 qfline_T *qfp;
4912 int i, eidx = 0;
4913 int prev_fnum = 0;
4914
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004915 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4916 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004917
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004918 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004919 qfp = qfl->qf_start;
4920
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004921 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004922 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004923 return 1;
4924
4925 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4926 {
4927 if (qfp->qf_valid)
4928 {
4929 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4930 {
4931 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4932 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004933 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004934 eidx++;
4935 prev_fnum = qfp->qf_fnum;
4936 }
4937 }
4938 else
4939 eidx++;
4940 }
4941 }
4942
4943 return eidx ? eidx : 1;
4944}
4945
4946/*
4947 * Get the 'n'th valid error entry in the quickfix or location list.
4948 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4949 * For :cdo and :ldo returns the 'n'th valid error entry.
4950 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4951 */
4952 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004953qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004954{
Bram Moolenaar95946f12019-03-31 15:31:59 +02004955 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004956 int i, eidx;
4957 int prev_fnum = 0;
4958
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004959 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004960 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004961 return 1;
4962
Bram Moolenaar95946f12019-03-31 15:31:59 +02004963 eidx = 0;
4964 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004965 {
4966 if (qfp->qf_valid)
4967 {
4968 if (fdo)
4969 {
4970 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4971 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004972 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004973 eidx++;
4974 prev_fnum = qfp->qf_fnum;
4975 }
4976 }
4977 else
4978 eidx++;
4979 }
4980
4981 if (eidx == n)
4982 break;
4983 }
4984
4985 if (i <= qfl->qf_count)
4986 return i;
4987 else
4988 return 1;
4989}
4990
4991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004993 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004994 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 */
4996 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004997ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004999 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005000 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005001
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005002 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5003 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005004
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005005 if (eap->addr_count > 0)
5006 errornr = (int)eap->line2;
5007 else
5008 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005009 switch (eap->cmdidx)
5010 {
5011 case CMD_cc: case CMD_ll:
5012 errornr = 0;
5013 break;
5014 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5015 case CMD_lfirst:
5016 errornr = 1;
5017 break;
5018 default:
5019 errornr = 32767;
5020 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005021 }
5022
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005023 // For cdo and ldo commands, jump to the nth valid error.
5024 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005025 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5026 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005027 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005028 eap->addr_count > 0 ? (int)eap->line1 : 1,
5029 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5030
5031 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032}
5033
5034/*
5035 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005036 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005037 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 */
5039 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005040ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005042 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005043 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005044 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005045
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005046 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5047 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005048
Bram Moolenaar55b69262017-08-13 13:42:01 +02005049 if (eap->addr_count > 0
5050 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5051 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005052 errornr = (int)eap->line2;
5053 else
5054 errornr = 1;
5055
Bram Moolenaar39665952018-08-15 20:59:48 +02005056 // Depending on the command jump to either next or previous entry/file.
5057 switch (eap->cmdidx)
5058 {
5059 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5060 dir = FORWARD;
5061 break;
5062 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5063 case CMD_lNext:
5064 dir = BACKWARD;
5065 break;
5066 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5067 dir = FORWARD_FILE;
5068 break;
5069 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5070 dir = BACKWARD_FILE;
5071 break;
5072 default:
5073 dir = FORWARD;
5074 break;
5075 }
5076
5077 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078}
5079
5080/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005081 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5082 * The index of the entry is stored in 'errornr'.
5083 * Returns NULL if an entry is not found.
5084 */
5085 static qfline_T *
5086qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5087{
5088 qfline_T *qfp = NULL;
5089 int idx = 0;
5090
5091 // Find the first entry in this file
5092 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5093 if (qfp->qf_fnum == bnr)
5094 break;
5095
5096 *errornr = idx;
5097 return qfp;
5098}
5099
5100/*
5101 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5102 * with the error number for the first entry. Assumes the entries are sorted in
5103 * the quickfix list by line number.
5104 */
5105 static qfline_T *
5106qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5107{
5108 while (!got_int
5109 && entry->qf_prev != NULL
5110 && entry->qf_fnum == entry->qf_prev->qf_fnum
5111 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5112 {
5113 entry = entry->qf_prev;
5114 --*errornr;
5115 }
5116
5117 return entry;
5118}
5119
5120/*
5121 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5122 * with the error number for the last entry. Assumes the entries are sorted in
5123 * the quickfix list by line number.
5124 */
5125 static qfline_T *
5126qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5127{
5128 while (!got_int &&
5129 entry->qf_next != NULL
5130 && entry->qf_fnum == entry->qf_next->qf_fnum
5131 && entry->qf_lnum == entry->qf_next->qf_lnum)
5132 {
5133 entry = entry->qf_next;
5134 ++*errornr;
5135 }
5136
5137 return entry;
5138}
5139
5140/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005141 * Returns TRUE if the specified quickfix entry is
5142 * after the given line (linewise is TRUE)
5143 * or after the line and column.
5144 */
5145 static int
5146qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5147{
5148 if (linewise)
5149 return qfp->qf_lnum > pos->lnum;
5150 else
5151 return (qfp->qf_lnum > pos->lnum ||
5152 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5153}
5154
5155/*
5156 * Returns TRUE if the specified quickfix entry is
5157 * before the given line (linewise is TRUE)
5158 * or before the line and column.
5159 */
5160 static int
5161qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5162{
5163 if (linewise)
5164 return qfp->qf_lnum < pos->lnum;
5165 else
5166 return (qfp->qf_lnum < pos->lnum ||
5167 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5168}
5169
5170/*
5171 * Returns TRUE if the specified quickfix entry is
5172 * on or after the given line (linewise is TRUE)
5173 * or on or after the line and column.
5174 */
5175 static int
5176qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5177{
5178 if (linewise)
5179 return qfp->qf_lnum >= pos->lnum;
5180 else
5181 return (qfp->qf_lnum > pos->lnum ||
5182 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5183}
5184
5185/*
5186 * Returns TRUE if the specified quickfix entry is
5187 * on or before the given line (linewise is TRUE)
5188 * or on or before the line and column.
5189 */
5190 static int
5191qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5192{
5193 if (linewise)
5194 return qfp->qf_lnum <= pos->lnum;
5195 else
5196 return (qfp->qf_lnum < pos->lnum ||
5197 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5198}
5199
5200/*
5201 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5202 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5203 * multiple entries on a single line as one. Otherwise returns the entry after
5204 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005205 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5206 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005207 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005208 */
5209 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005210qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005211 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005212 pos_T *pos,
5213 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005214 qfline_T *qfp,
5215 int *errornr)
5216{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005217 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005218 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005219 return qfp;
5220
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005221 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005222 while (qfp->qf_next != NULL
5223 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005224 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005225 {
5226 qfp = qfp->qf_next;
5227 ++*errornr;
5228 }
5229
5230 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005231 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005232 return NULL;
5233
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005234 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005235 qfp = qfp->qf_next;
5236 ++*errornr;
5237
5238 return qfp;
5239}
5240
5241/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005242 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5243 * If 'linewise' is TRUE, returns the entry before the specified line and
5244 * treats multiple entries on a single line as one. Otherwise returns the entry
5245 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005246 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5247 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005248 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005249 */
5250 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005251qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005252 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005253 pos_T *pos,
5254 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005255 qfline_T *qfp,
5256 int *errornr)
5257{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005258 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005259 while (qfp->qf_next != NULL
5260 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005261 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005262 {
5263 qfp = qfp->qf_next;
5264 ++*errornr;
5265 }
5266
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005267 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005268 return NULL;
5269
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005270 if (linewise)
5271 // If multiple entries are on the same line, then use the first entry
5272 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005273
5274 return qfp;
5275}
5276
5277/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005278 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005279 * the direction 'dir'.
5280 */
5281 static qfline_T *
5282qf_find_closest_entry(
5283 qf_list_T *qfl,
5284 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005285 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005286 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005287 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005288 int *errornr)
5289{
5290 qfline_T *qfp;
5291
5292 *errornr = 0;
5293
5294 // Find the first entry in this file
5295 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5296 if (qfp == NULL)
5297 return NULL; // no entry in this file
5298
5299 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005300 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005301 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005302 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005303
5304 return qfp;
5305}
5306
5307/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005308 * Get the nth quickfix entry below the specified entry. Searches forward in
5309 * the list. If linewise is TRUE, then treat multiple entries on a single line
5310 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005311 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005312 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005313qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005314{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005315 qfline_T *entry = entry_arg;
5316
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005317 while (n-- > 0 && !got_int)
5318 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005319 int first_errornr = *errornr;
5320
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005321 if (linewise)
5322 // Treat all the entries on the same line in this file as one
5323 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005324
5325 if (entry->qf_next == NULL
5326 || entry->qf_next->qf_fnum != entry->qf_fnum)
5327 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005328 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005329 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005330 break;
5331 }
5332
5333 entry = entry->qf_next;
5334 ++*errornr;
5335 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005336}
5337
5338/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005339 * Get the nth quickfix entry above the specified entry. Searches backwards in
5340 * the list. If linewise is TRUE, then treat multiple entries on a single line
5341 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005342 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005343 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005344qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005345{
5346 while (n-- > 0 && !got_int)
5347 {
5348 if (entry->qf_prev == NULL
5349 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5350 break;
5351
5352 entry = entry->qf_prev;
5353 --*errornr;
5354
5355 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005356 if (linewise)
5357 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005358 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005359}
5360
5361/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005362 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5363 * the specified direction. Returns the error number in the quickfix list or 0
5364 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005365 */
5366 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005367qf_find_nth_adj_entry(
5368 qf_list_T *qfl,
5369 int bnr,
5370 pos_T *pos,
5371 int n,
5372 int dir,
5373 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005374{
5375 qfline_T *adj_entry;
5376 int errornr;
5377
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005378 // Find an entry closest to the specified position
5379 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005380 if (adj_entry == NULL)
5381 return 0;
5382
5383 if (--n > 0)
5384 {
5385 // Go to the n'th entry in the current buffer
5386 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005387 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005388 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005389 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005390 }
5391
5392 return errornr;
5393}
5394
5395/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005396 * Jump to a quickfix entry in the current file nearest to the current line or
5397 * current line/col.
5398 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5399 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005400 */
5401 void
5402ex_cbelow(exarg_T *eap)
5403{
5404 qf_info_T *qi;
5405 qf_list_T *qfl;
5406 int dir;
5407 int buf_has_flag;
5408 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005409 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005410
5411 if (eap->addr_count > 0 && eap->line2 <= 0)
5412 {
5413 emsg(_(e_invrange));
5414 return;
5415 }
5416
5417 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005418 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5419 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005420 buf_has_flag = BUF_HAS_QF_ENTRY;
5421 else
5422 buf_has_flag = BUF_HAS_LL_ENTRY;
5423 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5424 {
5425 emsg(_(e_quickfix));
5426 return;
5427 }
5428
5429 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5430 return;
5431
5432 qfl = qf_get_curlist(qi);
5433 // check if the list has valid errors
5434 if (!qf_list_has_valid_entries(qfl))
5435 {
5436 emsg(_(e_quickfix));
5437 return;
5438 }
5439
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005440 if (eap->cmdidx == CMD_cbelow
5441 || eap->cmdidx == CMD_lbelow
5442 || eap->cmdidx == CMD_cafter
5443 || eap->cmdidx == CMD_lafter)
5444 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005445 dir = FORWARD;
5446 else
5447 dir = BACKWARD;
5448
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005449 pos = curwin->w_cursor;
5450 // A quickfix entry column number is 1 based whereas cursor column
5451 // number is 0 based. Adjust the column number.
5452 pos.col++;
5453 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5454 eap->addr_count > 0 ? eap->line2 : 0, dir,
5455 eap->cmdidx == CMD_cbelow
5456 || eap->cmdidx == CMD_lbelow
5457 || eap->cmdidx == CMD_cabove
5458 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005459
5460 if (errornr > 0)
5461 qf_jump(qi, 0, errornr, FALSE);
5462 else
5463 emsg(_(e_no_more_items));
5464}
5465
5466/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005467 * Return the autocmd name for the :cfile Ex commands
5468 */
5469 static char_u *
5470cfile_get_auname(cmdidx_T cmdidx)
5471{
5472 switch (cmdidx)
5473 {
5474 case CMD_cfile: return (char_u *)"cfile";
5475 case CMD_cgetfile: return (char_u *)"cgetfile";
5476 case CMD_caddfile: return (char_u *)"caddfile";
5477 case CMD_lfile: return (char_u *)"lfile";
5478 case CMD_lgetfile: return (char_u *)"lgetfile";
5479 case CMD_laddfile: return (char_u *)"laddfile";
5480 default: return NULL;
5481 }
5482}
5483
5484/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005485 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005486 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 */
5488 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005489ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005491 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005492 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005493 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005494 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005495 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005496 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005497
Bram Moolenaara16123a2019-03-28 20:31:07 +01005498 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005499 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5500 NULL, FALSE, curbuf))
5501 {
5502#ifdef FEAT_EVAL
5503 if (aborting())
5504 return;
5505#endif
5506 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005507
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005508 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005509#ifdef FEAT_BROWSE
5510 if (cmdmod.browse)
5511 {
5512 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005513 NULL, NULL,
5514 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005515 if (browse_file == NULL)
5516 return;
5517 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5518 vim_free(browse_file);
5519 }
5520 else
5521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005523 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005524
Bram Moolenaar39665952018-08-15 20:59:48 +02005525 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005526 wp = curwin;
5527
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005528 incr_quickfix_busy();
5529
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005530 // This function is used by the :cfile, :cgetfile and :caddfile
5531 // commands.
5532 // :cfile always creates a new quickfix list and jumps to the
5533 // first error.
5534 // :cgetfile creates a new quickfix list but doesn't jump to the
5535 // first error.
5536 // :caddfile adds to an existing quickfix list. If there is no
5537 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005538 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005539 && eap->cmdidx != CMD_laddfile),
5540 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005541 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005542 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005543 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005544 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005545 {
5546 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005547 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005548 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005549 }
5550 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005551 qf_list_changed(qf_get_curlist(qi));
5552 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005553 if (au_name != NULL)
5554 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005555
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005556 // Jump to the first error for a new list and if autocmds didn't
5557 // free the list.
5558 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5559 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005560 // display the first error
5561 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005562
5563 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005564}
5565
5566/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005567 * Return the vimgrep autocmd name.
5568 */
5569 static char_u *
5570vgr_get_auname(cmdidx_T cmdidx)
5571{
5572 switch (cmdidx)
5573 {
5574 case CMD_vimgrep: return (char_u *)"vimgrep";
5575 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5576 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5577 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5578 case CMD_grep: return (char_u *)"grep";
5579 case CMD_lgrep: return (char_u *)"lgrep";
5580 case CMD_grepadd: return (char_u *)"grepadd";
5581 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5582 default: return NULL;
5583 }
5584}
5585
5586/*
5587 * Initialize the regmatch used by vimgrep for pattern "s".
5588 */
5589 static void
5590vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5591{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005592 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005593 regmatch->regprog = NULL;
5594
5595 if (s == NULL || *s == NUL)
5596 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005597 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005598 if (last_search_pat() == NULL)
5599 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005600 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005601 return;
5602 }
5603 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5604 }
5605 else
5606 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5607
5608 regmatch->rmm_ic = p_ic;
5609 regmatch->rmm_maxcol = 0;
5610}
5611
5612/*
5613 * Display a file name when vimgrep is running.
5614 */
5615 static void
5616vgr_display_fname(char_u *fname)
5617{
5618 char_u *p;
5619
5620 msg_start();
5621 p = msg_strtrunc(fname, TRUE);
5622 if (p == NULL)
5623 msg_outtrans(fname);
5624 else
5625 {
5626 msg_outtrans(p);
5627 vim_free(p);
5628 }
5629 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005630 msg_didout = FALSE; // overwrite this message
5631 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005632 msg_col = 0;
5633 out_flush();
5634}
5635
5636/*
5637 * Load a dummy buffer to search for a pattern using vimgrep.
5638 */
5639 static buf_T *
5640vgr_load_dummy_buf(
5641 char_u *fname,
5642 char_u *dirname_start,
5643 char_u *dirname_now)
5644{
5645 int save_mls;
5646#if defined(FEAT_SYN_HL)
5647 char_u *save_ei = NULL;
5648#endif
5649 buf_T *buf;
5650
5651#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005652 // Don't do Filetype autocommands to avoid loading syntax and
5653 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005654 save_ei = au_event_disable(",Filetype");
5655#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005656 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005657 save_mls = p_mls;
5658 p_mls = 0;
5659
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005660 // Load file into a buffer, so that 'fileencoding' is detected,
5661 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005662 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5663
5664 p_mls = save_mls;
5665#if defined(FEAT_SYN_HL)
5666 au_event_restore(save_ei);
5667#endif
5668
5669 return buf;
5670}
5671
5672/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005673 * Check whether a quickfix/location list is valid. Autocmds may remove or
5674 * change a quickfix list when vimgrep is running. If the list is not found,
5675 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005676 */
5677 static int
5678vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005679 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005680 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005681 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005682 char_u *title)
5683{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005684 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005685 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005686 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005687 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005688 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005689 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005690 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005691 return FALSE;
5692 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005693 else
5694 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005695 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005696 qf_new_list(qi, title);
5697 return TRUE;
5698 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005699 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005700
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005701 if (qf_restore_list(qi, qfid) == FAIL)
5702 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005703
5704 return TRUE;
5705}
5706
5707/*
5708 * Search for a pattern in all the lines in a buffer and add the matching lines
5709 * to a quickfix list.
5710 */
5711 static int
5712vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005713 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005714 char_u *fname,
5715 buf_T *buf,
5716 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005717 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005718 int duplicate_name,
5719 int flags)
5720{
5721 int found_match = FALSE;
5722 long lnum;
5723 colnr_T col;
5724
Bram Moolenaar1c299432018-10-28 14:36:09 +01005725 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005726 {
5727 col = 0;
5728 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5729 col, NULL, NULL) > 0)
5730 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005731 // Pass the buffer number so that it gets used even for a
5732 // dummy buffer, unless duplicate_name is set, then the
5733 // buffer will be wiped out below.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005734 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005735 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005736 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005737 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005738 duplicate_name ? 0 : buf->b_fnum,
5739 ml_get_buf(buf,
5740 regmatch->startpos[0].lnum + lnum, FALSE),
5741 regmatch->startpos[0].lnum + lnum,
5742 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005743 FALSE, // vis_col
5744 NULL, // search pattern
5745 0, // nr
5746 0, // type
5747 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02005748 ) == QF_FAIL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005749 {
5750 got_int = TRUE;
5751 break;
5752 }
5753 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005754 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005755 break;
5756 if ((flags & VGR_GLOBAL) == 0
5757 || regmatch->endpos[0].lnum > 0)
5758 break;
5759 col = regmatch->endpos[0].col
5760 + (col == regmatch->endpos[0].col);
5761 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5762 break;
5763 }
5764 line_breakcheck();
5765 if (got_int)
5766 break;
5767 }
5768
5769 return found_match;
5770}
5771
5772/*
5773 * Jump to the first match and update the directory.
5774 */
5775 static void
5776vgr_jump_to_match(
5777 qf_info_T *qi,
5778 int forceit,
5779 int *redraw_for_dummy,
5780 buf_T *first_match_buf,
5781 char_u *target_dir)
5782{
5783 buf_T *buf;
5784
5785 buf = curbuf;
5786 qf_jump(qi, 0, 0, forceit);
5787 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005788 // If we jumped to another buffer redrawing will already be
5789 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005790 *redraw_for_dummy = FALSE;
5791
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005792 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005793 if (curbuf == first_match_buf && target_dir != NULL)
5794 {
5795 exarg_T ea;
5796
Bram Moolenaar4ca41532019-05-09 21:48:37 +02005797 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005798 ea.arg = target_dir;
5799 ea.cmdidx = CMD_lcd;
5800 ex_cd(&ea);
5801 }
5802}
5803
5804/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005805 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00005806 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005807typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00005808{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005809 long tomatch; // maximum number of matches to find
5810 char_u *spat; // search pattern
5811 int flags; // search modifier
5812 char_u **fnames; // list of files to search
5813 int fcount; // number of files
5814 regmmatch_T regmatch; // compiled search pattern
5815 char_u *qf_title; // quickfix list title
5816} vgr_args_T;
5817
5818/*
5819 * Process :vimgrep command arguments. The command syntax is:
5820 *
5821 * :{count}vimgrep /{pattern}/[g][j] {file} ...
5822 */
5823 static int
5824vgr_process_args(
5825 exarg_T *eap,
5826 vgr_args_T *args)
5827{
Bram Moolenaar748bf032005-02-02 23:04:36 +00005828 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005829
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005830 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005831
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005832 args->regmatch.regprog = NULL;
5833 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00005834
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005835 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005836 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005837 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005838 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005839
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005840 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005841 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005842 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005843 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005844 emsg(_(e_invalpat));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005845 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005846 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005847
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005848 vgr_init_regmatch(&args->regmatch, args->spat);
5849 if (args->regmatch.regprog == NULL)
5850 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005851
5852 p = skipwhite(p);
5853 if (*p == NUL)
5854 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005855 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005856 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005857 }
5858
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005859 // parse the list of arguments
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005860 if (get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL)
5861 return FAIL;
5862 if (args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005863 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005864 emsg(_(e_nomatch));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005865 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005866 }
5867
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005868 return OK;
5869}
5870
5871/*
5872 * Search for a pattern in a list of files and populate the quickfix list with
5873 * the matches.
5874 */
5875 static int
5876vgr_process_files(
5877 win_T *wp,
5878 qf_info_T *qi,
5879 vgr_args_T *cmd_args,
5880 int *redraw_for_dummy,
5881 buf_T **first_match_buf,
5882 char_u **target_dir)
5883{
5884 int status = FAIL;
5885 int_u save_qfid = qf_get_curlist(qi)->qf_id;
5886 time_t seconds = 0;
5887 char_u *fname;
5888 int fi;
5889 buf_T *buf;
5890 int duplicate_name = FALSE;
5891 int using_dummy;
5892 char_u *dirname_start = NULL;
5893 char_u *dirname_now = NULL;
5894 int found_match;
5895 aco_save_T aco;
5896
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005897 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5898 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005899 if (dirname_start == NULL || dirname_now == NULL)
5900 goto theend;
5901
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005902 // Remember the current directory, because a BufRead autocommand that does
5903 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005904 mch_dirname(dirname_start, MAXPATHL);
5905
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005906 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005907 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
5908 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005909 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005910 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005911 if (time(NULL) > seconds)
5912 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005913 // Display the file name every second or so, show the user we are
5914 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005915 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005916 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005917 }
5918
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005919 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00005920 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5921 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005922 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005923 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005924 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005925 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005926
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005927 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005928 }
5929 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005930 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005931 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005932
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005933 // Check whether the quickfix list is still valid. When loading a
5934 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005935 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005936 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005937
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005938 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005939
Bram Moolenaar81695252004-12-29 20:58:21 +00005940 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005941 {
5942 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005943 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005944 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005945 else
5946 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005947 // Try for a match in all lines of the buffer.
5948 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005949 found_match = vgr_match_buflines(qf_get_curlist(qi),
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005950 fname, buf, &cmd_args->regmatch,
5951 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005952
Bram Moolenaar81695252004-12-29 20:58:21 +00005953 if (using_dummy)
5954 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005955 if (found_match && *first_match_buf == NULL)
5956 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005957 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005958 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005959 // Never keep a dummy buffer if there is another buffer
5960 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005961 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005962 buf = NULL;
5963 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005964 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005965 || buf->b_p_bh[0] == 'u' // "unload"
5966 || buf->b_p_bh[0] == 'w' // "wipe"
5967 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005968 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005969 // When no match was found we don't need to remember the
5970 // buffer, wipe it out. If there was a match and it
5971 // wasn't the first one or we won't jump there: only
5972 // unload the buffer.
5973 // Ignore 'hidden' here, because it may lead to having too
5974 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005975 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005976 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005977 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005978 buf = NULL;
5979 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005980 else if (buf != *first_match_buf
5981 || (cmd_args->flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005982 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005983 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005984 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005985 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005986 buf = NULL;
5987 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005988 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005989
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005990 if (buf != NULL)
5991 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005992 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005993 buf->b_flags &= ~BF_DUMMY;
5994
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005995 // If the buffer is still loaded we need to use the
5996 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005997 if (buf == *first_match_buf
5998 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005999 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006000 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006001
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006002 // The buffer is still loaded, the Filetype autocommands
6003 // need to be done now, in that buffer. And the modelines
6004 // need to be done (again). But not the window-local
6005 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006006 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006007#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006008 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6009 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006010#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006011 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006012 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006013 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006014 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006015 }
6016 }
6017
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006018 status = OK;
6019
6020theend:
6021 vim_free(dirname_now);
6022 vim_free(dirname_start);
6023 return status;
6024}
6025
6026/*
6027 * ":vimgrep {pattern} file(s)"
6028 * ":vimgrepadd {pattern} file(s)"
6029 * ":lvimgrep {pattern} file(s)"
6030 * ":lvimgrepadd {pattern} file(s)"
6031 */
6032 void
6033ex_vimgrep(exarg_T *eap)
6034{
6035 vgr_args_T args;
6036 qf_info_T *qi;
6037 qf_list_T *qfl;
6038 int_u save_qfid;
6039 win_T *wp = NULL;
6040 int redraw_for_dummy = FALSE;
6041 buf_T *first_match_buf = NULL;
6042 char_u *target_dir = NULL;
6043 char_u *au_name = NULL;
6044 int status;
6045
6046 au_name = vgr_get_auname(eap->cmdidx);
6047 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6048 curbuf->b_fname, TRUE, curbuf))
6049 {
6050#ifdef FEAT_EVAL
6051 if (aborting())
6052 return;
6053#endif
6054 }
6055
6056 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6057 if (qi == NULL)
6058 return;
6059
6060 if (vgr_process_args(eap, &args) == FAIL)
6061 goto theend;
6062
6063 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6064 && eap->cmdidx != CMD_vimgrepadd
6065 && eap->cmdidx != CMD_lvimgrepadd)
6066 || qf_stack_empty(qi))
6067 // make place for a new list
6068 qf_new_list(qi, args.qf_title);
6069
6070 incr_quickfix_busy();
6071
6072 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6073 &first_match_buf, &target_dir);
6074 if (status != OK)
6075 {
6076 FreeWild(args.fcount, args.fnames);
6077 decr_quickfix_busy();
6078 goto theend;
6079 }
6080
6081 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006082
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006083 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006084 qfl->qf_nonevalid = FALSE;
6085 qfl->qf_ptr = qfl->qf_start;
6086 qfl->qf_index = 1;
6087 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006088
Bram Moolenaar864293a2016-06-02 13:40:04 +02006089 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006090
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006091 // Remember the current quickfix list identifier, so that we can check for
6092 // autocommands changing the current quickfix list.
6093 save_qfid = qf_get_curlist(qi)->qf_id;
6094
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006095 if (au_name != NULL)
6096 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6097 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006098 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6099 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006100 if (!qflist_valid(wp, save_qfid)
6101 || qf_restore_list(qi, save_qfid) == FAIL)
6102 {
6103 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006104 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006105 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006106
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006107 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006108 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006109 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006110 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006111 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6112 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006113 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006114 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006115 semsg(_(e_nomatch2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006116
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006117 decr_quickfix_busy();
6118
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006119 // If we loaded a dummy buffer into the current window, the autocommands
6120 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006121 if (redraw_for_dummy)
6122 {
6123#ifdef FEAT_FOLDING
6124 foldUpdateAll(curwin);
6125#else
6126 redraw_later(NOT_VALID);
6127#endif
6128 }
6129
Bram Moolenaar86b68352004-12-27 21:59:20 +00006130theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006131 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006132 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006133 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006134}
6135
6136/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006137 * Restore current working directory to "dirname_start" if they differ, taking
6138 * into account whether it is set locally or globally.
6139 */
6140 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006141restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006142{
6143 char_u *dirname_now = alloc(MAXPATHL);
6144
6145 if (NULL != dirname_now)
6146 {
6147 mch_dirname(dirname_now, MAXPATHL);
6148 if (STRCMP(dirname_start, dirname_now) != 0)
6149 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006150 // If the directory has changed, change it back by building up an
6151 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006152 exarg_T ea;
6153
Bram Moolenaar4ca41532019-05-09 21:48:37 +02006154 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006155 ea.arg = dirname_start;
6156 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6157 ex_cd(&ea);
6158 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006159 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006160 }
6161}
6162
6163/*
6164 * Load file "fname" into a dummy buffer and return the buffer pointer,
6165 * placing the directory resulting from the buffer load into the
6166 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6167 * prior to calling this function. Restores directory to "dirname_start" prior
6168 * to returning, if autocmds or the 'autochdir' option have changed it.
6169 *
6170 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6171 * or wipe_dummy_buffer() later!
6172 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006173 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006174 */
6175 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006176load_dummy_buffer(
6177 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006178 char_u *dirname_start, // in: old directory
6179 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006180{
6181 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006182 bufref_T newbufref;
6183 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006184 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006185 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006186 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006187
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006188 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006189 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6190 if (newbuf == NULL)
6191 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006192 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006193
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006194 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006195 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6196
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006197 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006198 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006199 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006200 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006201 ++newbuf->b_locked;
6202
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006203 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006204 aucmd_prepbuf(&aco, newbuf);
6205
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006206 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006207 (void)setfname(curbuf, fname, NULL, FALSE);
6208
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006209 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006210 check_need_swap(TRUE);
6211
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006212 // Remove the "dummy" flag, otherwise autocommands may not
6213 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006214 curbuf->b_flags &= ~BF_DUMMY;
6215
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006216 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006217 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006218 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006219 NULL, READ_NEW | READ_DUMMY);
6220 --newbuf->b_locked;
6221 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006222 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006223 && !(curbuf->b_flags & BF_NEW))
6224 {
6225 failed = FALSE;
6226 if (curbuf != newbuf)
6227 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006228 // Bloody autocommands changed the buffer! Can happen when
6229 // using netrw and editing a remote file. Use the current
6230 // buffer instead, delete the dummy one after restoring the
6231 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006232 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006233 newbuf = curbuf;
6234 }
6235 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006236
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006237 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006238 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006239 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6240 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006241
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006242 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6243 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006244 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006245 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006246
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006247 // When autocommands/'autochdir' option changed directory: go back.
6248 // Let the caller know what the resulting dir was first, in case it is
6249 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006250 mch_dirname(resulting_dir, MAXPATHL);
6251 restore_start_dir(dirname_start);
6252
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006253 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006254 return NULL;
6255 if (failed)
6256 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006257 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006258 return NULL;
6259 }
6260 return newbuf;
6261}
6262
6263/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006264 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6265 * directory to "dirname_start" prior to returning, if autocmds or the
6266 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006267 */
6268 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006269wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006270{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006271 // If any autocommand opened a window on the dummy buffer, close that
6272 // window. If we can't close them all then give up.
6273 while (buf->b_nwindows > 0)
6274 {
6275 int did_one = FALSE;
6276 win_T *wp;
6277
6278 if (firstwin->w_next != NULL)
6279 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6280 if (wp->w_buffer == buf)
6281 {
6282 if (win_close(wp, FALSE) == OK)
6283 did_one = TRUE;
6284 break;
6285 }
6286 if (!did_one)
6287 return;
6288 }
6289
6290 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006291 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006292#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006293 cleanup_T cs;
6294
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006295 // Reset the error/interrupt/exception state here so that aborting()
6296 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6297 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006298 enter_cleanup(&cs);
6299#endif
6300
Bram Moolenaar81695252004-12-29 20:58:21 +00006301 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006302
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006303#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006304 // Restore the error/interrupt/exception state if not discarded by a
6305 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006306 leave_cleanup(&cs);
6307#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006308 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006309 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006310 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006311}
6312
6313/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006314 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6315 * directory to "dirname_start" prior to returning, if autocmds or the
6316 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006317 */
6318 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006319unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006320{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006321 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006322 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006323 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006324
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006325 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006326 restore_start_dir(dirname_start);
6327 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006328}
6329
Bram Moolenaar05159a02005-02-26 23:04:13 +00006330#if defined(FEAT_EVAL) || defined(PROTO)
6331/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006332 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006333 * to 'list'. Returns OK on success.
6334 */
6335 static int
6336get_qfline_items(qfline_T *qfp, list_T *list)
6337{
6338 int bufnum;
6339 dict_T *dict;
6340 char_u buf[2];
6341
6342 // Handle entries with a non-existing buffer number.
6343 bufnum = qfp->qf_fnum;
6344 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6345 bufnum = 0;
6346
6347 if ((dict = dict_alloc()) == NULL)
6348 return FAIL;
6349 if (list_append_dict(list, dict) == FAIL)
6350 return FAIL;
6351
6352 buf[0] = qfp->qf_type;
6353 buf[1] = NUL;
6354 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
6355 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6356 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6357 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6358 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
6359 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6360 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6361 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6362 || dict_add_string(dict, "type", buf) == FAIL
6363 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6364 return FAIL;
6365
6366 return OK;
6367}
6368
6369/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006370 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006371 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006372 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006373 static int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006374get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006375{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006376 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006377 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006378 qfline_T *qfp;
6379 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006380
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006381 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006382 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006383 qi = &ql_info;
6384 if (wp != NULL)
6385 {
6386 qi = GET_LOC_LIST(wp);
6387 if (qi == NULL)
6388 return FAIL;
6389 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006390 }
6391
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006392 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006393 qf_idx = qi->qf_curlist;
6394
Bram Moolenaar0398e002019-03-21 21:12:49 +01006395 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006396 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006397
Bram Moolenaar0398e002019-03-21 21:12:49 +01006398 qfl = qf_get_list(qi, qf_idx);
6399 if (qf_list_empty(qfl))
6400 return FAIL;
6401
Bram Moolenaara16123a2019-03-28 20:31:07 +01006402 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006403 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01006404 if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006405 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006406 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006407
Bram Moolenaar05159a02005-02-26 23:04:13 +00006408 return OK;
6409}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006410
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006411// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006412enum {
6413 QF_GETLIST_NONE = 0x0,
6414 QF_GETLIST_TITLE = 0x1,
6415 QF_GETLIST_ITEMS = 0x2,
6416 QF_GETLIST_NR = 0x4,
6417 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006418 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006419 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006420 QF_GETLIST_IDX = 0x40,
6421 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006422 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006423 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006424 QF_GETLIST_QFBUFNR = 0x400,
6425 QF_GETLIST_ALL = 0x7FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006426};
6427
6428/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006429 * Parse text from 'di' and return the quickfix list items.
6430 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006431 */
6432 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006433qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006434{
6435 int status = FAIL;
6436 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006437 char_u *errorformat = p_efm;
6438 dictitem_T *efm_di;
6439 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006440
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006441 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006442 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006443 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006444 // If errorformat is supplied then use it, otherwise use the 'efm'
6445 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006446 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6447 {
6448 if (efm_di->di_tv.v_type != VAR_STRING ||
6449 efm_di->di_tv.vval.v_string == NULL)
6450 return FAIL;
6451 errorformat = efm_di->di_tv.vval.v_string;
6452 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006453
Bram Moolenaar36538222017-09-02 19:51:44 +02006454 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006455 if (l == NULL)
6456 return FAIL;
6457
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006458 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006459 if (qi != NULL)
6460 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006461 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006462 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6463 {
Bram Moolenaarda732532017-08-31 20:58:02 +02006464 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006465 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006466 }
6467 free(qi);
6468 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006469 dict_add_list(retdict, "items", l);
6470 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006471 }
6472
6473 return status;
6474}
6475
6476/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006477 * Return the quickfix/location list window identifier in the current tabpage.
6478 */
6479 static int
6480qf_winid(qf_info_T *qi)
6481{
6482 win_T *win;
6483
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006484 // The quickfix window can be opened even if the quickfix list is not set
6485 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006486 if (qi == NULL)
6487 return 0;
6488 win = qf_find_win(qi);
6489 if (win != NULL)
6490 return win->w_id;
6491 return 0;
6492}
6493
6494/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006495 * Returns the number of the buffer displayed in the quickfix/location list
6496 * window. If there is no buffer associated with the list, then returns 0.
6497 */
6498 static int
6499qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6500{
6501 return dict_add_number(retdict, "qfbufnr",
6502 (qi == NULL) ? 0 : qi->qf_bufnr);
6503}
6504
6505/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006506 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006507 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006508 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006509qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006510{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006511 int flags = QF_GETLIST_NONE;
6512
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006513 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006514 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006515 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006516 if (!loclist)
6517 // File window ID is applicable only to location list windows
6518 flags &= ~ QF_GETLIST_FILEWINID;
6519 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006520
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006521 if (dict_find(what, (char_u *)"title", -1) != NULL)
6522 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006523
Bram Moolenaara6d48492017-12-12 22:45:31 +01006524 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6525 flags |= QF_GETLIST_NR;
6526
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006527 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6528 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006529
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006530 if (dict_find(what, (char_u *)"context", -1) != NULL)
6531 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006532
Bram Moolenaara6d48492017-12-12 22:45:31 +01006533 if (dict_find(what, (char_u *)"id", -1) != NULL)
6534 flags |= QF_GETLIST_ID;
6535
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006536 if (dict_find(what, (char_u *)"items", -1) != NULL)
6537 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006538
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006539 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6540 flags |= QF_GETLIST_IDX;
6541
6542 if (dict_find(what, (char_u *)"size", -1) != NULL)
6543 flags |= QF_GETLIST_SIZE;
6544
Bram Moolenaarb254af32017-12-18 19:48:58 +01006545 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6546 flags |= QF_GETLIST_TICK;
6547
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006548 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6549 flags |= QF_GETLIST_FILEWINID;
6550
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006551 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6552 flags |= QF_GETLIST_QFBUFNR;
6553
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006554 return flags;
6555}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006556
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006557/*
6558 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6559 * If 'nr' and 'id' are not present in 'what' then return the current
6560 * quickfix list index.
6561 * If 'nr' is zero then return the current quickfix list index.
6562 * If 'nr' is '$' then return the last quickfix list index.
6563 * If 'id' is present then return the index of the quickfix list with that id.
6564 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6565 * Return -1, if quickfix list is not present or if the stack is empty.
6566 */
6567 static int
6568qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6569{
6570 int qf_idx;
6571 dictitem_T *di;
6572
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006573 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006574 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6575 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006576 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006577 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006578 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006579 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006580 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006581 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006582 qf_idx = di->di_tv.vval.v_number - 1;
6583 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006584 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006585 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006586 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006587 else if (di->di_tv.v_type == VAR_STRING
6588 && di->di_tv.vval.v_string != NULL
6589 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006590 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006591 qf_idx = qi->qf_listcount - 1;
6592 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006593 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006594 }
6595
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006596 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6597 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006598 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006599 if (di->di_tv.v_type == VAR_NUMBER)
6600 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006601 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006602 if (di->di_tv.vval.v_number != 0)
6603 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6604 }
6605 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006606 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006607 }
6608
6609 return qf_idx;
6610}
6611
6612/*
6613 * Return default values for quickfix list properties in retdict.
6614 */
6615 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006616qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006617{
6618 int status = OK;
6619
6620 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006621 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006622 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6623 {
6624 list_T *l = list_alloc();
6625 if (l != NULL)
6626 status = dict_add_list(retdict, "items", l);
6627 else
6628 status = FAIL;
6629 }
6630 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006631 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006632 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006633 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006634 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006635 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006636 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006637 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006638 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006639 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006640 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006641 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006642 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006643 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006644 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006645 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006646 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6647 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006648
6649 return status;
6650}
6651
6652/*
6653 * Return the quickfix list title as 'title' in retdict
6654 */
6655 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006656qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006657{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006658 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006659}
6660
6661/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006662 * Returns the identifier of the window used to display files from a location
6663 * list. If there is no associated window, then returns 0. Useful only when
6664 * called from a location list window.
6665 */
6666 static int
6667qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6668{
6669 int winid = 0;
6670
6671 if (wp != NULL && IS_LL_WINDOW(wp))
6672 {
6673 win_T *ll_wp = qf_find_win_with_loclist(qi);
6674 if (ll_wp != NULL)
6675 winid = ll_wp->w_id;
6676 }
6677
6678 return dict_add_number(retdict, "filewinid", winid);
6679}
6680
6681/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006682 * Return the quickfix list items/entries as 'items' in retdict
6683 */
6684 static int
6685qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6686{
6687 int status = OK;
6688 list_T *l = list_alloc();
6689 if (l != NULL)
6690 {
6691 (void)get_errorlist(qi, NULL, qf_idx, l);
6692 dict_add_list(retdict, "items", l);
6693 }
6694 else
6695 status = FAIL;
6696
6697 return status;
6698}
6699
6700/*
6701 * Return the quickfix list context (if any) as 'context' in retdict.
6702 */
6703 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006704qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006705{
6706 int status;
6707 dictitem_T *di;
6708
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006709 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006710 {
6711 di = dictitem_alloc((char_u *)"context");
6712 if (di != NULL)
6713 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006714 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006715 status = dict_add(retdict, di);
6716 if (status == FAIL)
6717 dictitem_free(di);
6718 }
6719 else
6720 status = FAIL;
6721 }
6722 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006723 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006724
6725 return status;
6726}
6727
6728/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006729 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006730 */
6731 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01006732qf_getprop_idx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006733{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006734 int curidx = qfl->qf_index;
6735 if (qf_list_empty(qfl))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006736 // For empty lists, current index is set to 0
6737 curidx = 0;
6738 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006739}
6740
6741/*
6742 * Return quickfix/location list details (title) as a
6743 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6744 * then current list is used. Otherwise the specified list is used.
6745 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006746 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006747qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6748{
6749 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006750 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006751 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006752 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006753 dictitem_T *di;
6754 int flags = QF_GETLIST_NONE;
6755
6756 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6757 return qf_get_list_from_lines(what, di, retdict);
6758
6759 if (wp != NULL)
6760 qi = GET_LOC_LIST(wp);
6761
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006762 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006763
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006764 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006765 qf_idx = qf_getprop_qfidx(qi, what);
6766
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006767 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006768 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006769 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006770
Bram Moolenaar0398e002019-03-21 21:12:49 +01006771 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006772
Bram Moolenaard823fa92016-08-12 16:29:27 +02006773 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006774 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006775 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006776 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006777 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006778 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006779 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006780 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006781 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006782 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006783 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006784 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006785 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar0398e002019-03-21 21:12:49 +01006786 status = qf_getprop_idx(qfl, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006787 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006788 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006789 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006790 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006791 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6792 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006793 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6794 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006795
Bram Moolenaard823fa92016-08-12 16:29:27 +02006796 return status;
6797}
6798
6799/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006800 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006801 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6802 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006803 */
6804 static int
6805qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01006806 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006807 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006808 int first_entry,
6809 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006810{
6811 static int did_bufnr_emsg;
6812 char_u *filename, *module, *pattern, *text, *type;
6813 int bufnum, valid, status, col, vcol, nr;
6814 long lnum;
6815
6816 if (first_entry)
6817 did_bufnr_emsg = FALSE;
6818
Bram Moolenaar8f667172018-12-14 15:38:31 +01006819 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6820 module = dict_get_string(d, (char_u *)"module", TRUE);
6821 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6822 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6823 col = (int)dict_get_number(d, (char_u *)"col");
6824 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6825 nr = (int)dict_get_number(d, (char_u *)"nr");
6826 type = dict_get_string(d, (char_u *)"type", TRUE);
6827 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6828 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006829 if (text == NULL)
6830 text = vim_strsave((char_u *)"");
6831
6832 valid = TRUE;
6833 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6834 valid = FALSE;
6835
6836 // Mark entries with non-existing buffer number as not valid. Give the
6837 // error message only once.
6838 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6839 {
6840 if (!did_bufnr_emsg)
6841 {
6842 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006843 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006844 }
6845 valid = FALSE;
6846 bufnum = 0;
6847 }
6848
6849 // If the 'valid' field is present it overrules the detected value.
6850 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006851 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006852
Bram Moolenaar0398e002019-03-21 21:12:49 +01006853 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006854 NULL, // dir
6855 filename,
6856 module,
6857 bufnum,
6858 text,
6859 lnum,
6860 col,
6861 vcol, // vis_col
6862 pattern, // search pattern
6863 nr,
6864 type == NULL ? NUL : *type,
6865 valid);
6866
6867 vim_free(filename);
6868 vim_free(module);
6869 vim_free(pattern);
6870 vim_free(text);
6871 vim_free(type);
6872
Bram Moolenaar9752c722018-12-22 16:49:34 +01006873 if (valid)
6874 *valid_entry = TRUE;
6875
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006876 return status;
6877}
6878
6879/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006880 * Add list of entries to quickfix/location list. Each list entry is
6881 * a dictionary with item information.
6882 */
6883 static int
6884qf_add_entries(
6885 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006886 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006887 list_T *list,
6888 char_u *title,
6889 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006890{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006891 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006892 listitem_T *li;
6893 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006894 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006895 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006896 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006897
Bram Moolenaara3921f42017-06-04 15:30:34 +02006898 if (action == ' ' || qf_idx == qi->qf_listcount)
6899 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006900 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006901 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006902 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006903 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006904 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01006905 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006906 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006907 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006908 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006909 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006910 qf_free_items(qfl);
6911 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006912 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006913
6914 for (li = list->lv_first; li != NULL; li = li->li_next)
6915 {
6916 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006917 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006918
6919 d = li->li_tv.vval.v_dict;
6920 if (d == NULL)
6921 continue;
6922
Bram Moolenaar0398e002019-03-21 21:12:49 +01006923 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006924 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02006925 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006926 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006927 }
6928
Bram Moolenaar9752c722018-12-22 16:49:34 +01006929 // Check if any valid error entries are added to the list.
6930 if (valid_entry)
6931 qfl->qf_nonevalid = FALSE;
6932 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006933 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006934 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006935
6936 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006937 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006938 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006939
6940 // Update the current error index if not appending to the list or if the
6941 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006942 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01006943 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006944
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006945 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006946 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006947
6948 return retval;
6949}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006950
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006951/*
6952 * Get the quickfix list index from 'nr' or 'id'
6953 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006954 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006955qf_setprop_get_qfidx(
6956 qf_info_T *qi,
6957 dict_T *what,
6958 int action,
6959 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006960{
6961 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006962 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006963
Bram Moolenaard823fa92016-08-12 16:29:27 +02006964 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6965 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006966 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006967 if (di->di_tv.v_type == VAR_NUMBER)
6968 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006969 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006970 if (di->di_tv.vval.v_number != 0)
6971 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006972
Bram Moolenaar55b69262017-08-13 13:42:01 +02006973 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6974 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006975 // When creating a new list, accept qf_idx pointing to the next
6976 // non-available list and add the new list at the end of the
6977 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006978 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006979 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006980 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006981 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006982 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006983 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006984 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006985 }
6986 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006987 && di->di_tv.vval.v_string != NULL
6988 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006989 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006990 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006991 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006992 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006993 qf_idx = 0;
6994 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006995 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006996 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006997 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006998 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006999 }
7000
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007001 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007002 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007003 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007004 if (di->di_tv.v_type != VAR_NUMBER)
7005 return INVALID_QFIDX;
7006
7007 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007008 }
7009
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007010 return qf_idx;
7011}
7012
7013/*
7014 * Set the quickfix list title.
7015 */
7016 static int
7017qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7018{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007019 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007020
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007021 if (di->di_tv.v_type != VAR_STRING)
7022 return FAIL;
7023
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007024 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01007025 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007026 if (qf_idx == qi->qf_curlist)
7027 qf_update_win_titlevar(qi);
7028
7029 return OK;
7030}
7031
7032/*
7033 * Set quickfix list items/entries.
7034 */
7035 static int
7036qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7037{
7038 int retval = FAIL;
7039 char_u *title_save;
7040
7041 if (di->di_tv.v_type != VAR_LIST)
7042 return FAIL;
7043
7044 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7045 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7046 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007047 vim_free(title_save);
7048
7049 return retval;
7050}
7051
7052/*
7053 * Set quickfix list items/entries from a list of lines.
7054 */
7055 static int
7056qf_setprop_items_from_lines(
7057 qf_info_T *qi,
7058 int qf_idx,
7059 dict_T *what,
7060 dictitem_T *di,
7061 int action)
7062{
7063 char_u *errorformat = p_efm;
7064 dictitem_T *efm_di;
7065 int retval = FAIL;
7066
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007067 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007068 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7069 {
7070 if (efm_di->di_tv.v_type != VAR_STRING ||
7071 efm_di->di_tv.vval.v_string == NULL)
7072 return FAIL;
7073 errorformat = efm_di->di_tv.vval.v_string;
7074 }
7075
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007076 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007077 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7078 return FAIL;
7079
7080 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007081 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007082 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
7083 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
7084 retval = OK;
7085
7086 return retval;
7087}
7088
7089/*
7090 * Set quickfix list context.
7091 */
7092 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007093qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007094{
7095 typval_T *ctx;
7096
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007097 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007098 ctx = alloc_tv();
7099 if (ctx != NULL)
7100 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007101 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007102
7103 return OK;
7104}
7105
7106/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007107 * Set the current index in the specified quickfix list
7108 */
7109 static int
7110qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7111{
7112 int denote = FALSE;
7113 int newidx;
7114 int old_qfidx;
7115 qfline_T *qf_ptr;
7116
7117 // If the specified index is '$', then use the last entry
7118 if (di->di_tv.v_type == VAR_STRING
7119 && di->di_tv.vval.v_string != NULL
7120 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7121 newidx = qfl->qf_count;
7122 else
7123 {
7124 // Otherwise use the specified index
7125 newidx = tv_get_number_chk(&di->di_tv, &denote);
7126 if (denote)
7127 return FAIL;
7128 }
7129
7130 if (newidx < 1) // sanity check
7131 return FAIL;
7132 if (newidx > qfl->qf_count)
7133 newidx = qfl->qf_count;
7134
7135 old_qfidx = qfl->qf_index;
7136 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7137 if (qf_ptr == NULL)
7138 return FAIL;
7139 qfl->qf_ptr = qf_ptr;
7140 qfl->qf_index = newidx;
7141
7142 // If the current list is modified and it is displayed in the quickfix
7143 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007144 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007145 qf_win_pos_update(qi, old_qfidx);
7146
7147 return OK;
7148}
7149
7150/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007151 * Set quickfix/location list properties (title, items, context).
7152 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007153 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007154 */
7155 static int
7156qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7157{
7158 dictitem_T *di;
7159 int retval = FAIL;
7160 int qf_idx;
7161 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007162 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007163
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007164 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007165 newlist = TRUE;
7166
7167 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007168 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007169 return FAIL;
7170
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007171 if (newlist)
7172 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007173 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007174 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007175 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007176 }
7177
Bram Moolenaar0398e002019-03-21 21:12:49 +01007178 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007179 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007180 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007181 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007182 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007183 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007184 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007185 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007186 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007187 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7188 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007189
Bram Moolenaarb254af32017-12-18 19:48:58 +01007190 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007191 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007192
Bram Moolenaard823fa92016-08-12 16:29:27 +02007193 return retval;
7194}
7195
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007196/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007197 * Free the entire quickfix/location list stack.
7198 * If the quickfix/location list window is open, then clear it.
7199 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007200 static void
7201qf_free_stack(win_T *wp, qf_info_T *qi)
7202{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007203 win_T *qfwin = qf_find_win(qi);
7204 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007205
7206 if (qfwin != NULL)
7207 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007208 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007209 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007210 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007211 qf_update_buffer(qi, NULL);
7212 }
7213
7214 if (wp != NULL && IS_LL_WINDOW(wp))
7215 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007216 // If in the location list window, then use the non-location list
7217 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007218 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007219 if (llwin != NULL)
7220 wp = llwin;
7221 }
7222
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007223 qf_free_all(wp);
7224 if (wp == NULL)
7225 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007226 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007227 qi->qf_curlist = 0;
7228 qi->qf_listcount = 0;
7229 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007230 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007231 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007232 // If the location list window is open, then create a new empty
7233 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007234 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007235
Bram Moolenaar95946f12019-03-31 15:31:59 +02007236 if (new_ll != NULL)
7237 {
7238 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007239
Bram Moolenaar95946f12019-03-31 15:31:59 +02007240 // first free the list reference in the location list window
7241 ll_free_all(&qfwin->w_llist_ref);
7242
7243 qfwin->w_llist_ref = new_ll;
7244 if (wp != qfwin)
7245 win_set_loclist(wp, new_ll);
7246 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007247 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007248}
7249
Bram Moolenaard823fa92016-08-12 16:29:27 +02007250/*
7251 * Populate the quickfix list with the items supplied in the list
7252 * of dictionaries. "title" will be copied to w:quickfix_title.
7253 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
7254 */
7255 int
7256set_errorlist(
7257 win_T *wp,
7258 list_T *list,
7259 int action,
7260 char_u *title,
7261 dict_T *what)
7262{
7263 qf_info_T *qi = &ql_info;
7264 int retval = OK;
7265
7266 if (wp != NULL)
7267 {
7268 qi = ll_get_or_alloc_list(wp);
7269 if (qi == NULL)
7270 return FAIL;
7271 }
7272
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007273 if (action == 'f')
7274 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007275 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007276 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007277 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007278 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007279
7280 incr_quickfix_busy();
7281
7282 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007283 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007284 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007285 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007286 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007287 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007288 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007289 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007290
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007291 decr_quickfix_busy();
7292
Bram Moolenaard823fa92016-08-12 16:29:27 +02007293 return retval;
7294}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007295
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007296/*
7297 * Mark the context as in use for all the lists in a quickfix stack.
7298 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007299 static int
7300mark_quickfix_ctx(qf_info_T *qi, int copyID)
7301{
7302 int i;
7303 int abort = FALSE;
7304 typval_T *ctx;
7305
7306 for (i = 0; i < LISTCOUNT && !abort; ++i)
7307 {
7308 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007309 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7310 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007311 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
7312 }
7313
7314 return abort;
7315}
7316
7317/*
7318 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007319 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007320 */
7321 int
7322set_ref_in_quickfix(int copyID)
7323{
7324 int abort = FALSE;
7325 tabpage_T *tp;
7326 win_T *win;
7327
7328 abort = mark_quickfix_ctx(&ql_info, copyID);
7329 if (abort)
7330 return abort;
7331
7332 FOR_ALL_TAB_WINDOWS(tp, win)
7333 {
7334 if (win->w_llist != NULL)
7335 {
7336 abort = mark_quickfix_ctx(win->w_llist, copyID);
7337 if (abort)
7338 return abort;
7339 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007340 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7341 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007342 // In a location list window and none of the other windows is
7343 // referring to this location list. Mark the location list
7344 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007345 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7346 if (abort)
7347 return abort;
7348 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007349 }
7350
7351 return abort;
7352}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007353#endif
7354
Bram Moolenaar81695252004-12-29 20:58:21 +00007355/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007356 * Return the autocmd name for the :cbuffer Ex commands
7357 */
7358 static char_u *
7359cbuffer_get_auname(cmdidx_T cmdidx)
7360{
7361 switch (cmdidx)
7362 {
7363 case CMD_cbuffer: return (char_u *)"cbuffer";
7364 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7365 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7366 case CMD_lbuffer: return (char_u *)"lbuffer";
7367 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7368 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7369 default: return NULL;
7370 }
7371}
7372
7373/*
7374 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7375 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7376 */
7377 static int
7378cbuffer_process_args(
7379 exarg_T *eap,
7380 buf_T **bufp,
7381 linenr_T *line1,
7382 linenr_T *line2)
7383{
7384 buf_T *buf = NULL;
7385
7386 if (*eap->arg == NUL)
7387 buf = curbuf;
7388 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7389 buf = buflist_findnr(atoi((char *)eap->arg));
7390
7391 if (buf == NULL)
7392 {
7393 emsg(_(e_invarg));
7394 return FAIL;
7395 }
7396
7397 if (buf->b_ml.ml_mfp == NULL)
7398 {
7399 emsg(_("E681: Buffer is not loaded"));
7400 return FAIL;
7401 }
7402
7403 if (eap->addr_count == 0)
7404 {
7405 eap->line1 = 1;
7406 eap->line2 = buf->b_ml.ml_line_count;
7407 }
7408
7409 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7410 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7411 {
7412 emsg(_(e_invrange));
7413 return FAIL;
7414 }
7415
7416 *line1 = eap->line1;
7417 *line2 = eap->line2;
7418 *bufp = buf;
7419
7420 return OK;
7421}
7422
7423/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007424 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007425 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007426 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007427 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007428 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007429 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007430 */
7431 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007432ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007433{
7434 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007435 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007436 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007437 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007438 int_u save_qfid;
7439 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007440 char_u *qf_title;
7441 linenr_T line1;
7442 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007443
Bram Moolenaara16123a2019-03-28 20:31:07 +01007444 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007445 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007446 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007447 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007448#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007449 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007450 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007451#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007452 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007453
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007454 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007455 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7456 if (qi == NULL)
7457 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007458
Bram Moolenaara16123a2019-03-28 20:31:07 +01007459 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7460 return;
7461
7462 qf_title = qf_cmdtitle(*eap->cmdlinep);
7463
7464 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007465 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007466 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7467 (char *)qf_title, (char *)buf->b_sfname);
7468 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007469 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007470
7471 incr_quickfix_busy();
7472
7473 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7474 (eap->cmdidx != CMD_caddbuffer
7475 && eap->cmdidx != CMD_laddbuffer),
7476 line1, line2,
7477 qf_title, NULL);
7478 if (qf_stack_empty(qi))
7479 {
7480 decr_quickfix_busy();
7481 return;
7482 }
7483 if (res >= 0)
7484 qf_list_changed(qf_get_curlist(qi));
7485
7486 // Remember the current quickfix list identifier, so that we can
7487 // check for autocommands changing the current quickfix list.
7488 save_qfid = qf_get_curlist(qi)->qf_id;
7489 if (au_name != NULL)
7490 {
7491 buf_T *curbuf_old = curbuf;
7492
7493 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7494 TRUE, curbuf);
7495 if (curbuf != curbuf_old)
7496 // Autocommands changed buffer, don't jump now, "qi" may
7497 // be invalid.
7498 res = 0;
7499 }
7500 // Jump to the first error for a new list and if autocmds didn't
7501 // free the list.
7502 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7503 eap->cmdidx == CMD_lbuffer)
7504 && qflist_valid(wp, save_qfid))
7505 // display the first error
7506 qf_jump_first(qi, save_qfid, eap->forceit);
7507
7508 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007509}
7510
Bram Moolenaar1e015462005-09-25 22:16:38 +00007511#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007512/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007513 * Return the autocmd name for the :cexpr Ex commands.
7514 */
7515 static char_u *
7516cexpr_get_auname(cmdidx_T cmdidx)
7517{
7518 switch (cmdidx)
7519 {
7520 case CMD_cexpr: return (char_u *)"cexpr";
7521 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7522 case CMD_caddexpr: return (char_u *)"caddexpr";
7523 case CMD_lexpr: return (char_u *)"lexpr";
7524 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7525 case CMD_laddexpr: return (char_u *)"laddexpr";
7526 default: return NULL;
7527 }
7528}
7529
7530/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007531 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7532 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007533 */
7534 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007535ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007536{
7537 typval_T *tv;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007538 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007539 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007540 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007541 int_u save_qfid;
7542 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007543
Bram Moolenaara16123a2019-03-28 20:31:07 +01007544 au_name = cexpr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007545 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7546 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007547 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007548#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007549 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007550 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007551#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007552 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007553
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007554 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7555 if (qi == NULL)
7556 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007557
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007558 // Evaluate the expression. When the result is a string or a list we can
7559 // use it to fill the errorlist.
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01007560 tv = eval_expr(eap->arg, &eap->nextcmd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007561 if (tv != NULL)
7562 {
7563 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7564 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7565 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007566 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007567 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00007568 (eap->cmdidx != CMD_caddexpr
7569 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007570 (linenr_T)0, (linenr_T)0,
7571 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007572 if (qf_stack_empty(qi))
7573 {
7574 decr_quickfix_busy();
7575 goto cleanup;
7576 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01007577 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007578 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007579
7580 // Remember the current quickfix list identifier, so that we can
7581 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007582 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007583 if (au_name != NULL)
7584 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7585 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007586
7587 // Jump to the first error for a new list and if autocmds didn't
7588 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02007589 if (res > 0 && (eap->cmdidx == CMD_cexpr
7590 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007591 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02007592 // display the first error
7593 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007594 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00007595 }
7596 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007597 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007598cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00007599 free_tv(tv);
7600 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007601}
Bram Moolenaar1e015462005-09-25 22:16:38 +00007602#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007603
7604/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007605 * Get the location list for ":lhelpgrep"
7606 */
7607 static qf_info_T *
7608hgr_get_ll(int *new_ll)
7609{
7610 win_T *wp;
7611 qf_info_T *qi;
7612
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007613 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007614 if (bt_help(curwin->w_buffer))
7615 wp = curwin;
7616 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007617 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007618 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007619
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007620 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007621 qi = NULL;
7622 else
7623 qi = wp->w_llist;
7624
7625 if (qi == NULL)
7626 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007627 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007628 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007629 return NULL;
7630 *new_ll = TRUE;
7631 }
7632
7633 return qi;
7634}
7635
7636/*
7637 * Search for a pattern in a help file.
7638 */
7639 static void
7640hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007641 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007642 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007643 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007644 regmatch_T *p_regmatch)
7645{
7646 FILE *fd;
7647 long lnum;
7648
7649 fd = mch_fopen((char *)fname, "r");
7650 if (fd == NULL)
7651 return;
7652
7653 lnum = 1;
7654 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7655 {
7656 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007657
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007658 // Convert a line if 'encoding' is not utf-8 and
7659 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007660 if (p_vc->vc_type != CONV_NONE
7661 && has_non_ascii(IObuff))
7662 {
7663 line = string_convert(p_vc, IObuff, NULL);
7664 if (line == NULL)
7665 line = IObuff;
7666 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007667
7668 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7669 {
7670 int l = (int)STRLEN(line);
7671
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007672 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007673 while (l > 0 && line[l - 1] <= ' ')
7674 line[--l] = NUL;
7675
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007676 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007677 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007678 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007679 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007680 0,
7681 line,
7682 lnum,
7683 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007684 + 1, // col
7685 FALSE, // vis_col
7686 NULL, // search pattern
7687 0, // nr
7688 1, // type
7689 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02007690 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007691 {
7692 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007693 if (line != IObuff)
7694 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007695 break;
7696 }
7697 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007698 if (line != IObuff)
7699 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007700 ++lnum;
7701 line_breakcheck();
7702 }
7703 fclose(fd);
7704}
7705
7706/*
7707 * Search for a pattern in all the help files in the doc directory under
7708 * the given directory.
7709 */
7710 static void
7711hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007712 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007713 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007714 regmatch_T *p_regmatch,
7715 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007716#ifdef FEAT_MULTI_LANG
7717 , char_u *lang
7718#endif
7719 )
7720{
7721 int fcount;
7722 char_u **fnames;
7723 int fi;
7724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007725 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007726 add_pathsep(dirname);
7727 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7728 if (gen_expand_wildcards(1, &dirname, &fcount,
7729 &fnames, EW_FILE|EW_SILENT) == OK
7730 && fcount > 0)
7731 {
7732 for (fi = 0; fi < fcount && !got_int; ++fi)
7733 {
7734#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007735 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007736 if (lang != NULL
7737 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007738 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007739 && !(STRNICMP(lang, "en", 2) == 0
7740 && STRNICMP("txt", fnames[fi]
7741 + STRLEN(fnames[fi]) - 3, 3) == 0))
7742 continue;
7743#endif
7744
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007745 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007746 }
7747 FreeWild(fcount, fnames);
7748 }
7749}
7750
7751/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007752 * Search for a pattern in all the help files in the 'runtimepath'
7753 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007754 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007755 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007756 */
7757 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007758hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007759{
7760 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007761
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007762 vimconv_T vc;
7763
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007764 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7765 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007766 vc.vc_type = CONV_NONE;
7767 if (!enc_utf8)
7768 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007769
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007770 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007771 p = p_rtp;
7772 while (*p != NUL && !got_int)
7773 {
7774 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7775
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007776 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007777#ifdef FEAT_MULTI_LANG
7778 , lang
7779#endif
7780 );
7781 }
7782
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007783 if (vc.vc_type != CONV_NONE)
7784 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007785}
7786
7787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 * ":helpgrep {pattern}"
7789 */
7790 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007791ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 regmatch_T regmatch;
7794 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007795 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007796 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007797 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007798 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799
Bram Moolenaar73633f82012-01-20 13:39:07 +01007800 switch (eap->cmdidx)
7801 {
7802 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7803 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7804 default: break;
7805 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007806 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7807 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007808 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007809#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007810 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007811 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007812#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007813 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007814
Bram Moolenaar39665952018-08-15 20:59:48 +02007815 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007816 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007817 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007818 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007819 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007820 }
7821
Bram Moolenaara16123a2019-03-28 20:31:07 +01007822 // Make 'cpoptions' empty, the 'l' flag should not be used here.
7823 save_cpo = p_cpo;
7824 p_cpo = empty_option;
7825
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007826 incr_quickfix_busy();
7827
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007828#ifdef FEAT_MULTI_LANG
7829 // Check for a specified language
7830 lang = check_help_lang(eap->arg);
7831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7833 regmatch.rm_ic = FALSE;
7834 if (regmatch.regprog != NULL)
7835 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007836 qf_list_T *qfl;
7837
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007838 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007839 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007840 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007842 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007843
Bram Moolenaar473de612013-06-08 18:19:48 +02007844 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007846 qfl->qf_nonevalid = FALSE;
7847 qfl->qf_ptr = qfl->qf_start;
7848 qfl->qf_index = 1;
7849 qf_list_changed(qfl);
7850 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 }
7852
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007853 if (p_cpo == empty_option)
7854 p_cpo = save_cpo;
7855 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007856 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007857 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858
Bram Moolenaar73633f82012-01-20 13:39:07 +01007859 if (au_name != NULL)
7860 {
7861 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7862 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007863 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007864 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007865 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007866 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007867 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007868 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007869 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007870
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007871 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007872 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007873 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007874 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007875 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007876
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007877 decr_quickfix_busy();
7878
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007879 if (eap->cmdidx == CMD_lhelpgrep)
7880 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007881 // If the help window is not opened or if it already points to the
7882 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007883 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007884 {
7885 if (new_qi)
7886 ll_free_all(&qi);
7887 }
7888 else if (curwin->w_llist == NULL)
7889 curwin->w_llist = qi;
7890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007892#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02007893
7894#if defined(FEAT_EVAL) || defined(PROTO)
7895# ifdef FEAT_QUICKFIX
7896 static void
7897get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
7898{
7899 if (what_arg->v_type == VAR_UNKNOWN)
7900 {
7901 if (rettv_list_alloc(rettv) == OK)
7902 if (is_qf || wp != NULL)
7903 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
7904 }
7905 else
7906 {
7907 if (rettv_dict_alloc(rettv) == OK)
7908 if (is_qf || (wp != NULL))
7909 {
7910 if (what_arg->v_type == VAR_DICT)
7911 {
7912 dict_T *d = what_arg->vval.v_dict;
7913
7914 if (d != NULL)
7915 qf_get_properties(wp, d, rettv->vval.v_dict);
7916 }
7917 else
7918 emsg(_(e_dictreq));
7919 }
7920 }
7921}
7922# endif
7923
7924/*
7925 * "getloclist()" function
7926 */
7927 void
7928f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7929{
7930# ifdef FEAT_QUICKFIX
7931 win_T *wp;
7932
7933 wp = find_win_by_nr_or_id(&argvars[0]);
7934 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
7935# endif
7936}
7937
7938/*
7939 * "getqflist()" function
7940 */
7941 void
7942f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7943{
7944# ifdef FEAT_QUICKFIX
7945 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
7946# endif
7947}
7948
7949/*
7950 * Used by "setqflist()" and "setloclist()" functions
7951 */
7952 static void
7953set_qf_ll_list(
7954 win_T *wp UNUSED,
7955 typval_T *list_arg UNUSED,
7956 typval_T *action_arg UNUSED,
7957 typval_T *what_arg UNUSED,
7958 typval_T *rettv)
7959{
7960# ifdef FEAT_QUICKFIX
7961 static char *e_invact = N_("E927: Invalid action: '%s'");
7962 char_u *act;
7963 int action = 0;
7964 static int recursive = 0;
7965# endif
7966
7967 rettv->vval.v_number = -1;
7968
7969# ifdef FEAT_QUICKFIX
7970 if (list_arg->v_type != VAR_LIST)
7971 emsg(_(e_listreq));
7972 else if (recursive != 0)
7973 emsg(_(e_au_recursive));
7974 else
7975 {
7976 list_T *l = list_arg->vval.v_list;
7977 dict_T *d = NULL;
7978 int valid_dict = TRUE;
7979
7980 if (action_arg->v_type == VAR_STRING)
7981 {
7982 act = tv_get_string_chk(action_arg);
7983 if (act == NULL)
7984 return; // type error; errmsg already given
7985 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
7986 act[1] == NUL)
7987 action = *act;
7988 else
7989 semsg(_(e_invact), act);
7990 }
7991 else if (action_arg->v_type == VAR_UNKNOWN)
7992 action = ' ';
7993 else
7994 emsg(_(e_stringreq));
7995
7996 if (action_arg->v_type != VAR_UNKNOWN
7997 && what_arg->v_type != VAR_UNKNOWN)
7998 {
7999 if (what_arg->v_type == VAR_DICT)
8000 d = what_arg->vval.v_dict;
8001 else
8002 {
8003 emsg(_(e_dictreq));
8004 valid_dict = FALSE;
8005 }
8006 }
8007
8008 ++recursive;
8009 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
8010 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
8011 d) == OK)
8012 rettv->vval.v_number = 0;
8013 --recursive;
8014 }
8015# endif
8016}
8017
8018/*
8019 * "setloclist()" function
8020 */
8021 void
8022f_setloclist(typval_T *argvars, typval_T *rettv)
8023{
8024 win_T *win;
8025
8026 rettv->vval.v_number = -1;
8027
8028 win = find_win_by_nr_or_id(&argvars[0]);
8029 if (win != NULL)
8030 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8031}
8032
8033/*
8034 * "setqflist()" function
8035 */
8036 void
8037f_setqflist(typval_T *argvars, typval_T *rettv)
8038{
8039 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8040}
8041#endif