blob: 091246b764100844fa28a34d882d778378fb8ad7 [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 Moolenaar858ba062020-05-31 23:11:59 +020085 char_u *qf_qftf; // 'quickfixtextfunc' setting for this list
Bram Moolenaara7df8c72017-07-19 13:23:06 +020086
87 struct dir_stack_T *qf_dir_stack;
88 char_u *qf_directory;
89 struct dir_stack_T *qf_file_stack;
90 char_u *qf_currfile;
91 int qf_multiline;
92 int qf_multiignore;
93 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010094 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000095} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020097/*
98 * Quickfix/Location list stack definition
99 * Contains a list of quickfix/location lists (qf_list_T)
100 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000101struct qf_info_S
102{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200103 // Count of references to this list. Used only for location lists.
104 // When a location list window reference this list, qf_refcount
105 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
106 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000107 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200108 int qf_listcount; // current number of lists
109 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100111 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100112 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000113};
114
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200115static qf_info_T ql_info; // global quickfix list
116static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200118#define FMT_PATTERNS 11 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
120/*
121 * Structure used to hold the info of one part of 'errorformat'
122 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000123typedef struct efm_S efm_T;
124struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200126 regprog_T *prog; // pre-formatted part of 'errorformat'
127 efm_T *next; // pointer to next (NULL if last)
128 char_u addr[FMT_PATTERNS]; // indices of used % patterns
129 char_u prefix; // prefix of this format line:
130 // 'D' enter directory
131 // 'X' leave directory
132 // 'A' start of multi-line message
133 // 'E' error message
134 // 'W' warning message
135 // 'I' informational message
Bram Moolenaare9283662020-06-07 14:10:47 +0200136 // 'N' note message
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200137 // 'C' continuation line
138 // 'Z' end of multi-line message
139 // 'G' general, unspecific message
140 // 'P' push file (partial) message
141 // 'Q' pop/quit file (partial) message
142 // 'O' overread (partial) message
143 char_u flags; // additional flags given in prefix
144 // '-' do not include this line
145 // '+' include whole line in message
146 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147};
148
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200149// List of location lists to be deleted.
150// Used to delay the deletion of locations lists by autocmds.
151typedef struct qf_delq_S
152{
153 struct qf_delq_S *next;
154 qf_info_T *qi;
155} qf_delq_T;
156static qf_delq_T *qf_delq_head = NULL;
157
158// Counter to prevent autocmds from freeing up location lists when they are
159// still being used.
160static int quickfix_busy = 0;
161
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200162static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100163
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100165static 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 +0200166static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100167static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100168static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200169static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200171static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200172static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100175static win_T *qf_find_win(qf_info_T *qi);
176static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200177static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +0200178static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100179static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
180static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
181static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
182static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar3ff33112019-05-03 21:56:35 +0200183static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200185// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000186#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200187// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000188#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200189
190// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100191#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
192#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
193#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
194#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200195
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000196/*
197 * Return location list for window 'wp'
198 * For location list window, return the referenced location list
199 */
200#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
201
Bram Moolenaar95946f12019-03-31 15:31:59 +0200202// Macro to loop through all the items in a quickfix list
203// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100204#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
Bram Moolenaar95946f12019-03-31 15:31:59 +0200205 for (i = 1, qfp = qfl->qf_start; \
206 !got_int && i <= qfl->qf_count && qfp != NULL; \
Bram Moolenaara16123a2019-03-28 20:31:07 +0100207 ++i, qfp = qfp->qf_next)
208
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200210 * Looking up a buffer can be slow if there are many. Remember the last one
211 * to make this a lot faster if there are multiple matches in the same file.
212 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200213static char_u *qf_last_bufname = NULL;
214static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200215
Bram Moolenaar3c097222017-12-21 20:54:49 +0100216static char *e_loc_list_changed =
217 N_("E926: Current location list was changed");
218
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200219/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200220 * Maximum number of bytes allowed per line while reading a errorfile.
221 */
222#define LINE_MAXLEN 4096
223
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200224static struct fmtpattern
225{
226 char_u convchar;
227 char *pattern;
228} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200229 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200230 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200231 {'n', "\\d\\+"},
232 {'l', "\\d\\+"},
233 {'c', "\\d\\+"},
234 {'t', "."},
235 {'m', ".\\+"},
236 {'r', ".*"},
237 {'p', "[- .]*"},
238 {'v', "\\d\\+"},
239 {'s', ".\\+"},
240 {'o', ".\\+"}
241 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200242
243/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200244 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200245 * See fmt_pat definition above for the list of supported patterns. The
246 * pattern specifier is supplied in "efmpat". The converted pattern is stored
247 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200248 */
249 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200250efmpat_to_regpat(
251 char_u *efmpat,
252 char_u *regpat,
253 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100255 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200256{
257 char_u *srcptr;
258
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200259 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200260 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200261 // Each errorformat pattern can occur only once
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100262 semsg(_("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200263 return NULL;
264 }
265 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200266 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200267 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200268 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200269 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100270 semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200271 return NULL;
272 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200273 efminfo->addr[idx] = (char_u)++round;
274 *regpat++ = '\\';
275 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200276#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200277 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200278 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200279 // Also match "c:" in the file name, even when
280 // checking for a colon next: "%f:".
281 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200282 STRCPY(regpat, "\\%(\\a:\\)\\=");
283 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200284 }
285#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200286 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200287 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200288 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200289 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200290 // A file name may contain spaces, but this isn't
291 // in "\f". For "%f:%l:%m" there may be a ":" in
292 // the file name. Use ".\{-1,}x" instead (x is
293 // the next character), the requirement that :999:
294 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200295 STRCPY(regpat, ".\\{-1,}");
296 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200297 }
298 else
299 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200300 // File name followed by '\\' or '%': include as
301 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200302 STRCPY(regpat, "\\f\\+");
303 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200304 }
305 }
306 else
307 {
308 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200309 while ((*regpat = *srcptr++) != NUL)
310 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200311 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200312 *regpat++ = '\\';
313 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200314
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200315 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200316}
317
318/*
319 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200320 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200321 */
322 static char_u *
323scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200324 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200325 char_u *efm,
326 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100327 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200328{
329 char_u *efmp = *pefmp;
330
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200331 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200332 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200333 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200334 {
335 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200336 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200337 if (efmp < efm + len)
338 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200339 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200340 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200341 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200342 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200343 if (efmp == efm + len)
344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100345 emsg(_("E374: Missing ] in format string"));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200346 return NULL;
347 }
348 }
349 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200350 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200351 *regpat++ = *++efmp;
352 *regpat++ = '\\';
353 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200354 }
355 else
356 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200357 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100358 semsg(_("E375: Unsupported %%%c in format string"), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200359 return NULL;
360 }
361
362 *pefmp = efmp;
363
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200364 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200365}
366
367/*
368 * Analyze/parse an errorformat prefix.
369 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200370 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100371efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200372{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200373 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200374 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200375 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200376 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200377 else
378 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100379 semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200380 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200381 }
382
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200383 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200384}
385
386/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200387 * Converts a 'errorformat' string part in 'efm' to a regular expression
388 * pattern. The resulting regex pattern is returned in "regpat". Additional
389 * information about the 'erroformat' pattern is returned in "fmt_ptr".
390 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200391 */
392 static int
393efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200394 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200395 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200396 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100397 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200398{
399 char_u *ptr;
400 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200401 int round;
402 int idx = 0;
403
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200404 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200405 ptr = regpat;
406 *ptr++ = '^';
407 round = 0;
408 for (efmp = efm; efmp < efm + len; ++efmp)
409 {
410 if (*efmp == '%')
411 {
412 ++efmp;
413 for (idx = 0; idx < FMT_PATTERNS; ++idx)
414 if (fmt_pat[idx].convchar == *efmp)
415 break;
416 if (idx < FMT_PATTERNS)
417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100418 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200419 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200420 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200421 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200422 }
423 else if (*efmp == '*')
424 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200425 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100426 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200427 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200428 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200429 }
430 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200431 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200432 else if (*efmp == '#')
433 *ptr++ = '*';
434 else if (*efmp == '>')
435 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200436 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200437 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200438 // prefix is allowed only at the beginning of the errorformat
439 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100440 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200441 if (efmp == NULL)
442 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200443 }
444 else
445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100446 semsg(_("E377: Invalid %%%c in format string"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200447 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200448 }
449 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200450 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200451 {
452 if (*efmp == '\\' && efmp + 1 < efm + len)
453 ++efmp;
454 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200455 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200456 if (*efmp)
457 *ptr++ = *efmp;
458 }
459 }
460 *ptr++ = '$';
461 *ptr = NUL;
462
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200463 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200464}
465
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200466/*
467 * Free the 'errorformat' information list
468 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200469 static void
470free_efm_list(efm_T **efm_first)
471{
472 efm_T *efm_ptr;
473
474 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
475 {
476 *efm_first = efm_ptr->next;
477 vim_regfree(efm_ptr->prog);
478 vim_free(efm_ptr);
479 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100480 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200481}
482
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200483/*
484 * Compute the size of the buffer used to convert a 'errorformat' pattern into
485 * a regular expression pattern.
486 */
487 static int
488efm_regpat_bufsz(char_u *efm)
489{
490 int sz;
491 int i;
492
493 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
494 for (i = FMT_PATTERNS; i > 0; )
495 sz += (int)STRLEN(fmt_pat[--i].pattern);
496#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200497 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200498#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200499 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200500#endif
501
502 return sz;
503}
504
505/*
506 * Return the length of a 'errorformat' option part (separated by ",").
507 */
508 static int
509efm_option_part_len(char_u *efm)
510{
511 int len;
512
513 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
514 if (efm[len] == '\\' && efm[len + 1] != NUL)
515 ++len;
516
517 return len;
518}
519
520/*
521 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
522 * are parsed and converted to regular expressions. Returns information about
523 * the parsed 'errorformat' option.
524 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200525 static efm_T *
526parse_efm_option(char_u *efm)
527{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200528 efm_T *fmt_ptr = NULL;
529 efm_T *fmt_first = NULL;
530 efm_T *fmt_last = NULL;
531 char_u *fmtstr = NULL;
532 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200533 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200534
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200535 // Each part of the format string is copied and modified from errorformat
536 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200537
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200538 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200539 sz = efm_regpat_bufsz(efm);
540 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200541 goto parse_efm_error;
542
543 while (efm[0] != NUL)
544 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200545 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200546 fmt_ptr = ALLOC_CLEAR_ONE(efm_T);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200547 if (fmt_ptr == NULL)
548 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200549 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200550 fmt_first = fmt_ptr;
551 else
552 fmt_last->next = fmt_ptr;
553 fmt_last = fmt_ptr;
554
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200555 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200556 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200557
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100558 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200559 goto parse_efm_error;
560 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
561 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200562 // Advance to next part
563 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200564 }
565
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200566 if (fmt_first == NULL) // nothing found
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100567 emsg(_("E378: 'errorformat' contains no pattern"));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200568
569 goto parse_efm_end;
570
571parse_efm_error:
572 free_efm_list(&fmt_first);
573
574parse_efm_end:
575 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200576
577 return fmt_first;
578}
579
Bram Moolenaare0d37972016-07-15 22:36:01 +0200580enum {
581 QF_FAIL = 0,
582 QF_OK = 1,
583 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200584 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200585 QF_IGNORE_LINE = 4,
586 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200587};
588
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200589/*
590 * State information used to parse lines and add entries to a quickfix/location
591 * list.
592 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200593typedef struct {
594 char_u *linebuf;
595 int linelen;
596 char_u *growbuf;
597 int growbufsiz;
598 FILE *fd;
599 typval_T *tv;
600 char_u *p_str;
601 listitem_T *p_li;
602 buf_T *buf;
603 linenr_T buflnum;
604 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100605 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200606} qfstate_T;
607
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200608/*
609 * Allocate more memory for the line buffer used for parsing lines.
610 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200611 static char_u *
612qf_grow_linebuf(qfstate_T *state, int newsz)
613{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200614 char_u *p;
615
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200616 // If the line exceeds LINE_MAXLEN exclude the last
617 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200618 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
619 if (state->growbuf == NULL)
620 {
621 state->growbuf = alloc(state->linelen + 1);
622 if (state->growbuf == NULL)
623 return NULL;
624 state->growbufsiz = state->linelen;
625 }
626 else if (state->linelen > state->growbufsiz)
627 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200628 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200629 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200630 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200631 state->growbufsiz = state->linelen;
632 }
633 return state->growbuf;
634}
635
636/*
637 * Get the next string (separated by newline) from state->p_str.
638 */
639 static int
640qf_get_next_str_line(qfstate_T *state)
641{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200642 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200643 char_u *p_str = state->p_str;
644 char_u *p;
645 int len;
646
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200647 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200648 return QF_END_OF_INPUT;
649
650 p = vim_strchr(p_str, '\n');
651 if (p != NULL)
652 len = (int)(p - p_str) + 1;
653 else
654 len = (int)STRLEN(p_str);
655
656 if (len > IOSIZE - 2)
657 {
658 state->linebuf = qf_grow_linebuf(state, len);
659 if (state->linebuf == NULL)
660 return QF_NOMEM;
661 }
662 else
663 {
664 state->linebuf = IObuff;
665 state->linelen = len;
666 }
667 vim_strncpy(state->linebuf, p_str, state->linelen);
668
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200669 // Increment using len in order to discard the rest of the
670 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200671 p_str += len;
672 state->p_str = p_str;
673
674 return QF_OK;
675}
676
677/*
678 * Get the next string from state->p_Li.
679 */
680 static int
681qf_get_next_list_line(qfstate_T *state)
682{
683 listitem_T *p_li = state->p_li;
684 int len;
685
686 while (p_li != NULL
687 && (p_li->li_tv.v_type != VAR_STRING
688 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200689 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200690
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200691 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200692 {
693 state->p_li = NULL;
694 return QF_END_OF_INPUT;
695 }
696
697 len = (int)STRLEN(p_li->li_tv.vval.v_string);
698 if (len > IOSIZE - 2)
699 {
700 state->linebuf = qf_grow_linebuf(state, len);
701 if (state->linebuf == NULL)
702 return QF_NOMEM;
703 }
704 else
705 {
706 state->linebuf = IObuff;
707 state->linelen = len;
708 }
709
710 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
711
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200712 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200713 return QF_OK;
714}
715
716/*
717 * Get the next string from state->buf.
718 */
719 static int
720qf_get_next_buf_line(qfstate_T *state)
721{
722 char_u *p_buf = NULL;
723 int len;
724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200725 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200726 if (state->buflnum > state->lnumlast)
727 return QF_END_OF_INPUT;
728
729 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
730 state->buflnum += 1;
731
732 len = (int)STRLEN(p_buf);
733 if (len > IOSIZE - 2)
734 {
735 state->linebuf = qf_grow_linebuf(state, len);
736 if (state->linebuf == NULL)
737 return QF_NOMEM;
738 }
739 else
740 {
741 state->linebuf = IObuff;
742 state->linelen = len;
743 }
744 vim_strncpy(state->linebuf, p_buf, state->linelen);
745
746 return QF_OK;
747}
748
749/*
750 * Get the next string from file state->fd.
751 */
752 static int
753qf_get_next_file_line(qfstate_T *state)
754{
755 int discard;
756 int growbuflen;
757
758 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
759 return QF_END_OF_INPUT;
760
761 discard = FALSE;
762 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200763 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200764 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200765 // The current line exceeds IObuff, continue reading using
766 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200767 if (state->growbuf == NULL)
768 {
769 state->growbufsiz = 2 * (IOSIZE - 1);
770 state->growbuf = alloc(state->growbufsiz);
771 if (state->growbuf == NULL)
772 return QF_NOMEM;
773 }
774
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200775 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200776 memcpy(state->growbuf, IObuff, IOSIZE - 1);
777 growbuflen = state->linelen;
778
779 for (;;)
780 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200781 char_u *p;
782
Bram Moolenaare0d37972016-07-15 22:36:01 +0200783 if (fgets((char *)state->growbuf + growbuflen,
784 state->growbufsiz - growbuflen, state->fd) == NULL)
785 break;
786 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
787 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200788 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200789 break;
790 if (state->growbufsiz == LINE_MAXLEN)
791 {
792 discard = TRUE;
793 break;
794 }
795
796 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
797 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200798 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200799 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200800 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200801 }
802
803 while (discard)
804 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200805 // The current line is longer than LINE_MAXLEN, continue
806 // reading but discard everything until EOL or EOF is
807 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200808 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
809 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200810 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200811 break;
812 }
813
814 state->linebuf = state->growbuf;
815 state->linelen = growbuflen;
816 }
817 else
818 state->linebuf = IObuff;
819
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200820 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200821 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
822 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100823 char_u *line;
824
825 line = string_convert(&state->vc, state->linebuf, &state->linelen);
826 if (line != NULL)
827 {
828 if (state->linelen < IOSIZE)
829 {
830 STRCPY(state->linebuf, line);
831 vim_free(line);
832 }
833 else
834 {
835 vim_free(state->growbuf);
836 state->linebuf = state->growbuf = line;
837 state->growbufsiz = state->linelen < LINE_MAXLEN
838 ? state->linelen : LINE_MAXLEN;
839 }
840 }
841 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100842
Bram Moolenaare0d37972016-07-15 22:36:01 +0200843 return QF_OK;
844}
845
846/*
847 * Get the next string from a file/buffer/list/string.
848 */
849 static int
850qf_get_nextline(qfstate_T *state)
851{
852 int status = QF_FAIL;
853
854 if (state->fd == NULL)
855 {
856 if (state->tv != NULL)
857 {
858 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200859 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200860 status = qf_get_next_str_line(state);
861 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200862 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200863 status = qf_get_next_list_line(state);
864 }
865 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200866 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200867 status = qf_get_next_buf_line(state);
868 }
869 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200870 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200871 status = qf_get_next_file_line(state);
872
873 if (status != QF_OK)
874 return status;
875
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200876 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200877 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200878 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200879 state->linebuf[state->linelen - 1] = NUL;
880#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200881 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
882 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200883#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200884 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200885
Bram Moolenaare0d37972016-07-15 22:36:01 +0200886 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200887
888 return QF_OK;
889}
890
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200891typedef struct {
892 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200893 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200894 char_u *errmsg;
895 int errmsglen;
896 long lnum;
897 int col;
898 char_u use_viscol;
899 char_u *pattern;
900 int enr;
901 int type;
902 int valid;
903} qffields_T;
904
905/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200906 * Parse the match for filename ('%f') pattern in regmatch.
907 * Return the matched value in "fields->namebuf".
908 */
909 static int
910qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
911{
912 int c;
913
914 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
915 return QF_FAIL;
916
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200917 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200918 c = *rmp->endp[midx];
919 *rmp->endp[midx] = NUL;
920 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
921 *rmp->endp[midx] = c;
922
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200923 // For separate filename patterns (%O, %P and %Q), the specified file
924 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200925 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
926 && mch_getperm(fields->namebuf) == -1)
927 return QF_FAIL;
928
929 return QF_OK;
930}
931
932/*
933 * Parse the match for error number ('%n') pattern in regmatch.
934 * Return the matched value in "fields->enr".
935 */
936 static int
937qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
938{
939 if (rmp->startp[midx] == NULL)
940 return QF_FAIL;
941 fields->enr = (int)atol((char *)rmp->startp[midx]);
942 return QF_OK;
943}
944
945/*
946 * Parse the match for line number (%l') pattern in regmatch.
947 * Return the matched value in "fields->lnum".
948 */
949 static int
950qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
951{
952 if (rmp->startp[midx] == NULL)
953 return QF_FAIL;
954 fields->lnum = atol((char *)rmp->startp[midx]);
955 return QF_OK;
956}
957
958/*
959 * Parse the match for column number ('%c') pattern in regmatch.
960 * Return the matched value in "fields->col".
961 */
962 static int
963qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
964{
965 if (rmp->startp[midx] == NULL)
966 return QF_FAIL;
967 fields->col = (int)atol((char *)rmp->startp[midx]);
968 return QF_OK;
969}
970
971/*
972 * Parse the match for error type ('%t') pattern in regmatch.
973 * Return the matched value in "fields->type".
974 */
975 static int
976qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
977{
978 if (rmp->startp[midx] == NULL)
979 return QF_FAIL;
980 fields->type = *rmp->startp[midx];
981 return QF_OK;
982}
983
984/*
Bram Moolenaarf4140482020-02-15 23:06:45 +0100985 * Copy a non-error line into the error string. Return the matched line in
986 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200987 */
988 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +0100989copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200990{
991 char_u *p;
992
993 if (linelen >= fields->errmsglen)
994 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200995 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200996 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
997 return QF_NOMEM;
998 fields->errmsg = p;
999 fields->errmsglen = linelen + 1;
1000 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001001 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001002 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001003
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001004 return QF_OK;
1005}
1006
1007/*
1008 * Parse the match for error message ('%m') pattern in regmatch.
1009 * Return the matched value in "fields->errmsg".
1010 */
1011 static int
1012qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1013{
1014 char_u *p;
1015 int len;
1016
1017 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1018 return QF_FAIL;
1019 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1020 if (len >= fields->errmsglen)
1021 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001022 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001023 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1024 return QF_NOMEM;
1025 fields->errmsg = p;
1026 fields->errmsglen = len + 1;
1027 }
1028 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1029 return QF_OK;
1030}
1031
1032/*
1033 * Parse the match for rest of a single-line file message ('%r') pattern.
1034 * Return the matched value in "tail".
1035 */
1036 static int
1037qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1038{
1039 if (rmp->startp[midx] == NULL)
1040 return QF_FAIL;
1041 *tail = rmp->startp[midx];
1042 return QF_OK;
1043}
1044
1045/*
1046 * Parse the match for the pointer line ('%p') pattern in regmatch.
1047 * Return the matched value in "fields->col".
1048 */
1049 static int
1050qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1051{
1052 char_u *match_ptr;
1053
1054 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1055 return QF_FAIL;
1056 fields->col = 0;
1057 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1058 ++match_ptr)
1059 {
1060 ++fields->col;
1061 if (*match_ptr == TAB)
1062 {
1063 fields->col += 7;
1064 fields->col -= fields->col % 8;
1065 }
1066 }
1067 ++fields->col;
1068 fields->use_viscol = TRUE;
1069 return QF_OK;
1070}
1071
1072/*
1073 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1074 * Return the matched value in "fields->col".
1075 */
1076 static int
1077qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1078{
1079 if (rmp->startp[midx] == NULL)
1080 return QF_FAIL;
1081 fields->col = (int)atol((char *)rmp->startp[midx]);
1082 fields->use_viscol = TRUE;
1083 return QF_OK;
1084}
1085
1086/*
1087 * Parse the match for the search text ('%s') pattern in regmatch.
1088 * Return the matched value in "fields->pattern".
1089 */
1090 static int
1091qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1092{
1093 int len;
1094
1095 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1096 return QF_FAIL;
1097 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1098 if (len > CMDBUFFSIZE - 5)
1099 len = CMDBUFFSIZE - 5;
1100 STRCPY(fields->pattern, "^\\V");
1101 STRNCAT(fields->pattern, rmp->startp[midx], len);
1102 fields->pattern[len + 3] = '\\';
1103 fields->pattern[len + 4] = '$';
1104 fields->pattern[len + 5] = NUL;
1105 return QF_OK;
1106}
1107
1108/*
1109 * Parse the match for the module ('%o') pattern in regmatch.
1110 * Return the matched value in "fields->module".
1111 */
1112 static int
1113qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1114{
1115 int len;
1116
1117 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1118 return QF_FAIL;
1119 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1120 if (len > CMDBUFFSIZE)
1121 len = CMDBUFFSIZE;
1122 STRNCAT(fields->module, rmp->startp[midx], len);
1123 return QF_OK;
1124}
1125
1126/*
1127 * 'errorformat' format pattern parser functions.
1128 * The '%f' and '%r' formats are parsed differently from other formats.
1129 * See qf_parse_match() for details.
1130 */
1131static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1132{
1133 NULL,
1134 qf_parse_fmt_n,
1135 qf_parse_fmt_l,
1136 qf_parse_fmt_c,
1137 qf_parse_fmt_t,
1138 qf_parse_fmt_m,
1139 NULL,
1140 qf_parse_fmt_p,
1141 qf_parse_fmt_v,
1142 qf_parse_fmt_s,
1143 qf_parse_fmt_o
1144};
1145
1146/*
1147 * Parse the error format pattern matches in "regmatch" and set the values in
1148 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1149 * match. Returns QF_OK if all the matches are successfully parsed. On
1150 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001151 */
1152 static int
1153qf_parse_match(
1154 char_u *linebuf,
1155 int linelen,
1156 efm_T *fmt_ptr,
1157 regmatch_T *regmatch,
1158 qffields_T *fields,
1159 int qf_multiline,
1160 int qf_multiscan,
1161 char_u **tail)
1162{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001163 int idx = fmt_ptr->prefix;
1164 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001165 int midx;
1166 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001167
1168 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1169 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001170 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001171 fields->type = idx;
1172 else
1173 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001174
1175 // Extract error message data from matched line.
1176 // We check for an actual submatch, because "\[" and "\]" in
1177 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001178 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001179 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001180 status = QF_OK;
1181 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001182 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001183 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1184 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001185 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001186 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001187 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001188 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001189 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001190 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001191 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001192 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001193 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001194 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001195
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001196 if (status != QF_OK)
1197 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001198 }
1199
1200 return QF_OK;
1201}
1202
1203/*
1204 * Parse an error line in 'linebuf' using a single error format string in
1205 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1206 * Returns QF_OK if the efm format matches completely and the fields are
1207 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1208 */
1209 static int
1210qf_parse_get_fields(
1211 char_u *linebuf,
1212 int linelen,
1213 efm_T *fmt_ptr,
1214 qffields_T *fields,
1215 int qf_multiline,
1216 int qf_multiscan,
1217 char_u **tail)
1218{
1219 regmatch_T regmatch;
1220 int status = QF_FAIL;
1221 int r;
1222
1223 if (qf_multiscan &&
1224 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1225 return QF_FAIL;
1226
1227 fields->namebuf[0] = NUL;
1228 fields->module[0] = NUL;
1229 fields->pattern[0] = NUL;
1230 if (!qf_multiscan)
1231 fields->errmsg[0] = NUL;
1232 fields->lnum = 0;
1233 fields->col = 0;
1234 fields->use_viscol = FALSE;
1235 fields->enr = -1;
1236 fields->type = 0;
1237 *tail = NULL;
1238
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001239 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001240 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001241 regmatch.regprog = fmt_ptr->prog;
1242 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1243 fmt_ptr->prog = regmatch.regprog;
1244 if (r)
1245 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1246 fields, qf_multiline, qf_multiscan, tail);
1247
1248 return status;
1249}
1250
1251/*
1252 * Parse directory error format prefixes (%D and %X).
1253 * Push and pop directories from the directory stack when scanning directory
1254 * names.
1255 */
1256 static int
1257qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1258{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001259 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001260 {
1261 if (*fields->namebuf == NUL)
1262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001263 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001264 return QF_FAIL;
1265 }
1266 qfl->qf_directory =
1267 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1268 if (qfl->qf_directory == NULL)
1269 return QF_FAIL;
1270 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001271 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001272 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1273
1274 return QF_OK;
1275}
1276
1277/*
1278 * Parse global file name error format prefixes (%O, %P and %Q).
1279 */
1280 static int
1281qf_parse_file_pfx(
1282 int idx,
1283 qffields_T *fields,
1284 qf_list_T *qfl,
1285 char_u *tail)
1286{
1287 fields->valid = FALSE;
1288 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1289 {
1290 if (*fields->namebuf && idx == 'P')
1291 qfl->qf_currfile =
1292 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1293 else if (idx == 'Q')
1294 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1295 *fields->namebuf = NUL;
1296 if (tail && *tail)
1297 {
1298 STRMOVE(IObuff, skipwhite(tail));
1299 qfl->qf_multiscan = TRUE;
1300 return QF_MULTISCAN;
1301 }
1302 }
1303
1304 return QF_OK;
1305}
1306
1307/*
1308 * Parse a non-error line (a line which doesn't match any of the error
1309 * format in 'efm').
1310 */
1311 static int
1312qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1313{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001314 fields->namebuf[0] = NUL; // no match found, remove file name
1315 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001316 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001317
Bram Moolenaarf4140482020-02-15 23:06:45 +01001318 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001319}
1320
1321/*
1322 * Parse multi-line error format prefixes (%C and %Z)
1323 */
1324 static int
1325qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001326 int idx,
1327 qf_list_T *qfl,
1328 qffields_T *fields)
1329{
1330 char_u *ptr;
1331 int len;
1332
1333 if (!qfl->qf_multiignore)
1334 {
1335 qfline_T *qfprev = qfl->qf_last;
1336
1337 if (qfprev == NULL)
1338 return QF_FAIL;
1339 if (*fields->errmsg && !qfl->qf_multiignore)
1340 {
1341 len = (int)STRLEN(qfprev->qf_text);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001342 if ((ptr = alloc(len + STRLEN(fields->errmsg) + 2))
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001343 == NULL)
1344 return QF_FAIL;
1345 STRCPY(ptr, qfprev->qf_text);
1346 vim_free(qfprev->qf_text);
1347 qfprev->qf_text = ptr;
1348 *(ptr += len) = '\n';
1349 STRCPY(++ptr, fields->errmsg);
1350 }
1351 if (qfprev->qf_nr == -1)
1352 qfprev->qf_nr = fields->enr;
1353 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001354 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001355 qfprev->qf_type = fields->type;
1356
1357 if (!qfprev->qf_lnum)
1358 qfprev->qf_lnum = fields->lnum;
1359 if (!qfprev->qf_col)
1360 qfprev->qf_col = fields->col;
1361 qfprev->qf_viscol = fields->use_viscol;
1362 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001363 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001364 qfl->qf_directory,
1365 *fields->namebuf || qfl->qf_directory != NULL
1366 ? fields->namebuf
1367 : qfl->qf_currfile != NULL && fields->valid
1368 ? qfl->qf_currfile : 0);
1369 }
1370 if (idx == 'Z')
1371 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1372 line_breakcheck();
1373
1374 return QF_IGNORE_LINE;
1375}
1376
1377/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001378 * Parse a line and get the quickfix fields.
1379 * Return the QF_ status.
1380 */
1381 static int
1382qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001383 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001384 char_u *linebuf,
1385 int linelen,
1386 efm_T *fmt_first,
1387 qffields_T *fields)
1388{
1389 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001390 int idx = 0;
1391 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001392 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001393
Bram Moolenaare333e792018-04-08 13:27:39 +02001394restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001395 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001396 if (fmt_start == NULL)
1397 fmt_ptr = fmt_first;
1398 else
1399 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001400 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001401 fmt_ptr = fmt_start;
1402 fmt_start = NULL;
1403 }
1404
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001405 // Try to match each part of 'errorformat' until we find a complete
1406 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001407 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001408 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1409 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001410 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001411 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1412 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1413 if (status == QF_NOMEM)
1414 return status;
1415 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001416 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001418 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001419
1420 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1421 {
1422 if (fmt_ptr != NULL)
1423 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001424 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001425 status = qf_parse_dir_pfx(idx, fields, qfl);
1426 if (status != QF_OK)
1427 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001428 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001429
1430 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1431 if (status != QF_OK)
1432 return status;
1433
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001434 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001435 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001436 }
1437 else if (fmt_ptr != NULL)
1438 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001439 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001440 if (fmt_ptr->conthere)
1441 fmt_start = fmt_ptr;
1442
Bram Moolenaare9283662020-06-07 14:10:47 +02001443 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001444 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001445 qfl->qf_multiline = TRUE; // start of a multi-line message
1446 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001447 }
1448 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001449 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001450 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001451 if (status != QF_OK)
1452 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001453 }
1454 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001455 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001456 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1457 if (status == QF_MULTISCAN)
1458 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001459 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001460 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001461 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001462 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001463 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001464 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001465 return QF_IGNORE_LINE;
1466 }
1467 }
1468
1469 return QF_OK;
1470}
1471
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001472/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001473 * Returns TRUE if the specified quickfix/location stack is empty
1474 */
1475 static int
1476qf_stack_empty(qf_info_T *qi)
1477{
1478 return qi == NULL || qi->qf_listcount <= 0;
1479}
1480
1481/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001482 * Returns TRUE if the specified quickfix/location list is empty.
1483 */
1484 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001485qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001486{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001487 return qfl == NULL || qfl->qf_count <= 0;
1488}
1489
1490/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001491 * Returns TRUE if the specified quickfix/location list is not empty and
1492 * has valid entries.
1493 */
1494 static int
1495qf_list_has_valid_entries(qf_list_T *qfl)
1496{
1497 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1498}
1499
1500/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001501 * Return a pointer to a list in the specified quickfix stack
1502 */
1503 static qf_list_T *
1504qf_get_list(qf_info_T *qi, int idx)
1505{
1506 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001507}
1508
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001509/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001510 * Allocate the fields used for parsing lines and populating a quickfix list.
1511 */
1512 static int
1513qf_alloc_fields(qffields_T *pfields)
1514{
1515 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1516 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1517 pfields->errmsglen = CMDBUFFSIZE + 1;
1518 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1519 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1520 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1521 || pfields->pattern == NULL || pfields->module == NULL)
1522 return FAIL;
1523
1524 return OK;
1525}
1526
1527/*
1528 * Free the fields used for parsing lines and populating a quickfix list.
1529 */
1530 static void
1531qf_free_fields(qffields_T *pfields)
1532{
1533 vim_free(pfields->namebuf);
1534 vim_free(pfields->module);
1535 vim_free(pfields->errmsg);
1536 vim_free(pfields->pattern);
1537}
1538
1539/*
1540 * Setup the state information used for parsing lines and populating a
1541 * quickfix list.
1542 */
1543 static int
1544qf_setup_state(
1545 qfstate_T *pstate,
1546 char_u *enc,
1547 char_u *efile,
1548 typval_T *tv,
1549 buf_T *buf,
1550 linenr_T lnumfirst,
1551 linenr_T lnumlast)
1552{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001553 pstate->vc.vc_type = CONV_NONE;
1554 if (enc != NULL && *enc != NUL)
1555 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001556
1557 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1558 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001559 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001560 return FAIL;
1561 }
1562
1563 if (tv != NULL)
1564 {
1565 if (tv->v_type == VAR_STRING)
1566 pstate->p_str = tv->vval.v_string;
1567 else if (tv->v_type == VAR_LIST)
1568 pstate->p_li = tv->vval.v_list->lv_first;
1569 pstate->tv = tv;
1570 }
1571 pstate->buf = buf;
1572 pstate->buflnum = lnumfirst;
1573 pstate->lnumlast = lnumlast;
1574
1575 return OK;
1576}
1577
1578/*
1579 * Cleanup the state information used for parsing lines and populating a
1580 * quickfix list.
1581 */
1582 static void
1583qf_cleanup_state(qfstate_T *pstate)
1584{
1585 if (pstate->fd != NULL)
1586 fclose(pstate->fd);
1587
1588 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001589 if (pstate->vc.vc_type != CONV_NONE)
1590 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001591}
1592
1593/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001594 * Process the next line from a file/buffer/list/string and add it
1595 * to the quickfix list 'qfl'.
1596 */
1597 static int
1598qf_init_process_nextline(
1599 qf_list_T *qfl,
1600 efm_T *fmt_first,
1601 qfstate_T *state,
1602 qffields_T *fields)
1603{
1604 int status;
1605
1606 // Get the next line from a file/buffer/list/string
1607 status = qf_get_nextline(state);
1608 if (status != QF_OK)
1609 return status;
1610
1611 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1612 fmt_first, fields);
1613 if (status != QF_OK)
1614 return status;
1615
1616 return qf_add_entry(qfl,
1617 qfl->qf_directory,
1618 (*fields->namebuf || qfl->qf_directory != NULL)
1619 ? fields->namebuf
1620 : ((qfl->qf_currfile != NULL && fields->valid)
1621 ? qfl->qf_currfile : (char_u *)NULL),
1622 fields->module,
1623 0,
1624 fields->errmsg,
1625 fields->lnum,
1626 fields->col,
1627 fields->use_viscol,
1628 fields->pattern,
1629 fields->enr,
1630 fields->type,
1631 fields->valid);
1632}
1633
1634/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001635 * Read the errorfile "efile" into memory, line by line, building the error
1636 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001637 * Alternative: when "efile" is NULL read errors from buffer "buf".
1638 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001639 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001640 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1641 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001642 * Return -1 for error, number of errors for success.
1643 */
1644 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001645qf_init_ext(
1646 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001647 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001648 char_u *efile,
1649 buf_T *buf,
1650 typval_T *tv,
1651 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001652 int newlist, // TRUE: start a new error list
1653 linenr_T lnumfirst, // first line number to use
1654 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001655 char_u *qf_title,
1656 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001657{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001658 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001659 qfstate_T state;
1660 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001661 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001662 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001663 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001665 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001666 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001667 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001669 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001670 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001671
Bram Moolenaara80faa82020-04-12 19:37:17 +02001672 CLEAR_FIELD(state);
1673 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001674 if ((qf_alloc_fields(&fields) == FAIL) ||
1675 (qf_setup_state(&state, enc, efile, tv, buf,
1676 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 goto qf_init_end;
1678
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001679 if (newlist || qf_idx == qi->qf_listcount)
1680 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001681 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001682 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001683 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001684 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001685 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001686 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001687 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001688 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001689 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001690 qfl = qf_get_list(qi, qf_idx);
1691 if (!qf_list_empty(qfl))
1692 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001695 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001696 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001697 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 else
1699 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001701 // If the errorformat didn't change between calls, then reuse the
1702 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001703 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1704 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001705 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001706 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001707 free_efm_list(&fmt_first);
1708
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001709 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001710 fmt_first = parse_efm_option(efm);
1711 if (fmt_first != NULL)
1712 last_efm = vim_strsave(efm);
1713 }
1714
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001715 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001718 // got_int is reset here, because it was probably set when killing the
1719 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 got_int = FALSE;
1721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001722 // Read the lines in the error file one by one.
1723 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001724 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001726 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001727 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001728 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001729 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001730 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001731 if (status == QF_FAIL)
1732 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 line_breakcheck();
1735 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001736 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001738 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001740 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001741 qfl->qf_ptr = qfl->qf_start;
1742 qfl->qf_index = 1;
1743 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 }
1745 else
1746 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001747 qfl->qf_nonevalid = FALSE;
1748 if (qfl->qf_ptr == NULL)
1749 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001751 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001752 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001753 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001755 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001757 if (!adding)
1758 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001759 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001760 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001761 qi->qf_listcount--;
1762 if (qi->qf_curlist > 0)
1763 --qi->qf_curlist;
1764 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001765qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001766 if (qf_idx == qi->qf_curlist)
1767 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001768 qf_cleanup_state(&state);
1769 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
1771 return retval;
1772}
1773
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001774/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001775 * Read the errorfile "efile" into memory, line by line, building the error
1776 * list. Set the error list's title to qf_title.
1777 * Return -1 for error, number of errors for success.
1778 */
1779 int
1780qf_init(win_T *wp,
1781 char_u *efile,
1782 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001783 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001784 char_u *qf_title,
1785 char_u *enc)
1786{
1787 qf_info_T *qi = &ql_info;
1788
1789 if (wp != NULL)
1790 {
1791 qi = ll_get_or_alloc_list(wp);
1792 if (qi == NULL)
1793 return FAIL;
1794 }
1795
1796 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1797 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1798}
1799
1800/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001801 * Set the title of the specified quickfix list. Frees the previous title.
1802 * Prepends ':' to the title.
1803 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001804 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001805qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001806{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001807 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001808
Bram Moolenaarfb604092014-07-23 15:55:00 +02001809 if (title != NULL)
1810 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02001811 char_u *p = alloc(STRLEN(title) + 2);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001812
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001813 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001814 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001815 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001816 }
1817}
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001820 * The title of a quickfix/location list is set, by default, to the command
1821 * that created the quickfix list with the ":" prefix.
1822 * Create a quickfix list title string by prepending ":" to a user command.
1823 * Returns a pointer to a static buffer with the title.
1824 */
1825 static char_u *
1826qf_cmdtitle(char_u *cmd)
1827{
1828 static char_u qftitle_str[IOSIZE];
1829
1830 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1831 return qftitle_str;
1832}
1833
1834/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001835 * Return a pointer to the current list in the specified quickfix stack
1836 */
1837 static qf_list_T *
1838qf_get_curlist(qf_info_T *qi)
1839{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001840 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001841}
1842
1843/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001844 * Prepare for adding a new quickfix list. If the current list is in the
1845 * middle of the stack, then all the following lists are freed and then
1846 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 */
1848 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001849qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850{
1851 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001852 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001854 // If the current entry is not the last entry, delete entries beyond
1855 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001856 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001857 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001858 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001860 // When the stack is full, remove to oldest entry
1861 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001862 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001864 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001866 qi->qf_lists[i - 1] = qi->qf_lists[i];
1867 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001870 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001871 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001872 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001873 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001874 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001875 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876}
1877
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001878/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001879 * Queue location list stack delete request.
1880 */
1881 static void
1882locstack_queue_delreq(qf_info_T *qi)
1883{
1884 qf_delq_T *q;
1885
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001886 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001887 if (q != NULL)
1888 {
1889 q->qi = qi;
1890 q->next = qf_delq_head;
1891 qf_delq_head = q;
1892 }
1893}
1894
1895/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001896 * Return the global quickfix stack window buffer number.
1897 */
1898 int
1899qf_stack_get_bufnr(void)
1900{
1901 return ql_info.qf_bufnr;
1902}
1903
1904/*
1905 * Wipe the quickfix window buffer (if present) for the specified
1906 * quickfix/location list.
1907 */
1908 static void
1909wipe_qf_buffer(qf_info_T *qi)
1910{
1911 buf_T *qfbuf;
1912
1913 if (qi->qf_bufnr != INVALID_QFBUFNR)
1914 {
1915 qfbuf = buflist_findnr(qi->qf_bufnr);
1916 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1917 {
1918 // If the quickfix buffer is not loaded in any window, then
1919 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001920 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001921 qi->qf_bufnr = INVALID_QFBUFNR;
1922 }
1923 }
1924}
1925
1926/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001927 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001928 */
1929 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001930ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001931{
1932 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001933 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001934
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001935 qi = *pqi;
1936 if (qi == NULL)
1937 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001938 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001939
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001940 // If the location list is still in use, then queue the delete request
1941 // to be processed later.
1942 if (quickfix_busy > 0)
1943 {
1944 locstack_queue_delreq(qi);
1945 return;
1946 }
1947
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001948 qi->qf_refcount--;
1949 if (qi->qf_refcount < 1)
1950 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001951 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001952 // If the quickfix window buffer is loaded, then wipe it
1953 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001954
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001955 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001956 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001957 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001958 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001959}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001960
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001961/*
1962 * Free all the quickfix/location lists in the stack.
1963 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001964 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001965qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001966{
1967 int i;
1968 qf_info_T *qi = &ql_info;
1969
1970 if (wp != NULL)
1971 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001972 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001973 ll_free_all(&wp->w_llist);
1974 ll_free_all(&wp->w_llist_ref);
1975 }
1976 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001977 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001978 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001979 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001983 * Delay freeing of location list stacks when the quickfix code is running.
1984 * Used to avoid problems with autocmds freeing location list stacks when the
1985 * quickfix code is still referencing the stack.
1986 * Must always call decr_quickfix_busy() exactly once after this.
1987 */
1988 static void
1989incr_quickfix_busy(void)
1990{
1991 quickfix_busy++;
1992}
1993
1994/*
1995 * Safe to free location list stacks. Process any delayed delete requests.
1996 */
1997 static void
1998decr_quickfix_busy(void)
1999{
2000 if (--quickfix_busy == 0)
2001 {
2002 // No longer referencing the location lists. Process all the pending
2003 // delete requests.
2004 while (qf_delq_head != NULL)
2005 {
2006 qf_delq_T *q = qf_delq_head;
2007
2008 qf_delq_head = q->next;
2009 ll_free_all(&q->qi);
2010 vim_free(q);
2011 }
2012 }
2013#ifdef ABORT_ON_INTERNAL_ERROR
2014 if (quickfix_busy < 0)
2015 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002016 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002017 abort();
2018 }
2019#endif
2020}
2021
2022#if defined(EXITFREE) || defined(PROTO)
2023 void
2024check_quickfix_busy(void)
2025{
2026 if (quickfix_busy != 0)
2027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002028 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002029# ifdef ABORT_ON_INTERNAL_ERROR
2030 abort();
2031# endif
2032 }
2033}
2034#endif
2035
2036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 * Add an entry to the end of the list of errors.
Bram Moolenaar95946f12019-03-31 15:31:59 +02002038 * Returns QF_OK or QF_FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 */
2040 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002041qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002042 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002043 char_u *dir, // optional directory name
2044 char_u *fname, // file name or NULL
2045 char_u *module, // module name or NULL
2046 int bufnum, // buffer number or zero
2047 char_u *mesg, // message
2048 long lnum, // line number
2049 int col, // column
2050 int vis_col, // using visual column
2051 char_u *pattern, // search pattern
2052 int nr, // error number
2053 int type, // type character
2054 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002056 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002057 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002059 if ((qfp = ALLOC_ONE(qfline_T)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002060 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002061 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002062 {
2063 buf_T *buf = buflist_findnr(bufnum);
2064
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002065 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002066 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002067 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002068 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002069 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002070 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002071 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2073 {
2074 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002075 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 }
2077 qfp->qf_lnum = lnum;
2078 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002079 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002080 if (pattern == NULL || *pattern == NUL)
2081 qfp->qf_pattern = NULL;
2082 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2083 {
2084 vim_free(qfp->qf_text);
2085 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002086 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002087 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002088 if (module == NULL || *module == NUL)
2089 qfp->qf_module = NULL;
2090 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2091 {
2092 vim_free(qfp->qf_text);
2093 vim_free(qfp->qf_pattern);
2094 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002095 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002098 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 type = 0;
2100 qfp->qf_type = type;
2101 qfp->qf_valid = valid;
2102
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002103 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002104 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002106 qfl->qf_start = qfp;
2107 qfl->qf_ptr = qfp;
2108 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002109 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
2111 else
2112 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002113 qfp->qf_prev = *lastp;
2114 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002116 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002118 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002119 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002120 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002122 qfl->qf_index = qfl->qf_count;
2123 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 }
2125
Bram Moolenaar95946f12019-03-31 15:31:59 +02002126 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127}
2128
2129/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002130 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002131 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002132 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002133qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002134{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002135 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002136
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002137 qi = ALLOC_CLEAR_ONE(qf_info_T);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002138 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002139 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002140 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002141 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002142 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002143 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002144 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002145}
2146
2147/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002148 * Return the location list stack for window 'wp'.
2149 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002150 */
2151 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002152ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002153{
2154 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002155 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002156 return wp->w_llist_ref;
2157
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002158 // For a non-location list window, w_llist_ref should not point to a
2159 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002160 ll_free_all(&wp->w_llist_ref);
2161
2162 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002163 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002164 return wp->w_llist;
2165}
2166
2167/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002168 * Get the quickfix/location list stack to use for the specified Ex command.
2169 * For a location list command, returns the stack for the current window. If
2170 * the location list is not found, then returns NULL and prints an error
2171 * message if 'print_emsg' is TRUE.
2172 */
2173 static qf_info_T *
2174qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2175{
2176 qf_info_T *qi = &ql_info;
2177
2178 if (is_loclist_cmd(eap->cmdidx))
2179 {
2180 qi = GET_LOC_LIST(curwin);
2181 if (qi == NULL)
2182 {
2183 if (print_emsg)
2184 emsg(_(e_loclist));
2185 return NULL;
2186 }
2187 }
2188
2189 return qi;
2190}
2191
2192/*
2193 * Get the quickfix/location list stack to use for the specified Ex command.
2194 * For a location list command, returns the stack for the current window.
2195 * If the location list is not present, then allocates a new one.
2196 * Returns NULL if the allocation fails. For a location list command, sets
2197 * 'pwinp' to curwin.
2198 */
2199 static qf_info_T *
2200qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2201{
2202 qf_info_T *qi = &ql_info;
2203
2204 if (is_loclist_cmd(eap->cmdidx))
2205 {
2206 qi = ll_get_or_alloc_list(curwin);
2207 if (qi == NULL)
2208 return NULL;
2209 *pwinp = curwin;
2210 }
2211
2212 return qi;
2213}
2214
2215/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002216 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2217 */
2218 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002219copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002220{
2221 int i;
2222 qfline_T *from_qfp;
2223 qfline_T *prevp;
2224
2225 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002226 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002227 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002228 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002229 NULL,
2230 NULL,
2231 from_qfp->qf_module,
2232 0,
2233 from_qfp->qf_text,
2234 from_qfp->qf_lnum,
2235 from_qfp->qf_col,
2236 from_qfp->qf_viscol,
2237 from_qfp->qf_pattern,
2238 from_qfp->qf_nr,
2239 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002240 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002241 return FAIL;
2242
2243 // qf_add_entry() will not set the qf_num field, as the
2244 // directory and file names are not supplied. So the qf_fnum
2245 // field is copied here.
2246 prevp = to_qfl->qf_last;
2247 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2248 prevp->qf_type = from_qfp->qf_type; // error type
2249 if (from_qfl->qf_ptr == from_qfp)
2250 to_qfl->qf_ptr = prevp; // current location
2251 }
2252
2253 return OK;
2254}
2255
2256/*
2257 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2258 */
2259 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002260copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002261{
2262 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002263 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002264 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2265 to_qfl->qf_count = 0;
2266 to_qfl->qf_index = 0;
2267 to_qfl->qf_start = NULL;
2268 to_qfl->qf_last = NULL;
2269 to_qfl->qf_ptr = NULL;
2270 if (from_qfl->qf_title != NULL)
2271 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2272 else
2273 to_qfl->qf_title = NULL;
2274 if (from_qfl->qf_ctx != NULL)
2275 {
2276 to_qfl->qf_ctx = alloc_tv();
2277 if (to_qfl->qf_ctx != NULL)
2278 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2279 }
2280 else
2281 to_qfl->qf_ctx = NULL;
Bram Moolenaar858ba062020-05-31 23:11:59 +02002282 if (from_qfl->qf_qftf != NULL)
2283 to_qfl->qf_qftf = vim_strsave(from_qfl->qf_qftf);
2284 else
2285 to_qfl->qf_qftf = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002286
2287 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002288 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002289 return FAIL;
2290
2291 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2292
2293 // Assign a new ID for the location list
2294 to_qfl->qf_id = ++last_qf_id;
2295 to_qfl->qf_changedtick = 0L;
2296
2297 // When no valid entries are present in the list, qf_ptr points to
2298 // the first item in the list
2299 if (to_qfl->qf_nonevalid)
2300 {
2301 to_qfl->qf_ptr = to_qfl->qf_start;
2302 to_qfl->qf_index = 1;
2303 }
2304
2305 return OK;
2306}
2307
2308/*
2309 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002310 */
2311 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002312copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002313{
2314 qf_info_T *qi;
2315 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002316
Bram Moolenaar09037502018-09-25 22:08:14 +02002317 // When copying from a location list window, copy the referenced
2318 // location list. For other windows, copy the location list for
2319 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002320 if (IS_LL_WINDOW(from))
2321 qi = from->w_llist_ref;
2322 else
2323 qi = from->w_llist;
2324
Bram Moolenaar09037502018-09-25 22:08:14 +02002325 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002326 return;
2327
Bram Moolenaar09037502018-09-25 22:08:14 +02002328 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002329 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002330 return;
2331
2332 to->w_llist->qf_listcount = qi->qf_listcount;
2333
Bram Moolenaar09037502018-09-25 22:08:14 +02002334 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002335 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002336 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002337 to->w_llist->qf_curlist = idx;
2338
Bram Moolenaar0398e002019-03-21 21:12:49 +01002339 if (copy_loclist(qf_get_list(qi, idx),
2340 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002341 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002342 qf_free_all(to);
2343 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002344 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002345 }
2346
Bram Moolenaar09037502018-09-25 22:08:14 +02002347 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002348}
2349
2350/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002351 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002352 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 */
2354 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002355qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356{
Bram Moolenaar82404332016-07-10 17:00:38 +02002357 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002358 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002359 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002360
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002361 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
Bram Moolenaare60acc12011-05-10 16:41:25 +02002364#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002365 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002366#endif
2367#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002368 if (directory != NULL)
2369 slash_adjust(directory);
2370 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002371#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002372 if (directory != NULL && !vim_isAbsName(fname)
2373 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2374 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002375 // Here we check if the file really exists.
2376 // This should normally be true, but if make works without
2377 // "leaving directory"-messages we might have missed a
2378 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002379 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002382 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002383 if (directory)
2384 ptr = concat_fnames(directory, fname, TRUE);
2385 else
2386 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002388 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002389 bufname = ptr;
2390 }
2391 else
2392 bufname = fname;
2393
2394 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002395 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002396 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002397 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002398 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002400 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002401 {
2402 vim_free(qf_last_bufname);
2403 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2404 if (bufname == ptr)
2405 qf_last_bufname = bufname;
2406 else
2407 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002408 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002409 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002410 if (buf == NULL)
2411 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002412
Bram Moolenaarc1542742016-07-20 21:44:37 +02002413 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002414 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002415 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416}
2417
2418/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002419 * Push dirbuf onto the directory stack and return pointer to actual dir or
2420 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 */
2422 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002423qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424{
2425 struct dir_stack_T *ds_new;
2426 struct dir_stack_T *ds_ptr;
2427
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002428 // allocate new stack element and hook it in
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002429 ds_new = ALLOC_ONE(struct dir_stack_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 if (ds_new == NULL)
2431 return NULL;
2432
2433 ds_new->next = *stackptr;
2434 *stackptr = ds_new;
2435
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002436 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 if (vim_isAbsName(dirbuf)
2438 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002439 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 (*stackptr)->dirname = vim_strsave(dirbuf);
2441 else
2442 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002443 // Okay we don't have an absolute path.
2444 // dirbuf must be a subdir of one of the directories on the stack.
2445 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 ds_new = (*stackptr)->next;
2447 (*stackptr)->dirname = NULL;
2448 while (ds_new)
2449 {
2450 vim_free((*stackptr)->dirname);
2451 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2452 TRUE);
2453 if (mch_isdir((*stackptr)->dirname) == TRUE)
2454 break;
2455
2456 ds_new = ds_new->next;
2457 }
2458
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002459 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 while ((*stackptr)->next != ds_new)
2461 {
2462 ds_ptr = (*stackptr)->next;
2463 (*stackptr)->next = (*stackptr)->next->next;
2464 vim_free(ds_ptr->dirname);
2465 vim_free(ds_ptr);
2466 }
2467
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002468 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 if (ds_new == NULL)
2470 {
2471 vim_free((*stackptr)->dirname);
2472 (*stackptr)->dirname = vim_strsave(dirbuf);
2473 }
2474 }
2475
2476 if ((*stackptr)->dirname != NULL)
2477 return (*stackptr)->dirname;
2478 else
2479 {
2480 ds_ptr = *stackptr;
2481 *stackptr = (*stackptr)->next;
2482 vim_free(ds_ptr);
2483 return NULL;
2484 }
2485}
2486
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487/*
2488 * pop dirbuf from the directory stack and return previous directory or NULL if
2489 * stack is empty
2490 */
2491 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002492qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 struct dir_stack_T *ds_ptr;
2495
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002496 // TODO: Should we check if dirbuf is the directory on top of the stack?
2497 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002499 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 if (*stackptr != NULL)
2501 {
2502 ds_ptr = *stackptr;
2503 *stackptr = (*stackptr)->next;
2504 vim_free(ds_ptr->dirname);
2505 vim_free(ds_ptr);
2506 }
2507
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002508 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 return *stackptr ? (*stackptr)->dirname : NULL;
2510}
2511
2512/*
2513 * clean up directory stack
2514 */
2515 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002516qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517{
2518 struct dir_stack_T *ds_ptr;
2519
2520 while ((ds_ptr = *stackptr) != NULL)
2521 {
2522 *stackptr = (*stackptr)->next;
2523 vim_free(ds_ptr->dirname);
2524 vim_free(ds_ptr);
2525 }
2526}
2527
2528/*
2529 * Check in which directory of the directory stack the given file can be
2530 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002531 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 * Cleans up intermediate directory entries.
2533 *
2534 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002535 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 * ./
2537 * ./aa
2538 * ./aa/bb
2539 * ./bb
2540 * ./bb/x.c
2541 * and make says:
2542 * making all in aa
2543 * making all in bb
2544 * x.c:9: Error
2545 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2546 * qf_guess_filepath will return NULL.
2547 */
2548 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002549qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
2551 struct dir_stack_T *ds_ptr;
2552 struct dir_stack_T *ds_tmp;
2553 char_u *fullname;
2554
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002555 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002556 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 return NULL;
2558
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002559 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 fullname = NULL;
2561 while (ds_ptr)
2562 {
2563 vim_free(fullname);
2564 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2565
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002566 // If concat_fnames failed, just go on. The worst thing that can happen
2567 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2569 break;
2570
2571 ds_ptr = ds_ptr->next;
2572 }
2573
2574 vim_free(fullname);
2575
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002576 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002577 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002579 ds_tmp = qfl->qf_dir_stack->next;
2580 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 vim_free(ds_tmp->dirname);
2582 vim_free(ds_tmp);
2583 }
2584
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002585 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586}
2587
2588/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002589 * Returns TRUE if a quickfix/location list with the given identifier exists.
2590 */
2591 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002592qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002593{
2594 qf_info_T *qi = &ql_info;
2595 int i;
2596
2597 if (wp != NULL)
2598 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002599 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002600 if (qi == NULL)
2601 return FALSE;
2602 }
2603
2604 for (i = 0; i < qi->qf_listcount; ++i)
2605 if (qi->qf_lists[i].qf_id == qf_id)
2606 return TRUE;
2607
2608 return FALSE;
2609}
2610
2611/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002612 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002613 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002614 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002615 * Similar to location list.
2616 */
2617 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002618is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002619{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002620 qfline_T *qfp;
2621 int i;
2622
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002623 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002624 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2625 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002626 break;
2627
Bram Moolenaar95946f12019-03-31 15:31:59 +02002628 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002629 return FALSE;
2630
2631 return TRUE;
2632}
2633
2634/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002635 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002636 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002637 */
2638 static qfline_T *
2639get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002640 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002641 qfline_T *qf_ptr,
2642 int *qf_index,
2643 int dir)
2644{
2645 int idx;
2646 int old_qf_fnum;
2647
2648 idx = *qf_index;
2649 old_qf_fnum = qf_ptr->qf_fnum;
2650
2651 do
2652 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002653 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002654 return NULL;
2655 ++idx;
2656 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002657 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002658 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2659
2660 *qf_index = idx;
2661 return qf_ptr;
2662}
2663
2664/*
2665 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002666 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002667 */
2668 static qfline_T *
2669get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002670 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002671 qfline_T *qf_ptr,
2672 int *qf_index,
2673 int dir)
2674{
2675 int idx;
2676 int old_qf_fnum;
2677
2678 idx = *qf_index;
2679 old_qf_fnum = qf_ptr->qf_fnum;
2680
2681 do
2682 {
2683 if (idx == 1 || qf_ptr->qf_prev == NULL)
2684 return NULL;
2685 --idx;
2686 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002687 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002688 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2689
2690 *qf_index = idx;
2691 return qf_ptr;
2692}
2693
2694/*
2695 * Get the n'th (errornr) previous/next valid entry from the current entry in
2696 * the quickfix list.
2697 * dir == FORWARD or FORWARD_FILE: next valid entry
2698 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2699 */
2700 static qfline_T *
2701get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002702 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002703 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002704 int dir,
2705 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002706{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002707 qfline_T *qf_ptr = qfl->qf_ptr;
2708 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002709 qfline_T *prev_qf_ptr;
2710 int prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002711 char_u *err = e_no_more_items;
2712
2713 while (errornr--)
2714 {
2715 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002716 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002717
2718 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002719 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002720 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002721 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002722 if (qf_ptr == NULL)
2723 {
2724 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002725 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002726 if (err != NULL)
2727 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002728 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002729 return NULL;
2730 }
2731 break;
2732 }
2733
2734 err = NULL;
2735 }
2736
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002737 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002738 return qf_ptr;
2739}
2740
2741/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002742 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2743 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002744 */
2745 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002746get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002747{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002748 qfline_T *qf_ptr = qfl->qf_ptr;
2749 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002750
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002751 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002752 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2753 {
2754 --qf_idx;
2755 qf_ptr = qf_ptr->qf_prev;
2756 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002757 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002758 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2759 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002760 {
2761 ++qf_idx;
2762 qf_ptr = qf_ptr->qf_next;
2763 }
2764
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002765 *new_qfidx = qf_idx;
2766 return qf_ptr;
2767}
2768
2769/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002770 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002771 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2772 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2773 * Returns a pointer to the entry and the index of the new entry is stored in
2774 * 'new_qfidx'.
2775 */
2776 static qfline_T *
2777qf_get_entry(
2778 qf_list_T *qfl,
2779 int errornr,
2780 int dir,
2781 int *new_qfidx)
2782{
2783 qfline_T *qf_ptr = qfl->qf_ptr;
2784 int qfidx = qfl->qf_index;
2785
2786 if (dir != 0) // next/prev valid entry
2787 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2788 else if (errornr != 0) // go to specified number
2789 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2790
2791 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002792 return qf_ptr;
2793}
2794
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002795/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002796 * Find a window displaying a Vim help file.
2797 */
2798 static win_T *
2799qf_find_help_win(void)
2800{
2801 win_T *wp;
2802
2803 FOR_ALL_WINDOWS(wp)
2804 if (bt_help(wp->w_buffer))
2805 return wp;
2806
2807 return NULL;
2808}
2809
2810/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002811 * Set the location list for the specified window to 'qi'.
2812 */
2813 static void
2814win_set_loclist(win_T *wp, qf_info_T *qi)
2815{
2816 wp->w_llist = qi;
2817 qi->qf_refcount++;
2818}
2819
2820/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002821 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2822 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002823 */
2824 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002825jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002826{
2827 win_T *wp;
2828 int flags;
2829
Bram Moolenaarb2443732018-11-11 22:50:27 +01002830 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002831 wp = NULL;
2832 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002833 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002834 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2835 win_enter(wp, TRUE);
2836 else
2837 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002838 // Split off help window; put it at far top if no position
2839 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002840 flags = WSP_HELP;
2841 if (cmdmod.split == 0 && curwin->w_width != Columns
2842 && curwin->w_width < 80)
2843 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002844 // If the user asks to open a new window, then copy the location list.
2845 // Otherwise, don't copy the location list.
2846 if (IS_LL_STACK(qi) && !newwin)
2847 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002848
2849 if (win_split(0, flags) == FAIL)
2850 return FAIL;
2851
2852 *opened_window = TRUE;
2853
2854 if (curwin->w_height < p_hh)
2855 win_setheight((int)p_hh);
2856
Bram Moolenaarb2443732018-11-11 22:50:27 +01002857 // When using location list, the new window should use the supplied
2858 // location list. If the user asks to open a new window, then the new
2859 // window will get a copy of the location list.
2860 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002861 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002862 }
2863
2864 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002865 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002866
2867 return OK;
2868}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002869
2870/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002871 * Find a non-quickfix window in the current tabpage using the given location
2872 * list stack.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002873 * Returns NULL if a matching window is not found.
2874 */
2875 static win_T *
2876qf_find_win_with_loclist(qf_info_T *ll)
2877{
2878 win_T *wp;
2879
2880 FOR_ALL_WINDOWS(wp)
2881 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2882 return wp;
2883
2884 return NULL;
2885}
2886
2887/*
2888 * Find a window containing a normal buffer
2889 */
2890 static win_T *
2891qf_find_win_with_normal_buf(void)
2892{
2893 win_T *wp;
2894
2895 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002896 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002897 return wp;
2898
2899 return NULL;
2900}
2901
2902/*
2903 * Go to a window in any tabpage containing the specified file. Returns TRUE
2904 * if successfully jumped to the window. Otherwise returns FALSE.
2905 */
2906 static int
2907qf_goto_tabwin_with_file(int fnum)
2908{
2909 tabpage_T *tp;
2910 win_T *wp;
2911
2912 FOR_ALL_TAB_WINDOWS(tp, wp)
2913 if (wp->w_buffer->b_fnum == fnum)
2914 {
2915 goto_tabpage_win(tp, wp);
2916 return TRUE;
2917 }
2918
2919 return FALSE;
2920}
2921
2922/*
2923 * Create a new window to show a file above the quickfix window. Called when
2924 * only the quickfix window is present.
2925 */
2926 static int
2927qf_open_new_file_win(qf_info_T *ll_ref)
2928{
2929 int flags;
2930
2931 flags = WSP_ABOVE;
2932 if (ll_ref != NULL)
2933 flags |= WSP_NEWLOC;
2934 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002935 return FAIL; // not enough room for window
2936 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002937 swb_flags = 0;
2938 RESET_BINDING(curwin);
2939 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002940 // The new window should use the location list from the
2941 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002942 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002943 return OK;
2944}
2945
2946/*
2947 * Go to a window that shows the right buffer. If the window is not found, go
2948 * to the window just above the location list window. This is used for opening
2949 * a file from a location window and not from a quickfix window. If some usable
2950 * window is previously found, then it is supplied in 'use_win'.
2951 */
2952 static void
2953qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2954{
2955 win_T *win = use_win;
2956
2957 if (win == NULL)
2958 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002959 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002960 FOR_ALL_WINDOWS(win)
2961 if (win->w_buffer->b_fnum == qf_fnum)
2962 break;
2963 if (win == NULL)
2964 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002965 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002966 win = curwin;
2967 do
2968 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002969 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002970 break;
2971 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002972 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002973 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002974 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002975 } while (win != curwin);
2976 }
2977 }
2978 win_goto(win);
2979
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002980 // If the location list for the window is not set, then set it
2981 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01002982 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002983 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002984}
2985
2986/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002987 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2988 * not found, then go to the window just above the quickfix window. This is
2989 * used for opening a file from a quickfix window and not from a location
2990 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002991 */
2992 static void
2993qf_goto_win_with_qfl_file(int qf_fnum)
2994{
2995 win_T *win;
2996 win_T *altwin;
2997
2998 win = curwin;
2999 altwin = NULL;
3000 for (;;)
3001 {
3002 if (win->w_buffer->b_fnum == qf_fnum)
3003 break;
3004 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003005 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003006 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003007 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003008
3009 if (IS_QF_WINDOW(win))
3010 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003011 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003012 // window, unless 'switchbuf' contains 'uselast': in this case we
3013 // try to jump to the previously used window first.
3014 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3015 win = prevwin;
3016 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003017 win = altwin;
3018 else if (curwin->w_prev != NULL)
3019 win = curwin->w_prev;
3020 else
3021 win = curwin->w_next;
3022 break;
3023 }
3024
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003025 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003026 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003027 altwin = win;
3028 }
3029
3030 win_goto(win);
3031}
3032
3033/*
3034 * Find a suitable window for opening a file (qf_fnum) from the
3035 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003036 * window, jump to it. Otherwise open a new window to display the file. If
3037 * 'newwin' is TRUE, then always open a new window. This is called from either
3038 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003039 */
3040 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003041qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003042{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003043 win_T *usable_wp = NULL;
3044 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003045 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003046
Bram Moolenaarb2443732018-11-11 22:50:27 +01003047 // If opening a new window, then don't use the location list referred by
3048 // the current window. Otherwise two windows will refer to the same
3049 // location list.
3050 if (!newwin)
3051 ll_ref = curwin->w_llist_ref;
3052
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003053 if (ll_ref != NULL)
3054 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003055 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003056 usable_wp = qf_find_win_with_loclist(ll_ref);
3057 if (usable_wp != NULL)
3058 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003059 }
3060
3061 if (!usable_win)
3062 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003063 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003064 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003065 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003066 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003067 }
3068
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003069 // If no usable window is found and 'switchbuf' contains "usetab"
3070 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003071 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003072 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003073
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003074 // If there is only one window and it is the quickfix window, create a
3075 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003076 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003077 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003078 if (qf_open_new_file_win(ll_ref) != OK)
3079 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003080 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003081 }
3082 else
3083 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003084 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003085 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003086 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003087 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003088 }
3089
3090 return OK;
3091}
3092
3093/*
3094 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003095 * Returns OK if successfully edited the file, FAIL on failing to open the
3096 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3097 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003098 */
3099 static int
3100qf_jump_edit_buffer(
3101 qf_info_T *qi,
3102 qfline_T *qf_ptr,
3103 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003104 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003105 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003106{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003107 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003108 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003109 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003110 int old_qf_curlist = qi->qf_curlist;
3111 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003112
3113 if (qf_ptr->qf_type == 1)
3114 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003115 // Open help file (do_ecmd() will set b_help flag, readfile() will
3116 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003117 if (!can_abandon(curbuf, forceit))
3118 {
3119 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003120 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003121 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003122
3123 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3124 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003125 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003126 }
3127 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003128 retval = buflist_getfile(qf_ptr->qf_fnum,
3129 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003130
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003131 // If a location list, check whether the associated window is still
3132 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003133 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003134 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003135 win_T *wp = win_id2wp(prev_winid);
3136 if (wp == NULL && curwin->w_llist != qi)
3137 {
3138 emsg(_("E924: Current window was closed"));
3139 *opened_window = FALSE;
3140 return NOTDONE;
3141 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003142 }
3143
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003144 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003146 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003147 return NOTDONE;
3148 }
3149
3150 if (old_qf_curlist != qi->qf_curlist
3151 || !is_qf_entry_present(qfl, qf_ptr))
3152 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003153 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003154 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003155 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003156 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003157 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003158 }
3159
3160 return retval;
3161}
3162
3163/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003164 * Go to the error line in the current file using either line/column number or
3165 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003166 */
3167 static void
3168qf_jump_goto_line(
3169 linenr_T qf_lnum,
3170 int qf_col,
3171 char_u qf_viscol,
3172 char_u *qf_pattern)
3173{
3174 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003175
3176 if (qf_pattern == NULL)
3177 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003178 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003179 i = qf_lnum;
3180 if (i > 0)
3181 {
3182 if (i > curbuf->b_ml.ml_line_count)
3183 i = curbuf->b_ml.ml_line_count;
3184 curwin->w_cursor.lnum = i;
3185 }
3186 if (qf_col > 0)
3187 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003188 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003189 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003190 coladvance(qf_col - 1);
3191 else
3192 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003193 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003194 check_cursor();
3195 }
3196 else
3197 beginline(BL_WHITE | BL_FIX);
3198 }
3199 else
3200 {
3201 pos_T save_cursor;
3202
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003203 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003204 save_cursor = curwin->w_cursor;
3205 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003206 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003207 curwin->w_cursor = save_cursor;
3208 }
3209}
3210
3211/*
3212 * Display quickfix list index and size message
3213 */
3214 static void
3215qf_jump_print_msg(
3216 qf_info_T *qi,
3217 int qf_index,
3218 qfline_T *qf_ptr,
3219 buf_T *old_curbuf,
3220 linenr_T old_lnum)
3221{
3222 linenr_T i;
3223 int len;
3224
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003225 // Update the screen before showing the message, unless the screen
3226 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003227 if (!msg_scrolled)
3228 update_topline_redraw();
3229 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003230 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003231 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3232 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003233 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003234 len = (int)STRLEN(IObuff);
3235 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3236
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003237 // Output the message. Overwrite to avoid scrolling when the 'O'
3238 // flag is present in 'shortmess'; But when not jumping, print the
3239 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003240 i = msg_scroll;
3241 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3242 msg_scroll = TRUE;
3243 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3244 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003245 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003246 msg_scroll = i;
3247}
3248
3249/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003250 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003251 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3252 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003253 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3254 * able to jump/open a window. Returns NOTDONE if a file is not associated
3255 * with the entry.
3256 */
3257 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003258qf_jump_open_window(
3259 qf_info_T *qi,
3260 qfline_T *qf_ptr,
3261 int newwin,
3262 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003263{
3264 // For ":helpgrep" find a help window or open one.
3265 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003266 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003267 return FAIL;
3268
3269 // If currently in the quickfix window, find another window to show the
3270 // file in.
3271 if (bt_quickfix(curbuf) && !*opened_window)
3272 {
3273 // If there is no file specified, we don't know where to go.
3274 // But do advance, otherwise ":cn" gets stuck.
3275 if (qf_ptr->qf_fnum == 0)
3276 return NOTDONE;
3277
Bram Moolenaarb2443732018-11-11 22:50:27 +01003278 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3279 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003280 return FAIL;
3281 }
3282
3283 return OK;
3284}
3285
3286/*
3287 * Edit a selected file from the quickfix/location list and jump to a
3288 * particular line/column, adjust the folds and display a message about the
3289 * jump.
3290 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3291 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3292 * the file.
3293 */
3294 static int
3295qf_jump_to_buffer(
3296 qf_info_T *qi,
3297 int qf_index,
3298 qfline_T *qf_ptr,
3299 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003300 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003301 int *opened_window,
3302 int openfold,
3303 int print_message)
3304{
3305 buf_T *old_curbuf;
3306 linenr_T old_lnum;
3307 int retval = OK;
3308
3309 // If there is a file name, read the wanted file if needed, and check
3310 // autowrite etc.
3311 old_curbuf = curbuf;
3312 old_lnum = curwin->w_cursor.lnum;
3313
3314 if (qf_ptr->qf_fnum != 0)
3315 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003316 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003317 opened_window);
3318 if (retval != OK)
3319 return retval;
3320 }
3321
3322 // When not switched to another buffer, still need to set pc mark
3323 if (curbuf == old_curbuf)
3324 setpcmark();
3325
3326 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3327 qf_ptr->qf_pattern);
3328
3329#ifdef FEAT_FOLDING
3330 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3331 foldOpenCursor();
3332#endif
3333 if (print_message)
3334 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3335
3336 return retval;
3337}
3338
3339/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003340 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 */
3342 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003343qf_jump(qf_info_T *qi,
3344 int dir,
3345 int errornr,
3346 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003348 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3349}
3350
3351/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003352 * Jump to a quickfix line.
3353 * If dir == 0 go to entry "errornr".
3354 * If dir == FORWARD go "errornr" valid entries forward.
3355 * If dir == BACKWARD go "errornr" valid entries backward.
3356 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3357 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3358 * else if "errornr" is zero, redisplay the same line
3359 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003360 * If 'newwin' is TRUE, then open the file in a new window.
3361 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003362 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003363qf_jump_newwin(qf_info_T *qi,
3364 int dir,
3365 int errornr,
3366 int forceit,
3367 int newwin)
3368{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003369 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003370 qfline_T *qf_ptr;
3371 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003374 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003375 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003376 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003379 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003380 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003382 if (qi == NULL)
3383 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003384
Bram Moolenaar0398e002019-03-21 21:12:49 +01003385 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003387 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 return;
3389 }
3390
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003391 incr_quickfix_busy();
3392
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003393 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003394
3395 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003397 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003399
3400 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3401 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003403 qf_ptr = old_qf_ptr;
3404 qf_index = old_qf_index;
3405 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003408 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003409 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003410 // No need to print the error message if it's visible in the error
3411 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 print_message = FALSE;
3413
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003414 prev_winid = curwin->w_id;
3415
Bram Moolenaarb2443732018-11-11 22:50:27 +01003416 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003417 if (retval == FAIL)
3418 goto failed;
3419 if (retval == NOTDONE)
3420 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003421
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003422 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003423 &opened_window, old_KeyTyped, print_message);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003424 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003426 // Quickfix/location list is freed by an autocmd
3427 qi = NULL;
3428 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003431 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003434 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003435 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003437 // Couldn't open file, so put index back where it was. This could
3438 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 qf_ptr = old_qf_ptr;
3441 qf_index = old_qf_index;
3442 }
3443 }
3444theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003445 if (qi != NULL)
3446 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003447 qfl->qf_ptr = qf_ptr;
3448 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003449 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003450 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003452 // Restore old 'switchbuf' value, but not when an autocommand or
3453 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003454 p_swb = old_swb;
3455 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003457 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458}
3459
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003460// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003461static int qfFileAttr;
3462static int qfSepAttr;
3463static int qfLineAttr;
3464
3465/*
3466 * Display information about a single entry from the quickfix/location list.
3467 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003468 * 'cursel' will be set to TRUE for the currently selected entry in the
3469 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003470 */
3471 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003472qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003473{
3474 char_u *fname;
3475 buf_T *buf;
3476 int filter_entry;
3477
3478 fname = NULL;
3479 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3480 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3481 (char *)qfp->qf_module);
3482 else {
3483 if (qfp->qf_fnum != 0
3484 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3485 {
3486 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003487 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003488 fname = gettail(fname);
3489 }
3490 if (fname == NULL)
3491 sprintf((char *)IObuff, "%2d", qf_idx);
3492 else
3493 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3494 qf_idx, (char *)fname);
3495 }
3496
3497 // Support for filtering entries using :filter /pat/ clist
3498 // Match against the module name, file name, search pattern and
3499 // text of the entry.
3500 filter_entry = TRUE;
3501 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3502 filter_entry &= message_filtered(qfp->qf_module);
3503 if (filter_entry && fname != NULL)
3504 filter_entry &= message_filtered(fname);
3505 if (filter_entry && qfp->qf_pattern != NULL)
3506 filter_entry &= message_filtered(qfp->qf_pattern);
3507 if (filter_entry)
3508 filter_entry &= message_filtered(qfp->qf_text);
3509 if (filter_entry)
3510 return;
3511
3512 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003513 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003514
3515 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003516 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003517 if (qfp->qf_lnum == 0)
3518 IObuff[0] = NUL;
3519 else if (qfp->qf_col == 0)
3520 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3521 else
3522 sprintf((char *)IObuff, "%ld col %d",
3523 qfp->qf_lnum, qfp->qf_col);
3524 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3525 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003526 msg_puts_attr((char *)IObuff, qfLineAttr);
3527 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003528 if (qfp->qf_pattern != NULL)
3529 {
3530 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003531 msg_puts((char *)IObuff);
3532 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003533 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003534 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003535
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003536 // Remove newlines and leading whitespace from the text. For an
3537 // unrecognized line keep the indent, the compiler may mark a word
3538 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003539 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3540 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3541 IObuff, IOSIZE);
3542 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003543 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003544}
3545
3546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003548 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 */
3550 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003551qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003553 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003554 qfline_T *qfp;
3555 int i;
3556 int idx1 = 1;
3557 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003558 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003559 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003560 int all = eap->forceit; // if not :cl!, only show
3561 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003562 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003564 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3565 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003566
Bram Moolenaar0398e002019-03-21 21:12:49 +01003567 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003569 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 return;
3571 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003572 if (*arg == '+')
3573 {
3574 ++arg;
3575 plus = TRUE;
3576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003579 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 return;
3581 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003582 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003583 if (plus)
3584 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003585 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003586 idx2 = i + idx1;
3587 idx1 = i;
3588 }
3589 else
3590 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003591 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003592 if (idx1 < 0)
3593 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3594 if (idx2 < 0)
3595 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003598 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003599 shorten_fnames(FALSE);
3600
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003601 // Get the attributes for the different quickfix highlight items. Note
3602 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003603 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3604 if (qfFileAttr == 0)
3605 qfFileAttr = HL_ATTR(HLF_D);
3606 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3607 if (qfSepAttr == 0)
3608 qfSepAttr = HL_ATTR(HLF_D);
3609 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3610 if (qfLineAttr == 0)
3611 qfLineAttr = HL_ATTR(HLF_N);
3612
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003613 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003615 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
3617 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003618 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003619
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 ui_breakcheck();
3621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622}
3623
3624/*
3625 * Remove newlines and leading whitespace from an error message.
3626 * Put the result in "buf[bufsize]".
3627 */
3628 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003629qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
3631 int i;
3632 char_u *p = text;
3633
3634 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3635 {
3636 if (*p == '\n')
3637 {
3638 buf[i] = ' ';
3639 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003640 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 break;
3642 }
3643 else
3644 buf[i] = *p++;
3645 }
3646 buf[i] = NUL;
3647}
3648
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003649/*
3650 * Display information (list number, list size and the title) about a
3651 * quickfix/location list.
3652 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003653 static void
3654qf_msg(qf_info_T *qi, int which, char *lead)
3655{
3656 char *title = (char *)qi->qf_lists[which].qf_title;
3657 int count = qi->qf_lists[which].qf_count;
3658 char_u buf[IOSIZE];
3659
3660 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3661 lead,
3662 which + 1,
3663 qi->qf_listcount,
3664 count);
3665
3666 if (title != NULL)
3667 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003668 size_t len = STRLEN(buf);
3669
3670 if (len < 34)
3671 {
3672 vim_memset(buf + len, ' ', 34 - len);
3673 buf[34] = NUL;
3674 }
3675 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003676 }
3677 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003678 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003679}
3680
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681/*
3682 * ":colder [count]": Up in the quickfix stack.
3683 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003684 * ":lolder [count]": Up in the location list stack.
3685 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 */
3687 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003688qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003690 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 int count;
3692
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003693 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3694 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003695
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 if (eap->addr_count != 0)
3697 count = eap->line2;
3698 else
3699 count = 1;
3700 while (count--)
3701 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003702 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003704 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003706 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003707 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003709 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711 else
3712 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003713 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003715 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003716 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003718 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003721 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003722 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723}
3724
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003725/*
3726 * Display the information about all the quickfix/location lists in the stack
3727 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003728 void
3729qf_history(exarg_T *eap)
3730{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003731 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003732 int i;
3733
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003734 if (eap->addr_count > 0)
3735 {
3736 if (qi == NULL)
3737 {
3738 emsg(_(e_loclist));
3739 return;
3740 }
3741
3742 // Jump to the specified quickfix list
3743 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3744 {
3745 qi->qf_curlist = eap->line2 - 1;
3746 qf_msg(qi, qi->qf_curlist, "");
3747 qf_update_buffer(qi, NULL);
3748 }
3749 else
3750 emsg(_(e_invrange));
3751
3752 return;
3753 }
3754
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003755 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003756 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003757 else
3758 for (i = 0; i < qi->qf_listcount; ++i)
3759 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3760}
3761
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003763 * Free all the entries in the error list "idx". Note that other information
3764 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 */
3766 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003767qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003769 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003770 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003771 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003773 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003775 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003776 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003777 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003778 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003779 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003780 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003781 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003782 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003783 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003784 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003785 // Somehow qf_count may have an incorrect value, set it to 1
3786 // to avoid crashing when it's wrong.
3787 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003788 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003789 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003790 qfl->qf_start = qfpnext;
3791 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003793
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003794 qfl->qf_index = 0;
3795 qfl->qf_start = NULL;
3796 qfl->qf_last = NULL;
3797 qfl->qf_ptr = NULL;
3798 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003799
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003800 qf_clean_dir_stack(&qfl->qf_dir_stack);
3801 qfl->qf_directory = NULL;
3802 qf_clean_dir_stack(&qfl->qf_file_stack);
3803 qfl->qf_currfile = NULL;
3804 qfl->qf_multiline = FALSE;
3805 qfl->qf_multiignore = FALSE;
3806 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807}
3808
3809/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003810 * Free error list "idx". Frees all the entries in the quickfix list,
3811 * associated context information and the title.
3812 */
3813 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003814qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003815{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003816 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003817
Bram Moolenaard23a8232018-02-10 18:45:26 +01003818 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003819 free_tv(qfl->qf_ctx);
3820 qfl->qf_ctx = NULL;
Bram Moolenaar858ba062020-05-31 23:11:59 +02003821 VIM_CLEAR(qfl->qf_qftf);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003822 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003823 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003824}
3825
3826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 * qf_mark_adjust: adjust marks
3828 */
3829 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003830qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003831 win_T *wp,
3832 linenr_T line1,
3833 linenr_T line2,
3834 long amount,
3835 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003837 int i;
3838 qfline_T *qfp;
3839 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003840 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003841 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003842 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
Bram Moolenaarc1542742016-07-20 21:44:37 +02003844 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003845 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003846 if (wp != NULL)
3847 {
3848 if (wp->w_llist == NULL)
3849 return;
3850 qi = wp->w_llist;
3851 }
3852
3853 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003854 {
3855 qf_list_T *qfl = qf_get_list(qi, idx);
3856
3857 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003858 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (qfp->qf_fnum == curbuf->b_fnum)
3860 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003861 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3863 {
3864 if (amount == MAXLNUM)
3865 qfp->qf_cleared = TRUE;
3866 else
3867 qfp->qf_lnum += amount;
3868 }
3869 else if (amount_after && qfp->qf_lnum > line2)
3870 qfp->qf_lnum += amount_after;
3871 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003872 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003873
3874 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003875 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876}
3877
3878/*
3879 * Make a nice message out of the error character and the error number:
3880 * char number message
3881 * e or E 0 " error"
3882 * w or W 0 " warning"
3883 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02003884 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 * 0 0 ""
3886 * other 0 " c"
3887 * e or E n " error n"
3888 * w or W n " warning n"
3889 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02003890 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 * 0 n " error n"
3892 * other n " c n"
3893 * 1 x "" :helpgrep
3894 */
3895 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003896qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
3898 static char_u buf[20];
3899 static char_u cc[3];
3900 char_u *p;
3901
3902 if (c == 'W' || c == 'w')
3903 p = (char_u *)" warning";
3904 else if (c == 'I' || c == 'i')
3905 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02003906 else if (c == 'N' || c == 'n')
3907 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3909 p = (char_u *)" error";
3910 else if (c == 0 || c == 1)
3911 p = (char_u *)"";
3912 else
3913 {
3914 cc[0] = ' ';
3915 cc[1] = c;
3916 cc[2] = NUL;
3917 p = cc;
3918 }
3919
3920 if (nr <= 0)
3921 return p;
3922
3923 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3924 return buf;
3925}
3926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003928 * When "split" is FALSE: Open the entry/result under the cursor.
3929 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3930 */
3931 void
3932qf_view_result(int split)
3933{
3934 qf_info_T *qi = &ql_info;
3935
3936 if (!bt_quickfix(curbuf))
3937 return;
3938
3939 if (IS_LL_WINDOW(curwin))
3940 qi = GET_LOC_LIST(curwin);
3941
Bram Moolenaar0398e002019-03-21 21:12:49 +01003942 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003943 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003944 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003945 return;
3946 }
3947
3948 if (split)
3949 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003950 // Open the selected entry in a new window
3951 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3952 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003953 return;
3954 }
3955
3956 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3957}
3958
3959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 * ":cwindow": open the quickfix window if we have errors to display,
3961 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003962 * ":lwindow": open the location list window if we have locations to display,
3963 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 */
3965 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003966ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003968 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003969 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 win_T *win;
3971
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003972 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3973 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003974
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003975 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003976
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003977 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003978 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003980 // If a quickfix window is open but we have no errors to display,
3981 // close the window. If a quickfix window is not open, then open
3982 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003983 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003984 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01003985 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
3987 if (win != NULL)
3988 ex_cclose(eap);
3989 }
3990 else if (win == NULL)
3991 ex_copen(eap);
3992}
3993
3994/*
3995 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003996 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003999ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004001 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004002 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004004 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4005 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004007 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004008 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 if (win != NULL)
4010 win_close(win, FALSE);
4011}
4012
4013/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004014 * Set "w:quickfix_title" if "qi" has a title.
4015 */
4016 static void
4017qf_set_title_var(qf_list_T *qfl)
4018{
4019 if (qfl->qf_title != NULL)
4020 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4021}
4022
4023/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004024 * Goto a quickfix or location list window (if present).
4025 * Returns OK if the window is found, FAIL otherwise.
4026 */
4027 static int
4028qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4029{
4030 win_T *win;
4031
4032 win = qf_find_win(qi);
4033 if (win == NULL)
4034 return FAIL;
4035
4036 win_goto(win);
4037 if (resize)
4038 {
4039 if (vertsplit)
4040 {
4041 if (sz != win->w_width)
4042 win_setwidth(sz);
4043 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004044 else if (sz != win->w_height && win->w_height
4045 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004046 win_setheight(sz);
4047 }
4048
4049 return OK;
4050}
4051
4052/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004053 * Set options for the buffer in the quickfix or location list window.
4054 */
4055 static void
4056qf_set_cwindow_options(void)
4057{
4058 // switch off 'swapfile'
4059 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4060 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4061 OPT_LOCAL);
4062 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4063 RESET_BINDING(curwin);
4064#ifdef FEAT_DIFF
4065 curwin->w_p_diff = FALSE;
4066#endif
4067#ifdef FEAT_FOLDING
4068 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4069 OPT_LOCAL);
4070#endif
4071}
4072
4073/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004074 * Open a new quickfix or location list window, load the quickfix buffer and
4075 * set the appropriate options for the window.
4076 * Returns FAIL if the window could not be opened.
4077 */
4078 static int
4079qf_open_new_cwindow(qf_info_T *qi, int height)
4080{
4081 buf_T *qf_buf;
4082 win_T *oldwin = curwin;
4083 tabpage_T *prevtab = curtab;
4084 int flags = 0;
4085 win_T *win;
4086
4087 qf_buf = qf_find_buf(qi);
4088
4089 // The current window becomes the previous window afterwards.
4090 win = curwin;
4091
4092 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4093 // Create the new quickfix window at the very bottom, except when
4094 // :belowright or :aboveleft is used.
4095 win_goto(lastwin);
4096 // Default is to open the window below the current window
4097 if (cmdmod.split == 0)
4098 flags = WSP_BELOW;
4099 flags |= WSP_NEWLOC;
4100 if (win_split(height, flags) == FAIL)
4101 return FAIL; // not enough room for window
4102 RESET_BINDING(curwin);
4103
4104 if (IS_LL_STACK(qi))
4105 {
4106 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004107 // location list stack from the window 'win'.
4108 curwin->w_llist_ref = qi;
4109 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004110 }
4111
4112 if (oldwin != curwin)
4113 oldwin = NULL; // don't store info when in another window
4114 if (qf_buf != NULL)
4115 {
4116 // Use the existing quickfix buffer
4117 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4118 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4119 }
4120 else
4121 {
4122 // Create a new quickfix buffer
4123 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4124
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004125 // save the number of the new buffer
4126 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004127 }
4128
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004129 // Set the options for the quickfix buffer/window (if not already done)
4130 // Do this even if the quickfix buffer was already present, as an autocmd
4131 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004132 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004133 qf_set_cwindow_options();
4134
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004135 // Only set the height when still in the same tab page and there is no
4136 // window to the side.
4137 if (curtab == prevtab && curwin->w_width == Columns)
4138 win_setheight(height);
4139 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4140 if (win_valid(win))
4141 prevwin = win;
4142
4143 return OK;
4144}
4145
4146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004148 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 */
4150 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004151ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004153 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004154 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004156 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004157 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004159 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4160 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004161
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004162 incr_quickfix_busy();
4163
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 if (eap->addr_count != 0)
4165 height = eap->line2;
4166 else
4167 height = QF_WINHEIGHT;
4168
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004169 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170#ifdef FEAT_GUI
4171 need_mouse_correct = TRUE;
4172#endif
4173
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004174 // Find an existing quickfix window, or open a new one.
4175 if (cmdmod.tab == 0)
4176 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4177 cmdmod.split & WSP_VERT);
4178 if (status == FAIL)
4179 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004180 {
4181 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004182 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004185 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004186 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004187 // Save the current index here, as updating the quickfix buffer may free
4188 // the quickfix list
4189 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004190
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004191 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004192 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004194 decr_quickfix_busy();
4195
4196 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 curwin->w_cursor.col = 0;
4198 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004199 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200}
4201
4202/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004203 * Move the cursor in the quickfix window to "lnum".
4204 */
4205 static void
4206qf_win_goto(win_T *win, linenr_T lnum)
4207{
4208 win_T *old_curwin = curwin;
4209
4210 curwin = win;
4211 curbuf = win->w_buffer;
4212 curwin->w_cursor.lnum = lnum;
4213 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004214 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004215 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004216 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004217 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004218 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004219 curwin = old_curwin;
4220 curbuf = curwin->w_buffer;
4221}
4222
4223/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004224 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004225 */
4226 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004227ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004228{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004229 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004230 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004231
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004232 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4233 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004234
4235 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004236 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4237 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4238}
4239
4240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * Return the number of the current entry (line number in the quickfix
4242 * window).
4243 */
4244 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004245qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004247 qf_info_T *qi = &ql_info;
4248
4249 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004250 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004251 qi = wp->w_llist_ref;
4252
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004253 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254}
4255
4256/*
4257 * Update the cursor position in the quickfix window to the current error.
4258 * Return TRUE if there is a quickfix window.
4259 */
4260 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004261qf_win_pos_update(
4262 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004263 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264{
4265 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004266 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004268 // Put the cursor on the current error in the quickfix window, so that
4269 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004270 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (win != NULL
4272 && qf_index <= win->w_buffer->b_ml.ml_line_count
4273 && old_qf_index != qf_index)
4274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 if (qf_index > old_qf_index)
4276 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004277 win->w_redraw_top = old_qf_index;
4278 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 else
4281 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004282 win->w_redraw_top = qf_index;
4283 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004285 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 return win != NULL;
4288}
4289
4290/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004291 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004292 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004293 */
4294 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004295is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004296{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004297 // A window displaying the quickfix buffer will have the w_llist_ref field
4298 // set to NULL.
4299 // A window displaying a location list buffer will have the w_llist_ref
4300 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004301 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004302 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4303 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004304 return TRUE;
4305
4306 return FALSE;
4307}
4308
4309/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004310 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004311 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004312 */
4313 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004314qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004315{
4316 win_T *win;
4317
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004318 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004319 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004320 return win;
4321 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004322}
4323
4324/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004325 * Find a quickfix buffer.
4326 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 */
4328 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004329qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004331 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004332 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004334 if (qi->qf_bufnr != INVALID_QFBUFNR)
4335 {
4336 buf_T *qfbuf;
4337 qfbuf = buflist_findnr(qi->qf_bufnr);
4338 if (qfbuf != NULL)
4339 return qfbuf;
4340 // buffer is no longer present
4341 qi->qf_bufnr = INVALID_QFBUFNR;
4342 }
4343
Bram Moolenaar9c102382006-05-03 21:26:49 +00004344 FOR_ALL_TAB_WINDOWS(tp, win)
4345 if (is_qf_win(win, qi))
4346 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004347
Bram Moolenaar9c102382006-05-03 21:26:49 +00004348 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349}
4350
4351/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004352 * Update the w:quickfix_title variable in the quickfix/location list window
4353 */
4354 static void
4355qf_update_win_titlevar(qf_info_T *qi)
4356{
4357 win_T *win;
4358 win_T *curwin_save;
4359
4360 if ((win = qf_find_win(qi)) != NULL)
4361 {
4362 curwin_save = curwin;
4363 curwin = win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004364 qf_set_title_var(qf_get_curlist(qi));
Bram Moolenaard823fa92016-08-12 16:29:27 +02004365 curwin = curwin_save;
4366 }
4367}
4368
4369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 * Find the quickfix buffer. If it exists, update the contents.
4371 */
4372 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004373qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374{
4375 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004376 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004379 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004380 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 if (buf != NULL)
4382 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004383 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004384 int qf_winid = 0;
4385
4386 if (IS_LL_STACK(qi))
4387 qf_winid = curwin->w_id;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004388
4389 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004390 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004391 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
Bram Moolenaard823fa92016-08-12 16:29:27 +02004393 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004394
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004395 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004396 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004397
Bram Moolenaar864293a2016-06-02 13:40:04 +02004398 if (old_last == NULL)
4399 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004400 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004401
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004402 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004403 aucmd_restbuf(&aco);
4404 }
4405
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004406 // Only redraw when added lines are visible. This avoids flickering
4407 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004408 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4409 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411}
4412
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004413/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004414 * Add an error line to the quickfix buffer.
4415 */
4416 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004417qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004418 buf_T *buf, // quickfix window buffer
4419 linenr_T lnum,
4420 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004421 char_u *dirname,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004422 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004423{
4424 int len;
4425 buf_T *errbuf;
4426
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004427 if (qftf_str != NULL)
4428 vim_strncpy(IObuff, qftf_str, IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004429 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004430 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004431 if (qfp->qf_module != NULL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004432 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004433 vim_strncpy(IObuff, qfp->qf_module, IOSIZE - 1);
4434 len = (int)STRLEN(IObuff);
4435 }
4436 else if (qfp->qf_fnum != 0
4437 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4438 && errbuf->b_fname != NULL)
4439 {
4440 if (qfp->qf_type == 1) // :helpgrep
4441 vim_strncpy(IObuff, gettail(errbuf->b_fname), IOSIZE - 1);
4442 else
4443 {
4444 // shorten the file name if not done already
4445 if (errbuf->b_sfname == NULL
4446 || mch_isFullName(errbuf->b_sfname))
4447 {
4448 if (*dirname == NUL)
4449 mch_dirname(dirname, MAXPATHL);
4450 shorten_buf_fname(errbuf, dirname, FALSE);
4451 }
4452 vim_strncpy(IObuff, errbuf->b_fname, IOSIZE - 1);
4453 }
4454 len = (int)STRLEN(IObuff);
4455 }
4456 else
4457 len = 0;
4458
4459 if (len < IOSIZE - 1)
4460 IObuff[len++] = '|';
4461
4462 if (qfp->qf_lnum > 0)
4463 {
4464 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%ld",
4465 qfp->qf_lnum);
4466 len += (int)STRLEN(IObuff + len);
4467
4468 if (qfp->qf_col > 0)
4469 {
4470 vim_snprintf((char *)IObuff + len, IOSIZE - len,
4471 " col %d", qfp->qf_col);
4472 len += (int)STRLEN(IObuff + len);
4473 }
4474
4475 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%s",
4476 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004477 len += (int)STRLEN(IObuff + len);
4478 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004479 else if (qfp->qf_pattern != NULL)
4480 {
4481 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4482 len += (int)STRLEN(IObuff + len);
4483 }
4484 if (len < IOSIZE - 2)
4485 {
4486 IObuff[len++] = '|';
4487 IObuff[len++] = ' ';
4488 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004489
Bram Moolenaar858ba062020-05-31 23:11:59 +02004490 // Remove newlines and leading whitespace from the text.
4491 // For an unrecognized line keep the indent, the compiler may
4492 // mark a word with ^^^^.
4493 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4494 IObuff + len, IOSIZE - len);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004495 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004496
4497 if (ml_append_buf(buf, lnum, IObuff,
4498 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4499 return FAIL;
4500
4501 return OK;
4502}
4503
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004504 static list_T *
4505call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4506{
4507 char_u *qftf = p_qftf;
4508 list_T *qftf_list = NULL;
4509
4510 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4511 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4512 // set.
4513 if (qfl->qf_qftf != NULL)
4514 qftf = qfl->qf_qftf;
4515 if (qftf != NULL && *qftf != NUL)
4516 {
4517 typval_T args[1];
4518 dict_T *d;
4519
4520 // create the dict argument
4521 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
4522 return NULL;
4523 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4524 dict_add_number(d, "winid", (long)qf_winid);
4525 dict_add_number(d, "id", (long)qfl->qf_id);
4526 dict_add_number(d, "start_idx", start_idx);
4527 dict_add_number(d, "end_idx", end_idx);
4528 ++d->dv_refcount;
4529 args[0].v_type = VAR_DICT;
4530 args[0].vval.v_dict = d;
4531
4532 qftf_list = call_func_retlist(qftf, 1, args);
4533 --d->dv_refcount;
4534 }
4535
4536 return qftf_list;
4537}
4538
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 * Fill current buffer with quickfix errors, replacing any previous contents.
4541 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004542 * If "old_last" is not NULL append the items after this one.
4543 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4544 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 */
4546 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004547qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004549 linenr_T lnum;
4550 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004551 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004552 list_T *qftf_list = NULL;
4553 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaar864293a2016-06-02 13:40:04 +02004555 if (old_last == NULL)
4556 {
4557 if (buf != curbuf)
4558 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004559 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004560 return;
4561 }
4562
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004563 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004564 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004565 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004568 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004569 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004571 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004572
4573 *dirname = NUL;
4574
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004575 // Add one line for each error
Bram Moolenaar99234f22020-02-10 22:56:54 +01004576 if (old_last == NULL || old_last->qf_next == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004577 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004578 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004579 lnum = 0;
4580 }
4581 else
4582 {
4583 qfp = old_last->qf_next;
4584 lnum = buf->b_ml.ml_line_count;
4585 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004586
4587 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4588 (long)qfl->qf_count);
4589 if (qftf_list != NULL)
4590 qftf_li = qftf_list->lv_first;
4591
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004592 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004594 char_u *qftf_str = NULL;
4595
4596 if (qftf_li != NULL)
4597 // Use the text supplied by the user defined function
4598 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
4599
4600 if (qf_buf_add_line(buf, lnum, qfp, dirname, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004602
Bram Moolenaar864293a2016-06-02 13:40:04 +02004603 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004605 if (qfp == NULL)
4606 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004607
4608 if (qftf_li != NULL)
4609 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004611
4612 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004613 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004614 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 }
4616
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004617 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 check_lnums(TRUE);
4619
Bram Moolenaar864293a2016-06-02 13:40:04 +02004620 if (old_last == NULL)
4621 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004622 // Set the 'filetype' to "qf" each time after filling the buffer.
4623 // This resembles reading a file into a buffer, it's more logical when
4624 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004625 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004626 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4627 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004629 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004630 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004632 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004634 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004635 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004636
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004637 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004638 redraw_curbuf_later(NOT_VALID);
4639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004641 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 KeyTyped = old_KeyTyped;
4643}
4644
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004645/*
4646 * For every change made to the quickfix list, update the changed tick.
4647 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004648 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004649qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004650{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004651 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004652}
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004655 * Return the quickfix/location list number with the given identifier.
4656 * Returns -1 if list is not found.
4657 */
4658 static int
4659qf_id2nr(qf_info_T *qi, int_u qfid)
4660{
4661 int qf_idx;
4662
4663 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4664 if (qi->qf_lists[qf_idx].qf_id == qfid)
4665 return qf_idx;
4666 return INVALID_QFIDX;
4667}
4668
4669/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004670 * If the current list is not "save_qfid" and we can find the list with that ID
4671 * then make it the current list.
4672 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004673 * Returns OK if successfully restored the list. Returns FAIL if the list with
4674 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004675 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004676 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004677qf_restore_list(qf_info_T *qi, int_u save_qfid)
4678{
4679 int curlist;
4680
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004681 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004682 {
4683 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004684 if (curlist < 0)
4685 // list is not present
4686 return FAIL;
4687 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004688 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004689 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004690}
4691
4692/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004693 * Jump to the first entry if there is one.
4694 */
4695 static void
4696qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4697{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004698 if (qf_restore_list(qi, save_qfid) == FAIL)
4699 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004700
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004701 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004702 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004703 qf_jump(qi, 0, 0, forceit);
4704}
4705
4706/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004707 * Return TRUE when using ":vimgrep" for ":grep".
4708 */
4709 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004710grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004711{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004712 return ((cmdidx == CMD_grep
4713 || cmdidx == CMD_lgrep
4714 || cmdidx == CMD_grepadd
4715 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004716 && STRCMP("internal",
4717 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4718}
4719
4720/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004721 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004723 static char_u *
4724make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004726 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004727 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004728 case CMD_make: return (char_u *)"make";
4729 case CMD_lmake: return (char_u *)"lmake";
4730 case CMD_grep: return (char_u *)"grep";
4731 case CMD_lgrep: return (char_u *)"lgrep";
4732 case CMD_grepadd: return (char_u *)"grepadd";
4733 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4734 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736}
4737
4738/*
4739 * Return the name for the errorfile, in allocated memory.
4740 * Find a new unique name when 'makeef' contains "##".
4741 * Returns NULL for error.
4742 */
4743 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004744get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746 char_u *p;
4747 char_u *name;
4748 static int start = -1;
4749 static int off = 0;
4750#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004751 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752#endif
4753
4754 if (*p_mef == NUL)
4755 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004756 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004758 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 return name;
4760 }
4761
4762 for (p = p_mef; *p; ++p)
4763 if (p[0] == '#' && p[1] == '#')
4764 break;
4765
4766 if (*p == NUL)
4767 return vim_strsave(p_mef);
4768
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004769 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 for (;;)
4771 {
4772 if (start == -1)
4773 start = mch_get_pid();
4774 else
4775 off += 19;
4776
Bram Moolenaar964b3742019-05-24 18:54:09 +02004777 name = alloc(STRLEN(p_mef) + 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 if (name == NULL)
4779 break;
4780 STRCPY(name, p_mef);
4781 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4782 STRCAT(name, p + 2);
4783 if (mch_getperm(name) < 0
4784#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004785 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 && mch_lstat((char *)name, &sb) < 0
4787#endif
4788 )
4789 break;
4790 vim_free(name);
4791 }
4792 return name;
4793}
4794
4795/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004796 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4797 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4798 */
4799 static char_u *
4800make_get_fullcmd(char_u *makecmd, char_u *fname)
4801{
4802 char_u *cmd;
4803 unsigned len;
4804
4805 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4806 if (*p_sp != NUL)
4807 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4808 cmd = alloc(len);
4809 if (cmd == NULL)
4810 return NULL;
4811 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4812 (char *)p_shq);
4813
4814 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4815 if (*p_sp != NUL)
4816 append_redir(cmd, len, p_sp, fname);
4817
4818 // Display the fully formed command. Output a newline if there's something
4819 // else than the :make command that was typed (in which case the cursor is
4820 // in column 0).
4821 if (msg_col == 0)
4822 msg_didout = FALSE;
4823 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004824 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004825 msg_outtrans(cmd); // show what we are doing
4826
4827 return cmd;
4828}
4829
4830/*
4831 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4832 */
4833 void
4834ex_make(exarg_T *eap)
4835{
4836 char_u *fname;
4837 char_u *cmd;
4838 char_u *enc = NULL;
4839 win_T *wp = NULL;
4840 qf_info_T *qi = &ql_info;
4841 int res;
4842 char_u *au_name = NULL;
4843 int_u save_qfid;
4844
4845 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4846 if (grep_internal(eap->cmdidx))
4847 {
4848 ex_vimgrep(eap);
4849 return;
4850 }
4851
4852 au_name = make_get_auname(eap->cmdidx);
4853 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4854 curbuf->b_fname, TRUE, curbuf))
4855 {
4856#ifdef FEAT_EVAL
4857 if (aborting())
4858 return;
4859#endif
4860 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004861 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004862
4863 if (is_loclist_cmd(eap->cmdidx))
4864 wp = curwin;
4865
4866 autowrite_all();
4867 fname = get_mef_name();
4868 if (fname == NULL)
4869 return;
4870 mch_remove(fname); // in case it's not unique
4871
4872 cmd = make_get_fullcmd(eap->arg, fname);
4873 if (cmd == NULL)
4874 return;
4875
4876 // let the shell know if we are redirecting output or not
4877 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4878
4879#ifdef AMIGA
4880 out_flush();
4881 // read window status report and redraw before message
4882 (void)char_avail();
4883#endif
4884
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004885 incr_quickfix_busy();
4886
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004887 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4888 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4889 (eap->cmdidx != CMD_grepadd
4890 && eap->cmdidx != CMD_lgrepadd),
4891 qf_cmdtitle(*eap->cmdlinep), enc);
4892 if (wp != NULL)
4893 {
4894 qi = GET_LOC_LIST(wp);
4895 if (qi == NULL)
4896 goto cleanup;
4897 }
4898 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004899 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004900
4901 // Remember the current quickfix list identifier, so that we can
4902 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004903 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004904 if (au_name != NULL)
4905 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4906 curbuf->b_fname, TRUE, curbuf);
4907 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4908 // display the first error
4909 qf_jump_first(qi, save_qfid, FALSE);
4910
4911cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004912 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004913 mch_remove(fname);
4914 vim_free(fname);
4915 vim_free(cmd);
4916}
4917
4918/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02004919 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004920 */
4921 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004922qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004923{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004924 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004925
4926 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4927 return 0;
4928 return qf_get_curlist(qi)->qf_count;
4929}
4930
4931/*
4932 * Returns the number of valid entries in the current quickfix/location list.
4933 */
4934 int
4935qf_get_valid_size(exarg_T *eap)
4936{
4937 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004938 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004939 qfline_T *qfp;
4940 int i, sz = 0;
4941 int prev_fnum = 0;
4942
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004943 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4944 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004945
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004946 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01004947 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004948 {
4949 if (qfp->qf_valid)
4950 {
4951 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004952 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004953 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4954 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004955 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004956 sz++;
4957 prev_fnum = qfp->qf_fnum;
4958 }
4959 }
4960 }
4961
4962 return sz;
4963}
4964
4965/*
4966 * Returns the current index of the quickfix/location list.
4967 * Returns 0 if there is an error.
4968 */
4969 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004970qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004971{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004972 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004973
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004974 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4975 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004976
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004977 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004978}
4979
4980/*
4981 * Returns the current index in the quickfix/location list (counting only valid
4982 * entries). If no valid entries are in the list, then returns 1.
4983 */
4984 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004985qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004986{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004987 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004988 qf_list_T *qfl;
4989 qfline_T *qfp;
4990 int i, eidx = 0;
4991 int prev_fnum = 0;
4992
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004993 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4994 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004995
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004996 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004997 qfp = qfl->qf_start;
4998
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004999 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005000 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005001 return 1;
5002
5003 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5004 {
5005 if (qfp->qf_valid)
5006 {
5007 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5008 {
5009 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5010 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005011 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005012 eidx++;
5013 prev_fnum = qfp->qf_fnum;
5014 }
5015 }
5016 else
5017 eidx++;
5018 }
5019 }
5020
5021 return eidx ? eidx : 1;
5022}
5023
5024/*
5025 * Get the 'n'th valid error entry in the quickfix or location list.
5026 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5027 * For :cdo and :ldo returns the 'n'th valid error entry.
5028 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5029 */
5030 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005031qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005032{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005033 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005034 int i, eidx;
5035 int prev_fnum = 0;
5036
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005037 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005038 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005039 return 1;
5040
Bram Moolenaar95946f12019-03-31 15:31:59 +02005041 eidx = 0;
5042 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005043 {
5044 if (qfp->qf_valid)
5045 {
5046 if (fdo)
5047 {
5048 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5049 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005050 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005051 eidx++;
5052 prev_fnum = qfp->qf_fnum;
5053 }
5054 }
5055 else
5056 eidx++;
5057 }
5058
5059 if (eidx == n)
5060 break;
5061 }
5062
5063 if (i <= qfl->qf_count)
5064 return i;
5065 else
5066 return 1;
5067}
5068
5069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005071 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005072 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 */
5074 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005075ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005077 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005078 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005079
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005080 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5081 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005082
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005083 if (eap->addr_count > 0)
5084 errornr = (int)eap->line2;
5085 else
5086 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005087 switch (eap->cmdidx)
5088 {
5089 case CMD_cc: case CMD_ll:
5090 errornr = 0;
5091 break;
5092 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5093 case CMD_lfirst:
5094 errornr = 1;
5095 break;
5096 default:
5097 errornr = 32767;
5098 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005099 }
5100
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005101 // For cdo and ldo commands, jump to the nth valid error.
5102 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005103 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5104 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005105 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005106 eap->addr_count > 0 ? (int)eap->line1 : 1,
5107 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5108
5109 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110}
5111
5112/*
5113 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005114 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005115 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 */
5117 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005118ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005120 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005121 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005122 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005123
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005124 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5125 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005126
Bram Moolenaar55b69262017-08-13 13:42:01 +02005127 if (eap->addr_count > 0
5128 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5129 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005130 errornr = (int)eap->line2;
5131 else
5132 errornr = 1;
5133
Bram Moolenaar39665952018-08-15 20:59:48 +02005134 // Depending on the command jump to either next or previous entry/file.
5135 switch (eap->cmdidx)
5136 {
5137 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5138 dir = FORWARD;
5139 break;
5140 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5141 case CMD_lNext:
5142 dir = BACKWARD;
5143 break;
5144 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5145 dir = FORWARD_FILE;
5146 break;
5147 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5148 dir = BACKWARD_FILE;
5149 break;
5150 default:
5151 dir = FORWARD;
5152 break;
5153 }
5154
5155 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156}
5157
5158/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005159 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5160 * The index of the entry is stored in 'errornr'.
5161 * Returns NULL if an entry is not found.
5162 */
5163 static qfline_T *
5164qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5165{
5166 qfline_T *qfp = NULL;
5167 int idx = 0;
5168
5169 // Find the first entry in this file
5170 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5171 if (qfp->qf_fnum == bnr)
5172 break;
5173
5174 *errornr = idx;
5175 return qfp;
5176}
5177
5178/*
5179 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5180 * with the error number for the first entry. Assumes the entries are sorted in
5181 * the quickfix list by line number.
5182 */
5183 static qfline_T *
5184qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5185{
5186 while (!got_int
5187 && entry->qf_prev != NULL
5188 && entry->qf_fnum == entry->qf_prev->qf_fnum
5189 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5190 {
5191 entry = entry->qf_prev;
5192 --*errornr;
5193 }
5194
5195 return entry;
5196}
5197
5198/*
5199 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5200 * with the error number for the last entry. Assumes the entries are sorted in
5201 * the quickfix list by line number.
5202 */
5203 static qfline_T *
5204qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5205{
5206 while (!got_int &&
5207 entry->qf_next != NULL
5208 && entry->qf_fnum == entry->qf_next->qf_fnum
5209 && entry->qf_lnum == entry->qf_next->qf_lnum)
5210 {
5211 entry = entry->qf_next;
5212 ++*errornr;
5213 }
5214
5215 return entry;
5216}
5217
5218/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005219 * Returns TRUE if the specified quickfix entry is
5220 * after the given line (linewise is TRUE)
5221 * or after the line and column.
5222 */
5223 static int
5224qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5225{
5226 if (linewise)
5227 return qfp->qf_lnum > pos->lnum;
5228 else
5229 return (qfp->qf_lnum > pos->lnum ||
5230 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5231}
5232
5233/*
5234 * Returns TRUE if the specified quickfix entry is
5235 * before the given line (linewise is TRUE)
5236 * or before the line and column.
5237 */
5238 static int
5239qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5240{
5241 if (linewise)
5242 return qfp->qf_lnum < pos->lnum;
5243 else
5244 return (qfp->qf_lnum < pos->lnum ||
5245 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5246}
5247
5248/*
5249 * Returns TRUE if the specified quickfix entry is
5250 * on or after the given line (linewise is TRUE)
5251 * or on or after the line and column.
5252 */
5253 static int
5254qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5255{
5256 if (linewise)
5257 return qfp->qf_lnum >= pos->lnum;
5258 else
5259 return (qfp->qf_lnum > pos->lnum ||
5260 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5261}
5262
5263/*
5264 * Returns TRUE if the specified quickfix entry is
5265 * on or before the given line (linewise is TRUE)
5266 * or on or before the line and column.
5267 */
5268 static int
5269qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5270{
5271 if (linewise)
5272 return qfp->qf_lnum <= pos->lnum;
5273 else
5274 return (qfp->qf_lnum < pos->lnum ||
5275 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5276}
5277
5278/*
5279 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5280 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5281 * multiple entries on a single line as one. Otherwise returns the entry after
5282 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005283 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5284 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005285 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005286 */
5287 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005288qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005289 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005290 pos_T *pos,
5291 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005292 qfline_T *qfp,
5293 int *errornr)
5294{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005295 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005296 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005297 return qfp;
5298
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005299 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005300 while (qfp->qf_next != NULL
5301 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005302 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005303 {
5304 qfp = qfp->qf_next;
5305 ++*errornr;
5306 }
5307
5308 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005309 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005310 return NULL;
5311
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005312 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005313 qfp = qfp->qf_next;
5314 ++*errornr;
5315
5316 return qfp;
5317}
5318
5319/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005320 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5321 * If 'linewise' is TRUE, returns the entry before the specified line and
5322 * treats multiple entries on a single line as one. Otherwise returns the entry
5323 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005324 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5325 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005326 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005327 */
5328 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005329qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005330 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005331 pos_T *pos,
5332 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005333 qfline_T *qfp,
5334 int *errornr)
5335{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005336 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005337 while (qfp->qf_next != NULL
5338 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005339 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005340 {
5341 qfp = qfp->qf_next;
5342 ++*errornr;
5343 }
5344
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005345 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005346 return NULL;
5347
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005348 if (linewise)
5349 // If multiple entries are on the same line, then use the first entry
5350 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005351
5352 return qfp;
5353}
5354
5355/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005356 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005357 * the direction 'dir'.
5358 */
5359 static qfline_T *
5360qf_find_closest_entry(
5361 qf_list_T *qfl,
5362 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005363 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005364 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005365 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005366 int *errornr)
5367{
5368 qfline_T *qfp;
5369
5370 *errornr = 0;
5371
5372 // Find the first entry in this file
5373 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5374 if (qfp == NULL)
5375 return NULL; // no entry in this file
5376
5377 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005378 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005379 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005380 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005381
5382 return qfp;
5383}
5384
5385/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005386 * Get the nth quickfix entry below the specified entry. Searches forward in
5387 * the list. If linewise is TRUE, then treat multiple entries on a single line
5388 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005389 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005390 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005391qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005392{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005393 qfline_T *entry = entry_arg;
5394
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005395 while (n-- > 0 && !got_int)
5396 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005397 int first_errornr = *errornr;
5398
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005399 if (linewise)
5400 // Treat all the entries on the same line in this file as one
5401 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005402
5403 if (entry->qf_next == NULL
5404 || entry->qf_next->qf_fnum != entry->qf_fnum)
5405 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005406 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005407 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005408 break;
5409 }
5410
5411 entry = entry->qf_next;
5412 ++*errornr;
5413 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005414}
5415
5416/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005417 * Get the nth quickfix entry above the specified entry. Searches backwards in
5418 * the list. If linewise is TRUE, then treat multiple entries on a single line
5419 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005420 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005421 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005422qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005423{
5424 while (n-- > 0 && !got_int)
5425 {
5426 if (entry->qf_prev == NULL
5427 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5428 break;
5429
5430 entry = entry->qf_prev;
5431 --*errornr;
5432
5433 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005434 if (linewise)
5435 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005436 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005437}
5438
5439/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005440 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5441 * the specified direction. Returns the error number in the quickfix list or 0
5442 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005443 */
5444 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005445qf_find_nth_adj_entry(
5446 qf_list_T *qfl,
5447 int bnr,
5448 pos_T *pos,
5449 int n,
5450 int dir,
5451 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005452{
5453 qfline_T *adj_entry;
5454 int errornr;
5455
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005456 // Find an entry closest to the specified position
5457 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005458 if (adj_entry == NULL)
5459 return 0;
5460
5461 if (--n > 0)
5462 {
5463 // Go to the n'th entry in the current buffer
5464 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005465 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005466 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005467 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005468 }
5469
5470 return errornr;
5471}
5472
5473/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005474 * Jump to a quickfix entry in the current file nearest to the current line or
5475 * current line/col.
5476 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5477 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005478 */
5479 void
5480ex_cbelow(exarg_T *eap)
5481{
5482 qf_info_T *qi;
5483 qf_list_T *qfl;
5484 int dir;
5485 int buf_has_flag;
5486 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005487 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005488
5489 if (eap->addr_count > 0 && eap->line2 <= 0)
5490 {
5491 emsg(_(e_invrange));
5492 return;
5493 }
5494
5495 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005496 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5497 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005498 buf_has_flag = BUF_HAS_QF_ENTRY;
5499 else
5500 buf_has_flag = BUF_HAS_LL_ENTRY;
5501 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5502 {
5503 emsg(_(e_quickfix));
5504 return;
5505 }
5506
5507 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5508 return;
5509
5510 qfl = qf_get_curlist(qi);
5511 // check if the list has valid errors
5512 if (!qf_list_has_valid_entries(qfl))
5513 {
5514 emsg(_(e_quickfix));
5515 return;
5516 }
5517
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005518 if (eap->cmdidx == CMD_cbelow
5519 || eap->cmdidx == CMD_lbelow
5520 || eap->cmdidx == CMD_cafter
5521 || eap->cmdidx == CMD_lafter)
5522 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005523 dir = FORWARD;
5524 else
5525 dir = BACKWARD;
5526
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005527 pos = curwin->w_cursor;
5528 // A quickfix entry column number is 1 based whereas cursor column
5529 // number is 0 based. Adjust the column number.
5530 pos.col++;
5531 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5532 eap->addr_count > 0 ? eap->line2 : 0, dir,
5533 eap->cmdidx == CMD_cbelow
5534 || eap->cmdidx == CMD_lbelow
5535 || eap->cmdidx == CMD_cabove
5536 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005537
5538 if (errornr > 0)
5539 qf_jump(qi, 0, errornr, FALSE);
5540 else
5541 emsg(_(e_no_more_items));
5542}
5543
5544/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005545 * Return the autocmd name for the :cfile Ex commands
5546 */
5547 static char_u *
5548cfile_get_auname(cmdidx_T cmdidx)
5549{
5550 switch (cmdidx)
5551 {
5552 case CMD_cfile: return (char_u *)"cfile";
5553 case CMD_cgetfile: return (char_u *)"cgetfile";
5554 case CMD_caddfile: return (char_u *)"caddfile";
5555 case CMD_lfile: return (char_u *)"lfile";
5556 case CMD_lgetfile: return (char_u *)"lgetfile";
5557 case CMD_laddfile: return (char_u *)"laddfile";
5558 default: return NULL;
5559 }
5560}
5561
5562/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005563 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005564 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 */
5566 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005567ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005569 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005570 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005571 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005572 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005573 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005574 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005575
Bram Moolenaara16123a2019-03-28 20:31:07 +01005576 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005577 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5578 NULL, FALSE, curbuf))
5579 {
5580#ifdef FEAT_EVAL
5581 if (aborting())
5582 return;
5583#endif
5584 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005585
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005586 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005587#ifdef FEAT_BROWSE
5588 if (cmdmod.browse)
5589 {
5590 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005591 NULL, NULL,
5592 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005593 if (browse_file == NULL)
5594 return;
5595 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5596 vim_free(browse_file);
5597 }
5598 else
5599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005601 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005602
Bram Moolenaar39665952018-08-15 20:59:48 +02005603 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005604 wp = curwin;
5605
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005606 incr_quickfix_busy();
5607
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005608 // This function is used by the :cfile, :cgetfile and :caddfile
5609 // commands.
5610 // :cfile always creates a new quickfix list and jumps to the
5611 // first error.
5612 // :cgetfile creates a new quickfix list but doesn't jump to the
5613 // first error.
5614 // :caddfile adds to an existing quickfix list. If there is no
5615 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005616 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005617 && eap->cmdidx != CMD_laddfile),
5618 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005619 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005620 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005621 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005622 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005623 {
5624 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005625 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005626 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005627 }
5628 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005629 qf_list_changed(qf_get_curlist(qi));
5630 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005631 if (au_name != NULL)
5632 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005633
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005634 // Jump to the first error for a new list and if autocmds didn't
5635 // free the list.
5636 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5637 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005638 // display the first error
5639 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005640
5641 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005642}
5643
5644/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005645 * Return the vimgrep autocmd name.
5646 */
5647 static char_u *
5648vgr_get_auname(cmdidx_T cmdidx)
5649{
5650 switch (cmdidx)
5651 {
5652 case CMD_vimgrep: return (char_u *)"vimgrep";
5653 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5654 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5655 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5656 case CMD_grep: return (char_u *)"grep";
5657 case CMD_lgrep: return (char_u *)"lgrep";
5658 case CMD_grepadd: return (char_u *)"grepadd";
5659 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5660 default: return NULL;
5661 }
5662}
5663
5664/*
5665 * Initialize the regmatch used by vimgrep for pattern "s".
5666 */
5667 static void
5668vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5669{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005670 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005671 regmatch->regprog = NULL;
5672
5673 if (s == NULL || *s == NUL)
5674 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005675 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005676 if (last_search_pat() == NULL)
5677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005678 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005679 return;
5680 }
5681 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5682 }
5683 else
5684 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5685
5686 regmatch->rmm_ic = p_ic;
5687 regmatch->rmm_maxcol = 0;
5688}
5689
5690/*
5691 * Display a file name when vimgrep is running.
5692 */
5693 static void
5694vgr_display_fname(char_u *fname)
5695{
5696 char_u *p;
5697
5698 msg_start();
5699 p = msg_strtrunc(fname, TRUE);
5700 if (p == NULL)
5701 msg_outtrans(fname);
5702 else
5703 {
5704 msg_outtrans(p);
5705 vim_free(p);
5706 }
5707 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005708 msg_didout = FALSE; // overwrite this message
5709 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005710 msg_col = 0;
5711 out_flush();
5712}
5713
5714/*
5715 * Load a dummy buffer to search for a pattern using vimgrep.
5716 */
5717 static buf_T *
5718vgr_load_dummy_buf(
5719 char_u *fname,
5720 char_u *dirname_start,
5721 char_u *dirname_now)
5722{
5723 int save_mls;
5724#if defined(FEAT_SYN_HL)
5725 char_u *save_ei = NULL;
5726#endif
5727 buf_T *buf;
5728
5729#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005730 // Don't do Filetype autocommands to avoid loading syntax and
5731 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005732 save_ei = au_event_disable(",Filetype");
5733#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005734 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005735 save_mls = p_mls;
5736 p_mls = 0;
5737
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005738 // Load file into a buffer, so that 'fileencoding' is detected,
5739 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005740 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5741
5742 p_mls = save_mls;
5743#if defined(FEAT_SYN_HL)
5744 au_event_restore(save_ei);
5745#endif
5746
5747 return buf;
5748}
5749
5750/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005751 * Check whether a quickfix/location list is valid. Autocmds may remove or
5752 * change a quickfix list when vimgrep is running. If the list is not found,
5753 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005754 */
5755 static int
5756vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005757 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005758 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005759 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005760 char_u *title)
5761{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005762 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005763 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005764 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005765 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005766 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005767 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005768 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005769 return FALSE;
5770 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005771 else
5772 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005773 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005774 qf_new_list(qi, title);
5775 return TRUE;
5776 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005777 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005778
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005779 if (qf_restore_list(qi, qfid) == FAIL)
5780 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005781
5782 return TRUE;
5783}
5784
5785/*
5786 * Search for a pattern in all the lines in a buffer and add the matching lines
5787 * to a quickfix list.
5788 */
5789 static int
5790vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005791 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005792 char_u *fname,
5793 buf_T *buf,
5794 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005795 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005796 int duplicate_name,
5797 int flags)
5798{
5799 int found_match = FALSE;
5800 long lnum;
5801 colnr_T col;
5802
Bram Moolenaar1c299432018-10-28 14:36:09 +01005803 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005804 {
5805 col = 0;
5806 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5807 col, NULL, NULL) > 0)
5808 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005809 // Pass the buffer number so that it gets used even for a
5810 // dummy buffer, unless duplicate_name is set, then the
5811 // buffer will be wiped out below.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005812 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005813 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005814 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005815 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005816 duplicate_name ? 0 : buf->b_fnum,
5817 ml_get_buf(buf,
5818 regmatch->startpos[0].lnum + lnum, FALSE),
5819 regmatch->startpos[0].lnum + lnum,
5820 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005821 FALSE, // vis_col
5822 NULL, // search pattern
5823 0, // nr
5824 0, // type
5825 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02005826 ) == QF_FAIL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005827 {
5828 got_int = TRUE;
5829 break;
5830 }
5831 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005832 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005833 break;
5834 if ((flags & VGR_GLOBAL) == 0
5835 || regmatch->endpos[0].lnum > 0)
5836 break;
5837 col = regmatch->endpos[0].col
5838 + (col == regmatch->endpos[0].col);
5839 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5840 break;
5841 }
5842 line_breakcheck();
5843 if (got_int)
5844 break;
5845 }
5846
5847 return found_match;
5848}
5849
5850/*
5851 * Jump to the first match and update the directory.
5852 */
5853 static void
5854vgr_jump_to_match(
5855 qf_info_T *qi,
5856 int forceit,
5857 int *redraw_for_dummy,
5858 buf_T *first_match_buf,
5859 char_u *target_dir)
5860{
5861 buf_T *buf;
5862
5863 buf = curbuf;
5864 qf_jump(qi, 0, 0, forceit);
5865 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005866 // If we jumped to another buffer redrawing will already be
5867 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005868 *redraw_for_dummy = FALSE;
5869
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005870 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005871 if (curbuf == first_match_buf && target_dir != NULL)
5872 {
5873 exarg_T ea;
5874
Bram Moolenaara80faa82020-04-12 19:37:17 +02005875 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005876 ea.arg = target_dir;
5877 ea.cmdidx = CMD_lcd;
5878 ex_cd(&ea);
5879 }
5880}
5881
5882/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005883 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00005884 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005885typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00005886{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005887 long tomatch; // maximum number of matches to find
5888 char_u *spat; // search pattern
5889 int flags; // search modifier
5890 char_u **fnames; // list of files to search
5891 int fcount; // number of files
5892 regmmatch_T regmatch; // compiled search pattern
5893 char_u *qf_title; // quickfix list title
5894} vgr_args_T;
5895
5896/*
5897 * Process :vimgrep command arguments. The command syntax is:
5898 *
5899 * :{count}vimgrep /{pattern}/[g][j] {file} ...
5900 */
5901 static int
5902vgr_process_args(
5903 exarg_T *eap,
5904 vgr_args_T *args)
5905{
Bram Moolenaar748bf032005-02-02 23:04:36 +00005906 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005907
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005908 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005909
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005910 args->regmatch.regprog = NULL;
5911 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00005912
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005913 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005914 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005915 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005916 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005917
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005918 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005919 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005920 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005922 emsg(_(e_invalpat));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005923 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005924 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005925
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005926 vgr_init_regmatch(&args->regmatch, args->spat);
5927 if (args->regmatch.regprog == NULL)
5928 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005929
5930 p = skipwhite(p);
5931 if (*p == NUL)
5932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005933 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005934 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005935 }
5936
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005937 // parse the list of arguments
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005938 if (get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL)
5939 return FAIL;
5940 if (args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005941 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005942 emsg(_(e_nomatch));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005943 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005944 }
5945
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005946 return OK;
5947}
5948
5949/*
5950 * Search for a pattern in a list of files and populate the quickfix list with
5951 * the matches.
5952 */
5953 static int
5954vgr_process_files(
5955 win_T *wp,
5956 qf_info_T *qi,
5957 vgr_args_T *cmd_args,
5958 int *redraw_for_dummy,
5959 buf_T **first_match_buf,
5960 char_u **target_dir)
5961{
5962 int status = FAIL;
5963 int_u save_qfid = qf_get_curlist(qi)->qf_id;
5964 time_t seconds = 0;
5965 char_u *fname;
5966 int fi;
5967 buf_T *buf;
5968 int duplicate_name = FALSE;
5969 int using_dummy;
5970 char_u *dirname_start = NULL;
5971 char_u *dirname_now = NULL;
5972 int found_match;
5973 aco_save_T aco;
5974
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005975 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5976 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005977 if (dirname_start == NULL || dirname_now == NULL)
5978 goto theend;
5979
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005980 // Remember the current directory, because a BufRead autocommand that does
5981 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005982 mch_dirname(dirname_start, MAXPATHL);
5983
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005984 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005985 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
5986 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005987 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005988 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005989 if (time(NULL) > seconds)
5990 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005991 // Display the file name every second or so, show the user we are
5992 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005993 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005994 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005995 }
5996
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005997 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00005998 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5999 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006000 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006001 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006002 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006003 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006004
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006005 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006006 }
6007 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006008 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006009 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006010
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006011 // Check whether the quickfix list is still valid. When loading a
6012 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006013 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006014 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006015
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006016 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006017
Bram Moolenaar81695252004-12-29 20:58:21 +00006018 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006019 {
6020 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006021 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006022 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006023 else
6024 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006025 // Try for a match in all lines of the buffer.
6026 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006027 found_match = vgr_match_buflines(qf_get_curlist(qi),
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006028 fname, buf, &cmd_args->regmatch,
6029 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006030
Bram Moolenaar81695252004-12-29 20:58:21 +00006031 if (using_dummy)
6032 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006033 if (found_match && *first_match_buf == NULL)
6034 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006035 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006036 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006037 // Never keep a dummy buffer if there is another buffer
6038 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006039 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006040 buf = NULL;
6041 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00006042 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006043 || buf->b_p_bh[0] == 'u' // "unload"
6044 || buf->b_p_bh[0] == 'w' // "wipe"
6045 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006046 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006047 // When no match was found we don't need to remember the
6048 // buffer, wipe it out. If there was a match and it
6049 // wasn't the first one or we won't jump there: only
6050 // unload the buffer.
6051 // Ignore 'hidden' here, because it may lead to having too
6052 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006053 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006054 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006055 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006056 buf = NULL;
6057 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006058 else if (buf != *first_match_buf
6059 || (cmd_args->flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006060 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006061 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006062 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006063 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006064 buf = NULL;
6065 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006066 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006067
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006068 if (buf != NULL)
6069 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006070 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006071 buf->b_flags &= ~BF_DUMMY;
6072
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006073 // If the buffer is still loaded we need to use the
6074 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006075 if (buf == *first_match_buf
6076 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006077 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006078 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006079
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006080 // The buffer is still loaded, the Filetype autocommands
6081 // need to be done now, in that buffer. And the modelines
6082 // need to be done (again). But not the window-local
6083 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006084 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006085#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006086 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6087 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006088#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006089 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006090 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006091 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006092 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006093 }
6094 }
6095
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006096 status = OK;
6097
6098theend:
6099 vim_free(dirname_now);
6100 vim_free(dirname_start);
6101 return status;
6102}
6103
6104/*
6105 * ":vimgrep {pattern} file(s)"
6106 * ":vimgrepadd {pattern} file(s)"
6107 * ":lvimgrep {pattern} file(s)"
6108 * ":lvimgrepadd {pattern} file(s)"
6109 */
6110 void
6111ex_vimgrep(exarg_T *eap)
6112{
6113 vgr_args_T args;
6114 qf_info_T *qi;
6115 qf_list_T *qfl;
6116 int_u save_qfid;
6117 win_T *wp = NULL;
6118 int redraw_for_dummy = FALSE;
6119 buf_T *first_match_buf = NULL;
6120 char_u *target_dir = NULL;
6121 char_u *au_name = NULL;
6122 int status;
6123
6124 au_name = vgr_get_auname(eap->cmdidx);
6125 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6126 curbuf->b_fname, TRUE, curbuf))
6127 {
6128#ifdef FEAT_EVAL
6129 if (aborting())
6130 return;
6131#endif
6132 }
6133
6134 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6135 if (qi == NULL)
6136 return;
6137
6138 if (vgr_process_args(eap, &args) == FAIL)
6139 goto theend;
6140
6141 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6142 && eap->cmdidx != CMD_vimgrepadd
6143 && eap->cmdidx != CMD_lvimgrepadd)
6144 || qf_stack_empty(qi))
6145 // make place for a new list
6146 qf_new_list(qi, args.qf_title);
6147
6148 incr_quickfix_busy();
6149
6150 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6151 &first_match_buf, &target_dir);
6152 if (status != OK)
6153 {
6154 FreeWild(args.fcount, args.fnames);
6155 decr_quickfix_busy();
6156 goto theend;
6157 }
6158
6159 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006160
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006161 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006162 qfl->qf_nonevalid = FALSE;
6163 qfl->qf_ptr = qfl->qf_start;
6164 qfl->qf_index = 1;
6165 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006166
Bram Moolenaar864293a2016-06-02 13:40:04 +02006167 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006168
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006169 // Remember the current quickfix list identifier, so that we can check for
6170 // autocommands changing the current quickfix list.
6171 save_qfid = qf_get_curlist(qi)->qf_id;
6172
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006173 if (au_name != NULL)
6174 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6175 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006176 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6177 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006178 if (!qflist_valid(wp, save_qfid)
6179 || qf_restore_list(qi, save_qfid) == FAIL)
6180 {
6181 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006182 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006183 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006184
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006185 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006186 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006187 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006188 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006189 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6190 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006191 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006192 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006193 semsg(_(e_nomatch2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006194
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006195 decr_quickfix_busy();
6196
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006197 // If we loaded a dummy buffer into the current window, the autocommands
6198 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006199 if (redraw_for_dummy)
6200 {
6201#ifdef FEAT_FOLDING
6202 foldUpdateAll(curwin);
6203#else
6204 redraw_later(NOT_VALID);
6205#endif
6206 }
6207
Bram Moolenaar86b68352004-12-27 21:59:20 +00006208theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006209 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006210 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006211 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006212}
6213
6214/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006215 * Restore current working directory to "dirname_start" if they differ, taking
6216 * into account whether it is set locally or globally.
6217 */
6218 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006219restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006220{
6221 char_u *dirname_now = alloc(MAXPATHL);
6222
6223 if (NULL != dirname_now)
6224 {
6225 mch_dirname(dirname_now, MAXPATHL);
6226 if (STRCMP(dirname_start, dirname_now) != 0)
6227 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006228 // If the directory has changed, change it back by building up an
6229 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006230 exarg_T ea;
6231
Bram Moolenaara80faa82020-04-12 19:37:17 +02006232 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006233 ea.arg = dirname_start;
6234 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6235 ex_cd(&ea);
6236 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006237 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006238 }
6239}
6240
6241/*
6242 * Load file "fname" into a dummy buffer and return the buffer pointer,
6243 * placing the directory resulting from the buffer load into the
6244 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6245 * prior to calling this function. Restores directory to "dirname_start" prior
6246 * to returning, if autocmds or the 'autochdir' option have changed it.
6247 *
6248 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6249 * or wipe_dummy_buffer() later!
6250 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006251 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006252 */
6253 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006254load_dummy_buffer(
6255 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006256 char_u *dirname_start, // in: old directory
6257 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006258{
6259 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006260 bufref_T newbufref;
6261 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006262 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006263 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006264 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006265
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006266 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006267 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6268 if (newbuf == NULL)
6269 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006270 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006271
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006272 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006273 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6274
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006275 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006276 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006277 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006278 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006279 ++newbuf->b_locked;
6280
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006281 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006282 aucmd_prepbuf(&aco, newbuf);
6283
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006284 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006285 (void)setfname(curbuf, fname, NULL, FALSE);
6286
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006287 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006288 check_need_swap(TRUE);
6289
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006290 // Remove the "dummy" flag, otherwise autocommands may not
6291 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006292 curbuf->b_flags &= ~BF_DUMMY;
6293
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006294 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006295 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006296 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006297 NULL, READ_NEW | READ_DUMMY);
6298 --newbuf->b_locked;
6299 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006300 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006301 && !(curbuf->b_flags & BF_NEW))
6302 {
6303 failed = FALSE;
6304 if (curbuf != newbuf)
6305 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006306 // Bloody autocommands changed the buffer! Can happen when
6307 // using netrw and editing a remote file. Use the current
6308 // buffer instead, delete the dummy one after restoring the
6309 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006310 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006311 newbuf = curbuf;
6312 }
6313 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006314
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006315 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006316 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006317 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6318 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006319
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006320 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6321 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006322 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006323 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006324
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006325 // When autocommands/'autochdir' option changed directory: go back.
6326 // Let the caller know what the resulting dir was first, in case it is
6327 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006328 mch_dirname(resulting_dir, MAXPATHL);
6329 restore_start_dir(dirname_start);
6330
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006331 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006332 return NULL;
6333 if (failed)
6334 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006335 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006336 return NULL;
6337 }
6338 return newbuf;
6339}
6340
6341/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006342 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6343 * directory to "dirname_start" prior to returning, if autocmds or the
6344 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006345 */
6346 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006347wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006348{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006349 // If any autocommand opened a window on the dummy buffer, close that
6350 // window. If we can't close them all then give up.
6351 while (buf->b_nwindows > 0)
6352 {
6353 int did_one = FALSE;
6354 win_T *wp;
6355
6356 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006357 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006358 if (wp->w_buffer == buf)
6359 {
6360 if (win_close(wp, FALSE) == OK)
6361 did_one = TRUE;
6362 break;
6363 }
6364 if (!did_one)
6365 return;
6366 }
6367
6368 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006369 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006370#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006371 cleanup_T cs;
6372
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006373 // Reset the error/interrupt/exception state here so that aborting()
6374 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6375 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006376 enter_cleanup(&cs);
6377#endif
6378
Bram Moolenaar81695252004-12-29 20:58:21 +00006379 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006380
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006381#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006382 // Restore the error/interrupt/exception state if not discarded by a
6383 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006384 leave_cleanup(&cs);
6385#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006386 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006387 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006388 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006389}
6390
6391/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006392 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6393 * directory to "dirname_start" prior to returning, if autocmds or the
6394 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006395 */
6396 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006397unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006398{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006399 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006400 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006401 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006402
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006403 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006404 restore_start_dir(dirname_start);
6405 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006406}
6407
Bram Moolenaar05159a02005-02-26 23:04:13 +00006408#if defined(FEAT_EVAL) || defined(PROTO)
6409/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006410 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006411 * to 'list'. Returns OK on success.
6412 */
6413 static int
6414get_qfline_items(qfline_T *qfp, list_T *list)
6415{
6416 int bufnum;
6417 dict_T *dict;
6418 char_u buf[2];
6419
6420 // Handle entries with a non-existing buffer number.
6421 bufnum = qfp->qf_fnum;
6422 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6423 bufnum = 0;
6424
6425 if ((dict = dict_alloc()) == NULL)
6426 return FAIL;
6427 if (list_append_dict(list, dict) == FAIL)
6428 return FAIL;
6429
6430 buf[0] = qfp->qf_type;
6431 buf[1] = NUL;
6432 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
6433 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6434 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6435 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6436 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
6437 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6438 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6439 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6440 || dict_add_string(dict, "type", buf) == FAIL
6441 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6442 return FAIL;
6443
6444 return OK;
6445}
6446
6447/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006448 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006449 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006450 * If eidx is not 0, then return only the specified entry. Otherwise return
6451 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006452 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006453 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006454get_errorlist(
6455 qf_info_T *qi_arg,
6456 win_T *wp,
6457 int qf_idx,
6458 int eidx,
6459 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006460{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006461 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006462 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006463 qfline_T *qfp;
6464 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006465
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006466 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006467 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006468 qi = &ql_info;
6469 if (wp != NULL)
6470 {
6471 qi = GET_LOC_LIST(wp);
6472 if (qi == NULL)
6473 return FAIL;
6474 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006475 }
6476
Bram Moolenaar858ba062020-05-31 23:11:59 +02006477 if (eidx < 0)
6478 return OK;
6479
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006480 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006481 qf_idx = qi->qf_curlist;
6482
Bram Moolenaar0398e002019-03-21 21:12:49 +01006483 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006484 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006485
Bram Moolenaar0398e002019-03-21 21:12:49 +01006486 qfl = qf_get_list(qi, qf_idx);
6487 if (qf_list_empty(qfl))
6488 return FAIL;
6489
Bram Moolenaara16123a2019-03-28 20:31:07 +01006490 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006491 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006492 if (eidx > 0)
6493 {
6494 if (eidx == i)
6495 return get_qfline_items(qfp, list);
6496 }
6497 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006498 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006499 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006500
Bram Moolenaar05159a02005-02-26 23:04:13 +00006501 return OK;
6502}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006503
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006504// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006505enum {
6506 QF_GETLIST_NONE = 0x0,
6507 QF_GETLIST_TITLE = 0x1,
6508 QF_GETLIST_ITEMS = 0x2,
6509 QF_GETLIST_NR = 0x4,
6510 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006511 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006512 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006513 QF_GETLIST_IDX = 0x40,
6514 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006515 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006516 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006517 QF_GETLIST_QFBUFNR = 0x400,
6518 QF_GETLIST_ALL = 0x7FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006519};
6520
6521/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006522 * Parse text from 'di' and return the quickfix list items.
6523 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006524 */
6525 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006526qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006527{
6528 int status = FAIL;
6529 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006530 char_u *errorformat = p_efm;
6531 dictitem_T *efm_di;
6532 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006533
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006534 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006535 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006536 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006537 // If errorformat is supplied then use it, otherwise use the 'efm'
6538 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006539 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6540 {
6541 if (efm_di->di_tv.v_type != VAR_STRING ||
6542 efm_di->di_tv.vval.v_string == NULL)
6543 return FAIL;
6544 errorformat = efm_di->di_tv.vval.v_string;
6545 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006546
Bram Moolenaar36538222017-09-02 19:51:44 +02006547 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006548 if (l == NULL)
6549 return FAIL;
6550
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006551 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006552 if (qi != NULL)
6553 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006554 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006555 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6556 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006557 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006558 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006559 }
6560 free(qi);
6561 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006562 dict_add_list(retdict, "items", l);
6563 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006564 }
6565
6566 return status;
6567}
6568
6569/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006570 * Return the quickfix/location list window identifier in the current tabpage.
6571 */
6572 static int
6573qf_winid(qf_info_T *qi)
6574{
6575 win_T *win;
6576
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006577 // The quickfix window can be opened even if the quickfix list is not set
6578 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006579 if (qi == NULL)
6580 return 0;
6581 win = qf_find_win(qi);
6582 if (win != NULL)
6583 return win->w_id;
6584 return 0;
6585}
6586
6587/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006588 * Returns the number of the buffer displayed in the quickfix/location list
6589 * window. If there is no buffer associated with the list, then returns 0.
6590 */
6591 static int
6592qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6593{
6594 return dict_add_number(retdict, "qfbufnr",
6595 (qi == NULL) ? 0 : qi->qf_bufnr);
6596}
6597
6598/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006599 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006600 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006601 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006602qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006603{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006604 int flags = QF_GETLIST_NONE;
6605
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006606 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006607 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006608 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006609 if (!loclist)
6610 // File window ID is applicable only to location list windows
6611 flags &= ~ QF_GETLIST_FILEWINID;
6612 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006613
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006614 if (dict_find(what, (char_u *)"title", -1) != NULL)
6615 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006616
Bram Moolenaara6d48492017-12-12 22:45:31 +01006617 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6618 flags |= QF_GETLIST_NR;
6619
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006620 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6621 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006622
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006623 if (dict_find(what, (char_u *)"context", -1) != NULL)
6624 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006625
Bram Moolenaara6d48492017-12-12 22:45:31 +01006626 if (dict_find(what, (char_u *)"id", -1) != NULL)
6627 flags |= QF_GETLIST_ID;
6628
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006629 if (dict_find(what, (char_u *)"items", -1) != NULL)
6630 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006631
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006632 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6633 flags |= QF_GETLIST_IDX;
6634
6635 if (dict_find(what, (char_u *)"size", -1) != NULL)
6636 flags |= QF_GETLIST_SIZE;
6637
Bram Moolenaarb254af32017-12-18 19:48:58 +01006638 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6639 flags |= QF_GETLIST_TICK;
6640
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006641 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6642 flags |= QF_GETLIST_FILEWINID;
6643
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006644 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6645 flags |= QF_GETLIST_QFBUFNR;
6646
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006647 return flags;
6648}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006649
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006650/*
6651 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6652 * If 'nr' and 'id' are not present in 'what' then return the current
6653 * quickfix list index.
6654 * If 'nr' is zero then return the current quickfix list index.
6655 * If 'nr' is '$' then return the last quickfix list index.
6656 * If 'id' is present then return the index of the quickfix list with that id.
6657 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6658 * Return -1, if quickfix list is not present or if the stack is empty.
6659 */
6660 static int
6661qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6662{
6663 int qf_idx;
6664 dictitem_T *di;
6665
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006666 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006667 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6668 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006669 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006670 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006671 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006672 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006673 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006674 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006675 qf_idx = di->di_tv.vval.v_number - 1;
6676 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006677 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006678 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006679 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006680 else if (di->di_tv.v_type == VAR_STRING
6681 && di->di_tv.vval.v_string != NULL
6682 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006683 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006684 qf_idx = qi->qf_listcount - 1;
6685 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006686 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006687 }
6688
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006689 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6690 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006691 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006692 if (di->di_tv.v_type == VAR_NUMBER)
6693 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006694 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006695 if (di->di_tv.vval.v_number != 0)
6696 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6697 }
6698 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006699 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006700 }
6701
6702 return qf_idx;
6703}
6704
6705/*
6706 * Return default values for quickfix list properties in retdict.
6707 */
6708 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006709qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006710{
6711 int status = OK;
6712
6713 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006714 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006715 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6716 {
6717 list_T *l = list_alloc();
6718 if (l != NULL)
6719 status = dict_add_list(retdict, "items", l);
6720 else
6721 status = FAIL;
6722 }
6723 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006724 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006725 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006726 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006727 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006728 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006729 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006730 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006731 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006732 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006733 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006734 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006735 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006736 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006737 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006738 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006739 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6740 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006741
6742 return status;
6743}
6744
6745/*
6746 * Return the quickfix list title as 'title' in retdict
6747 */
6748 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006749qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006750{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006751 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006752}
6753
6754/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006755 * Returns the identifier of the window used to display files from a location
6756 * list. If there is no associated window, then returns 0. Useful only when
6757 * called from a location list window.
6758 */
6759 static int
6760qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6761{
6762 int winid = 0;
6763
6764 if (wp != NULL && IS_LL_WINDOW(wp))
6765 {
6766 win_T *ll_wp = qf_find_win_with_loclist(qi);
6767 if (ll_wp != NULL)
6768 winid = ll_wp->w_id;
6769 }
6770
6771 return dict_add_number(retdict, "filewinid", winid);
6772}
6773
6774/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02006775 * Return the quickfix list items/entries as 'items' in retdict.
6776 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006777 */
6778 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006779qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006780{
6781 int status = OK;
6782 list_T *l = list_alloc();
6783 if (l != NULL)
6784 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006785 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006786 dict_add_list(retdict, "items", l);
6787 }
6788 else
6789 status = FAIL;
6790
6791 return status;
6792}
6793
6794/*
6795 * Return the quickfix list context (if any) as 'context' in retdict.
6796 */
6797 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006798qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006799{
6800 int status;
6801 dictitem_T *di;
6802
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006803 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006804 {
6805 di = dictitem_alloc((char_u *)"context");
6806 if (di != NULL)
6807 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006808 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006809 status = dict_add(retdict, di);
6810 if (status == FAIL)
6811 dictitem_free(di);
6812 }
6813 else
6814 status = FAIL;
6815 }
6816 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006817 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006818
6819 return status;
6820}
6821
6822/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02006823 * Return the current quickfix list index as 'idx' in retdict.
6824 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006825 */
6826 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006827qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006828{
Bram Moolenaar858ba062020-05-31 23:11:59 +02006829 if (eidx == 0)
6830 {
6831 eidx = qfl->qf_index;
6832 if (qf_list_empty(qfl))
6833 // For empty lists, current index is set to 0
6834 eidx = 0;
6835 }
6836 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006837}
6838
6839/*
6840 * Return quickfix/location list details (title) as a
6841 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6842 * then current list is used. Otherwise the specified list is used.
6843 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006844 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006845qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6846{
6847 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006848 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006849 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006850 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02006851 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006852 dictitem_T *di;
6853 int flags = QF_GETLIST_NONE;
6854
6855 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6856 return qf_get_list_from_lines(what, di, retdict);
6857
6858 if (wp != NULL)
6859 qi = GET_LOC_LIST(wp);
6860
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006861 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006862
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006863 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006864 qf_idx = qf_getprop_qfidx(qi, what);
6865
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006866 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006867 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006868 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006869
Bram Moolenaar0398e002019-03-21 21:12:49 +01006870 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006871
Bram Moolenaar858ba062020-05-31 23:11:59 +02006872 // If an entry index is specified, use that
6873 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6874 {
6875 if (di->di_tv.v_type != VAR_NUMBER)
6876 return FAIL;
6877 eidx = di->di_tv.vval.v_number;
6878 }
6879
Bram Moolenaard823fa92016-08-12 16:29:27 +02006880 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006881 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006882 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006883 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006884 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006885 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006886 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02006887 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006888 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006889 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006890 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006891 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006892 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02006893 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006894 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006895 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006896 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006897 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006898 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6899 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006900 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6901 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006902
Bram Moolenaard823fa92016-08-12 16:29:27 +02006903 return status;
6904}
6905
6906/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006907 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006908 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6909 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006910 */
6911 static int
6912qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01006913 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006914 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006915 int first_entry,
6916 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006917{
6918 static int did_bufnr_emsg;
6919 char_u *filename, *module, *pattern, *text, *type;
6920 int bufnum, valid, status, col, vcol, nr;
6921 long lnum;
6922
6923 if (first_entry)
6924 did_bufnr_emsg = FALSE;
6925
Bram Moolenaar8f667172018-12-14 15:38:31 +01006926 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6927 module = dict_get_string(d, (char_u *)"module", TRUE);
6928 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6929 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6930 col = (int)dict_get_number(d, (char_u *)"col");
6931 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6932 nr = (int)dict_get_number(d, (char_u *)"nr");
6933 type = dict_get_string(d, (char_u *)"type", TRUE);
6934 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6935 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006936 if (text == NULL)
6937 text = vim_strsave((char_u *)"");
6938
6939 valid = TRUE;
6940 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6941 valid = FALSE;
6942
6943 // Mark entries with non-existing buffer number as not valid. Give the
6944 // error message only once.
6945 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6946 {
6947 if (!did_bufnr_emsg)
6948 {
6949 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006950 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006951 }
6952 valid = FALSE;
6953 bufnum = 0;
6954 }
6955
6956 // If the 'valid' field is present it overrules the detected value.
6957 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006958 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006959
Bram Moolenaar0398e002019-03-21 21:12:49 +01006960 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006961 NULL, // dir
6962 filename,
6963 module,
6964 bufnum,
6965 text,
6966 lnum,
6967 col,
6968 vcol, // vis_col
6969 pattern, // search pattern
6970 nr,
6971 type == NULL ? NUL : *type,
6972 valid);
6973
6974 vim_free(filename);
6975 vim_free(module);
6976 vim_free(pattern);
6977 vim_free(text);
6978 vim_free(type);
6979
Bram Moolenaar9752c722018-12-22 16:49:34 +01006980 if (valid)
6981 *valid_entry = TRUE;
6982
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006983 return status;
6984}
6985
6986/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006987 * Add list of entries to quickfix/location list. Each list entry is
6988 * a dictionary with item information.
6989 */
6990 static int
6991qf_add_entries(
6992 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006993 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006994 list_T *list,
6995 char_u *title,
6996 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006997{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006998 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006999 listitem_T *li;
7000 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007001 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007002 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007003 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007004
Bram Moolenaara3921f42017-06-04 15:30:34 +02007005 if (action == ' ' || qf_idx == qi->qf_listcount)
7006 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007007 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007008 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007009 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007010 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007011 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007012 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007013 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007014 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007015 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007016 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007017 qf_free_items(qfl);
7018 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007019 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007020
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007021 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007022 {
7023 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007024 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007025
7026 d = li->li_tv.vval.v_dict;
7027 if (d == NULL)
7028 continue;
7029
Bram Moolenaar0398e002019-03-21 21:12:49 +01007030 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007031 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007032 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007033 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007034 }
7035
Bram Moolenaar9752c722018-12-22 16:49:34 +01007036 // Check if any valid error entries are added to the list.
7037 if (valid_entry)
7038 qfl->qf_nonevalid = FALSE;
7039 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007040 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007041 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007042
7043 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007044 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007045 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007046
7047 // Update the current error index if not appending to the list or if the
7048 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007049 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007050 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007051
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007052 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007053 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007054
7055 return retval;
7056}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007057
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007058/*
7059 * Get the quickfix list index from 'nr' or 'id'
7060 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007061 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007062qf_setprop_get_qfidx(
7063 qf_info_T *qi,
7064 dict_T *what,
7065 int action,
7066 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007067{
7068 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007069 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007070
Bram Moolenaard823fa92016-08-12 16:29:27 +02007071 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7072 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007073 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007074 if (di->di_tv.v_type == VAR_NUMBER)
7075 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007076 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007077 if (di->di_tv.vval.v_number != 0)
7078 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007079
Bram Moolenaar55b69262017-08-13 13:42:01 +02007080 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7081 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007082 // When creating a new list, accept qf_idx pointing to the next
7083 // non-available list and add the new list at the end of the
7084 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007085 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007086 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007087 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007088 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007089 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007090 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007091 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007092 }
7093 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007094 && di->di_tv.vval.v_string != NULL
7095 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007096 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007097 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007098 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007099 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007100 qf_idx = 0;
7101 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007102 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007103 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007104 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007105 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007106 }
7107
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007108 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007109 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007110 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007111 if (di->di_tv.v_type != VAR_NUMBER)
7112 return INVALID_QFIDX;
7113
7114 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007115 }
7116
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007117 return qf_idx;
7118}
7119
7120/*
7121 * Set the quickfix list title.
7122 */
7123 static int
7124qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7125{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007126 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007127
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007128 if (di->di_tv.v_type != VAR_STRING)
7129 return FAIL;
7130
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007131 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01007132 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007133 if (qf_idx == qi->qf_curlist)
7134 qf_update_win_titlevar(qi);
7135
7136 return OK;
7137}
7138
7139/*
7140 * Set quickfix list items/entries.
7141 */
7142 static int
7143qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7144{
7145 int retval = FAIL;
7146 char_u *title_save;
7147
7148 if (di->di_tv.v_type != VAR_LIST)
7149 return FAIL;
7150
7151 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7152 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7153 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007154 vim_free(title_save);
7155
7156 return retval;
7157}
7158
7159/*
7160 * Set quickfix list items/entries from a list of lines.
7161 */
7162 static int
7163qf_setprop_items_from_lines(
7164 qf_info_T *qi,
7165 int qf_idx,
7166 dict_T *what,
7167 dictitem_T *di,
7168 int action)
7169{
7170 char_u *errorformat = p_efm;
7171 dictitem_T *efm_di;
7172 int retval = FAIL;
7173
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007174 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007175 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7176 {
7177 if (efm_di->di_tv.v_type != VAR_STRING ||
7178 efm_di->di_tv.vval.v_string == NULL)
7179 return FAIL;
7180 errorformat = efm_di->di_tv.vval.v_string;
7181 }
7182
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007183 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007184 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7185 return FAIL;
7186
7187 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007188 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007189 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
7190 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
7191 retval = OK;
7192
7193 return retval;
7194}
7195
7196/*
7197 * Set quickfix list context.
7198 */
7199 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007200qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007201{
7202 typval_T *ctx;
7203
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007204 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007205 ctx = alloc_tv();
7206 if (ctx != NULL)
7207 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007208 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007209
7210 return OK;
7211}
7212
7213/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007214 * Set the current index in the specified quickfix list
7215 */
7216 static int
7217qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7218{
7219 int denote = FALSE;
7220 int newidx;
7221 int old_qfidx;
7222 qfline_T *qf_ptr;
7223
7224 // If the specified index is '$', then use the last entry
7225 if (di->di_tv.v_type == VAR_STRING
7226 && di->di_tv.vval.v_string != NULL
7227 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7228 newidx = qfl->qf_count;
7229 else
7230 {
7231 // Otherwise use the specified index
7232 newidx = tv_get_number_chk(&di->di_tv, &denote);
7233 if (denote)
7234 return FAIL;
7235 }
7236
7237 if (newidx < 1) // sanity check
7238 return FAIL;
7239 if (newidx > qfl->qf_count)
7240 newidx = qfl->qf_count;
7241
7242 old_qfidx = qfl->qf_index;
7243 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7244 if (qf_ptr == NULL)
7245 return FAIL;
7246 qfl->qf_ptr = qf_ptr;
7247 qfl->qf_index = newidx;
7248
7249 // If the current list is modified and it is displayed in the quickfix
7250 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007251 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007252 qf_win_pos_update(qi, old_qfidx);
7253
7254 return OK;
7255}
7256
7257/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007258 * Set the current index in the specified quickfix list
7259 */
7260 static int
7261qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7262{
7263 VIM_CLEAR(qfl->qf_qftf);
7264 if (di->di_tv.v_type == VAR_STRING
7265 && di->di_tv.vval.v_string != NULL)
7266 qfl->qf_qftf = vim_strsave(di->di_tv.vval.v_string);
7267
7268 return OK;
7269}
7270
7271/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007272 * Set quickfix/location list properties (title, items, context).
7273 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007274 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007275 */
7276 static int
7277qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7278{
7279 dictitem_T *di;
7280 int retval = FAIL;
7281 int qf_idx;
7282 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007283 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007284
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007285 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007286 newlist = TRUE;
7287
7288 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007289 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007290 return FAIL;
7291
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007292 if (newlist)
7293 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007294 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007295 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007296 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007297 }
7298
Bram Moolenaar0398e002019-03-21 21:12:49 +01007299 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007300 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007301 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007302 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007303 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007304 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007305 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007306 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007307 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007308 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7309 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007310 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7311 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007312
Bram Moolenaarb254af32017-12-18 19:48:58 +01007313 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007314 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007315
Bram Moolenaard823fa92016-08-12 16:29:27 +02007316 return retval;
7317}
7318
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007319/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007320 * Free the entire quickfix/location list stack.
7321 * If the quickfix/location list window is open, then clear it.
7322 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007323 static void
7324qf_free_stack(win_T *wp, qf_info_T *qi)
7325{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007326 win_T *qfwin = qf_find_win(qi);
7327 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007328
7329 if (qfwin != NULL)
7330 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007331 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007332 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007333 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007334 qf_update_buffer(qi, NULL);
7335 }
7336
7337 if (wp != NULL && IS_LL_WINDOW(wp))
7338 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007339 // If in the location list window, then use the non-location list
7340 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007341 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007342 if (llwin != NULL)
7343 wp = llwin;
7344 }
7345
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007346 qf_free_all(wp);
7347 if (wp == NULL)
7348 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007349 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007350 qi->qf_curlist = 0;
7351 qi->qf_listcount = 0;
7352 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007353 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007354 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007355 // If the location list window is open, then create a new empty
7356 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007357 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007358
Bram Moolenaar95946f12019-03-31 15:31:59 +02007359 if (new_ll != NULL)
7360 {
7361 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007362
Bram Moolenaar95946f12019-03-31 15:31:59 +02007363 // first free the list reference in the location list window
7364 ll_free_all(&qfwin->w_llist_ref);
7365
7366 qfwin->w_llist_ref = new_ll;
7367 if (wp != qfwin)
7368 win_set_loclist(wp, new_ll);
7369 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007370 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007371}
7372
Bram Moolenaard823fa92016-08-12 16:29:27 +02007373/*
7374 * Populate the quickfix list with the items supplied in the list
7375 * of dictionaries. "title" will be copied to w:quickfix_title.
7376 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
7377 */
7378 int
7379set_errorlist(
7380 win_T *wp,
7381 list_T *list,
7382 int action,
7383 char_u *title,
7384 dict_T *what)
7385{
7386 qf_info_T *qi = &ql_info;
7387 int retval = OK;
7388
7389 if (wp != NULL)
7390 {
7391 qi = ll_get_or_alloc_list(wp);
7392 if (qi == NULL)
7393 return FAIL;
7394 }
7395
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007396 if (action == 'f')
7397 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007398 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007399 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007400 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007401 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007402
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007403 // A dict argument cannot be specified with a non-empty list argument
7404 if (list != NULL && list->lv_len != 0 && what != NULL)
7405 {
7406 semsg(_(e_invarg2),
7407 _("cannot have both a list and a \"what\" argument"));
7408 return FAIL;
7409 }
7410
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007411 incr_quickfix_busy();
7412
7413 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007414 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007415 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007416 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007417 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007418 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007419 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007420 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007421
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007422 decr_quickfix_busy();
7423
Bram Moolenaard823fa92016-08-12 16:29:27 +02007424 return retval;
7425}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007426
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007427/*
7428 * Mark the context as in use for all the lists in a quickfix stack.
7429 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007430 static int
7431mark_quickfix_ctx(qf_info_T *qi, int copyID)
7432{
7433 int i;
7434 int abort = FALSE;
7435 typval_T *ctx;
7436
7437 for (i = 0; i < LISTCOUNT && !abort; ++i)
7438 {
7439 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007440 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7441 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007442 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
7443 }
7444
7445 return abort;
7446}
7447
7448/*
7449 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007450 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007451 */
7452 int
7453set_ref_in_quickfix(int copyID)
7454{
7455 int abort = FALSE;
7456 tabpage_T *tp;
7457 win_T *win;
7458
7459 abort = mark_quickfix_ctx(&ql_info, copyID);
7460 if (abort)
7461 return abort;
7462
7463 FOR_ALL_TAB_WINDOWS(tp, win)
7464 {
7465 if (win->w_llist != NULL)
7466 {
7467 abort = mark_quickfix_ctx(win->w_llist, copyID);
7468 if (abort)
7469 return abort;
7470 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007471 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7472 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007473 // In a location list window and none of the other windows is
7474 // referring to this location list. Mark the location list
7475 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007476 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7477 if (abort)
7478 return abort;
7479 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007480 }
7481
7482 return abort;
7483}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007484#endif
7485
Bram Moolenaar81695252004-12-29 20:58:21 +00007486/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007487 * Return the autocmd name for the :cbuffer Ex commands
7488 */
7489 static char_u *
7490cbuffer_get_auname(cmdidx_T cmdidx)
7491{
7492 switch (cmdidx)
7493 {
7494 case CMD_cbuffer: return (char_u *)"cbuffer";
7495 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7496 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7497 case CMD_lbuffer: return (char_u *)"lbuffer";
7498 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7499 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7500 default: return NULL;
7501 }
7502}
7503
7504/*
7505 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7506 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7507 */
7508 static int
7509cbuffer_process_args(
7510 exarg_T *eap,
7511 buf_T **bufp,
7512 linenr_T *line1,
7513 linenr_T *line2)
7514{
7515 buf_T *buf = NULL;
7516
7517 if (*eap->arg == NUL)
7518 buf = curbuf;
7519 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7520 buf = buflist_findnr(atoi((char *)eap->arg));
7521
7522 if (buf == NULL)
7523 {
7524 emsg(_(e_invarg));
7525 return FAIL;
7526 }
7527
7528 if (buf->b_ml.ml_mfp == NULL)
7529 {
7530 emsg(_("E681: Buffer is not loaded"));
7531 return FAIL;
7532 }
7533
7534 if (eap->addr_count == 0)
7535 {
7536 eap->line1 = 1;
7537 eap->line2 = buf->b_ml.ml_line_count;
7538 }
7539
7540 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7541 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7542 {
7543 emsg(_(e_invrange));
7544 return FAIL;
7545 }
7546
7547 *line1 = eap->line1;
7548 *line2 = eap->line2;
7549 *bufp = buf;
7550
7551 return OK;
7552}
7553
7554/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007555 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007556 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007557 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007558 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007559 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007560 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007561 */
7562 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007563ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007564{
7565 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007566 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007567 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007568 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007569 int_u save_qfid;
7570 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007571 char_u *qf_title;
7572 linenr_T line1;
7573 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007574
Bram Moolenaara16123a2019-03-28 20:31:07 +01007575 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007576 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007577 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007578 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007579#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007580 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007581 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007582#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007583 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007584
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007585 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007586 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7587 if (qi == NULL)
7588 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007589
Bram Moolenaara16123a2019-03-28 20:31:07 +01007590 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7591 return;
7592
7593 qf_title = qf_cmdtitle(*eap->cmdlinep);
7594
7595 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007596 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007597 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7598 (char *)qf_title, (char *)buf->b_sfname);
7599 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007600 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007601
7602 incr_quickfix_busy();
7603
7604 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7605 (eap->cmdidx != CMD_caddbuffer
7606 && eap->cmdidx != CMD_laddbuffer),
7607 line1, line2,
7608 qf_title, NULL);
7609 if (qf_stack_empty(qi))
7610 {
7611 decr_quickfix_busy();
7612 return;
7613 }
7614 if (res >= 0)
7615 qf_list_changed(qf_get_curlist(qi));
7616
7617 // Remember the current quickfix list identifier, so that we can
7618 // check for autocommands changing the current quickfix list.
7619 save_qfid = qf_get_curlist(qi)->qf_id;
7620 if (au_name != NULL)
7621 {
7622 buf_T *curbuf_old = curbuf;
7623
7624 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7625 TRUE, curbuf);
7626 if (curbuf != curbuf_old)
7627 // Autocommands changed buffer, don't jump now, "qi" may
7628 // be invalid.
7629 res = 0;
7630 }
7631 // Jump to the first error for a new list and if autocmds didn't
7632 // free the list.
7633 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7634 eap->cmdidx == CMD_lbuffer)
7635 && qflist_valid(wp, save_qfid))
7636 // display the first error
7637 qf_jump_first(qi, save_qfid, eap->forceit);
7638
7639 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007640}
7641
Bram Moolenaar1e015462005-09-25 22:16:38 +00007642#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007643/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007644 * Return the autocmd name for the :cexpr Ex commands.
7645 */
7646 static char_u *
7647cexpr_get_auname(cmdidx_T cmdidx)
7648{
7649 switch (cmdidx)
7650 {
7651 case CMD_cexpr: return (char_u *)"cexpr";
7652 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7653 case CMD_caddexpr: return (char_u *)"caddexpr";
7654 case CMD_lexpr: return (char_u *)"lexpr";
7655 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7656 case CMD_laddexpr: return (char_u *)"laddexpr";
7657 default: return NULL;
7658 }
7659}
7660
7661/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007662 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7663 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007664 */
7665 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007666ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007667{
7668 typval_T *tv;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007669 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007670 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007671 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007672 int_u save_qfid;
7673 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007674
Bram Moolenaara16123a2019-03-28 20:31:07 +01007675 au_name = cexpr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007676 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7677 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007678 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007679#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007680 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007681 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007682#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007683 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007684
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007685 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7686 if (qi == NULL)
7687 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007688
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007689 // Evaluate the expression. When the result is a string or a list we can
7690 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02007691 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007692 if (tv != NULL)
7693 {
7694 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7695 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7696 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007697 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007698 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00007699 (eap->cmdidx != CMD_caddexpr
7700 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007701 (linenr_T)0, (linenr_T)0,
7702 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007703 if (qf_stack_empty(qi))
7704 {
7705 decr_quickfix_busy();
7706 goto cleanup;
7707 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01007708 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007709 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007710
7711 // Remember the current quickfix list identifier, so that we can
7712 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007713 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007714 if (au_name != NULL)
7715 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7716 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007717
7718 // Jump to the first error for a new list and if autocmds didn't
7719 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02007720 if (res > 0 && (eap->cmdidx == CMD_cexpr
7721 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007722 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02007723 // display the first error
7724 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007725 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00007726 }
7727 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007728 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007729cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00007730 free_tv(tv);
7731 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007732}
Bram Moolenaar1e015462005-09-25 22:16:38 +00007733#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007734
7735/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007736 * Get the location list for ":lhelpgrep"
7737 */
7738 static qf_info_T *
7739hgr_get_ll(int *new_ll)
7740{
7741 win_T *wp;
7742 qf_info_T *qi;
7743
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007744 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007745 if (bt_help(curwin->w_buffer))
7746 wp = curwin;
7747 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007748 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007749 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007750
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007751 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007752 qi = NULL;
7753 else
7754 qi = wp->w_llist;
7755
7756 if (qi == NULL)
7757 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007758 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007759 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007760 return NULL;
7761 *new_ll = TRUE;
7762 }
7763
7764 return qi;
7765}
7766
7767/*
7768 * Search for a pattern in a help file.
7769 */
7770 static void
7771hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007772 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007773 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007774 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007775 regmatch_T *p_regmatch)
7776{
7777 FILE *fd;
7778 long lnum;
7779
7780 fd = mch_fopen((char *)fname, "r");
7781 if (fd == NULL)
7782 return;
7783
7784 lnum = 1;
7785 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7786 {
7787 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007788
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007789 // Convert a line if 'encoding' is not utf-8 and
7790 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007791 if (p_vc->vc_type != CONV_NONE
7792 && has_non_ascii(IObuff))
7793 {
7794 line = string_convert(p_vc, IObuff, NULL);
7795 if (line == NULL)
7796 line = IObuff;
7797 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007798
7799 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7800 {
7801 int l = (int)STRLEN(line);
7802
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007803 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007804 while (l > 0 && line[l - 1] <= ' ')
7805 line[--l] = NUL;
7806
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007807 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007808 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007809 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007810 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007811 0,
7812 line,
7813 lnum,
7814 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007815 + 1, // col
7816 FALSE, // vis_col
7817 NULL, // search pattern
7818 0, // nr
7819 1, // type
7820 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02007821 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007822 {
7823 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007824 if (line != IObuff)
7825 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007826 break;
7827 }
7828 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007829 if (line != IObuff)
7830 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007831 ++lnum;
7832 line_breakcheck();
7833 }
7834 fclose(fd);
7835}
7836
7837/*
7838 * Search for a pattern in all the help files in the doc directory under
7839 * the given directory.
7840 */
7841 static void
7842hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007843 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007844 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007845 regmatch_T *p_regmatch,
7846 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007847#ifdef FEAT_MULTI_LANG
7848 , char_u *lang
7849#endif
7850 )
7851{
7852 int fcount;
7853 char_u **fnames;
7854 int fi;
7855
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007856 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007857 add_pathsep(dirname);
7858 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7859 if (gen_expand_wildcards(1, &dirname, &fcount,
7860 &fnames, EW_FILE|EW_SILENT) == OK
7861 && fcount > 0)
7862 {
7863 for (fi = 0; fi < fcount && !got_int; ++fi)
7864 {
7865#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007866 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007867 if (lang != NULL
7868 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007869 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007870 && !(STRNICMP(lang, "en", 2) == 0
7871 && STRNICMP("txt", fnames[fi]
7872 + STRLEN(fnames[fi]) - 3, 3) == 0))
7873 continue;
7874#endif
7875
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007876 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007877 }
7878 FreeWild(fcount, fnames);
7879 }
7880}
7881
7882/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007883 * Search for a pattern in all the help files in the 'runtimepath'
7884 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007885 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007886 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007887 */
7888 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007889hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007890{
7891 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007892
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007893 vimconv_T vc;
7894
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007895 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7896 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007897 vc.vc_type = CONV_NONE;
7898 if (!enc_utf8)
7899 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007900
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007901 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007902 p = p_rtp;
7903 while (*p != NUL && !got_int)
7904 {
7905 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7906
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007907 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007908#ifdef FEAT_MULTI_LANG
7909 , lang
7910#endif
7911 );
7912 }
7913
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007914 if (vc.vc_type != CONV_NONE)
7915 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007916}
7917
7918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 * ":helpgrep {pattern}"
7920 */
7921 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007922ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923{
7924 regmatch_T regmatch;
7925 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007926 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007927 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007928 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007929 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
Bram Moolenaar73633f82012-01-20 13:39:07 +01007931 switch (eap->cmdidx)
7932 {
7933 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7934 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7935 default: break;
7936 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007937 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7938 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007939 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007940#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007941 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007942 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007943#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007944 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007945
Bram Moolenaar39665952018-08-15 20:59:48 +02007946 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007947 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007948 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007949 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007950 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007951 }
7952
Bram Moolenaara16123a2019-03-28 20:31:07 +01007953 // Make 'cpoptions' empty, the 'l' flag should not be used here.
7954 save_cpo = p_cpo;
7955 p_cpo = empty_option;
7956
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007957 incr_quickfix_busy();
7958
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007959#ifdef FEAT_MULTI_LANG
7960 // Check for a specified language
7961 lang = check_help_lang(eap->arg);
7962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7964 regmatch.rm_ic = FALSE;
7965 if (regmatch.regprog != NULL)
7966 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007967 qf_list_T *qfl;
7968
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007969 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007970 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007971 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007973 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007974
Bram Moolenaar473de612013-06-08 18:19:48 +02007975 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007977 qfl->qf_nonevalid = FALSE;
7978 qfl->qf_ptr = qfl->qf_start;
7979 qfl->qf_index = 1;
7980 qf_list_changed(qfl);
7981 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 }
7983
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007984 if (p_cpo == empty_option)
7985 p_cpo = save_cpo;
7986 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007987 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007988 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
Bram Moolenaar73633f82012-01-20 13:39:07 +01007990 if (au_name != NULL)
7991 {
7992 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7993 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02007994 // When adding a location list to an existing location list stack,
7995 // if the autocmd made the stack invalid, then just return.
7996 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007997 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007998 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007999 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008000 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008001 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008002
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008003 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008004 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008005 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008006 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008007 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008008
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008009 decr_quickfix_busy();
8010
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008011 if (eap->cmdidx == CMD_lhelpgrep)
8012 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008013 // If the help window is not opened or if it already points to the
8014 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008015 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008016 {
8017 if (new_qi)
8018 ll_free_all(&qi);
8019 }
8020 else if (curwin->w_llist == NULL)
8021 curwin->w_llist = qi;
8022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008024#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008025
8026#if defined(FEAT_EVAL) || defined(PROTO)
8027# ifdef FEAT_QUICKFIX
8028 static void
8029get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8030{
8031 if (what_arg->v_type == VAR_UNKNOWN)
8032 {
8033 if (rettv_list_alloc(rettv) == OK)
8034 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008035 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008036 }
8037 else
8038 {
8039 if (rettv_dict_alloc(rettv) == OK)
8040 if (is_qf || (wp != NULL))
8041 {
8042 if (what_arg->v_type == VAR_DICT)
8043 {
8044 dict_T *d = what_arg->vval.v_dict;
8045
8046 if (d != NULL)
8047 qf_get_properties(wp, d, rettv->vval.v_dict);
8048 }
8049 else
8050 emsg(_(e_dictreq));
8051 }
8052 }
8053}
8054# endif
8055
8056/*
8057 * "getloclist()" function
8058 */
8059 void
8060f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8061{
8062# ifdef FEAT_QUICKFIX
8063 win_T *wp;
8064
8065 wp = find_win_by_nr_or_id(&argvars[0]);
8066 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8067# endif
8068}
8069
8070/*
8071 * "getqflist()" function
8072 */
8073 void
8074f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8075{
8076# ifdef FEAT_QUICKFIX
8077 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8078# endif
8079}
8080
8081/*
8082 * Used by "setqflist()" and "setloclist()" functions
8083 */
8084 static void
8085set_qf_ll_list(
8086 win_T *wp UNUSED,
8087 typval_T *list_arg UNUSED,
8088 typval_T *action_arg UNUSED,
8089 typval_T *what_arg UNUSED,
8090 typval_T *rettv)
8091{
8092# ifdef FEAT_QUICKFIX
8093 static char *e_invact = N_("E927: Invalid action: '%s'");
8094 char_u *act;
8095 int action = 0;
8096 static int recursive = 0;
8097# endif
8098
8099 rettv->vval.v_number = -1;
8100
8101# ifdef FEAT_QUICKFIX
8102 if (list_arg->v_type != VAR_LIST)
8103 emsg(_(e_listreq));
8104 else if (recursive != 0)
8105 emsg(_(e_au_recursive));
8106 else
8107 {
8108 list_T *l = list_arg->vval.v_list;
8109 dict_T *d = NULL;
8110 int valid_dict = TRUE;
8111
8112 if (action_arg->v_type == VAR_STRING)
8113 {
8114 act = tv_get_string_chk(action_arg);
8115 if (act == NULL)
8116 return; // type error; errmsg already given
8117 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8118 act[1] == NUL)
8119 action = *act;
8120 else
8121 semsg(_(e_invact), act);
8122 }
8123 else if (action_arg->v_type == VAR_UNKNOWN)
8124 action = ' ';
8125 else
8126 emsg(_(e_stringreq));
8127
8128 if (action_arg->v_type != VAR_UNKNOWN
8129 && what_arg->v_type != VAR_UNKNOWN)
8130 {
8131 if (what_arg->v_type == VAR_DICT)
8132 d = what_arg->vval.v_dict;
8133 else
8134 {
8135 emsg(_(e_dictreq));
8136 valid_dict = FALSE;
8137 }
8138 }
8139
8140 ++recursive;
8141 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
8142 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
8143 d) == OK)
8144 rettv->vval.v_number = 0;
8145 --recursive;
8146 }
8147# endif
8148}
8149
8150/*
8151 * "setloclist()" function
8152 */
8153 void
8154f_setloclist(typval_T *argvars, typval_T *rettv)
8155{
8156 win_T *win;
8157
8158 rettv->vval.v_number = -1;
8159
8160 win = find_win_by_nr_or_id(&argvars[0]);
8161 if (win != NULL)
8162 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8163}
8164
8165/*
8166 * "setqflist()" function
8167 */
8168 void
8169f_setqflist(typval_T *argvars, typval_T *rettv)
8170{
8171 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8172}
8173#endif