blob: acd28a14351d5f44b31cf192c68245b10811c82b [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
136 // 'C' continuation line
137 // 'Z' end of multi-line message
138 // 'G' general, unspecific message
139 // 'P' push file (partial) message
140 // 'Q' pop/quit file (partial) message
141 // 'O' overread (partial) message
142 char_u flags; // additional flags given in prefix
143 // '-' do not include this line
144 // '+' include whole line in message
145 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146};
147
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200148// List of location lists to be deleted.
149// Used to delay the deletion of locations lists by autocmds.
150typedef struct qf_delq_S
151{
152 struct qf_delq_S *next;
153 qf_info_T *qi;
154} qf_delq_T;
155static qf_delq_T *qf_delq_head = NULL;
156
157// Counter to prevent autocmds from freeing up location lists when they are
158// still being used.
159static int quickfix_busy = 0;
160
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200161static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100162
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100164static 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 +0200165static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100166static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100167static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200168static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100169static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200170static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200171static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100172static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static win_T *qf_find_win(qf_info_T *qi);
175static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200176static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +0100177static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100178static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
179static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
180static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
181static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar3ff33112019-05-03 21:56:35 +0200182static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200184// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000185#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200186// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000187#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200188
189// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100190#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
191#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
192#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
193#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200194
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000195/*
196 * Return location list for window 'wp'
197 * For location list window, return the referenced location list
198 */
199#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
200
Bram Moolenaar95946f12019-03-31 15:31:59 +0200201// Macro to loop through all the items in a quickfix list
202// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100203#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
Bram Moolenaar95946f12019-03-31 15:31:59 +0200204 for (i = 1, qfp = qfl->qf_start; \
205 !got_int && i <= qfl->qf_count && qfp != NULL; \
Bram Moolenaara16123a2019-03-28 20:31:07 +0100206 ++i, qfp = qfp->qf_next)
207
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200209 * Looking up a buffer can be slow if there are many. Remember the last one
210 * to make this a lot faster if there are multiple matches in the same file.
211 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200212static char_u *qf_last_bufname = NULL;
213static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200214
Bram Moolenaar3c097222017-12-21 20:54:49 +0100215static char *e_loc_list_changed =
216 N_("E926: Current location list was changed");
217
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200218/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200219 * Maximum number of bytes allowed per line while reading a errorfile.
220 */
221#define LINE_MAXLEN 4096
222
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200223static struct fmtpattern
224{
225 char_u convchar;
226 char *pattern;
227} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200228 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200229 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200230 {'n', "\\d\\+"},
231 {'l', "\\d\\+"},
232 {'c', "\\d\\+"},
233 {'t', "."},
234 {'m', ".\\+"},
235 {'r', ".*"},
236 {'p', "[- .]*"},
237 {'v', "\\d\\+"},
238 {'s', ".\\+"},
239 {'o', ".\\+"}
240 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200241
242/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200243 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200244 * See fmt_pat definition above for the list of supported patterns. The
245 * pattern specifier is supplied in "efmpat". The converted pattern is stored
246 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200247 */
248 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200249efmpat_to_regpat(
250 char_u *efmpat,
251 char_u *regpat,
252 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200253 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100254 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200255{
256 char_u *srcptr;
257
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200258 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200259 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200260 // Each errorformat pattern can occur only once
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100261 semsg(_("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200262 return NULL;
263 }
264 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200265 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200266 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200267 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200268 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100269 semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200270 return NULL;
271 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200272 efminfo->addr[idx] = (char_u)++round;
273 *regpat++ = '\\';
274 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200275#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200276 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200277 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200278 // Also match "c:" in the file name, even when
279 // checking for a colon next: "%f:".
280 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200281 STRCPY(regpat, "\\%(\\a:\\)\\=");
282 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200283 }
284#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200285 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200286 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200287 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200288 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200289 // A file name may contain spaces, but this isn't
290 // in "\f". For "%f:%l:%m" there may be a ":" in
291 // the file name. Use ".\{-1,}x" instead (x is
292 // the next character), the requirement that :999:
293 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200294 STRCPY(regpat, ".\\{-1,}");
295 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200296 }
297 else
298 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200299 // File name followed by '\\' or '%': include as
300 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200301 STRCPY(regpat, "\\f\\+");
302 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200303 }
304 }
305 else
306 {
307 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200308 while ((*regpat = *srcptr++) != NUL)
309 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200310 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200311 *regpat++ = '\\';
312 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200313
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200314 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200315}
316
317/*
318 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200319 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200320 */
321 static char_u *
322scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200323 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200324 char_u *efm,
325 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100326 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200327{
328 char_u *efmp = *pefmp;
329
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200330 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200331 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200332 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200333 {
334 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200335 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200336 if (efmp < efm + len)
337 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200338 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200339 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200340 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200341 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200342 if (efmp == efm + len)
343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100344 emsg(_("E374: Missing ] in format string"));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200345 return NULL;
346 }
347 }
348 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200349 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200350 *regpat++ = *++efmp;
351 *regpat++ = '\\';
352 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200353 }
354 else
355 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200356 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100357 semsg(_("E375: Unsupported %%%c in format string"), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200358 return NULL;
359 }
360
361 *pefmp = efmp;
362
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200363 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364}
365
366/*
367 * Analyze/parse an errorformat prefix.
368 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200369 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100370efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200371{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200372 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200373 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200374 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200375 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200376 else
377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100378 semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200379 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200380 }
381
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200382 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200383}
384
385/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200386 * Converts a 'errorformat' string part in 'efm' to a regular expression
387 * pattern. The resulting regex pattern is returned in "regpat". Additional
388 * information about the 'erroformat' pattern is returned in "fmt_ptr".
389 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200390 */
391 static int
392efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200393 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200394 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200395 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100396 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200397{
398 char_u *ptr;
399 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200400 int round;
401 int idx = 0;
402
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200403 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200404 ptr = regpat;
405 *ptr++ = '^';
406 round = 0;
407 for (efmp = efm; efmp < efm + len; ++efmp)
408 {
409 if (*efmp == '%')
410 {
411 ++efmp;
412 for (idx = 0; idx < FMT_PATTERNS; ++idx)
413 if (fmt_pat[idx].convchar == *efmp)
414 break;
415 if (idx < FMT_PATTERNS)
416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100417 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200418 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200419 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200420 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200421 }
422 else if (*efmp == '*')
423 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200424 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100425 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200426 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200427 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200428 }
429 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200430 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200431 else if (*efmp == '#')
432 *ptr++ = '*';
433 else if (*efmp == '>')
434 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200435 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200436 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200437 // prefix is allowed only at the beginning of the errorformat
438 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100439 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200440 if (efmp == NULL)
441 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200442 }
443 else
444 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100445 semsg(_("E377: Invalid %%%c in format string"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200446 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200447 }
448 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200449 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200450 {
451 if (*efmp == '\\' && efmp + 1 < efm + len)
452 ++efmp;
453 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200454 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200455 if (*efmp)
456 *ptr++ = *efmp;
457 }
458 }
459 *ptr++ = '$';
460 *ptr = NUL;
461
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200462 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200463}
464
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200465/*
466 * Free the 'errorformat' information list
467 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200468 static void
469free_efm_list(efm_T **efm_first)
470{
471 efm_T *efm_ptr;
472
473 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
474 {
475 *efm_first = efm_ptr->next;
476 vim_regfree(efm_ptr->prog);
477 vim_free(efm_ptr);
478 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100479 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200480}
481
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200482/*
483 * Compute the size of the buffer used to convert a 'errorformat' pattern into
484 * a regular expression pattern.
485 */
486 static int
487efm_regpat_bufsz(char_u *efm)
488{
489 int sz;
490 int i;
491
492 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
493 for (i = FMT_PATTERNS; i > 0; )
494 sz += (int)STRLEN(fmt_pat[--i].pattern);
495#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200496 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200497#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200498 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200499#endif
500
501 return sz;
502}
503
504/*
505 * Return the length of a 'errorformat' option part (separated by ",").
506 */
507 static int
508efm_option_part_len(char_u *efm)
509{
510 int len;
511
512 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
513 if (efm[len] == '\\' && efm[len + 1] != NUL)
514 ++len;
515
516 return len;
517}
518
519/*
520 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
521 * are parsed and converted to regular expressions. Returns information about
522 * the parsed 'errorformat' option.
523 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200524 static efm_T *
525parse_efm_option(char_u *efm)
526{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200527 efm_T *fmt_ptr = NULL;
528 efm_T *fmt_first = NULL;
529 efm_T *fmt_last = NULL;
530 char_u *fmtstr = NULL;
531 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200532 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200533
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200534 // Each part of the format string is copied and modified from errorformat
535 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200536
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200537 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200538 sz = efm_regpat_bufsz(efm);
539 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200540 goto parse_efm_error;
541
542 while (efm[0] != NUL)
543 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200544 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200545 fmt_ptr = ALLOC_CLEAR_ONE(efm_T);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200546 if (fmt_ptr == NULL)
547 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200548 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200549 fmt_first = fmt_ptr;
550 else
551 fmt_last->next = fmt_ptr;
552 fmt_last = fmt_ptr;
553
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200554 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200555 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200556
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100557 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200558 goto parse_efm_error;
559 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
560 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200561 // Advance to next part
562 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200563 }
564
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200565 if (fmt_first == NULL) // nothing found
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100566 emsg(_("E378: 'errorformat' contains no pattern"));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200567
568 goto parse_efm_end;
569
570parse_efm_error:
571 free_efm_list(&fmt_first);
572
573parse_efm_end:
574 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200575
576 return fmt_first;
577}
578
Bram Moolenaare0d37972016-07-15 22:36:01 +0200579enum {
580 QF_FAIL = 0,
581 QF_OK = 1,
582 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200583 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200584 QF_IGNORE_LINE = 4,
585 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200586};
587
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200588/*
589 * State information used to parse lines and add entries to a quickfix/location
590 * list.
591 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200592typedef struct {
593 char_u *linebuf;
594 int linelen;
595 char_u *growbuf;
596 int growbufsiz;
597 FILE *fd;
598 typval_T *tv;
599 char_u *p_str;
600 listitem_T *p_li;
601 buf_T *buf;
602 linenr_T buflnum;
603 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100604 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200605} qfstate_T;
606
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200607/*
608 * Allocate more memory for the line buffer used for parsing lines.
609 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200610 static char_u *
611qf_grow_linebuf(qfstate_T *state, int newsz)
612{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200613 char_u *p;
614
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200615 // If the line exceeds LINE_MAXLEN exclude the last
616 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200617 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
618 if (state->growbuf == NULL)
619 {
620 state->growbuf = alloc(state->linelen + 1);
621 if (state->growbuf == NULL)
622 return NULL;
623 state->growbufsiz = state->linelen;
624 }
625 else if (state->linelen > state->growbufsiz)
626 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200627 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200628 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200629 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200630 state->growbufsiz = state->linelen;
631 }
632 return state->growbuf;
633}
634
635/*
636 * Get the next string (separated by newline) from state->p_str.
637 */
638 static int
639qf_get_next_str_line(qfstate_T *state)
640{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200641 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200642 char_u *p_str = state->p_str;
643 char_u *p;
644 int len;
645
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200646 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200647 return QF_END_OF_INPUT;
648
649 p = vim_strchr(p_str, '\n');
650 if (p != NULL)
651 len = (int)(p - p_str) + 1;
652 else
653 len = (int)STRLEN(p_str);
654
655 if (len > IOSIZE - 2)
656 {
657 state->linebuf = qf_grow_linebuf(state, len);
658 if (state->linebuf == NULL)
659 return QF_NOMEM;
660 }
661 else
662 {
663 state->linebuf = IObuff;
664 state->linelen = len;
665 }
666 vim_strncpy(state->linebuf, p_str, state->linelen);
667
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200668 // Increment using len in order to discard the rest of the
669 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200670 p_str += len;
671 state->p_str = p_str;
672
673 return QF_OK;
674}
675
676/*
677 * Get the next string from state->p_Li.
678 */
679 static int
680qf_get_next_list_line(qfstate_T *state)
681{
682 listitem_T *p_li = state->p_li;
683 int len;
684
685 while (p_li != NULL
686 && (p_li->li_tv.v_type != VAR_STRING
687 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200688 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200689
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200690 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200691 {
692 state->p_li = NULL;
693 return QF_END_OF_INPUT;
694 }
695
696 len = (int)STRLEN(p_li->li_tv.vval.v_string);
697 if (len > IOSIZE - 2)
698 {
699 state->linebuf = qf_grow_linebuf(state, len);
700 if (state->linebuf == NULL)
701 return QF_NOMEM;
702 }
703 else
704 {
705 state->linebuf = IObuff;
706 state->linelen = len;
707 }
708
709 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
710
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200711 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200712 return QF_OK;
713}
714
715/*
716 * Get the next string from state->buf.
717 */
718 static int
719qf_get_next_buf_line(qfstate_T *state)
720{
721 char_u *p_buf = NULL;
722 int len;
723
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200724 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200725 if (state->buflnum > state->lnumlast)
726 return QF_END_OF_INPUT;
727
728 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
729 state->buflnum += 1;
730
731 len = (int)STRLEN(p_buf);
732 if (len > IOSIZE - 2)
733 {
734 state->linebuf = qf_grow_linebuf(state, len);
735 if (state->linebuf == NULL)
736 return QF_NOMEM;
737 }
738 else
739 {
740 state->linebuf = IObuff;
741 state->linelen = len;
742 }
743 vim_strncpy(state->linebuf, p_buf, state->linelen);
744
745 return QF_OK;
746}
747
748/*
749 * Get the next string from file state->fd.
750 */
751 static int
752qf_get_next_file_line(qfstate_T *state)
753{
754 int discard;
755 int growbuflen;
756
757 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
758 return QF_END_OF_INPUT;
759
760 discard = FALSE;
761 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200762 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200763 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200764 // The current line exceeds IObuff, continue reading using
765 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200766 if (state->growbuf == NULL)
767 {
768 state->growbufsiz = 2 * (IOSIZE - 1);
769 state->growbuf = alloc(state->growbufsiz);
770 if (state->growbuf == NULL)
771 return QF_NOMEM;
772 }
773
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200774 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200775 memcpy(state->growbuf, IObuff, IOSIZE - 1);
776 growbuflen = state->linelen;
777
778 for (;;)
779 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200780 char_u *p;
781
Bram Moolenaare0d37972016-07-15 22:36:01 +0200782 if (fgets((char *)state->growbuf + growbuflen,
783 state->growbufsiz - growbuflen, state->fd) == NULL)
784 break;
785 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
786 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200787 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200788 break;
789 if (state->growbufsiz == LINE_MAXLEN)
790 {
791 discard = TRUE;
792 break;
793 }
794
795 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
796 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200797 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200798 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200799 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200800 }
801
802 while (discard)
803 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200804 // The current line is longer than LINE_MAXLEN, continue
805 // reading but discard everything until EOL or EOF is
806 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200807 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
808 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200809 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200810 break;
811 }
812
813 state->linebuf = state->growbuf;
814 state->linelen = growbuflen;
815 }
816 else
817 state->linebuf = IObuff;
818
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200819 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200820 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
821 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100822 char_u *line;
823
824 line = string_convert(&state->vc, state->linebuf, &state->linelen);
825 if (line != NULL)
826 {
827 if (state->linelen < IOSIZE)
828 {
829 STRCPY(state->linebuf, line);
830 vim_free(line);
831 }
832 else
833 {
834 vim_free(state->growbuf);
835 state->linebuf = state->growbuf = line;
836 state->growbufsiz = state->linelen < LINE_MAXLEN
837 ? state->linelen : LINE_MAXLEN;
838 }
839 }
840 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100841
Bram Moolenaare0d37972016-07-15 22:36:01 +0200842 return QF_OK;
843}
844
845/*
846 * Get the next string from a file/buffer/list/string.
847 */
848 static int
849qf_get_nextline(qfstate_T *state)
850{
851 int status = QF_FAIL;
852
853 if (state->fd == NULL)
854 {
855 if (state->tv != NULL)
856 {
857 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200858 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200859 status = qf_get_next_str_line(state);
860 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200861 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200862 status = qf_get_next_list_line(state);
863 }
864 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200865 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200866 status = qf_get_next_buf_line(state);
867 }
868 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200869 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200870 status = qf_get_next_file_line(state);
871
872 if (status != QF_OK)
873 return status;
874
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200875 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200876 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200877 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200878 state->linebuf[state->linelen - 1] = NUL;
879#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200880 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
881 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200882#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200883 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200884
Bram Moolenaare0d37972016-07-15 22:36:01 +0200885 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200886
887 return QF_OK;
888}
889
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200890typedef struct {
891 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200892 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200893 char_u *errmsg;
894 int errmsglen;
895 long lnum;
896 int col;
897 char_u use_viscol;
898 char_u *pattern;
899 int enr;
900 int type;
901 int valid;
902} qffields_T;
903
904/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200905 * Parse the match for filename ('%f') pattern in regmatch.
906 * Return the matched value in "fields->namebuf".
907 */
908 static int
909qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
910{
911 int c;
912
913 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
914 return QF_FAIL;
915
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200916 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200917 c = *rmp->endp[midx];
918 *rmp->endp[midx] = NUL;
919 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
920 *rmp->endp[midx] = c;
921
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200922 // For separate filename patterns (%O, %P and %Q), the specified file
923 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200924 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
925 && mch_getperm(fields->namebuf) == -1)
926 return QF_FAIL;
927
928 return QF_OK;
929}
930
931/*
932 * Parse the match for error number ('%n') pattern in regmatch.
933 * Return the matched value in "fields->enr".
934 */
935 static int
936qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
937{
938 if (rmp->startp[midx] == NULL)
939 return QF_FAIL;
940 fields->enr = (int)atol((char *)rmp->startp[midx]);
941 return QF_OK;
942}
943
944/*
945 * Parse the match for line number (%l') pattern in regmatch.
946 * Return the matched value in "fields->lnum".
947 */
948 static int
949qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
950{
951 if (rmp->startp[midx] == NULL)
952 return QF_FAIL;
953 fields->lnum = atol((char *)rmp->startp[midx]);
954 return QF_OK;
955}
956
957/*
958 * Parse the match for column number ('%c') pattern in regmatch.
959 * Return the matched value in "fields->col".
960 */
961 static int
962qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
963{
964 if (rmp->startp[midx] == NULL)
965 return QF_FAIL;
966 fields->col = (int)atol((char *)rmp->startp[midx]);
967 return QF_OK;
968}
969
970/*
971 * Parse the match for error type ('%t') pattern in regmatch.
972 * Return the matched value in "fields->type".
973 */
974 static int
975qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
976{
977 if (rmp->startp[midx] == NULL)
978 return QF_FAIL;
979 fields->type = *rmp->startp[midx];
980 return QF_OK;
981}
982
983/*
Bram Moolenaarf4140482020-02-15 23:06:45 +0100984 * Copy a non-error line into the error string. Return the matched line in
985 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200986 */
987 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +0100988copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200989{
990 char_u *p;
991
992 if (linelen >= fields->errmsglen)
993 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200994 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200995 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
996 return QF_NOMEM;
997 fields->errmsg = p;
998 fields->errmsglen = linelen + 1;
999 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001000 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001001 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001002
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001003 return QF_OK;
1004}
1005
1006/*
1007 * Parse the match for error message ('%m') pattern in regmatch.
1008 * Return the matched value in "fields->errmsg".
1009 */
1010 static int
1011qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1012{
1013 char_u *p;
1014 int len;
1015
1016 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1017 return QF_FAIL;
1018 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1019 if (len >= fields->errmsglen)
1020 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001021 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001022 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1023 return QF_NOMEM;
1024 fields->errmsg = p;
1025 fields->errmsglen = len + 1;
1026 }
1027 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1028 return QF_OK;
1029}
1030
1031/*
1032 * Parse the match for rest of a single-line file message ('%r') pattern.
1033 * Return the matched value in "tail".
1034 */
1035 static int
1036qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1037{
1038 if (rmp->startp[midx] == NULL)
1039 return QF_FAIL;
1040 *tail = rmp->startp[midx];
1041 return QF_OK;
1042}
1043
1044/*
1045 * Parse the match for the pointer line ('%p') pattern in regmatch.
1046 * Return the matched value in "fields->col".
1047 */
1048 static int
1049qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1050{
1051 char_u *match_ptr;
1052
1053 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1054 return QF_FAIL;
1055 fields->col = 0;
1056 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1057 ++match_ptr)
1058 {
1059 ++fields->col;
1060 if (*match_ptr == TAB)
1061 {
1062 fields->col += 7;
1063 fields->col -= fields->col % 8;
1064 }
1065 }
1066 ++fields->col;
1067 fields->use_viscol = TRUE;
1068 return QF_OK;
1069}
1070
1071/*
1072 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1073 * Return the matched value in "fields->col".
1074 */
1075 static int
1076qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1077{
1078 if (rmp->startp[midx] == NULL)
1079 return QF_FAIL;
1080 fields->col = (int)atol((char *)rmp->startp[midx]);
1081 fields->use_viscol = TRUE;
1082 return QF_OK;
1083}
1084
1085/*
1086 * Parse the match for the search text ('%s') pattern in regmatch.
1087 * Return the matched value in "fields->pattern".
1088 */
1089 static int
1090qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1091{
1092 int len;
1093
1094 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1095 return QF_FAIL;
1096 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1097 if (len > CMDBUFFSIZE - 5)
1098 len = CMDBUFFSIZE - 5;
1099 STRCPY(fields->pattern, "^\\V");
1100 STRNCAT(fields->pattern, rmp->startp[midx], len);
1101 fields->pattern[len + 3] = '\\';
1102 fields->pattern[len + 4] = '$';
1103 fields->pattern[len + 5] = NUL;
1104 return QF_OK;
1105}
1106
1107/*
1108 * Parse the match for the module ('%o') pattern in regmatch.
1109 * Return the matched value in "fields->module".
1110 */
1111 static int
1112qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1113{
1114 int len;
1115
1116 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1117 return QF_FAIL;
1118 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1119 if (len > CMDBUFFSIZE)
1120 len = CMDBUFFSIZE;
1121 STRNCAT(fields->module, rmp->startp[midx], len);
1122 return QF_OK;
1123}
1124
1125/*
1126 * 'errorformat' format pattern parser functions.
1127 * The '%f' and '%r' formats are parsed differently from other formats.
1128 * See qf_parse_match() for details.
1129 */
1130static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1131{
1132 NULL,
1133 qf_parse_fmt_n,
1134 qf_parse_fmt_l,
1135 qf_parse_fmt_c,
1136 qf_parse_fmt_t,
1137 qf_parse_fmt_m,
1138 NULL,
1139 qf_parse_fmt_p,
1140 qf_parse_fmt_v,
1141 qf_parse_fmt_s,
1142 qf_parse_fmt_o
1143};
1144
1145/*
1146 * Parse the error format pattern matches in "regmatch" and set the values in
1147 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1148 * match. Returns QF_OK if all the matches are successfully parsed. On
1149 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001150 */
1151 static int
1152qf_parse_match(
1153 char_u *linebuf,
1154 int linelen,
1155 efm_T *fmt_ptr,
1156 regmatch_T *regmatch,
1157 qffields_T *fields,
1158 int qf_multiline,
1159 int qf_multiscan,
1160 char_u **tail)
1161{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001162 int idx = fmt_ptr->prefix;
1163 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001164 int midx;
1165 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001166
1167 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1168 return QF_FAIL;
1169 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1170 fields->type = idx;
1171 else
1172 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001173
1174 // Extract error message data from matched line.
1175 // We check for an actual submatch, because "\[" and "\]" in
1176 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001177 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001178 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001179 status = QF_OK;
1180 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001181 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001182 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1183 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001184 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001185 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001186 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001187 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001188 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001189 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001190 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001191 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001192 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001193 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001194
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001195 if (status != QF_OK)
1196 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001197 }
1198
1199 return QF_OK;
1200}
1201
1202/*
1203 * Parse an error line in 'linebuf' using a single error format string in
1204 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1205 * Returns QF_OK if the efm format matches completely and the fields are
1206 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1207 */
1208 static int
1209qf_parse_get_fields(
1210 char_u *linebuf,
1211 int linelen,
1212 efm_T *fmt_ptr,
1213 qffields_T *fields,
1214 int qf_multiline,
1215 int qf_multiscan,
1216 char_u **tail)
1217{
1218 regmatch_T regmatch;
1219 int status = QF_FAIL;
1220 int r;
1221
1222 if (qf_multiscan &&
1223 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1224 return QF_FAIL;
1225
1226 fields->namebuf[0] = NUL;
1227 fields->module[0] = NUL;
1228 fields->pattern[0] = NUL;
1229 if (!qf_multiscan)
1230 fields->errmsg[0] = NUL;
1231 fields->lnum = 0;
1232 fields->col = 0;
1233 fields->use_viscol = FALSE;
1234 fields->enr = -1;
1235 fields->type = 0;
1236 *tail = NULL;
1237
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001238 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001239 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001240 regmatch.regprog = fmt_ptr->prog;
1241 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1242 fmt_ptr->prog = regmatch.regprog;
1243 if (r)
1244 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1245 fields, qf_multiline, qf_multiscan, tail);
1246
1247 return status;
1248}
1249
1250/*
1251 * Parse directory error format prefixes (%D and %X).
1252 * Push and pop directories from the directory stack when scanning directory
1253 * names.
1254 */
1255 static int
1256qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1257{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001258 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001259 {
1260 if (*fields->namebuf == NUL)
1261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001262 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001263 return QF_FAIL;
1264 }
1265 qfl->qf_directory =
1266 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1267 if (qfl->qf_directory == NULL)
1268 return QF_FAIL;
1269 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001270 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001271 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1272
1273 return QF_OK;
1274}
1275
1276/*
1277 * Parse global file name error format prefixes (%O, %P and %Q).
1278 */
1279 static int
1280qf_parse_file_pfx(
1281 int idx,
1282 qffields_T *fields,
1283 qf_list_T *qfl,
1284 char_u *tail)
1285{
1286 fields->valid = FALSE;
1287 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1288 {
1289 if (*fields->namebuf && idx == 'P')
1290 qfl->qf_currfile =
1291 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1292 else if (idx == 'Q')
1293 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1294 *fields->namebuf = NUL;
1295 if (tail && *tail)
1296 {
1297 STRMOVE(IObuff, skipwhite(tail));
1298 qfl->qf_multiscan = TRUE;
1299 return QF_MULTISCAN;
1300 }
1301 }
1302
1303 return QF_OK;
1304}
1305
1306/*
1307 * Parse a non-error line (a line which doesn't match any of the error
1308 * format in 'efm').
1309 */
1310 static int
1311qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1312{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001313 fields->namebuf[0] = NUL; // no match found, remove file name
1314 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001315 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001316
Bram Moolenaarf4140482020-02-15 23:06:45 +01001317 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001318}
1319
1320/*
1321 * Parse multi-line error format prefixes (%C and %Z)
1322 */
1323 static int
1324qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001325 int idx,
1326 qf_list_T *qfl,
1327 qffields_T *fields)
1328{
1329 char_u *ptr;
1330 int len;
1331
1332 if (!qfl->qf_multiignore)
1333 {
1334 qfline_T *qfprev = qfl->qf_last;
1335
1336 if (qfprev == NULL)
1337 return QF_FAIL;
1338 if (*fields->errmsg && !qfl->qf_multiignore)
1339 {
1340 len = (int)STRLEN(qfprev->qf_text);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001341 if ((ptr = alloc(len + STRLEN(fields->errmsg) + 2))
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001342 == NULL)
1343 return QF_FAIL;
1344 STRCPY(ptr, qfprev->qf_text);
1345 vim_free(qfprev->qf_text);
1346 qfprev->qf_text = ptr;
1347 *(ptr += len) = '\n';
1348 STRCPY(++ptr, fields->errmsg);
1349 }
1350 if (qfprev->qf_nr == -1)
1351 qfprev->qf_nr = fields->enr;
1352 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001353 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001354 qfprev->qf_type = fields->type;
1355
1356 if (!qfprev->qf_lnum)
1357 qfprev->qf_lnum = fields->lnum;
1358 if (!qfprev->qf_col)
1359 qfprev->qf_col = fields->col;
1360 qfprev->qf_viscol = fields->use_viscol;
1361 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001362 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001363 qfl->qf_directory,
1364 *fields->namebuf || qfl->qf_directory != NULL
1365 ? fields->namebuf
1366 : qfl->qf_currfile != NULL && fields->valid
1367 ? qfl->qf_currfile : 0);
1368 }
1369 if (idx == 'Z')
1370 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1371 line_breakcheck();
1372
1373 return QF_IGNORE_LINE;
1374}
1375
1376/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001377 * Parse a line and get the quickfix fields.
1378 * Return the QF_ status.
1379 */
1380 static int
1381qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001382 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001383 char_u *linebuf,
1384 int linelen,
1385 efm_T *fmt_first,
1386 qffields_T *fields)
1387{
1388 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001389 int idx = 0;
1390 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001391 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001392
Bram Moolenaare333e792018-04-08 13:27:39 +02001393restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001394 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001395 if (fmt_start == NULL)
1396 fmt_ptr = fmt_first;
1397 else
1398 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001399 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001400 fmt_ptr = fmt_start;
1401 fmt_start = NULL;
1402 }
1403
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001404 // Try to match each part of 'errorformat' until we find a complete
1405 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001406 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001407 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1408 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001409 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001410 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1411 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1412 if (status == QF_NOMEM)
1413 return status;
1414 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001415 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001416 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001417 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001418
1419 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1420 {
1421 if (fmt_ptr != NULL)
1422 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001423 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001424 status = qf_parse_dir_pfx(idx, fields, qfl);
1425 if (status != QF_OK)
1426 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001427 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001428
1429 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1430 if (status != QF_OK)
1431 return status;
1432
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001433 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001434 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001435 }
1436 else if (fmt_ptr != NULL)
1437 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001438 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001439 if (fmt_ptr->conthere)
1440 fmt_start = fmt_ptr;
1441
1442 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1443 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001444 qfl->qf_multiline = TRUE; // start of a multi-line message
1445 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001446 }
1447 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001448 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001449 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001450 if (status != QF_OK)
1451 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001452 }
1453 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001454 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001455 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1456 if (status == QF_MULTISCAN)
1457 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001458 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001459 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001461 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001462 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001463 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001464 return QF_IGNORE_LINE;
1465 }
1466 }
1467
1468 return QF_OK;
1469}
1470
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001471/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001472 * Returns TRUE if the specified quickfix/location stack is empty
1473 */
1474 static int
1475qf_stack_empty(qf_info_T *qi)
1476{
1477 return qi == NULL || qi->qf_listcount <= 0;
1478}
1479
1480/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001481 * Returns TRUE if the specified quickfix/location list is empty.
1482 */
1483 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001484qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001485{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001486 return qfl == NULL || qfl->qf_count <= 0;
1487}
1488
1489/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001490 * Returns TRUE if the specified quickfix/location list is not empty and
1491 * has valid entries.
1492 */
1493 static int
1494qf_list_has_valid_entries(qf_list_T *qfl)
1495{
1496 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1497}
1498
1499/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001500 * Return a pointer to a list in the specified quickfix stack
1501 */
1502 static qf_list_T *
1503qf_get_list(qf_info_T *qi, int idx)
1504{
1505 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001506}
1507
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001508/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001509 * Allocate the fields used for parsing lines and populating a quickfix list.
1510 */
1511 static int
1512qf_alloc_fields(qffields_T *pfields)
1513{
1514 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1515 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1516 pfields->errmsglen = CMDBUFFSIZE + 1;
1517 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1518 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1519 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1520 || pfields->pattern == NULL || pfields->module == NULL)
1521 return FAIL;
1522
1523 return OK;
1524}
1525
1526/*
1527 * Free the fields used for parsing lines and populating a quickfix list.
1528 */
1529 static void
1530qf_free_fields(qffields_T *pfields)
1531{
1532 vim_free(pfields->namebuf);
1533 vim_free(pfields->module);
1534 vim_free(pfields->errmsg);
1535 vim_free(pfields->pattern);
1536}
1537
1538/*
1539 * Setup the state information used for parsing lines and populating a
1540 * quickfix list.
1541 */
1542 static int
1543qf_setup_state(
1544 qfstate_T *pstate,
1545 char_u *enc,
1546 char_u *efile,
1547 typval_T *tv,
1548 buf_T *buf,
1549 linenr_T lnumfirst,
1550 linenr_T lnumlast)
1551{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001552 pstate->vc.vc_type = CONV_NONE;
1553 if (enc != NULL && *enc != NUL)
1554 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001555
1556 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1557 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001558 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001559 return FAIL;
1560 }
1561
1562 if (tv != NULL)
1563 {
1564 if (tv->v_type == VAR_STRING)
1565 pstate->p_str = tv->vval.v_string;
1566 else if (tv->v_type == VAR_LIST)
1567 pstate->p_li = tv->vval.v_list->lv_first;
1568 pstate->tv = tv;
1569 }
1570 pstate->buf = buf;
1571 pstate->buflnum = lnumfirst;
1572 pstate->lnumlast = lnumlast;
1573
1574 return OK;
1575}
1576
1577/*
1578 * Cleanup the state information used for parsing lines and populating a
1579 * quickfix list.
1580 */
1581 static void
1582qf_cleanup_state(qfstate_T *pstate)
1583{
1584 if (pstate->fd != NULL)
1585 fclose(pstate->fd);
1586
1587 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001588 if (pstate->vc.vc_type != CONV_NONE)
1589 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001590}
1591
1592/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001593 * Process the next line from a file/buffer/list/string and add it
1594 * to the quickfix list 'qfl'.
1595 */
1596 static int
1597qf_init_process_nextline(
1598 qf_list_T *qfl,
1599 efm_T *fmt_first,
1600 qfstate_T *state,
1601 qffields_T *fields)
1602{
1603 int status;
1604
1605 // Get the next line from a file/buffer/list/string
1606 status = qf_get_nextline(state);
1607 if (status != QF_OK)
1608 return status;
1609
1610 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1611 fmt_first, fields);
1612 if (status != QF_OK)
1613 return status;
1614
1615 return qf_add_entry(qfl,
1616 qfl->qf_directory,
1617 (*fields->namebuf || qfl->qf_directory != NULL)
1618 ? fields->namebuf
1619 : ((qfl->qf_currfile != NULL && fields->valid)
1620 ? qfl->qf_currfile : (char_u *)NULL),
1621 fields->module,
1622 0,
1623 fields->errmsg,
1624 fields->lnum,
1625 fields->col,
1626 fields->use_viscol,
1627 fields->pattern,
1628 fields->enr,
1629 fields->type,
1630 fields->valid);
1631}
1632
1633/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001634 * Read the errorfile "efile" into memory, line by line, building the error
1635 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001636 * Alternative: when "efile" is NULL read errors from buffer "buf".
1637 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001638 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001639 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1640 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001641 * Return -1 for error, number of errors for success.
1642 */
1643 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001644qf_init_ext(
1645 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001646 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001647 char_u *efile,
1648 buf_T *buf,
1649 typval_T *tv,
1650 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001651 int newlist, // TRUE: start a new error list
1652 linenr_T lnumfirst, // first line number to use
1653 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001654 char_u *qf_title,
1655 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001656{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001657 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001658 qfstate_T state;
1659 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001660 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001661 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001662 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001664 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001665 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001666 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001668 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001669 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001670
Bram Moolenaara80faa82020-04-12 19:37:17 +02001671 CLEAR_FIELD(state);
1672 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001673 if ((qf_alloc_fields(&fields) == FAIL) ||
1674 (qf_setup_state(&state, enc, efile, tv, buf,
1675 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 goto qf_init_end;
1677
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001678 if (newlist || qf_idx == qi->qf_listcount)
1679 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001680 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001681 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001682 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001683 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001684 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001685 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001686 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001687 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001688 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001689 qfl = qf_get_list(qi, qf_idx);
1690 if (!qf_list_empty(qfl))
1691 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001694 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001695 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001696 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 else
1698 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001700 // If the errorformat didn't change between calls, then reuse the
1701 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001702 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1703 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001704 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001705 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001706 free_efm_list(&fmt_first);
1707
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001708 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001709 fmt_first = parse_efm_option(efm);
1710 if (fmt_first != NULL)
1711 last_efm = vim_strsave(efm);
1712 }
1713
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001714 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001717 // got_int is reset here, because it was probably set when killing the
1718 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 got_int = FALSE;
1720
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001721 // Read the lines in the error file one by one.
1722 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001723 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001725 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001726 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001727 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001728 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001729 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001730 if (status == QF_FAIL)
1731 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 line_breakcheck();
1734 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001735 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001737 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001739 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001740 qfl->qf_ptr = qfl->qf_start;
1741 qfl->qf_index = 1;
1742 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
1744 else
1745 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001746 qfl->qf_nonevalid = FALSE;
1747 if (qfl->qf_ptr == NULL)
1748 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001750 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001751 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001752 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001754 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001756 if (!adding)
1757 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001758 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001759 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001760 qi->qf_listcount--;
1761 if (qi->qf_curlist > 0)
1762 --qi->qf_curlist;
1763 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001764qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001765 if (qf_idx == qi->qf_curlist)
1766 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001767 qf_cleanup_state(&state);
1768 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
1770 return retval;
1771}
1772
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001773/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001774 * Read the errorfile "efile" into memory, line by line, building the error
1775 * list. Set the error list's title to qf_title.
1776 * Return -1 for error, number of errors for success.
1777 */
1778 int
1779qf_init(win_T *wp,
1780 char_u *efile,
1781 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001782 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001783 char_u *qf_title,
1784 char_u *enc)
1785{
1786 qf_info_T *qi = &ql_info;
1787
1788 if (wp != NULL)
1789 {
1790 qi = ll_get_or_alloc_list(wp);
1791 if (qi == NULL)
1792 return FAIL;
1793 }
1794
1795 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1796 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1797}
1798
1799/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001800 * Set the title of the specified quickfix list. Frees the previous title.
1801 * Prepends ':' to the title.
1802 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001803 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001804qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001805{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001806 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001807
Bram Moolenaarfb604092014-07-23 15:55:00 +02001808 if (title != NULL)
1809 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02001810 char_u *p = alloc(STRLEN(title) + 2);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001811
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001812 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001813 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001814 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001815 }
1816}
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001819 * The title of a quickfix/location list is set, by default, to the command
1820 * that created the quickfix list with the ":" prefix.
1821 * Create a quickfix list title string by prepending ":" to a user command.
1822 * Returns a pointer to a static buffer with the title.
1823 */
1824 static char_u *
1825qf_cmdtitle(char_u *cmd)
1826{
1827 static char_u qftitle_str[IOSIZE];
1828
1829 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1830 return qftitle_str;
1831}
1832
1833/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001834 * Return a pointer to the current list in the specified quickfix stack
1835 */
1836 static qf_list_T *
1837qf_get_curlist(qf_info_T *qi)
1838{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001839 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001840}
1841
1842/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001843 * Prepare for adding a new quickfix list. If the current list is in the
1844 * middle of the stack, then all the following lists are freed and then
1845 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 */
1847 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001848qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849{
1850 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001851 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001853 // If the current entry is not the last entry, delete entries beyond
1854 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001855 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001856 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001857 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001859 // When the stack is full, remove to oldest entry
1860 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001861 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001863 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001865 qi->qf_lists[i - 1] = qi->qf_lists[i];
1866 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001869 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001870 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001871 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001872 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001873 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001874 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875}
1876
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001877/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001878 * Queue location list stack delete request.
1879 */
1880 static void
1881locstack_queue_delreq(qf_info_T *qi)
1882{
1883 qf_delq_T *q;
1884
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001885 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001886 if (q != NULL)
1887 {
1888 q->qi = qi;
1889 q->next = qf_delq_head;
1890 qf_delq_head = q;
1891 }
1892}
1893
1894/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001895 * Return the global quickfix stack window buffer number.
1896 */
1897 int
1898qf_stack_get_bufnr(void)
1899{
1900 return ql_info.qf_bufnr;
1901}
1902
1903/*
1904 * Wipe the quickfix window buffer (if present) for the specified
1905 * quickfix/location list.
1906 */
1907 static void
1908wipe_qf_buffer(qf_info_T *qi)
1909{
1910 buf_T *qfbuf;
1911
1912 if (qi->qf_bufnr != INVALID_QFBUFNR)
1913 {
1914 qfbuf = buflist_findnr(qi->qf_bufnr);
1915 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1916 {
1917 // If the quickfix buffer is not loaded in any window, then
1918 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001919 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001920 qi->qf_bufnr = INVALID_QFBUFNR;
1921 }
1922 }
1923}
1924
1925/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001926 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001927 */
1928 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001929ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001930{
1931 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001932 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001933
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001934 qi = *pqi;
1935 if (qi == NULL)
1936 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001937 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001938
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001939 // If the location list is still in use, then queue the delete request
1940 // to be processed later.
1941 if (quickfix_busy > 0)
1942 {
1943 locstack_queue_delreq(qi);
1944 return;
1945 }
1946
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001947 qi->qf_refcount--;
1948 if (qi->qf_refcount < 1)
1949 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001950 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001951 // If the quickfix window buffer is loaded, then wipe it
1952 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001953
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001954 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001955 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001956 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001957 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001958}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001959
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001960/*
1961 * Free all the quickfix/location lists in the stack.
1962 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001963 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001964qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001965{
1966 int i;
1967 qf_info_T *qi = &ql_info;
1968
1969 if (wp != NULL)
1970 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001971 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001972 ll_free_all(&wp->w_llist);
1973 ll_free_all(&wp->w_llist_ref);
1974 }
1975 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001976 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001977 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001978 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001979}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001982 * Delay freeing of location list stacks when the quickfix code is running.
1983 * Used to avoid problems with autocmds freeing location list stacks when the
1984 * quickfix code is still referencing the stack.
1985 * Must always call decr_quickfix_busy() exactly once after this.
1986 */
1987 static void
1988incr_quickfix_busy(void)
1989{
1990 quickfix_busy++;
1991}
1992
1993/*
1994 * Safe to free location list stacks. Process any delayed delete requests.
1995 */
1996 static void
1997decr_quickfix_busy(void)
1998{
1999 if (--quickfix_busy == 0)
2000 {
2001 // No longer referencing the location lists. Process all the pending
2002 // delete requests.
2003 while (qf_delq_head != NULL)
2004 {
2005 qf_delq_T *q = qf_delq_head;
2006
2007 qf_delq_head = q->next;
2008 ll_free_all(&q->qi);
2009 vim_free(q);
2010 }
2011 }
2012#ifdef ABORT_ON_INTERNAL_ERROR
2013 if (quickfix_busy < 0)
2014 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002015 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002016 abort();
2017 }
2018#endif
2019}
2020
2021#if defined(EXITFREE) || defined(PROTO)
2022 void
2023check_quickfix_busy(void)
2024{
2025 if (quickfix_busy != 0)
2026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002027 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002028# ifdef ABORT_ON_INTERNAL_ERROR
2029 abort();
2030# endif
2031 }
2032}
2033#endif
2034
2035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 * Add an entry to the end of the list of errors.
Bram Moolenaar95946f12019-03-31 15:31:59 +02002037 * Returns QF_OK or QF_FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 */
2039 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002040qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002041 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002042 char_u *dir, // optional directory name
2043 char_u *fname, // file name or NULL
2044 char_u *module, // module name or NULL
2045 int bufnum, // buffer number or zero
2046 char_u *mesg, // message
2047 long lnum, // line number
2048 int col, // column
2049 int vis_col, // using visual column
2050 char_u *pattern, // search pattern
2051 int nr, // error number
2052 int type, // type character
2053 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002055 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002056 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002058 if ((qfp = ALLOC_ONE(qfline_T)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002059 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002060 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002061 {
2062 buf_T *buf = buflist_findnr(bufnum);
2063
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002064 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002065 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002066 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002067 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002068 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002069 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002070 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2072 {
2073 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002074 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 }
2076 qfp->qf_lnum = lnum;
2077 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002078 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002079 if (pattern == NULL || *pattern == NUL)
2080 qfp->qf_pattern = NULL;
2081 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2082 {
2083 vim_free(qfp->qf_text);
2084 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002085 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002086 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002087 if (module == NULL || *module == NUL)
2088 qfp->qf_module = NULL;
2089 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2090 {
2091 vim_free(qfp->qf_text);
2092 vim_free(qfp->qf_pattern);
2093 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002094 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002097 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 type = 0;
2099 qfp->qf_type = type;
2100 qfp->qf_valid = valid;
2101
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002102 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002103 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002105 qfl->qf_start = qfp;
2106 qfl->qf_ptr = qfp;
2107 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002108 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 }
2110 else
2111 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002112 qfp->qf_prev = *lastp;
2113 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002115 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002117 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002118 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002119 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002121 qfl->qf_index = qfl->qf_count;
2122 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
2124
Bram Moolenaar95946f12019-03-31 15:31:59 +02002125 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126}
2127
2128/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002129 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002130 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002131 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002132qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002133{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002134 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002135
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002136 qi = ALLOC_CLEAR_ONE(qf_info_T);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002137 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002138 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002139 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002140 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002141 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002142 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002143 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002144}
2145
2146/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002147 * Return the location list stack for window 'wp'.
2148 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002149 */
2150 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002151ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002152{
2153 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002154 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002155 return wp->w_llist_ref;
2156
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002157 // For a non-location list window, w_llist_ref should not point to a
2158 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002159 ll_free_all(&wp->w_llist_ref);
2160
2161 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002162 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002163 return wp->w_llist;
2164}
2165
2166/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002167 * Get the quickfix/location list stack to use for the specified Ex command.
2168 * For a location list command, returns the stack for the current window. If
2169 * the location list is not found, then returns NULL and prints an error
2170 * message if 'print_emsg' is TRUE.
2171 */
2172 static qf_info_T *
2173qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2174{
2175 qf_info_T *qi = &ql_info;
2176
2177 if (is_loclist_cmd(eap->cmdidx))
2178 {
2179 qi = GET_LOC_LIST(curwin);
2180 if (qi == NULL)
2181 {
2182 if (print_emsg)
2183 emsg(_(e_loclist));
2184 return NULL;
2185 }
2186 }
2187
2188 return qi;
2189}
2190
2191/*
2192 * Get the quickfix/location list stack to use for the specified Ex command.
2193 * For a location list command, returns the stack for the current window.
2194 * If the location list is not present, then allocates a new one.
2195 * Returns NULL if the allocation fails. For a location list command, sets
2196 * 'pwinp' to curwin.
2197 */
2198 static qf_info_T *
2199qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2200{
2201 qf_info_T *qi = &ql_info;
2202
2203 if (is_loclist_cmd(eap->cmdidx))
2204 {
2205 qi = ll_get_or_alloc_list(curwin);
2206 if (qi == NULL)
2207 return NULL;
2208 *pwinp = curwin;
2209 }
2210
2211 return qi;
2212}
2213
2214/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002215 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2216 */
2217 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002218copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002219{
2220 int i;
2221 qfline_T *from_qfp;
2222 qfline_T *prevp;
2223
2224 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002225 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002226 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002227 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002228 NULL,
2229 NULL,
2230 from_qfp->qf_module,
2231 0,
2232 from_qfp->qf_text,
2233 from_qfp->qf_lnum,
2234 from_qfp->qf_col,
2235 from_qfp->qf_viscol,
2236 from_qfp->qf_pattern,
2237 from_qfp->qf_nr,
2238 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002239 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002240 return FAIL;
2241
2242 // qf_add_entry() will not set the qf_num field, as the
2243 // directory and file names are not supplied. So the qf_fnum
2244 // field is copied here.
2245 prevp = to_qfl->qf_last;
2246 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2247 prevp->qf_type = from_qfp->qf_type; // error type
2248 if (from_qfl->qf_ptr == from_qfp)
2249 to_qfl->qf_ptr = prevp; // current location
2250 }
2251
2252 return OK;
2253}
2254
2255/*
2256 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2257 */
2258 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002259copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002260{
2261 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002262 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002263 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2264 to_qfl->qf_count = 0;
2265 to_qfl->qf_index = 0;
2266 to_qfl->qf_start = NULL;
2267 to_qfl->qf_last = NULL;
2268 to_qfl->qf_ptr = NULL;
2269 if (from_qfl->qf_title != NULL)
2270 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2271 else
2272 to_qfl->qf_title = NULL;
2273 if (from_qfl->qf_ctx != NULL)
2274 {
2275 to_qfl->qf_ctx = alloc_tv();
2276 if (to_qfl->qf_ctx != NULL)
2277 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2278 }
2279 else
2280 to_qfl->qf_ctx = NULL;
Bram Moolenaar858ba062020-05-31 23:11:59 +02002281 if (from_qfl->qf_qftf != NULL)
2282 to_qfl->qf_qftf = vim_strsave(from_qfl->qf_qftf);
2283 else
2284 to_qfl->qf_qftf = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002285
2286 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002287 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002288 return FAIL;
2289
2290 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2291
2292 // Assign a new ID for the location list
2293 to_qfl->qf_id = ++last_qf_id;
2294 to_qfl->qf_changedtick = 0L;
2295
2296 // When no valid entries are present in the list, qf_ptr points to
2297 // the first item in the list
2298 if (to_qfl->qf_nonevalid)
2299 {
2300 to_qfl->qf_ptr = to_qfl->qf_start;
2301 to_qfl->qf_index = 1;
2302 }
2303
2304 return OK;
2305}
2306
2307/*
2308 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002309 */
2310 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002311copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002312{
2313 qf_info_T *qi;
2314 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002315
Bram Moolenaar09037502018-09-25 22:08:14 +02002316 // When copying from a location list window, copy the referenced
2317 // location list. For other windows, copy the location list for
2318 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002319 if (IS_LL_WINDOW(from))
2320 qi = from->w_llist_ref;
2321 else
2322 qi = from->w_llist;
2323
Bram Moolenaar09037502018-09-25 22:08:14 +02002324 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002325 return;
2326
Bram Moolenaar09037502018-09-25 22:08:14 +02002327 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002328 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002329 return;
2330
2331 to->w_llist->qf_listcount = qi->qf_listcount;
2332
Bram Moolenaar09037502018-09-25 22:08:14 +02002333 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002334 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002335 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002336 to->w_llist->qf_curlist = idx;
2337
Bram Moolenaar0398e002019-03-21 21:12:49 +01002338 if (copy_loclist(qf_get_list(qi, idx),
2339 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002340 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002341 qf_free_all(to);
2342 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002343 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002344 }
2345
Bram Moolenaar09037502018-09-25 22:08:14 +02002346 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002347}
2348
2349/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002350 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002351 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 */
2353 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002354qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
Bram Moolenaar82404332016-07-10 17:00:38 +02002356 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002357 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002358 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002359
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002360 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
Bram Moolenaare60acc12011-05-10 16:41:25 +02002363#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002364 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002365#endif
2366#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002367 if (directory != NULL)
2368 slash_adjust(directory);
2369 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002370#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002371 if (directory != NULL && !vim_isAbsName(fname)
2372 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2373 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002374 // Here we check if the file really exists.
2375 // This should normally be true, but if make works without
2376 // "leaving directory"-messages we might have missed a
2377 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002378 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002381 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002382 if (directory)
2383 ptr = concat_fnames(directory, fname, TRUE);
2384 else
2385 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002387 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002388 bufname = ptr;
2389 }
2390 else
2391 bufname = fname;
2392
2393 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002394 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002395 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002396 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002397 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002399 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002400 {
2401 vim_free(qf_last_bufname);
2402 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2403 if (bufname == ptr)
2404 qf_last_bufname = bufname;
2405 else
2406 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002407 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002408 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002409 if (buf == NULL)
2410 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002411
Bram Moolenaarc1542742016-07-20 21:44:37 +02002412 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002413 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002414 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415}
2416
2417/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002418 * Push dirbuf onto the directory stack and return pointer to actual dir or
2419 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 */
2421 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002422qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424 struct dir_stack_T *ds_new;
2425 struct dir_stack_T *ds_ptr;
2426
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002427 // allocate new stack element and hook it in
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002428 ds_new = ALLOC_ONE(struct dir_stack_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 if (ds_new == NULL)
2430 return NULL;
2431
2432 ds_new->next = *stackptr;
2433 *stackptr = ds_new;
2434
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002435 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 if (vim_isAbsName(dirbuf)
2437 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002438 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 (*stackptr)->dirname = vim_strsave(dirbuf);
2440 else
2441 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002442 // Okay we don't have an absolute path.
2443 // dirbuf must be a subdir of one of the directories on the stack.
2444 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 ds_new = (*stackptr)->next;
2446 (*stackptr)->dirname = NULL;
2447 while (ds_new)
2448 {
2449 vim_free((*stackptr)->dirname);
2450 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2451 TRUE);
2452 if (mch_isdir((*stackptr)->dirname) == TRUE)
2453 break;
2454
2455 ds_new = ds_new->next;
2456 }
2457
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002458 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 while ((*stackptr)->next != ds_new)
2460 {
2461 ds_ptr = (*stackptr)->next;
2462 (*stackptr)->next = (*stackptr)->next->next;
2463 vim_free(ds_ptr->dirname);
2464 vim_free(ds_ptr);
2465 }
2466
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002467 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 if (ds_new == NULL)
2469 {
2470 vim_free((*stackptr)->dirname);
2471 (*stackptr)->dirname = vim_strsave(dirbuf);
2472 }
2473 }
2474
2475 if ((*stackptr)->dirname != NULL)
2476 return (*stackptr)->dirname;
2477 else
2478 {
2479 ds_ptr = *stackptr;
2480 *stackptr = (*stackptr)->next;
2481 vim_free(ds_ptr);
2482 return NULL;
2483 }
2484}
2485
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486/*
2487 * pop dirbuf from the directory stack and return previous directory or NULL if
2488 * stack is empty
2489 */
2490 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002491qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493 struct dir_stack_T *ds_ptr;
2494
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002495 // TODO: Should we check if dirbuf is the directory on top of the stack?
2496 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002498 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 if (*stackptr != NULL)
2500 {
2501 ds_ptr = *stackptr;
2502 *stackptr = (*stackptr)->next;
2503 vim_free(ds_ptr->dirname);
2504 vim_free(ds_ptr);
2505 }
2506
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002507 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 return *stackptr ? (*stackptr)->dirname : NULL;
2509}
2510
2511/*
2512 * clean up directory stack
2513 */
2514 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002515qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516{
2517 struct dir_stack_T *ds_ptr;
2518
2519 while ((ds_ptr = *stackptr) != NULL)
2520 {
2521 *stackptr = (*stackptr)->next;
2522 vim_free(ds_ptr->dirname);
2523 vim_free(ds_ptr);
2524 }
2525}
2526
2527/*
2528 * Check in which directory of the directory stack the given file can be
2529 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002530 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 * Cleans up intermediate directory entries.
2532 *
2533 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002534 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 * ./
2536 * ./aa
2537 * ./aa/bb
2538 * ./bb
2539 * ./bb/x.c
2540 * and make says:
2541 * making all in aa
2542 * making all in bb
2543 * x.c:9: Error
2544 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2545 * qf_guess_filepath will return NULL.
2546 */
2547 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002548qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549{
2550 struct dir_stack_T *ds_ptr;
2551 struct dir_stack_T *ds_tmp;
2552 char_u *fullname;
2553
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002554 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002555 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 return NULL;
2557
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002558 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 fullname = NULL;
2560 while (ds_ptr)
2561 {
2562 vim_free(fullname);
2563 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2564
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002565 // If concat_fnames failed, just go on. The worst thing that can happen
2566 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2568 break;
2569
2570 ds_ptr = ds_ptr->next;
2571 }
2572
2573 vim_free(fullname);
2574
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002575 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002576 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002578 ds_tmp = qfl->qf_dir_stack->next;
2579 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 vim_free(ds_tmp->dirname);
2581 vim_free(ds_tmp);
2582 }
2583
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002584 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585}
2586
2587/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002588 * Returns TRUE if a quickfix/location list with the given identifier exists.
2589 */
2590 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002591qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002592{
2593 qf_info_T *qi = &ql_info;
2594 int i;
2595
2596 if (wp != NULL)
2597 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002598 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002599 if (qi == NULL)
2600 return FALSE;
2601 }
2602
2603 for (i = 0; i < qi->qf_listcount; ++i)
2604 if (qi->qf_lists[i].qf_id == qf_id)
2605 return TRUE;
2606
2607 return FALSE;
2608}
2609
2610/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002611 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002612 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002613 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002614 * Similar to location list.
2615 */
2616 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002617is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002618{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002619 qfline_T *qfp;
2620 int i;
2621
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002622 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002623 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2624 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002625 break;
2626
Bram Moolenaar95946f12019-03-31 15:31:59 +02002627 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002628 return FALSE;
2629
2630 return TRUE;
2631}
2632
2633/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002634 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002635 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002636 */
2637 static qfline_T *
2638get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002639 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002640 qfline_T *qf_ptr,
2641 int *qf_index,
2642 int dir)
2643{
2644 int idx;
2645 int old_qf_fnum;
2646
2647 idx = *qf_index;
2648 old_qf_fnum = qf_ptr->qf_fnum;
2649
2650 do
2651 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002652 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002653 return NULL;
2654 ++idx;
2655 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002656 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002657 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2658
2659 *qf_index = idx;
2660 return qf_ptr;
2661}
2662
2663/*
2664 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002665 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002666 */
2667 static qfline_T *
2668get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002669 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002670 qfline_T *qf_ptr,
2671 int *qf_index,
2672 int dir)
2673{
2674 int idx;
2675 int old_qf_fnum;
2676
2677 idx = *qf_index;
2678 old_qf_fnum = qf_ptr->qf_fnum;
2679
2680 do
2681 {
2682 if (idx == 1 || qf_ptr->qf_prev == NULL)
2683 return NULL;
2684 --idx;
2685 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002686 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002687 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2688
2689 *qf_index = idx;
2690 return qf_ptr;
2691}
2692
2693/*
2694 * Get the n'th (errornr) previous/next valid entry from the current entry in
2695 * the quickfix list.
2696 * dir == FORWARD or FORWARD_FILE: next valid entry
2697 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2698 */
2699 static qfline_T *
2700get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002701 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002702 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002703 int dir,
2704 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002705{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002706 qfline_T *qf_ptr = qfl->qf_ptr;
2707 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002708 qfline_T *prev_qf_ptr;
2709 int prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002710 char_u *err = e_no_more_items;
2711
2712 while (errornr--)
2713 {
2714 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002715 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002716
2717 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002718 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002719 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002720 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002721 if (qf_ptr == NULL)
2722 {
2723 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002724 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002725 if (err != NULL)
2726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002727 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002728 return NULL;
2729 }
2730 break;
2731 }
2732
2733 err = NULL;
2734 }
2735
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002736 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002737 return qf_ptr;
2738}
2739
2740/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002741 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2742 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002743 */
2744 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002745get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002746{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002747 qfline_T *qf_ptr = qfl->qf_ptr;
2748 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002749
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002750 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002751 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2752 {
2753 --qf_idx;
2754 qf_ptr = qf_ptr->qf_prev;
2755 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002756 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002757 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2758 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002759 {
2760 ++qf_idx;
2761 qf_ptr = qf_ptr->qf_next;
2762 }
2763
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002764 *new_qfidx = qf_idx;
2765 return qf_ptr;
2766}
2767
2768/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002769 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002770 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2771 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2772 * Returns a pointer to the entry and the index of the new entry is stored in
2773 * 'new_qfidx'.
2774 */
2775 static qfline_T *
2776qf_get_entry(
2777 qf_list_T *qfl,
2778 int errornr,
2779 int dir,
2780 int *new_qfidx)
2781{
2782 qfline_T *qf_ptr = qfl->qf_ptr;
2783 int qfidx = qfl->qf_index;
2784
2785 if (dir != 0) // next/prev valid entry
2786 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2787 else if (errornr != 0) // go to specified number
2788 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2789
2790 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002791 return qf_ptr;
2792}
2793
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002794/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002795 * Find a window displaying a Vim help file.
2796 */
2797 static win_T *
2798qf_find_help_win(void)
2799{
2800 win_T *wp;
2801
2802 FOR_ALL_WINDOWS(wp)
2803 if (bt_help(wp->w_buffer))
2804 return wp;
2805
2806 return NULL;
2807}
2808
2809/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002810 * Set the location list for the specified window to 'qi'.
2811 */
2812 static void
2813win_set_loclist(win_T *wp, qf_info_T *qi)
2814{
2815 wp->w_llist = qi;
2816 qi->qf_refcount++;
2817}
2818
2819/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002820 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2821 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002822 */
2823 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002824jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002825{
2826 win_T *wp;
2827 int flags;
2828
Bram Moolenaarb2443732018-11-11 22:50:27 +01002829 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002830 wp = NULL;
2831 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002832 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002833 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2834 win_enter(wp, TRUE);
2835 else
2836 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002837 // Split off help window; put it at far top if no position
2838 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002839 flags = WSP_HELP;
2840 if (cmdmod.split == 0 && curwin->w_width != Columns
2841 && curwin->w_width < 80)
2842 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002843 // If the user asks to open a new window, then copy the location list.
2844 // Otherwise, don't copy the location list.
2845 if (IS_LL_STACK(qi) && !newwin)
2846 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002847
2848 if (win_split(0, flags) == FAIL)
2849 return FAIL;
2850
2851 *opened_window = TRUE;
2852
2853 if (curwin->w_height < p_hh)
2854 win_setheight((int)p_hh);
2855
Bram Moolenaarb2443732018-11-11 22:50:27 +01002856 // When using location list, the new window should use the supplied
2857 // location list. If the user asks to open a new window, then the new
2858 // window will get a copy of the location list.
2859 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002860 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002861 }
2862
2863 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002864 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002865
2866 return OK;
2867}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002868
2869/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002870 * Find a non-quickfix window in the current tabpage using the given location
2871 * list stack.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002872 * Returns NULL if a matching window is not found.
2873 */
2874 static win_T *
2875qf_find_win_with_loclist(qf_info_T *ll)
2876{
2877 win_T *wp;
2878
2879 FOR_ALL_WINDOWS(wp)
2880 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2881 return wp;
2882
2883 return NULL;
2884}
2885
2886/*
2887 * Find a window containing a normal buffer
2888 */
2889 static win_T *
2890qf_find_win_with_normal_buf(void)
2891{
2892 win_T *wp;
2893
2894 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002895 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002896 return wp;
2897
2898 return NULL;
2899}
2900
2901/*
2902 * Go to a window in any tabpage containing the specified file. Returns TRUE
2903 * if successfully jumped to the window. Otherwise returns FALSE.
2904 */
2905 static int
2906qf_goto_tabwin_with_file(int fnum)
2907{
2908 tabpage_T *tp;
2909 win_T *wp;
2910
2911 FOR_ALL_TAB_WINDOWS(tp, wp)
2912 if (wp->w_buffer->b_fnum == fnum)
2913 {
2914 goto_tabpage_win(tp, wp);
2915 return TRUE;
2916 }
2917
2918 return FALSE;
2919}
2920
2921/*
2922 * Create a new window to show a file above the quickfix window. Called when
2923 * only the quickfix window is present.
2924 */
2925 static int
2926qf_open_new_file_win(qf_info_T *ll_ref)
2927{
2928 int flags;
2929
2930 flags = WSP_ABOVE;
2931 if (ll_ref != NULL)
2932 flags |= WSP_NEWLOC;
2933 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002934 return FAIL; // not enough room for window
2935 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002936 swb_flags = 0;
2937 RESET_BINDING(curwin);
2938 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002939 // The new window should use the location list from the
2940 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002941 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002942 return OK;
2943}
2944
2945/*
2946 * Go to a window that shows the right buffer. If the window is not found, go
2947 * to the window just above the location list window. This is used for opening
2948 * a file from a location window and not from a quickfix window. If some usable
2949 * window is previously found, then it is supplied in 'use_win'.
2950 */
2951 static void
2952qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2953{
2954 win_T *win = use_win;
2955
2956 if (win == NULL)
2957 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002958 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002959 FOR_ALL_WINDOWS(win)
2960 if (win->w_buffer->b_fnum == qf_fnum)
2961 break;
2962 if (win == NULL)
2963 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002964 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002965 win = curwin;
2966 do
2967 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002968 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002969 break;
2970 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002971 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002972 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002973 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002974 } while (win != curwin);
2975 }
2976 }
2977 win_goto(win);
2978
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002979 // If the location list for the window is not set, then set it
2980 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01002981 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002982 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002983}
2984
2985/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002986 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2987 * not found, then go to the window just above the quickfix window. This is
2988 * used for opening a file from a quickfix window and not from a location
2989 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002990 */
2991 static void
2992qf_goto_win_with_qfl_file(int qf_fnum)
2993{
2994 win_T *win;
2995 win_T *altwin;
2996
2997 win = curwin;
2998 altwin = NULL;
2999 for (;;)
3000 {
3001 if (win->w_buffer->b_fnum == qf_fnum)
3002 break;
3003 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003004 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003005 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003006 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003007
3008 if (IS_QF_WINDOW(win))
3009 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003010 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003011 // window, unless 'switchbuf' contains 'uselast': in this case we
3012 // try to jump to the previously used window first.
3013 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3014 win = prevwin;
3015 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003016 win = altwin;
3017 else if (curwin->w_prev != NULL)
3018 win = curwin->w_prev;
3019 else
3020 win = curwin->w_next;
3021 break;
3022 }
3023
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003024 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003025 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003026 altwin = win;
3027 }
3028
3029 win_goto(win);
3030}
3031
3032/*
3033 * Find a suitable window for opening a file (qf_fnum) from the
3034 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003035 * window, jump to it. Otherwise open a new window to display the file. If
3036 * 'newwin' is TRUE, then always open a new window. This is called from either
3037 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003038 */
3039 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003040qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003041{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003042 win_T *usable_wp = NULL;
3043 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003044 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003045
Bram Moolenaarb2443732018-11-11 22:50:27 +01003046 // If opening a new window, then don't use the location list referred by
3047 // the current window. Otherwise two windows will refer to the same
3048 // location list.
3049 if (!newwin)
3050 ll_ref = curwin->w_llist_ref;
3051
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003052 if (ll_ref != NULL)
3053 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003054 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003055 usable_wp = qf_find_win_with_loclist(ll_ref);
3056 if (usable_wp != NULL)
3057 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003058 }
3059
3060 if (!usable_win)
3061 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003062 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003063 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003064 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003065 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003066 }
3067
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003068 // If no usable window is found and 'switchbuf' contains "usetab"
3069 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003070 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003071 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003072
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003073 // If there is only one window and it is the quickfix window, create a
3074 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003075 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003076 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003077 if (qf_open_new_file_win(ll_ref) != OK)
3078 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003079 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003080 }
3081 else
3082 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003083 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003084 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003085 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003086 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003087 }
3088
3089 return OK;
3090}
3091
3092/*
3093 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003094 * Returns OK if successfully edited the file, FAIL on failing to open the
3095 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3096 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003097 */
3098 static int
3099qf_jump_edit_buffer(
3100 qf_info_T *qi,
3101 qfline_T *qf_ptr,
3102 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003103 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003104 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003105{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003106 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003107 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003108 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003109 int old_qf_curlist = qi->qf_curlist;
3110 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003111
3112 if (qf_ptr->qf_type == 1)
3113 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003114 // Open help file (do_ecmd() will set b_help flag, readfile() will
3115 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003116 if (!can_abandon(curbuf, forceit))
3117 {
3118 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003119 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003120 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003121
3122 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3123 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003124 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003125 }
3126 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003127 retval = buflist_getfile(qf_ptr->qf_fnum,
3128 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003129
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003130 // If a location list, check whether the associated window is still
3131 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003132 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003133 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003134 win_T *wp = win_id2wp(prev_winid);
3135 if (wp == NULL && curwin->w_llist != qi)
3136 {
3137 emsg(_("E924: Current window was closed"));
3138 *opened_window = FALSE;
3139 return NOTDONE;
3140 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003141 }
3142
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003143 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003145 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003146 return NOTDONE;
3147 }
3148
3149 if (old_qf_curlist != qi->qf_curlist
3150 || !is_qf_entry_present(qfl, qf_ptr))
3151 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003152 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003153 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003154 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003155 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003156 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003157 }
3158
3159 return retval;
3160}
3161
3162/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003163 * Go to the error line in the current file using either line/column number or
3164 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003165 */
3166 static void
3167qf_jump_goto_line(
3168 linenr_T qf_lnum,
3169 int qf_col,
3170 char_u qf_viscol,
3171 char_u *qf_pattern)
3172{
3173 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003174
3175 if (qf_pattern == NULL)
3176 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003177 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003178 i = qf_lnum;
3179 if (i > 0)
3180 {
3181 if (i > curbuf->b_ml.ml_line_count)
3182 i = curbuf->b_ml.ml_line_count;
3183 curwin->w_cursor.lnum = i;
3184 }
3185 if (qf_col > 0)
3186 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003187 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003188 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003189 coladvance(qf_col - 1);
3190 else
3191 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003192 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003193 check_cursor();
3194 }
3195 else
3196 beginline(BL_WHITE | BL_FIX);
3197 }
3198 else
3199 {
3200 pos_T save_cursor;
3201
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003202 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003203 save_cursor = curwin->w_cursor;
3204 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003205 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003206 curwin->w_cursor = save_cursor;
3207 }
3208}
3209
3210/*
3211 * Display quickfix list index and size message
3212 */
3213 static void
3214qf_jump_print_msg(
3215 qf_info_T *qi,
3216 int qf_index,
3217 qfline_T *qf_ptr,
3218 buf_T *old_curbuf,
3219 linenr_T old_lnum)
3220{
3221 linenr_T i;
3222 int len;
3223
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003224 // Update the screen before showing the message, unless the screen
3225 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003226 if (!msg_scrolled)
3227 update_topline_redraw();
3228 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003229 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003230 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3231 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003232 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003233 len = (int)STRLEN(IObuff);
3234 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3235
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003236 // Output the message. Overwrite to avoid scrolling when the 'O'
3237 // flag is present in 'shortmess'; But when not jumping, print the
3238 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003239 i = msg_scroll;
3240 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3241 msg_scroll = TRUE;
3242 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3243 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003244 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003245 msg_scroll = i;
3246}
3247
3248/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003249 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003250 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3251 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003252 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3253 * able to jump/open a window. Returns NOTDONE if a file is not associated
3254 * with the entry.
3255 */
3256 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003257qf_jump_open_window(
3258 qf_info_T *qi,
3259 qfline_T *qf_ptr,
3260 int newwin,
3261 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003262{
3263 // For ":helpgrep" find a help window or open one.
3264 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003265 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003266 return FAIL;
3267
3268 // If currently in the quickfix window, find another window to show the
3269 // file in.
3270 if (bt_quickfix(curbuf) && !*opened_window)
3271 {
3272 // If there is no file specified, we don't know where to go.
3273 // But do advance, otherwise ":cn" gets stuck.
3274 if (qf_ptr->qf_fnum == 0)
3275 return NOTDONE;
3276
Bram Moolenaarb2443732018-11-11 22:50:27 +01003277 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3278 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003279 return FAIL;
3280 }
3281
3282 return OK;
3283}
3284
3285/*
3286 * Edit a selected file from the quickfix/location list and jump to a
3287 * particular line/column, adjust the folds and display a message about the
3288 * jump.
3289 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3290 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3291 * the file.
3292 */
3293 static int
3294qf_jump_to_buffer(
3295 qf_info_T *qi,
3296 int qf_index,
3297 qfline_T *qf_ptr,
3298 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003299 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003300 int *opened_window,
3301 int openfold,
3302 int print_message)
3303{
3304 buf_T *old_curbuf;
3305 linenr_T old_lnum;
3306 int retval = OK;
3307
3308 // If there is a file name, read the wanted file if needed, and check
3309 // autowrite etc.
3310 old_curbuf = curbuf;
3311 old_lnum = curwin->w_cursor.lnum;
3312
3313 if (qf_ptr->qf_fnum != 0)
3314 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003315 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003316 opened_window);
3317 if (retval != OK)
3318 return retval;
3319 }
3320
3321 // When not switched to another buffer, still need to set pc mark
3322 if (curbuf == old_curbuf)
3323 setpcmark();
3324
3325 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3326 qf_ptr->qf_pattern);
3327
3328#ifdef FEAT_FOLDING
3329 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3330 foldOpenCursor();
3331#endif
3332 if (print_message)
3333 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3334
3335 return retval;
3336}
3337
3338/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003339 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 */
3341 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003342qf_jump(qf_info_T *qi,
3343 int dir,
3344 int errornr,
3345 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003347 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3348}
3349
3350/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003351 * Jump to a quickfix line.
3352 * If dir == 0 go to entry "errornr".
3353 * If dir == FORWARD go "errornr" valid entries forward.
3354 * If dir == BACKWARD go "errornr" valid entries backward.
3355 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3356 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3357 * else if "errornr" is zero, redisplay the same line
3358 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003359 * If 'newwin' is TRUE, then open the file in a new window.
3360 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003361 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003362qf_jump_newwin(qf_info_T *qi,
3363 int dir,
3364 int errornr,
3365 int forceit,
3366 int newwin)
3367{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003368 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003369 qfline_T *qf_ptr;
3370 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003373 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003374 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003375 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003378 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003379 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003381 if (qi == NULL)
3382 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003383
Bram Moolenaar0398e002019-03-21 21:12:49 +01003384 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003386 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 return;
3388 }
3389
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003390 incr_quickfix_busy();
3391
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003392 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003393
3394 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003396 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003398
3399 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3400 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003402 qf_ptr = old_qf_ptr;
3403 qf_index = old_qf_index;
3404 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003407 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003408 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003409 // No need to print the error message if it's visible in the error
3410 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 print_message = FALSE;
3412
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003413 prev_winid = curwin->w_id;
3414
Bram Moolenaarb2443732018-11-11 22:50:27 +01003415 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003416 if (retval == FAIL)
3417 goto failed;
3418 if (retval == NOTDONE)
3419 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003420
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003421 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003422 &opened_window, old_KeyTyped, print_message);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003423 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003425 // Quickfix/location list is freed by an autocmd
3426 qi = NULL;
3427 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003430 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003433 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003434 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003436 // Couldn't open file, so put index back where it was. This could
3437 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 qf_ptr = old_qf_ptr;
3440 qf_index = old_qf_index;
3441 }
3442 }
3443theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003444 if (qi != NULL)
3445 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003446 qfl->qf_ptr = qf_ptr;
3447 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003448 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003449 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003451 // Restore old 'switchbuf' value, but not when an autocommand or
3452 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003453 p_swb = old_swb;
3454 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003456 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457}
3458
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003459// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003460static int qfFileAttr;
3461static int qfSepAttr;
3462static int qfLineAttr;
3463
3464/*
3465 * Display information about a single entry from the quickfix/location list.
3466 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003467 * 'cursel' will be set to TRUE for the currently selected entry in the
3468 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003469 */
3470 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003471qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003472{
3473 char_u *fname;
3474 buf_T *buf;
3475 int filter_entry;
3476
3477 fname = NULL;
3478 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3479 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3480 (char *)qfp->qf_module);
3481 else {
3482 if (qfp->qf_fnum != 0
3483 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3484 {
3485 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003486 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003487 fname = gettail(fname);
3488 }
3489 if (fname == NULL)
3490 sprintf((char *)IObuff, "%2d", qf_idx);
3491 else
3492 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3493 qf_idx, (char *)fname);
3494 }
3495
3496 // Support for filtering entries using :filter /pat/ clist
3497 // Match against the module name, file name, search pattern and
3498 // text of the entry.
3499 filter_entry = TRUE;
3500 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3501 filter_entry &= message_filtered(qfp->qf_module);
3502 if (filter_entry && fname != NULL)
3503 filter_entry &= message_filtered(fname);
3504 if (filter_entry && qfp->qf_pattern != NULL)
3505 filter_entry &= message_filtered(qfp->qf_pattern);
3506 if (filter_entry)
3507 filter_entry &= message_filtered(qfp->qf_text);
3508 if (filter_entry)
3509 return;
3510
3511 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003512 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003513
3514 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003515 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003516 if (qfp->qf_lnum == 0)
3517 IObuff[0] = NUL;
3518 else if (qfp->qf_col == 0)
3519 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3520 else
3521 sprintf((char *)IObuff, "%ld col %d",
3522 qfp->qf_lnum, qfp->qf_col);
3523 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3524 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003525 msg_puts_attr((char *)IObuff, qfLineAttr);
3526 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003527 if (qfp->qf_pattern != NULL)
3528 {
3529 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003530 msg_puts((char *)IObuff);
3531 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003532 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003533 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003534
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003535 // Remove newlines and leading whitespace from the text. For an
3536 // unrecognized line keep the indent, the compiler may mark a word
3537 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003538 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3539 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3540 IObuff, IOSIZE);
3541 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003542 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003543}
3544
3545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003547 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 */
3549 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003550qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003552 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003553 qfline_T *qfp;
3554 int i;
3555 int idx1 = 1;
3556 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003557 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003558 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003559 int all = eap->forceit; // if not :cl!, only show
3560 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003561 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003563 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3564 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003565
Bram Moolenaar0398e002019-03-21 21:12:49 +01003566 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003568 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 return;
3570 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003571 if (*arg == '+')
3572 {
3573 ++arg;
3574 plus = TRUE;
3575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3577 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003578 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 return;
3580 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003581 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003582 if (plus)
3583 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003584 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003585 idx2 = i + idx1;
3586 idx1 = i;
3587 }
3588 else
3589 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003590 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003591 if (idx1 < 0)
3592 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3593 if (idx2 < 0)
3594 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003597 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003598 shorten_fnames(FALSE);
3599
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003600 // Get the attributes for the different quickfix highlight items. Note
3601 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003602 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3603 if (qfFileAttr == 0)
3604 qfFileAttr = HL_ATTR(HLF_D);
3605 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3606 if (qfSepAttr == 0)
3607 qfSepAttr = HL_ATTR(HLF_D);
3608 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3609 if (qfLineAttr == 0)
3610 qfLineAttr = HL_ATTR(HLF_N);
3611
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003612 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003614 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
3616 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003617 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 ui_breakcheck();
3620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621}
3622
3623/*
3624 * Remove newlines and leading whitespace from an error message.
3625 * Put the result in "buf[bufsize]".
3626 */
3627 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003628qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629{
3630 int i;
3631 char_u *p = text;
3632
3633 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3634 {
3635 if (*p == '\n')
3636 {
3637 buf[i] = ' ';
3638 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003639 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 break;
3641 }
3642 else
3643 buf[i] = *p++;
3644 }
3645 buf[i] = NUL;
3646}
3647
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003648/*
3649 * Display information (list number, list size and the title) about a
3650 * quickfix/location list.
3651 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003652 static void
3653qf_msg(qf_info_T *qi, int which, char *lead)
3654{
3655 char *title = (char *)qi->qf_lists[which].qf_title;
3656 int count = qi->qf_lists[which].qf_count;
3657 char_u buf[IOSIZE];
3658
3659 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3660 lead,
3661 which + 1,
3662 qi->qf_listcount,
3663 count);
3664
3665 if (title != NULL)
3666 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003667 size_t len = STRLEN(buf);
3668
3669 if (len < 34)
3670 {
3671 vim_memset(buf + len, ' ', 34 - len);
3672 buf[34] = NUL;
3673 }
3674 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003675 }
3676 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003677 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003678}
3679
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680/*
3681 * ":colder [count]": Up in the quickfix stack.
3682 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003683 * ":lolder [count]": Up in the location list stack.
3684 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 */
3686 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003687qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003689 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 int count;
3691
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003692 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3693 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 if (eap->addr_count != 0)
3696 count = eap->line2;
3697 else
3698 count = 1;
3699 while (count--)
3700 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003701 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003703 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003705 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003706 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003708 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
3710 else
3711 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003712 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003714 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003715 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003717 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
3719 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003720 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003721 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722}
3723
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003724/*
3725 * Display the information about all the quickfix/location lists in the stack
3726 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003727 void
3728qf_history(exarg_T *eap)
3729{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003730 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003731 int i;
3732
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003733 if (eap->addr_count > 0)
3734 {
3735 if (qi == NULL)
3736 {
3737 emsg(_(e_loclist));
3738 return;
3739 }
3740
3741 // Jump to the specified quickfix list
3742 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3743 {
3744 qi->qf_curlist = eap->line2 - 1;
3745 qf_msg(qi, qi->qf_curlist, "");
3746 qf_update_buffer(qi, NULL);
3747 }
3748 else
3749 emsg(_(e_invrange));
3750
3751 return;
3752 }
3753
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003754 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003755 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003756 else
3757 for (i = 0; i < qi->qf_listcount; ++i)
3758 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3759}
3760
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003762 * Free all the entries in the error list "idx". Note that other information
3763 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 */
3765 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003766qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003768 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003769 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003770 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003772 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003774 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003775 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003776 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003777 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003778 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003779 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003780 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003781 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003782 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003783 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003784 // Somehow qf_count may have an incorrect value, set it to 1
3785 // to avoid crashing when it's wrong.
3786 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003787 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003788 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003789 qfl->qf_start = qfpnext;
3790 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003792
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003793 qfl->qf_index = 0;
3794 qfl->qf_start = NULL;
3795 qfl->qf_last = NULL;
3796 qfl->qf_ptr = NULL;
3797 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003798
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003799 qf_clean_dir_stack(&qfl->qf_dir_stack);
3800 qfl->qf_directory = NULL;
3801 qf_clean_dir_stack(&qfl->qf_file_stack);
3802 qfl->qf_currfile = NULL;
3803 qfl->qf_multiline = FALSE;
3804 qfl->qf_multiignore = FALSE;
3805 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806}
3807
3808/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003809 * Free error list "idx". Frees all the entries in the quickfix list,
3810 * associated context information and the title.
3811 */
3812 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003813qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003814{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003815 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003816
Bram Moolenaard23a8232018-02-10 18:45:26 +01003817 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003818 free_tv(qfl->qf_ctx);
3819 qfl->qf_ctx = NULL;
Bram Moolenaar858ba062020-05-31 23:11:59 +02003820 VIM_CLEAR(qfl->qf_qftf);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003821 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003822 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003823}
3824
3825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 * qf_mark_adjust: adjust marks
3827 */
3828 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003829qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003830 win_T *wp,
3831 linenr_T line1,
3832 linenr_T line2,
3833 long amount,
3834 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003836 int i;
3837 qfline_T *qfp;
3838 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003839 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003840 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003841 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
Bram Moolenaarc1542742016-07-20 21:44:37 +02003843 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003844 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003845 if (wp != NULL)
3846 {
3847 if (wp->w_llist == NULL)
3848 return;
3849 qi = wp->w_llist;
3850 }
3851
3852 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003853 {
3854 qf_list_T *qfl = qf_get_list(qi, idx);
3855
3856 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003857 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 if (qfp->qf_fnum == curbuf->b_fnum)
3859 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003860 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3862 {
3863 if (amount == MAXLNUM)
3864 qfp->qf_cleared = TRUE;
3865 else
3866 qfp->qf_lnum += amount;
3867 }
3868 else if (amount_after && qfp->qf_lnum > line2)
3869 qfp->qf_lnum += amount_after;
3870 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003871 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003872
3873 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003874 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875}
3876
3877/*
3878 * Make a nice message out of the error character and the error number:
3879 * char number message
3880 * e or E 0 " error"
3881 * w or W 0 " warning"
3882 * i or I 0 " info"
3883 * 0 0 ""
3884 * other 0 " c"
3885 * e or E n " error n"
3886 * w or W n " warning n"
3887 * i or I n " info n"
3888 * 0 n " error n"
3889 * other n " c n"
3890 * 1 x "" :helpgrep
3891 */
3892 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003893qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894{
3895 static char_u buf[20];
3896 static char_u cc[3];
3897 char_u *p;
3898
3899 if (c == 'W' || c == 'w')
3900 p = (char_u *)" warning";
3901 else if (c == 'I' || c == 'i')
3902 p = (char_u *)" info";
3903 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3904 p = (char_u *)" error";
3905 else if (c == 0 || c == 1)
3906 p = (char_u *)"";
3907 else
3908 {
3909 cc[0] = ' ';
3910 cc[1] = c;
3911 cc[2] = NUL;
3912 p = cc;
3913 }
3914
3915 if (nr <= 0)
3916 return p;
3917
3918 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3919 return buf;
3920}
3921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003923 * When "split" is FALSE: Open the entry/result under the cursor.
3924 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3925 */
3926 void
3927qf_view_result(int split)
3928{
3929 qf_info_T *qi = &ql_info;
3930
3931 if (!bt_quickfix(curbuf))
3932 return;
3933
3934 if (IS_LL_WINDOW(curwin))
3935 qi = GET_LOC_LIST(curwin);
3936
Bram Moolenaar0398e002019-03-21 21:12:49 +01003937 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003938 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003939 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003940 return;
3941 }
3942
3943 if (split)
3944 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003945 // Open the selected entry in a new window
3946 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3947 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003948 return;
3949 }
3950
3951 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3952}
3953
3954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 * ":cwindow": open the quickfix window if we have errors to display,
3956 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003957 * ":lwindow": open the location list window if we have locations to display,
3958 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 */
3960 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003961ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003963 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003964 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 win_T *win;
3966
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003967 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3968 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003969
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003970 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003971
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003972 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003973 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003975 // If a quickfix window is open but we have no errors to display,
3976 // close the window. If a quickfix window is not open, then open
3977 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003978 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003979 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01003980 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 {
3982 if (win != NULL)
3983 ex_cclose(eap);
3984 }
3985 else if (win == NULL)
3986 ex_copen(eap);
3987}
3988
3989/*
3990 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003991 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003994ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003996 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003997 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003999 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4000 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004002 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004003 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (win != NULL)
4005 win_close(win, FALSE);
4006}
4007
4008/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004009 * Set "w:quickfix_title" if "qi" has a title.
4010 */
4011 static void
4012qf_set_title_var(qf_list_T *qfl)
4013{
4014 if (qfl->qf_title != NULL)
4015 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4016}
4017
4018/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004019 * Goto a quickfix or location list window (if present).
4020 * Returns OK if the window is found, FAIL otherwise.
4021 */
4022 static int
4023qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4024{
4025 win_T *win;
4026
4027 win = qf_find_win(qi);
4028 if (win == NULL)
4029 return FAIL;
4030
4031 win_goto(win);
4032 if (resize)
4033 {
4034 if (vertsplit)
4035 {
4036 if (sz != win->w_width)
4037 win_setwidth(sz);
4038 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004039 else if (sz != win->w_height && win->w_height
4040 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004041 win_setheight(sz);
4042 }
4043
4044 return OK;
4045}
4046
4047/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004048 * Set options for the buffer in the quickfix or location list window.
4049 */
4050 static void
4051qf_set_cwindow_options(void)
4052{
4053 // switch off 'swapfile'
4054 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4055 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4056 OPT_LOCAL);
4057 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4058 RESET_BINDING(curwin);
4059#ifdef FEAT_DIFF
4060 curwin->w_p_diff = FALSE;
4061#endif
4062#ifdef FEAT_FOLDING
4063 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4064 OPT_LOCAL);
4065#endif
4066}
4067
4068/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004069 * Open a new quickfix or location list window, load the quickfix buffer and
4070 * set the appropriate options for the window.
4071 * Returns FAIL if the window could not be opened.
4072 */
4073 static int
4074qf_open_new_cwindow(qf_info_T *qi, int height)
4075{
4076 buf_T *qf_buf;
4077 win_T *oldwin = curwin;
4078 tabpage_T *prevtab = curtab;
4079 int flags = 0;
4080 win_T *win;
4081
4082 qf_buf = qf_find_buf(qi);
4083
4084 // The current window becomes the previous window afterwards.
4085 win = curwin;
4086
4087 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4088 // Create the new quickfix window at the very bottom, except when
4089 // :belowright or :aboveleft is used.
4090 win_goto(lastwin);
4091 // Default is to open the window below the current window
4092 if (cmdmod.split == 0)
4093 flags = WSP_BELOW;
4094 flags |= WSP_NEWLOC;
4095 if (win_split(height, flags) == FAIL)
4096 return FAIL; // not enough room for window
4097 RESET_BINDING(curwin);
4098
4099 if (IS_LL_STACK(qi))
4100 {
4101 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004102 // location list stack from the window 'win'.
4103 curwin->w_llist_ref = qi;
4104 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004105 }
4106
4107 if (oldwin != curwin)
4108 oldwin = NULL; // don't store info when in another window
4109 if (qf_buf != NULL)
4110 {
4111 // Use the existing quickfix buffer
4112 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4113 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4114 }
4115 else
4116 {
4117 // Create a new quickfix buffer
4118 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4119
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004120 // save the number of the new buffer
4121 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004122 }
4123
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004124 // Set the options for the quickfix buffer/window (if not already done)
4125 // Do this even if the quickfix buffer was already present, as an autocmd
4126 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004127 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004128 qf_set_cwindow_options();
4129
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004130 // Only set the height when still in the same tab page and there is no
4131 // window to the side.
4132 if (curtab == prevtab && curwin->w_width == Columns)
4133 win_setheight(height);
4134 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4135 if (win_valid(win))
4136 prevwin = win;
4137
4138 return OK;
4139}
4140
4141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004143 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 */
4145 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004146ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004148 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004149 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004151 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004152 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004154 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4155 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004156
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004157 incr_quickfix_busy();
4158
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 if (eap->addr_count != 0)
4160 height = eap->line2;
4161 else
4162 height = QF_WINHEIGHT;
4163
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004164 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165#ifdef FEAT_GUI
4166 need_mouse_correct = TRUE;
4167#endif
4168
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004169 // Find an existing quickfix window, or open a new one.
4170 if (cmdmod.tab == 0)
4171 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4172 cmdmod.split & WSP_VERT);
4173 if (status == FAIL)
4174 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004175 {
4176 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004177 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004180 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004181 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004182 // Save the current index here, as updating the quickfix buffer may free
4183 // the quickfix list
4184 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004185
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004186 // Fill the buffer with the quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004187 qf_fill_buffer(qfl, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004189 decr_quickfix_busy();
4190
4191 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 curwin->w_cursor.col = 0;
4193 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004194 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195}
4196
4197/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004198 * Move the cursor in the quickfix window to "lnum".
4199 */
4200 static void
4201qf_win_goto(win_T *win, linenr_T lnum)
4202{
4203 win_T *old_curwin = curwin;
4204
4205 curwin = win;
4206 curbuf = win->w_buffer;
4207 curwin->w_cursor.lnum = lnum;
4208 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004209 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004210 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004211 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004212 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004213 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004214 curwin = old_curwin;
4215 curbuf = curwin->w_buffer;
4216}
4217
4218/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004219 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004220 */
4221 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004222ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004223{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004224 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004225 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004226
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004227 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4228 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004229
4230 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004231 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4232 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4233}
4234
4235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 * Return the number of the current entry (line number in the quickfix
4237 * window).
4238 */
4239 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004240qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004242 qf_info_T *qi = &ql_info;
4243
4244 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004245 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004246 qi = wp->w_llist_ref;
4247
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004248 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249}
4250
4251/*
4252 * Update the cursor position in the quickfix window to the current error.
4253 * Return TRUE if there is a quickfix window.
4254 */
4255 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004256qf_win_pos_update(
4257 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004258 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259{
4260 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004261 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004263 // Put the cursor on the current error in the quickfix window, so that
4264 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004265 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 if (win != NULL
4267 && qf_index <= win->w_buffer->b_ml.ml_line_count
4268 && old_qf_index != qf_index)
4269 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 if (qf_index > old_qf_index)
4271 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004272 win->w_redraw_top = old_qf_index;
4273 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 else
4276 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004277 win->w_redraw_top = qf_index;
4278 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004280 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 return win != NULL;
4283}
4284
4285/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004286 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004287 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004288 */
4289 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004290is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004291{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004292 // A window displaying the quickfix buffer will have the w_llist_ref field
4293 // set to NULL.
4294 // A window displaying a location list buffer will have the w_llist_ref
4295 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004296 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004297 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4298 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004299 return TRUE;
4300
4301 return FALSE;
4302}
4303
4304/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004305 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004306 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004307 */
4308 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004309qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004310{
4311 win_T *win;
4312
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004313 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004314 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004315 return win;
4316 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004317}
4318
4319/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004320 * Find a quickfix buffer.
4321 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 */
4323 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004324qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004326 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004327 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004329 if (qi->qf_bufnr != INVALID_QFBUFNR)
4330 {
4331 buf_T *qfbuf;
4332 qfbuf = buflist_findnr(qi->qf_bufnr);
4333 if (qfbuf != NULL)
4334 return qfbuf;
4335 // buffer is no longer present
4336 qi->qf_bufnr = INVALID_QFBUFNR;
4337 }
4338
Bram Moolenaar9c102382006-05-03 21:26:49 +00004339 FOR_ALL_TAB_WINDOWS(tp, win)
4340 if (is_qf_win(win, qi))
4341 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004342
Bram Moolenaar9c102382006-05-03 21:26:49 +00004343 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344}
4345
4346/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004347 * Update the w:quickfix_title variable in the quickfix/location list window
4348 */
4349 static void
4350qf_update_win_titlevar(qf_info_T *qi)
4351{
4352 win_T *win;
4353 win_T *curwin_save;
4354
4355 if ((win = qf_find_win(qi)) != NULL)
4356 {
4357 curwin_save = curwin;
4358 curwin = win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004359 qf_set_title_var(qf_get_curlist(qi));
Bram Moolenaard823fa92016-08-12 16:29:27 +02004360 curwin = curwin_save;
4361 }
4362}
4363
4364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 * Find the quickfix buffer. If it exists, update the contents.
4366 */
4367 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004368qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369{
4370 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004371 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004374 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004375 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 if (buf != NULL)
4377 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004378 linenr_T old_line_count = buf->b_ml.ml_line_count;
4379
4380 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004381 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004382 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
Bram Moolenaard823fa92016-08-12 16:29:27 +02004384 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004385
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004386 qf_fill_buffer(qf_get_curlist(qi), buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004387 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004388
Bram Moolenaar864293a2016-06-02 13:40:04 +02004389 if (old_last == NULL)
4390 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004391 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004392
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004393 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004394 aucmd_restbuf(&aco);
4395 }
4396
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004397 // Only redraw when added lines are visible. This avoids flickering
4398 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004399 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4400 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
4402}
4403
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004404/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004405 * Add an error line to the quickfix buffer.
4406 */
4407 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004408qf_buf_add_line(
4409 qf_list_T *qfl, // quickfix list
4410 buf_T *buf, // quickfix window buffer
4411 linenr_T lnum,
4412 qfline_T *qfp,
4413 char_u *dirname)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004414{
4415 int len;
4416 buf_T *errbuf;
Bram Moolenaar858ba062020-05-31 23:11:59 +02004417 char_u *qftf;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004418
Bram Moolenaar858ba062020-05-31 23:11:59 +02004419 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4420 // the text to display
4421 qftf = p_qftf;
4422 // Use the local value of 'quickfixtextfunc' if it is set.
4423 if (qfl->qf_qftf != NULL)
4424 qftf = qfl->qf_qftf;
4425 if (qftf != NULL && *qftf != NUL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004426 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004427 char_u *qfbuf_text;
4428 typval_T args[1];
4429 dict_T *d;
4430
4431 // create 'info' dict argument
4432 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
4433 return FAIL;
4434 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4435 dict_add_number(d, "id", (long)qfl->qf_id);
4436 dict_add_number(d, "idx", (long)(lnum + 1));
4437 ++d->dv_refcount;
4438 args[0].v_type = VAR_DICT;
4439 args[0].vval.v_dict = d;
4440
4441 qfbuf_text = call_func_retstr(qftf, 1, args);
4442 --d->dv_refcount;
4443
4444 if (qfbuf_text == NULL)
4445 return FAIL;
4446
4447 vim_strncpy(IObuff, qfbuf_text, IOSIZE - 1);
4448 vim_free(qfbuf_text);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004449 }
4450 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004451 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004452 if (qfp->qf_module != NULL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004453 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004454 vim_strncpy(IObuff, qfp->qf_module, IOSIZE - 1);
4455 len = (int)STRLEN(IObuff);
4456 }
4457 else if (qfp->qf_fnum != 0
4458 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4459 && errbuf->b_fname != NULL)
4460 {
4461 if (qfp->qf_type == 1) // :helpgrep
4462 vim_strncpy(IObuff, gettail(errbuf->b_fname), IOSIZE - 1);
4463 else
4464 {
4465 // shorten the file name if not done already
4466 if (errbuf->b_sfname == NULL
4467 || mch_isFullName(errbuf->b_sfname))
4468 {
4469 if (*dirname == NUL)
4470 mch_dirname(dirname, MAXPATHL);
4471 shorten_buf_fname(errbuf, dirname, FALSE);
4472 }
4473 vim_strncpy(IObuff, errbuf->b_fname, IOSIZE - 1);
4474 }
4475 len = (int)STRLEN(IObuff);
4476 }
4477 else
4478 len = 0;
4479
4480 if (len < IOSIZE - 1)
4481 IObuff[len++] = '|';
4482
4483 if (qfp->qf_lnum > 0)
4484 {
4485 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%ld",
4486 qfp->qf_lnum);
4487 len += (int)STRLEN(IObuff + len);
4488
4489 if (qfp->qf_col > 0)
4490 {
4491 vim_snprintf((char *)IObuff + len, IOSIZE - len,
4492 " col %d", qfp->qf_col);
4493 len += (int)STRLEN(IObuff + len);
4494 }
4495
4496 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%s",
4497 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004498 len += (int)STRLEN(IObuff + len);
4499 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004500 else if (qfp->qf_pattern != NULL)
4501 {
4502 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4503 len += (int)STRLEN(IObuff + len);
4504 }
4505 if (len < IOSIZE - 2)
4506 {
4507 IObuff[len++] = '|';
4508 IObuff[len++] = ' ';
4509 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004510
Bram Moolenaar858ba062020-05-31 23:11:59 +02004511 // Remove newlines and leading whitespace from the text.
4512 // For an unrecognized line keep the indent, the compiler may
4513 // mark a word with ^^^^.
4514 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4515 IObuff + len, IOSIZE - len);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004516 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004517
4518 if (ml_append_buf(buf, lnum, IObuff,
4519 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4520 return FAIL;
4521
4522 return OK;
4523}
4524
4525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 * Fill current buffer with quickfix errors, replacing any previous contents.
4527 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004528 * If "old_last" is not NULL append the items after this one.
4529 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4530 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 */
4532 static void
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004533qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004535 linenr_T lnum;
4536 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004537 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538
Bram Moolenaar864293a2016-06-02 13:40:04 +02004539 if (old_last == NULL)
4540 {
4541 if (buf != curbuf)
4542 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004543 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004544 return;
4545 }
4546
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004547 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004548 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004549 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004552 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004553 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004555 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004556
4557 *dirname = NUL;
4558
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004559 // Add one line for each error
Bram Moolenaar99234f22020-02-10 22:56:54 +01004560 if (old_last == NULL || old_last->qf_next == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004561 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004562 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004563 lnum = 0;
4564 }
4565 else
4566 {
4567 qfp = old_last->qf_next;
4568 lnum = buf->b_ml.ml_line_count;
4569 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004570 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004572 if (qf_buf_add_line(qfl, buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004574
Bram Moolenaar864293a2016-06-02 13:40:04 +02004575 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004577 if (qfp == NULL)
4578 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004580
4581 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004582 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004583 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
4585
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004586 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 check_lnums(TRUE);
4588
Bram Moolenaar864293a2016-06-02 13:40:04 +02004589 if (old_last == NULL)
4590 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004591 // Set the 'filetype' to "qf" each time after filling the buffer.
4592 // This resembles reading a file into a buffer, it's more logical when
4593 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004594 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004595 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4596 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004598 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004599 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004601 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004603 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004604 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004605
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004606 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004607 redraw_curbuf_later(NOT_VALID);
4608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004610 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 KeyTyped = old_KeyTyped;
4612}
4613
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004614/*
4615 * For every change made to the quickfix list, update the changed tick.
4616 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004617 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004618qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004619{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004620 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004621}
4622
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004624 * Return the quickfix/location list number with the given identifier.
4625 * Returns -1 if list is not found.
4626 */
4627 static int
4628qf_id2nr(qf_info_T *qi, int_u qfid)
4629{
4630 int qf_idx;
4631
4632 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4633 if (qi->qf_lists[qf_idx].qf_id == qfid)
4634 return qf_idx;
4635 return INVALID_QFIDX;
4636}
4637
4638/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004639 * If the current list is not "save_qfid" and we can find the list with that ID
4640 * then make it the current list.
4641 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004642 * Returns OK if successfully restored the list. Returns FAIL if the list with
4643 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004644 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004645 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004646qf_restore_list(qf_info_T *qi, int_u save_qfid)
4647{
4648 int curlist;
4649
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004650 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004651 {
4652 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004653 if (curlist < 0)
4654 // list is not present
4655 return FAIL;
4656 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004657 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004658 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004659}
4660
4661/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004662 * Jump to the first entry if there is one.
4663 */
4664 static void
4665qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4666{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004667 if (qf_restore_list(qi, save_qfid) == FAIL)
4668 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004669
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004670 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004671 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004672 qf_jump(qi, 0, 0, forceit);
4673}
4674
4675/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004676 * Return TRUE when using ":vimgrep" for ":grep".
4677 */
4678 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004679grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004680{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004681 return ((cmdidx == CMD_grep
4682 || cmdidx == CMD_lgrep
4683 || cmdidx == CMD_grepadd
4684 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004685 && STRCMP("internal",
4686 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4687}
4688
4689/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004690 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004692 static char_u *
4693make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004695 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004696 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004697 case CMD_make: return (char_u *)"make";
4698 case CMD_lmake: return (char_u *)"lmake";
4699 case CMD_grep: return (char_u *)"grep";
4700 case CMD_lgrep: return (char_u *)"lgrep";
4701 case CMD_grepadd: return (char_u *)"grepadd";
4702 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4703 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705}
4706
4707/*
4708 * Return the name for the errorfile, in allocated memory.
4709 * Find a new unique name when 'makeef' contains "##".
4710 * Returns NULL for error.
4711 */
4712 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004713get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 char_u *p;
4716 char_u *name;
4717 static int start = -1;
4718 static int off = 0;
4719#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004720 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721#endif
4722
4723 if (*p_mef == NUL)
4724 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004725 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004727 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 return name;
4729 }
4730
4731 for (p = p_mef; *p; ++p)
4732 if (p[0] == '#' && p[1] == '#')
4733 break;
4734
4735 if (*p == NUL)
4736 return vim_strsave(p_mef);
4737
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004738 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 for (;;)
4740 {
4741 if (start == -1)
4742 start = mch_get_pid();
4743 else
4744 off += 19;
4745
Bram Moolenaar964b3742019-05-24 18:54:09 +02004746 name = alloc(STRLEN(p_mef) + 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (name == NULL)
4748 break;
4749 STRCPY(name, p_mef);
4750 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4751 STRCAT(name, p + 2);
4752 if (mch_getperm(name) < 0
4753#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004754 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 && mch_lstat((char *)name, &sb) < 0
4756#endif
4757 )
4758 break;
4759 vim_free(name);
4760 }
4761 return name;
4762}
4763
4764/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004765 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4766 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4767 */
4768 static char_u *
4769make_get_fullcmd(char_u *makecmd, char_u *fname)
4770{
4771 char_u *cmd;
4772 unsigned len;
4773
4774 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4775 if (*p_sp != NUL)
4776 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4777 cmd = alloc(len);
4778 if (cmd == NULL)
4779 return NULL;
4780 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4781 (char *)p_shq);
4782
4783 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4784 if (*p_sp != NUL)
4785 append_redir(cmd, len, p_sp, fname);
4786
4787 // Display the fully formed command. Output a newline if there's something
4788 // else than the :make command that was typed (in which case the cursor is
4789 // in column 0).
4790 if (msg_col == 0)
4791 msg_didout = FALSE;
4792 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004793 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004794 msg_outtrans(cmd); // show what we are doing
4795
4796 return cmd;
4797}
4798
4799/*
4800 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4801 */
4802 void
4803ex_make(exarg_T *eap)
4804{
4805 char_u *fname;
4806 char_u *cmd;
4807 char_u *enc = NULL;
4808 win_T *wp = NULL;
4809 qf_info_T *qi = &ql_info;
4810 int res;
4811 char_u *au_name = NULL;
4812 int_u save_qfid;
4813
4814 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4815 if (grep_internal(eap->cmdidx))
4816 {
4817 ex_vimgrep(eap);
4818 return;
4819 }
4820
4821 au_name = make_get_auname(eap->cmdidx);
4822 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4823 curbuf->b_fname, TRUE, curbuf))
4824 {
4825#ifdef FEAT_EVAL
4826 if (aborting())
4827 return;
4828#endif
4829 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004830 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004831
4832 if (is_loclist_cmd(eap->cmdidx))
4833 wp = curwin;
4834
4835 autowrite_all();
4836 fname = get_mef_name();
4837 if (fname == NULL)
4838 return;
4839 mch_remove(fname); // in case it's not unique
4840
4841 cmd = make_get_fullcmd(eap->arg, fname);
4842 if (cmd == NULL)
4843 return;
4844
4845 // let the shell know if we are redirecting output or not
4846 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4847
4848#ifdef AMIGA
4849 out_flush();
4850 // read window status report and redraw before message
4851 (void)char_avail();
4852#endif
4853
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004854 incr_quickfix_busy();
4855
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004856 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4857 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4858 (eap->cmdidx != CMD_grepadd
4859 && eap->cmdidx != CMD_lgrepadd),
4860 qf_cmdtitle(*eap->cmdlinep), enc);
4861 if (wp != NULL)
4862 {
4863 qi = GET_LOC_LIST(wp);
4864 if (qi == NULL)
4865 goto cleanup;
4866 }
4867 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004868 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004869
4870 // Remember the current quickfix list identifier, so that we can
4871 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004872 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004873 if (au_name != NULL)
4874 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4875 curbuf->b_fname, TRUE, curbuf);
4876 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4877 // display the first error
4878 qf_jump_first(qi, save_qfid, FALSE);
4879
4880cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004881 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004882 mch_remove(fname);
4883 vim_free(fname);
4884 vim_free(cmd);
4885}
4886
4887/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02004888 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004889 */
4890 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004891qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004892{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004893 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004894
4895 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4896 return 0;
4897 return qf_get_curlist(qi)->qf_count;
4898}
4899
4900/*
4901 * Returns the number of valid entries in the current quickfix/location list.
4902 */
4903 int
4904qf_get_valid_size(exarg_T *eap)
4905{
4906 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004907 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004908 qfline_T *qfp;
4909 int i, sz = 0;
4910 int prev_fnum = 0;
4911
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004912 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4913 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004914
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004915 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01004916 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004917 {
4918 if (qfp->qf_valid)
4919 {
4920 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004921 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004922 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4923 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004924 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004925 sz++;
4926 prev_fnum = qfp->qf_fnum;
4927 }
4928 }
4929 }
4930
4931 return sz;
4932}
4933
4934/*
4935 * Returns the current index of the quickfix/location list.
4936 * Returns 0 if there is an error.
4937 */
4938 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004939qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004940{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004941 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004942
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 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004947}
4948
4949/*
4950 * Returns the current index in the quickfix/location list (counting only valid
4951 * entries). If no valid entries are in the list, then returns 1.
4952 */
4953 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004954qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004955{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004956 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004957 qf_list_T *qfl;
4958 qfline_T *qfp;
4959 int i, eidx = 0;
4960 int prev_fnum = 0;
4961
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004962 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4963 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004964
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004965 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004966 qfp = qfl->qf_start;
4967
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004968 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004969 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004970 return 1;
4971
4972 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4973 {
4974 if (qfp->qf_valid)
4975 {
4976 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4977 {
4978 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4979 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004980 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004981 eidx++;
4982 prev_fnum = qfp->qf_fnum;
4983 }
4984 }
4985 else
4986 eidx++;
4987 }
4988 }
4989
4990 return eidx ? eidx : 1;
4991}
4992
4993/*
4994 * Get the 'n'th valid error entry in the quickfix or location list.
4995 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4996 * For :cdo and :ldo returns the 'n'th valid error entry.
4997 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4998 */
4999 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005000qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005001{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005002 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005003 int i, eidx;
5004 int prev_fnum = 0;
5005
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005006 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005007 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005008 return 1;
5009
Bram Moolenaar95946f12019-03-31 15:31:59 +02005010 eidx = 0;
5011 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005012 {
5013 if (qfp->qf_valid)
5014 {
5015 if (fdo)
5016 {
5017 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5018 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005019 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005020 eidx++;
5021 prev_fnum = qfp->qf_fnum;
5022 }
5023 }
5024 else
5025 eidx++;
5026 }
5027
5028 if (eidx == n)
5029 break;
5030 }
5031
5032 if (i <= qfl->qf_count)
5033 return i;
5034 else
5035 return 1;
5036}
5037
5038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005040 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005041 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 */
5043 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005044ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005046 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005047 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005048
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005049 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5050 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005051
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005052 if (eap->addr_count > 0)
5053 errornr = (int)eap->line2;
5054 else
5055 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005056 switch (eap->cmdidx)
5057 {
5058 case CMD_cc: case CMD_ll:
5059 errornr = 0;
5060 break;
5061 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5062 case CMD_lfirst:
5063 errornr = 1;
5064 break;
5065 default:
5066 errornr = 32767;
5067 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005068 }
5069
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005070 // For cdo and ldo commands, jump to the nth valid error.
5071 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005072 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5073 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005074 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005075 eap->addr_count > 0 ? (int)eap->line1 : 1,
5076 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5077
5078 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079}
5080
5081/*
5082 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005083 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005084 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 */
5086 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005087ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005089 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005090 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005091 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005092
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005093 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5094 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005095
Bram Moolenaar55b69262017-08-13 13:42:01 +02005096 if (eap->addr_count > 0
5097 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5098 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005099 errornr = (int)eap->line2;
5100 else
5101 errornr = 1;
5102
Bram Moolenaar39665952018-08-15 20:59:48 +02005103 // Depending on the command jump to either next or previous entry/file.
5104 switch (eap->cmdidx)
5105 {
5106 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5107 dir = FORWARD;
5108 break;
5109 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5110 case CMD_lNext:
5111 dir = BACKWARD;
5112 break;
5113 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5114 dir = FORWARD_FILE;
5115 break;
5116 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5117 dir = BACKWARD_FILE;
5118 break;
5119 default:
5120 dir = FORWARD;
5121 break;
5122 }
5123
5124 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125}
5126
5127/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005128 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5129 * The index of the entry is stored in 'errornr'.
5130 * Returns NULL if an entry is not found.
5131 */
5132 static qfline_T *
5133qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5134{
5135 qfline_T *qfp = NULL;
5136 int idx = 0;
5137
5138 // Find the first entry in this file
5139 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5140 if (qfp->qf_fnum == bnr)
5141 break;
5142
5143 *errornr = idx;
5144 return qfp;
5145}
5146
5147/*
5148 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5149 * with the error number for the first entry. Assumes the entries are sorted in
5150 * the quickfix list by line number.
5151 */
5152 static qfline_T *
5153qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5154{
5155 while (!got_int
5156 && entry->qf_prev != NULL
5157 && entry->qf_fnum == entry->qf_prev->qf_fnum
5158 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5159 {
5160 entry = entry->qf_prev;
5161 --*errornr;
5162 }
5163
5164 return entry;
5165}
5166
5167/*
5168 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5169 * with the error number for the last entry. Assumes the entries are sorted in
5170 * the quickfix list by line number.
5171 */
5172 static qfline_T *
5173qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5174{
5175 while (!got_int &&
5176 entry->qf_next != NULL
5177 && entry->qf_fnum == entry->qf_next->qf_fnum
5178 && entry->qf_lnum == entry->qf_next->qf_lnum)
5179 {
5180 entry = entry->qf_next;
5181 ++*errornr;
5182 }
5183
5184 return entry;
5185}
5186
5187/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005188 * Returns TRUE if the specified quickfix entry is
5189 * after the given line (linewise is TRUE)
5190 * or after the line and column.
5191 */
5192 static int
5193qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5194{
5195 if (linewise)
5196 return qfp->qf_lnum > pos->lnum;
5197 else
5198 return (qfp->qf_lnum > pos->lnum ||
5199 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5200}
5201
5202/*
5203 * Returns TRUE if the specified quickfix entry is
5204 * before the given line (linewise is TRUE)
5205 * or before the line and column.
5206 */
5207 static int
5208qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5209{
5210 if (linewise)
5211 return qfp->qf_lnum < pos->lnum;
5212 else
5213 return (qfp->qf_lnum < pos->lnum ||
5214 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5215}
5216
5217/*
5218 * Returns TRUE if the specified quickfix entry is
5219 * on or after the given line (linewise is TRUE)
5220 * or on or after the line and column.
5221 */
5222 static int
5223qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5224{
5225 if (linewise)
5226 return qfp->qf_lnum >= pos->lnum;
5227 else
5228 return (qfp->qf_lnum > pos->lnum ||
5229 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5230}
5231
5232/*
5233 * Returns TRUE if the specified quickfix entry is
5234 * on or before the given line (linewise is TRUE)
5235 * or on or before the line and column.
5236 */
5237 static int
5238qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5239{
5240 if (linewise)
5241 return qfp->qf_lnum <= pos->lnum;
5242 else
5243 return (qfp->qf_lnum < pos->lnum ||
5244 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5245}
5246
5247/*
5248 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5249 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5250 * multiple entries on a single line as one. Otherwise returns the entry after
5251 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005252 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5253 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005254 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005255 */
5256 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005257qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005258 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005259 pos_T *pos,
5260 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005261 qfline_T *qfp,
5262 int *errornr)
5263{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005264 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005265 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005266 return qfp;
5267
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005268 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005269 while (qfp->qf_next != NULL
5270 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005271 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005272 {
5273 qfp = qfp->qf_next;
5274 ++*errornr;
5275 }
5276
5277 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005278 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005279 return NULL;
5280
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005281 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005282 qfp = qfp->qf_next;
5283 ++*errornr;
5284
5285 return qfp;
5286}
5287
5288/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005289 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5290 * If 'linewise' is TRUE, returns the entry before the specified line and
5291 * treats multiple entries on a single line as one. Otherwise returns the entry
5292 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005293 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5294 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005295 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005296 */
5297 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005298qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005299 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005300 pos_T *pos,
5301 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005302 qfline_T *qfp,
5303 int *errornr)
5304{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005305 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005306 while (qfp->qf_next != NULL
5307 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005308 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005309 {
5310 qfp = qfp->qf_next;
5311 ++*errornr;
5312 }
5313
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005314 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005315 return NULL;
5316
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005317 if (linewise)
5318 // If multiple entries are on the same line, then use the first entry
5319 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005320
5321 return qfp;
5322}
5323
5324/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005325 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005326 * the direction 'dir'.
5327 */
5328 static qfline_T *
5329qf_find_closest_entry(
5330 qf_list_T *qfl,
5331 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005332 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005333 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005334 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005335 int *errornr)
5336{
5337 qfline_T *qfp;
5338
5339 *errornr = 0;
5340
5341 // Find the first entry in this file
5342 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5343 if (qfp == NULL)
5344 return NULL; // no entry in this file
5345
5346 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005347 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005348 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005349 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005350
5351 return qfp;
5352}
5353
5354/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005355 * Get the nth quickfix entry below the specified entry. Searches forward in
5356 * the list. If linewise is TRUE, then treat multiple entries on a single line
5357 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005358 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005359 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005360qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005361{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005362 qfline_T *entry = entry_arg;
5363
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005364 while (n-- > 0 && !got_int)
5365 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005366 int first_errornr = *errornr;
5367
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005368 if (linewise)
5369 // Treat all the entries on the same line in this file as one
5370 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005371
5372 if (entry->qf_next == NULL
5373 || entry->qf_next->qf_fnum != entry->qf_fnum)
5374 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005375 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005376 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005377 break;
5378 }
5379
5380 entry = entry->qf_next;
5381 ++*errornr;
5382 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005383}
5384
5385/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005386 * Get the nth quickfix entry above the specified entry. Searches backwards 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 Moolenaarcf6a55c2019-05-05 15:02:30 +02005391qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005392{
5393 while (n-- > 0 && !got_int)
5394 {
5395 if (entry->qf_prev == NULL
5396 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5397 break;
5398
5399 entry = entry->qf_prev;
5400 --*errornr;
5401
5402 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005403 if (linewise)
5404 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005405 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005406}
5407
5408/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005409 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5410 * the specified direction. Returns the error number in the quickfix list or 0
5411 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005412 */
5413 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005414qf_find_nth_adj_entry(
5415 qf_list_T *qfl,
5416 int bnr,
5417 pos_T *pos,
5418 int n,
5419 int dir,
5420 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005421{
5422 qfline_T *adj_entry;
5423 int errornr;
5424
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005425 // Find an entry closest to the specified position
5426 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005427 if (adj_entry == NULL)
5428 return 0;
5429
5430 if (--n > 0)
5431 {
5432 // Go to the n'th entry in the current buffer
5433 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005434 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005435 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005436 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005437 }
5438
5439 return errornr;
5440}
5441
5442/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005443 * Jump to a quickfix entry in the current file nearest to the current line or
5444 * current line/col.
5445 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5446 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005447 */
5448 void
5449ex_cbelow(exarg_T *eap)
5450{
5451 qf_info_T *qi;
5452 qf_list_T *qfl;
5453 int dir;
5454 int buf_has_flag;
5455 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005456 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005457
5458 if (eap->addr_count > 0 && eap->line2 <= 0)
5459 {
5460 emsg(_(e_invrange));
5461 return;
5462 }
5463
5464 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005465 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5466 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005467 buf_has_flag = BUF_HAS_QF_ENTRY;
5468 else
5469 buf_has_flag = BUF_HAS_LL_ENTRY;
5470 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5471 {
5472 emsg(_(e_quickfix));
5473 return;
5474 }
5475
5476 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5477 return;
5478
5479 qfl = qf_get_curlist(qi);
5480 // check if the list has valid errors
5481 if (!qf_list_has_valid_entries(qfl))
5482 {
5483 emsg(_(e_quickfix));
5484 return;
5485 }
5486
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005487 if (eap->cmdidx == CMD_cbelow
5488 || eap->cmdidx == CMD_lbelow
5489 || eap->cmdidx == CMD_cafter
5490 || eap->cmdidx == CMD_lafter)
5491 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005492 dir = FORWARD;
5493 else
5494 dir = BACKWARD;
5495
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005496 pos = curwin->w_cursor;
5497 // A quickfix entry column number is 1 based whereas cursor column
5498 // number is 0 based. Adjust the column number.
5499 pos.col++;
5500 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5501 eap->addr_count > 0 ? eap->line2 : 0, dir,
5502 eap->cmdidx == CMD_cbelow
5503 || eap->cmdidx == CMD_lbelow
5504 || eap->cmdidx == CMD_cabove
5505 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005506
5507 if (errornr > 0)
5508 qf_jump(qi, 0, errornr, FALSE);
5509 else
5510 emsg(_(e_no_more_items));
5511}
5512
5513/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005514 * Return the autocmd name for the :cfile Ex commands
5515 */
5516 static char_u *
5517cfile_get_auname(cmdidx_T cmdidx)
5518{
5519 switch (cmdidx)
5520 {
5521 case CMD_cfile: return (char_u *)"cfile";
5522 case CMD_cgetfile: return (char_u *)"cgetfile";
5523 case CMD_caddfile: return (char_u *)"caddfile";
5524 case CMD_lfile: return (char_u *)"lfile";
5525 case CMD_lgetfile: return (char_u *)"lgetfile";
5526 case CMD_laddfile: return (char_u *)"laddfile";
5527 default: return NULL;
5528 }
5529}
5530
5531/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005532 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005533 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 */
5535 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005536ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005538 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005539 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005540 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005541 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005542 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005543 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005544
Bram Moolenaara16123a2019-03-28 20:31:07 +01005545 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005546 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5547 NULL, FALSE, curbuf))
5548 {
5549#ifdef FEAT_EVAL
5550 if (aborting())
5551 return;
5552#endif
5553 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005554
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005555 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005556#ifdef FEAT_BROWSE
5557 if (cmdmod.browse)
5558 {
5559 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005560 NULL, NULL,
5561 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005562 if (browse_file == NULL)
5563 return;
5564 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5565 vim_free(browse_file);
5566 }
5567 else
5568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005570 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005571
Bram Moolenaar39665952018-08-15 20:59:48 +02005572 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005573 wp = curwin;
5574
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005575 incr_quickfix_busy();
5576
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005577 // This function is used by the :cfile, :cgetfile and :caddfile
5578 // commands.
5579 // :cfile always creates a new quickfix list and jumps to the
5580 // first error.
5581 // :cgetfile creates a new quickfix list but doesn't jump to the
5582 // first error.
5583 // :caddfile adds to an existing quickfix list. If there is no
5584 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005585 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005586 && eap->cmdidx != CMD_laddfile),
5587 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005588 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005589 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005590 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005591 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005592 {
5593 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005594 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005595 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005596 }
5597 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005598 qf_list_changed(qf_get_curlist(qi));
5599 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005600 if (au_name != NULL)
5601 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005602
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005603 // Jump to the first error for a new list and if autocmds didn't
5604 // free the list.
5605 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5606 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005607 // display the first error
5608 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005609
5610 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005611}
5612
5613/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005614 * Return the vimgrep autocmd name.
5615 */
5616 static char_u *
5617vgr_get_auname(cmdidx_T cmdidx)
5618{
5619 switch (cmdidx)
5620 {
5621 case CMD_vimgrep: return (char_u *)"vimgrep";
5622 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5623 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5624 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5625 case CMD_grep: return (char_u *)"grep";
5626 case CMD_lgrep: return (char_u *)"lgrep";
5627 case CMD_grepadd: return (char_u *)"grepadd";
5628 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5629 default: return NULL;
5630 }
5631}
5632
5633/*
5634 * Initialize the regmatch used by vimgrep for pattern "s".
5635 */
5636 static void
5637vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5638{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005639 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005640 regmatch->regprog = NULL;
5641
5642 if (s == NULL || *s == NUL)
5643 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005644 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005645 if (last_search_pat() == NULL)
5646 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005647 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005648 return;
5649 }
5650 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5651 }
5652 else
5653 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5654
5655 regmatch->rmm_ic = p_ic;
5656 regmatch->rmm_maxcol = 0;
5657}
5658
5659/*
5660 * Display a file name when vimgrep is running.
5661 */
5662 static void
5663vgr_display_fname(char_u *fname)
5664{
5665 char_u *p;
5666
5667 msg_start();
5668 p = msg_strtrunc(fname, TRUE);
5669 if (p == NULL)
5670 msg_outtrans(fname);
5671 else
5672 {
5673 msg_outtrans(p);
5674 vim_free(p);
5675 }
5676 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005677 msg_didout = FALSE; // overwrite this message
5678 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005679 msg_col = 0;
5680 out_flush();
5681}
5682
5683/*
5684 * Load a dummy buffer to search for a pattern using vimgrep.
5685 */
5686 static buf_T *
5687vgr_load_dummy_buf(
5688 char_u *fname,
5689 char_u *dirname_start,
5690 char_u *dirname_now)
5691{
5692 int save_mls;
5693#if defined(FEAT_SYN_HL)
5694 char_u *save_ei = NULL;
5695#endif
5696 buf_T *buf;
5697
5698#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005699 // Don't do Filetype autocommands to avoid loading syntax and
5700 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005701 save_ei = au_event_disable(",Filetype");
5702#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005703 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005704 save_mls = p_mls;
5705 p_mls = 0;
5706
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005707 // Load file into a buffer, so that 'fileencoding' is detected,
5708 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005709 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5710
5711 p_mls = save_mls;
5712#if defined(FEAT_SYN_HL)
5713 au_event_restore(save_ei);
5714#endif
5715
5716 return buf;
5717}
5718
5719/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005720 * Check whether a quickfix/location list is valid. Autocmds may remove or
5721 * change a quickfix list when vimgrep is running. If the list is not found,
5722 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005723 */
5724 static int
5725vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005726 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005727 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005728 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005729 char_u *title)
5730{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005731 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005732 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005733 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005734 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005735 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005736 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005737 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005738 return FALSE;
5739 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005740 else
5741 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005742 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005743 qf_new_list(qi, title);
5744 return TRUE;
5745 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005746 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005747
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005748 if (qf_restore_list(qi, qfid) == FAIL)
5749 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005750
5751 return TRUE;
5752}
5753
5754/*
5755 * Search for a pattern in all the lines in a buffer and add the matching lines
5756 * to a quickfix list.
5757 */
5758 static int
5759vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005760 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005761 char_u *fname,
5762 buf_T *buf,
5763 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005764 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005765 int duplicate_name,
5766 int flags)
5767{
5768 int found_match = FALSE;
5769 long lnum;
5770 colnr_T col;
5771
Bram Moolenaar1c299432018-10-28 14:36:09 +01005772 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005773 {
5774 col = 0;
5775 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5776 col, NULL, NULL) > 0)
5777 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005778 // Pass the buffer number so that it gets used even for a
5779 // dummy buffer, unless duplicate_name is set, then the
5780 // buffer will be wiped out below.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005781 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005782 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005783 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005784 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005785 duplicate_name ? 0 : buf->b_fnum,
5786 ml_get_buf(buf,
5787 regmatch->startpos[0].lnum + lnum, FALSE),
5788 regmatch->startpos[0].lnum + lnum,
5789 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005790 FALSE, // vis_col
5791 NULL, // search pattern
5792 0, // nr
5793 0, // type
5794 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02005795 ) == QF_FAIL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005796 {
5797 got_int = TRUE;
5798 break;
5799 }
5800 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005801 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005802 break;
5803 if ((flags & VGR_GLOBAL) == 0
5804 || regmatch->endpos[0].lnum > 0)
5805 break;
5806 col = regmatch->endpos[0].col
5807 + (col == regmatch->endpos[0].col);
5808 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5809 break;
5810 }
5811 line_breakcheck();
5812 if (got_int)
5813 break;
5814 }
5815
5816 return found_match;
5817}
5818
5819/*
5820 * Jump to the first match and update the directory.
5821 */
5822 static void
5823vgr_jump_to_match(
5824 qf_info_T *qi,
5825 int forceit,
5826 int *redraw_for_dummy,
5827 buf_T *first_match_buf,
5828 char_u *target_dir)
5829{
5830 buf_T *buf;
5831
5832 buf = curbuf;
5833 qf_jump(qi, 0, 0, forceit);
5834 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005835 // If we jumped to another buffer redrawing will already be
5836 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005837 *redraw_for_dummy = FALSE;
5838
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005839 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005840 if (curbuf == first_match_buf && target_dir != NULL)
5841 {
5842 exarg_T ea;
5843
Bram Moolenaara80faa82020-04-12 19:37:17 +02005844 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005845 ea.arg = target_dir;
5846 ea.cmdidx = CMD_lcd;
5847 ex_cd(&ea);
5848 }
5849}
5850
5851/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005852 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00005853 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005854typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00005855{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005856 long tomatch; // maximum number of matches to find
5857 char_u *spat; // search pattern
5858 int flags; // search modifier
5859 char_u **fnames; // list of files to search
5860 int fcount; // number of files
5861 regmmatch_T regmatch; // compiled search pattern
5862 char_u *qf_title; // quickfix list title
5863} vgr_args_T;
5864
5865/*
5866 * Process :vimgrep command arguments. The command syntax is:
5867 *
5868 * :{count}vimgrep /{pattern}/[g][j] {file} ...
5869 */
5870 static int
5871vgr_process_args(
5872 exarg_T *eap,
5873 vgr_args_T *args)
5874{
Bram Moolenaar748bf032005-02-02 23:04:36 +00005875 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005876
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005877 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005878
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005879 args->regmatch.regprog = NULL;
5880 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00005881
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005882 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005883 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005884 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005885 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005886
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005887 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005888 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005889 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005891 emsg(_(e_invalpat));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005892 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005893 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005894
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005895 vgr_init_regmatch(&args->regmatch, args->spat);
5896 if (args->regmatch.regprog == NULL)
5897 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005898
5899 p = skipwhite(p);
5900 if (*p == NUL)
5901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005902 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005903 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005904 }
5905
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005906 // parse the list of arguments
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005907 if (get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL)
5908 return FAIL;
5909 if (args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005910 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005911 emsg(_(e_nomatch));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005912 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005913 }
5914
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005915 return OK;
5916}
5917
5918/*
5919 * Search for a pattern in a list of files and populate the quickfix list with
5920 * the matches.
5921 */
5922 static int
5923vgr_process_files(
5924 win_T *wp,
5925 qf_info_T *qi,
5926 vgr_args_T *cmd_args,
5927 int *redraw_for_dummy,
5928 buf_T **first_match_buf,
5929 char_u **target_dir)
5930{
5931 int status = FAIL;
5932 int_u save_qfid = qf_get_curlist(qi)->qf_id;
5933 time_t seconds = 0;
5934 char_u *fname;
5935 int fi;
5936 buf_T *buf;
5937 int duplicate_name = FALSE;
5938 int using_dummy;
5939 char_u *dirname_start = NULL;
5940 char_u *dirname_now = NULL;
5941 int found_match;
5942 aco_save_T aco;
5943
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005944 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5945 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005946 if (dirname_start == NULL || dirname_now == NULL)
5947 goto theend;
5948
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005949 // Remember the current directory, because a BufRead autocommand that does
5950 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005951 mch_dirname(dirname_start, MAXPATHL);
5952
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005953 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005954 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
5955 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005956 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005957 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005958 if (time(NULL) > seconds)
5959 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005960 // Display the file name every second or so, show the user we are
5961 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005962 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005963 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005964 }
5965
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005966 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00005967 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5968 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005969 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005970 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005971 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005972 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005973
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005974 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005975 }
5976 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005977 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005978 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005979
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005980 // Check whether the quickfix list is still valid. When loading a
5981 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005982 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005983 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005984
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005985 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005986
Bram Moolenaar81695252004-12-29 20:58:21 +00005987 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005988 {
5989 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005990 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005991 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005992 else
5993 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005994 // Try for a match in all lines of the buffer.
5995 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005996 found_match = vgr_match_buflines(qf_get_curlist(qi),
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005997 fname, buf, &cmd_args->regmatch,
5998 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005999
Bram Moolenaar81695252004-12-29 20:58:21 +00006000 if (using_dummy)
6001 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006002 if (found_match && *first_match_buf == NULL)
6003 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006004 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006005 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006006 // Never keep a dummy buffer if there is another buffer
6007 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006008 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006009 buf = NULL;
6010 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00006011 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006012 || buf->b_p_bh[0] == 'u' // "unload"
6013 || buf->b_p_bh[0] == 'w' // "wipe"
6014 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006015 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006016 // When no match was found we don't need to remember the
6017 // buffer, wipe it out. If there was a match and it
6018 // wasn't the first one or we won't jump there: only
6019 // unload the buffer.
6020 // Ignore 'hidden' here, because it may lead to having too
6021 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006022 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006023 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006024 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006025 buf = NULL;
6026 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006027 else if (buf != *first_match_buf
6028 || (cmd_args->flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006029 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006030 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006031 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006032 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006033 buf = NULL;
6034 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006035 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006036
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006037 if (buf != NULL)
6038 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006039 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006040 buf->b_flags &= ~BF_DUMMY;
6041
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006042 // If the buffer is still loaded we need to use the
6043 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006044 if (buf == *first_match_buf
6045 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006046 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006047 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006048
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006049 // The buffer is still loaded, the Filetype autocommands
6050 // need to be done now, in that buffer. And the modelines
6051 // need to be done (again). But not the window-local
6052 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006053 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006054#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006055 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6056 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006057#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006058 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006059 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006060 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006061 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006062 }
6063 }
6064
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006065 status = OK;
6066
6067theend:
6068 vim_free(dirname_now);
6069 vim_free(dirname_start);
6070 return status;
6071}
6072
6073/*
6074 * ":vimgrep {pattern} file(s)"
6075 * ":vimgrepadd {pattern} file(s)"
6076 * ":lvimgrep {pattern} file(s)"
6077 * ":lvimgrepadd {pattern} file(s)"
6078 */
6079 void
6080ex_vimgrep(exarg_T *eap)
6081{
6082 vgr_args_T args;
6083 qf_info_T *qi;
6084 qf_list_T *qfl;
6085 int_u save_qfid;
6086 win_T *wp = NULL;
6087 int redraw_for_dummy = FALSE;
6088 buf_T *first_match_buf = NULL;
6089 char_u *target_dir = NULL;
6090 char_u *au_name = NULL;
6091 int status;
6092
6093 au_name = vgr_get_auname(eap->cmdidx);
6094 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6095 curbuf->b_fname, TRUE, curbuf))
6096 {
6097#ifdef FEAT_EVAL
6098 if (aborting())
6099 return;
6100#endif
6101 }
6102
6103 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6104 if (qi == NULL)
6105 return;
6106
6107 if (vgr_process_args(eap, &args) == FAIL)
6108 goto theend;
6109
6110 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6111 && eap->cmdidx != CMD_vimgrepadd
6112 && eap->cmdidx != CMD_lvimgrepadd)
6113 || qf_stack_empty(qi))
6114 // make place for a new list
6115 qf_new_list(qi, args.qf_title);
6116
6117 incr_quickfix_busy();
6118
6119 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6120 &first_match_buf, &target_dir);
6121 if (status != OK)
6122 {
6123 FreeWild(args.fcount, args.fnames);
6124 decr_quickfix_busy();
6125 goto theend;
6126 }
6127
6128 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006129
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006130 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006131 qfl->qf_nonevalid = FALSE;
6132 qfl->qf_ptr = qfl->qf_start;
6133 qfl->qf_index = 1;
6134 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006135
Bram Moolenaar864293a2016-06-02 13:40:04 +02006136 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006137
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006138 // Remember the current quickfix list identifier, so that we can check for
6139 // autocommands changing the current quickfix list.
6140 save_qfid = qf_get_curlist(qi)->qf_id;
6141
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006142 if (au_name != NULL)
6143 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6144 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006145 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6146 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006147 if (!qflist_valid(wp, save_qfid)
6148 || qf_restore_list(qi, save_qfid) == FAIL)
6149 {
6150 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006151 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006152 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006153
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006154 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006155 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006156 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006157 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006158 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6159 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006160 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006161 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006162 semsg(_(e_nomatch2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006163
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006164 decr_quickfix_busy();
6165
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006166 // If we loaded a dummy buffer into the current window, the autocommands
6167 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006168 if (redraw_for_dummy)
6169 {
6170#ifdef FEAT_FOLDING
6171 foldUpdateAll(curwin);
6172#else
6173 redraw_later(NOT_VALID);
6174#endif
6175 }
6176
Bram Moolenaar86b68352004-12-27 21:59:20 +00006177theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006178 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006179 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006180 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006181}
6182
6183/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006184 * Restore current working directory to "dirname_start" if they differ, taking
6185 * into account whether it is set locally or globally.
6186 */
6187 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006188restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006189{
6190 char_u *dirname_now = alloc(MAXPATHL);
6191
6192 if (NULL != dirname_now)
6193 {
6194 mch_dirname(dirname_now, MAXPATHL);
6195 if (STRCMP(dirname_start, dirname_now) != 0)
6196 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006197 // If the directory has changed, change it back by building up an
6198 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006199 exarg_T ea;
6200
Bram Moolenaara80faa82020-04-12 19:37:17 +02006201 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006202 ea.arg = dirname_start;
6203 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6204 ex_cd(&ea);
6205 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006206 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006207 }
6208}
6209
6210/*
6211 * Load file "fname" into a dummy buffer and return the buffer pointer,
6212 * placing the directory resulting from the buffer load into the
6213 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6214 * prior to calling this function. Restores directory to "dirname_start" prior
6215 * to returning, if autocmds or the 'autochdir' option have changed it.
6216 *
6217 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6218 * or wipe_dummy_buffer() later!
6219 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006220 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006221 */
6222 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006223load_dummy_buffer(
6224 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006225 char_u *dirname_start, // in: old directory
6226 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006227{
6228 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006229 bufref_T newbufref;
6230 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006231 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006232 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006233 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006234
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006235 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006236 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6237 if (newbuf == NULL)
6238 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006239 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006240
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006241 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006242 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6243
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006244 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006245 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006246 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006247 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006248 ++newbuf->b_locked;
6249
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006250 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006251 aucmd_prepbuf(&aco, newbuf);
6252
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006253 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006254 (void)setfname(curbuf, fname, NULL, FALSE);
6255
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006256 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006257 check_need_swap(TRUE);
6258
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006259 // Remove the "dummy" flag, otherwise autocommands may not
6260 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006261 curbuf->b_flags &= ~BF_DUMMY;
6262
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006263 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006264 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006265 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006266 NULL, READ_NEW | READ_DUMMY);
6267 --newbuf->b_locked;
6268 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006269 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006270 && !(curbuf->b_flags & BF_NEW))
6271 {
6272 failed = FALSE;
6273 if (curbuf != newbuf)
6274 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006275 // Bloody autocommands changed the buffer! Can happen when
6276 // using netrw and editing a remote file. Use the current
6277 // buffer instead, delete the dummy one after restoring the
6278 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006279 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006280 newbuf = curbuf;
6281 }
6282 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006283
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006284 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006285 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006286 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6287 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006288
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006289 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6290 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006291 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006292 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006293
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006294 // When autocommands/'autochdir' option changed directory: go back.
6295 // Let the caller know what the resulting dir was first, in case it is
6296 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006297 mch_dirname(resulting_dir, MAXPATHL);
6298 restore_start_dir(dirname_start);
6299
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006300 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006301 return NULL;
6302 if (failed)
6303 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006304 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006305 return NULL;
6306 }
6307 return newbuf;
6308}
6309
6310/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006311 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6312 * directory to "dirname_start" prior to returning, if autocmds or the
6313 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006314 */
6315 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006316wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006317{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006318 // If any autocommand opened a window on the dummy buffer, close that
6319 // window. If we can't close them all then give up.
6320 while (buf->b_nwindows > 0)
6321 {
6322 int did_one = FALSE;
6323 win_T *wp;
6324
6325 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006326 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006327 if (wp->w_buffer == buf)
6328 {
6329 if (win_close(wp, FALSE) == OK)
6330 did_one = TRUE;
6331 break;
6332 }
6333 if (!did_one)
6334 return;
6335 }
6336
6337 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006338 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006339#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006340 cleanup_T cs;
6341
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006342 // Reset the error/interrupt/exception state here so that aborting()
6343 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6344 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006345 enter_cleanup(&cs);
6346#endif
6347
Bram Moolenaar81695252004-12-29 20:58:21 +00006348 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006349
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006350#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006351 // Restore the error/interrupt/exception state if not discarded by a
6352 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006353 leave_cleanup(&cs);
6354#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006355 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006356 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006357 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006358}
6359
6360/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006361 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6362 * directory to "dirname_start" prior to returning, if autocmds or the
6363 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006364 */
6365 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006366unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006367{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006368 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006369 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006370 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006371
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006372 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006373 restore_start_dir(dirname_start);
6374 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006375}
6376
Bram Moolenaar05159a02005-02-26 23:04:13 +00006377#if defined(FEAT_EVAL) || defined(PROTO)
6378/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006379 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006380 * to 'list'. Returns OK on success.
6381 */
6382 static int
6383get_qfline_items(qfline_T *qfp, list_T *list)
6384{
6385 int bufnum;
6386 dict_T *dict;
6387 char_u buf[2];
6388
6389 // Handle entries with a non-existing buffer number.
6390 bufnum = qfp->qf_fnum;
6391 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6392 bufnum = 0;
6393
6394 if ((dict = dict_alloc()) == NULL)
6395 return FAIL;
6396 if (list_append_dict(list, dict) == FAIL)
6397 return FAIL;
6398
6399 buf[0] = qfp->qf_type;
6400 buf[1] = NUL;
6401 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
6402 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6403 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6404 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6405 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
6406 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6407 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6408 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6409 || dict_add_string(dict, "type", buf) == FAIL
6410 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6411 return FAIL;
6412
6413 return OK;
6414}
6415
6416/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006417 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006418 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006419 * If eidx is not 0, then return only the specified entry. Otherwise return
6420 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006421 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006422 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006423get_errorlist(
6424 qf_info_T *qi_arg,
6425 win_T *wp,
6426 int qf_idx,
6427 int eidx,
6428 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006429{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006430 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006431 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006432 qfline_T *qfp;
6433 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006435 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006436 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006437 qi = &ql_info;
6438 if (wp != NULL)
6439 {
6440 qi = GET_LOC_LIST(wp);
6441 if (qi == NULL)
6442 return FAIL;
6443 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006444 }
6445
Bram Moolenaar858ba062020-05-31 23:11:59 +02006446 if (eidx < 0)
6447 return OK;
6448
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006449 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006450 qf_idx = qi->qf_curlist;
6451
Bram Moolenaar0398e002019-03-21 21:12:49 +01006452 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006453 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006454
Bram Moolenaar0398e002019-03-21 21:12:49 +01006455 qfl = qf_get_list(qi, qf_idx);
6456 if (qf_list_empty(qfl))
6457 return FAIL;
6458
Bram Moolenaara16123a2019-03-28 20:31:07 +01006459 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006460 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006461 if (eidx > 0)
6462 {
6463 if (eidx == i)
6464 return get_qfline_items(qfp, list);
6465 }
6466 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006467 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006468 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006469
Bram Moolenaar05159a02005-02-26 23:04:13 +00006470 return OK;
6471}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006472
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006473// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006474enum {
6475 QF_GETLIST_NONE = 0x0,
6476 QF_GETLIST_TITLE = 0x1,
6477 QF_GETLIST_ITEMS = 0x2,
6478 QF_GETLIST_NR = 0x4,
6479 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006480 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006481 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006482 QF_GETLIST_IDX = 0x40,
6483 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006484 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006485 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006486 QF_GETLIST_QFBUFNR = 0x400,
6487 QF_GETLIST_ALL = 0x7FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006488};
6489
6490/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006491 * Parse text from 'di' and return the quickfix list items.
6492 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006493 */
6494 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006495qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006496{
6497 int status = FAIL;
6498 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006499 char_u *errorformat = p_efm;
6500 dictitem_T *efm_di;
6501 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006502
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006503 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006504 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006505 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006506 // If errorformat is supplied then use it, otherwise use the 'efm'
6507 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006508 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6509 {
6510 if (efm_di->di_tv.v_type != VAR_STRING ||
6511 efm_di->di_tv.vval.v_string == NULL)
6512 return FAIL;
6513 errorformat = efm_di->di_tv.vval.v_string;
6514 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006515
Bram Moolenaar36538222017-09-02 19:51:44 +02006516 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006517 if (l == NULL)
6518 return FAIL;
6519
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006520 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006521 if (qi != NULL)
6522 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006523 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006524 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6525 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006526 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006527 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006528 }
6529 free(qi);
6530 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006531 dict_add_list(retdict, "items", l);
6532 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006533 }
6534
6535 return status;
6536}
6537
6538/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006539 * Return the quickfix/location list window identifier in the current tabpage.
6540 */
6541 static int
6542qf_winid(qf_info_T *qi)
6543{
6544 win_T *win;
6545
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006546 // The quickfix window can be opened even if the quickfix list is not set
6547 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006548 if (qi == NULL)
6549 return 0;
6550 win = qf_find_win(qi);
6551 if (win != NULL)
6552 return win->w_id;
6553 return 0;
6554}
6555
6556/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006557 * Returns the number of the buffer displayed in the quickfix/location list
6558 * window. If there is no buffer associated with the list, then returns 0.
6559 */
6560 static int
6561qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6562{
6563 return dict_add_number(retdict, "qfbufnr",
6564 (qi == NULL) ? 0 : qi->qf_bufnr);
6565}
6566
6567/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006568 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006569 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006570 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006571qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006572{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006573 int flags = QF_GETLIST_NONE;
6574
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006575 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006576 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006577 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006578 if (!loclist)
6579 // File window ID is applicable only to location list windows
6580 flags &= ~ QF_GETLIST_FILEWINID;
6581 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006582
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006583 if (dict_find(what, (char_u *)"title", -1) != NULL)
6584 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006585
Bram Moolenaara6d48492017-12-12 22:45:31 +01006586 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6587 flags |= QF_GETLIST_NR;
6588
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006589 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6590 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006591
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006592 if (dict_find(what, (char_u *)"context", -1) != NULL)
6593 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006594
Bram Moolenaara6d48492017-12-12 22:45:31 +01006595 if (dict_find(what, (char_u *)"id", -1) != NULL)
6596 flags |= QF_GETLIST_ID;
6597
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006598 if (dict_find(what, (char_u *)"items", -1) != NULL)
6599 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006600
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006601 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6602 flags |= QF_GETLIST_IDX;
6603
6604 if (dict_find(what, (char_u *)"size", -1) != NULL)
6605 flags |= QF_GETLIST_SIZE;
6606
Bram Moolenaarb254af32017-12-18 19:48:58 +01006607 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6608 flags |= QF_GETLIST_TICK;
6609
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006610 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6611 flags |= QF_GETLIST_FILEWINID;
6612
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006613 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6614 flags |= QF_GETLIST_QFBUFNR;
6615
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006616 return flags;
6617}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006618
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006619/*
6620 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6621 * If 'nr' and 'id' are not present in 'what' then return the current
6622 * quickfix list index.
6623 * If 'nr' is zero then return the current quickfix list index.
6624 * If 'nr' is '$' then return the last quickfix list index.
6625 * If 'id' is present then return the index of the quickfix list with that id.
6626 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6627 * Return -1, if quickfix list is not present or if the stack is empty.
6628 */
6629 static int
6630qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6631{
6632 int qf_idx;
6633 dictitem_T *di;
6634
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006635 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006636 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6637 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006638 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006639 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006640 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006641 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006642 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006643 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006644 qf_idx = di->di_tv.vval.v_number - 1;
6645 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006646 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006647 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006648 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006649 else if (di->di_tv.v_type == VAR_STRING
6650 && di->di_tv.vval.v_string != NULL
6651 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006652 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006653 qf_idx = qi->qf_listcount - 1;
6654 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006655 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006656 }
6657
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006658 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6659 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006660 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006661 if (di->di_tv.v_type == VAR_NUMBER)
6662 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006663 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006664 if (di->di_tv.vval.v_number != 0)
6665 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6666 }
6667 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006668 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006669 }
6670
6671 return qf_idx;
6672}
6673
6674/*
6675 * Return default values for quickfix list properties in retdict.
6676 */
6677 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006678qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006679{
6680 int status = OK;
6681
6682 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006683 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006684 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6685 {
6686 list_T *l = list_alloc();
6687 if (l != NULL)
6688 status = dict_add_list(retdict, "items", l);
6689 else
6690 status = FAIL;
6691 }
6692 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006693 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006694 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006695 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006696 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006697 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006698 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006699 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006700 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006701 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006702 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006703 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006704 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006705 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006706 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006707 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006708 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6709 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006710
6711 return status;
6712}
6713
6714/*
6715 * Return the quickfix list title as 'title' in retdict
6716 */
6717 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006718qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006719{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006720 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006721}
6722
6723/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006724 * Returns the identifier of the window used to display files from a location
6725 * list. If there is no associated window, then returns 0. Useful only when
6726 * called from a location list window.
6727 */
6728 static int
6729qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6730{
6731 int winid = 0;
6732
6733 if (wp != NULL && IS_LL_WINDOW(wp))
6734 {
6735 win_T *ll_wp = qf_find_win_with_loclist(qi);
6736 if (ll_wp != NULL)
6737 winid = ll_wp->w_id;
6738 }
6739
6740 return dict_add_number(retdict, "filewinid", winid);
6741}
6742
6743/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02006744 * Return the quickfix list items/entries as 'items' in retdict.
6745 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006746 */
6747 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006748qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006749{
6750 int status = OK;
6751 list_T *l = list_alloc();
6752 if (l != NULL)
6753 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006754 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006755 dict_add_list(retdict, "items", l);
6756 }
6757 else
6758 status = FAIL;
6759
6760 return status;
6761}
6762
6763/*
6764 * Return the quickfix list context (if any) as 'context' in retdict.
6765 */
6766 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006767qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006768{
6769 int status;
6770 dictitem_T *di;
6771
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006772 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006773 {
6774 di = dictitem_alloc((char_u *)"context");
6775 if (di != NULL)
6776 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006777 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006778 status = dict_add(retdict, di);
6779 if (status == FAIL)
6780 dictitem_free(di);
6781 }
6782 else
6783 status = FAIL;
6784 }
6785 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006786 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006787
6788 return status;
6789}
6790
6791/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02006792 * Return the current quickfix list index as 'idx' in retdict.
6793 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006794 */
6795 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006796qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006797{
Bram Moolenaar858ba062020-05-31 23:11:59 +02006798 if (eidx == 0)
6799 {
6800 eidx = qfl->qf_index;
6801 if (qf_list_empty(qfl))
6802 // For empty lists, current index is set to 0
6803 eidx = 0;
6804 }
6805 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006806}
6807
6808/*
6809 * Return quickfix/location list details (title) as a
6810 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6811 * then current list is used. Otherwise the specified list is used.
6812 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006813 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006814qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6815{
6816 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006817 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006818 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006819 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02006820 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006821 dictitem_T *di;
6822 int flags = QF_GETLIST_NONE;
6823
6824 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6825 return qf_get_list_from_lines(what, di, retdict);
6826
6827 if (wp != NULL)
6828 qi = GET_LOC_LIST(wp);
6829
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006830 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006831
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006832 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006833 qf_idx = qf_getprop_qfidx(qi, what);
6834
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006835 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006836 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006837 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006838
Bram Moolenaar0398e002019-03-21 21:12:49 +01006839 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006840
Bram Moolenaar858ba062020-05-31 23:11:59 +02006841 // If an entry index is specified, use that
6842 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6843 {
6844 if (di->di_tv.v_type != VAR_NUMBER)
6845 return FAIL;
6846 eidx = di->di_tv.vval.v_number;
6847 }
6848
Bram Moolenaard823fa92016-08-12 16:29:27 +02006849 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006850 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006851 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006852 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006853 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006854 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006855 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02006856 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006857 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006858 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006859 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006860 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006861 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02006862 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006863 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006864 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006865 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006866 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006867 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6868 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006869 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6870 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006871
Bram Moolenaard823fa92016-08-12 16:29:27 +02006872 return status;
6873}
6874
6875/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006876 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006877 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6878 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006879 */
6880 static int
6881qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01006882 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006883 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006884 int first_entry,
6885 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006886{
6887 static int did_bufnr_emsg;
6888 char_u *filename, *module, *pattern, *text, *type;
6889 int bufnum, valid, status, col, vcol, nr;
6890 long lnum;
6891
6892 if (first_entry)
6893 did_bufnr_emsg = FALSE;
6894
Bram Moolenaar8f667172018-12-14 15:38:31 +01006895 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6896 module = dict_get_string(d, (char_u *)"module", TRUE);
6897 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6898 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6899 col = (int)dict_get_number(d, (char_u *)"col");
6900 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6901 nr = (int)dict_get_number(d, (char_u *)"nr");
6902 type = dict_get_string(d, (char_u *)"type", TRUE);
6903 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6904 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006905 if (text == NULL)
6906 text = vim_strsave((char_u *)"");
6907
6908 valid = TRUE;
6909 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6910 valid = FALSE;
6911
6912 // Mark entries with non-existing buffer number as not valid. Give the
6913 // error message only once.
6914 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6915 {
6916 if (!did_bufnr_emsg)
6917 {
6918 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006919 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006920 }
6921 valid = FALSE;
6922 bufnum = 0;
6923 }
6924
6925 // If the 'valid' field is present it overrules the detected value.
6926 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006927 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006928
Bram Moolenaar0398e002019-03-21 21:12:49 +01006929 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006930 NULL, // dir
6931 filename,
6932 module,
6933 bufnum,
6934 text,
6935 lnum,
6936 col,
6937 vcol, // vis_col
6938 pattern, // search pattern
6939 nr,
6940 type == NULL ? NUL : *type,
6941 valid);
6942
6943 vim_free(filename);
6944 vim_free(module);
6945 vim_free(pattern);
6946 vim_free(text);
6947 vim_free(type);
6948
Bram Moolenaar9752c722018-12-22 16:49:34 +01006949 if (valid)
6950 *valid_entry = TRUE;
6951
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006952 return status;
6953}
6954
6955/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006956 * Add list of entries to quickfix/location list. Each list entry is
6957 * a dictionary with item information.
6958 */
6959 static int
6960qf_add_entries(
6961 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006962 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006963 list_T *list,
6964 char_u *title,
6965 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006966{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006967 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006968 listitem_T *li;
6969 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006970 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006971 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006972 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006973
Bram Moolenaara3921f42017-06-04 15:30:34 +02006974 if (action == ' ' || qf_idx == qi->qf_listcount)
6975 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006976 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006977 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006978 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006979 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006980 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01006981 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006982 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006983 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006984 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006985 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006986 qf_free_items(qfl);
6987 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006988 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006989
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006990 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006991 {
6992 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006993 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006994
6995 d = li->li_tv.vval.v_dict;
6996 if (d == NULL)
6997 continue;
6998
Bram Moolenaar0398e002019-03-21 21:12:49 +01006999 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007000 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007001 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007002 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007003 }
7004
Bram Moolenaar9752c722018-12-22 16:49:34 +01007005 // Check if any valid error entries are added to the list.
7006 if (valid_entry)
7007 qfl->qf_nonevalid = FALSE;
7008 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007009 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007010 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007011
7012 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007013 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007014 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007015
7016 // Update the current error index if not appending to the list or if the
7017 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007018 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007019 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007020
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007021 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007022 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007023
7024 return retval;
7025}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007026
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007027/*
7028 * Get the quickfix list index from 'nr' or 'id'
7029 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007030 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007031qf_setprop_get_qfidx(
7032 qf_info_T *qi,
7033 dict_T *what,
7034 int action,
7035 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007036{
7037 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007038 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007039
Bram Moolenaard823fa92016-08-12 16:29:27 +02007040 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7041 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007042 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007043 if (di->di_tv.v_type == VAR_NUMBER)
7044 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007045 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007046 if (di->di_tv.vval.v_number != 0)
7047 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007048
Bram Moolenaar55b69262017-08-13 13:42:01 +02007049 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7050 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007051 // When creating a new list, accept qf_idx pointing to the next
7052 // non-available list and add the new list at the end of the
7053 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007054 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007055 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007056 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007057 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007058 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007059 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007060 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007061 }
7062 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007063 && di->di_tv.vval.v_string != NULL
7064 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007065 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007066 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007067 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007068 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007069 qf_idx = 0;
7070 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007071 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007072 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007073 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007074 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007075 }
7076
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007077 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007078 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007079 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007080 if (di->di_tv.v_type != VAR_NUMBER)
7081 return INVALID_QFIDX;
7082
7083 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007084 }
7085
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007086 return qf_idx;
7087}
7088
7089/*
7090 * Set the quickfix list title.
7091 */
7092 static int
7093qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7094{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007095 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007096
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007097 if (di->di_tv.v_type != VAR_STRING)
7098 return FAIL;
7099
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007100 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01007101 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007102 if (qf_idx == qi->qf_curlist)
7103 qf_update_win_titlevar(qi);
7104
7105 return OK;
7106}
7107
7108/*
7109 * Set quickfix list items/entries.
7110 */
7111 static int
7112qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7113{
7114 int retval = FAIL;
7115 char_u *title_save;
7116
7117 if (di->di_tv.v_type != VAR_LIST)
7118 return FAIL;
7119
7120 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7121 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7122 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007123 vim_free(title_save);
7124
7125 return retval;
7126}
7127
7128/*
7129 * Set quickfix list items/entries from a list of lines.
7130 */
7131 static int
7132qf_setprop_items_from_lines(
7133 qf_info_T *qi,
7134 int qf_idx,
7135 dict_T *what,
7136 dictitem_T *di,
7137 int action)
7138{
7139 char_u *errorformat = p_efm;
7140 dictitem_T *efm_di;
7141 int retval = FAIL;
7142
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007143 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007144 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7145 {
7146 if (efm_di->di_tv.v_type != VAR_STRING ||
7147 efm_di->di_tv.vval.v_string == NULL)
7148 return FAIL;
7149 errorformat = efm_di->di_tv.vval.v_string;
7150 }
7151
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007152 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007153 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7154 return FAIL;
7155
7156 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007157 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007158 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
7159 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
7160 retval = OK;
7161
7162 return retval;
7163}
7164
7165/*
7166 * Set quickfix list context.
7167 */
7168 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007169qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007170{
7171 typval_T *ctx;
7172
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007173 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007174 ctx = alloc_tv();
7175 if (ctx != NULL)
7176 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007177 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007178
7179 return OK;
7180}
7181
7182/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007183 * Set the current index in the specified quickfix list
7184 */
7185 static int
7186qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7187{
7188 int denote = FALSE;
7189 int newidx;
7190 int old_qfidx;
7191 qfline_T *qf_ptr;
7192
7193 // If the specified index is '$', then use the last entry
7194 if (di->di_tv.v_type == VAR_STRING
7195 && di->di_tv.vval.v_string != NULL
7196 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7197 newidx = qfl->qf_count;
7198 else
7199 {
7200 // Otherwise use the specified index
7201 newidx = tv_get_number_chk(&di->di_tv, &denote);
7202 if (denote)
7203 return FAIL;
7204 }
7205
7206 if (newidx < 1) // sanity check
7207 return FAIL;
7208 if (newidx > qfl->qf_count)
7209 newidx = qfl->qf_count;
7210
7211 old_qfidx = qfl->qf_index;
7212 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7213 if (qf_ptr == NULL)
7214 return FAIL;
7215 qfl->qf_ptr = qf_ptr;
7216 qfl->qf_index = newidx;
7217
7218 // If the current list is modified and it is displayed in the quickfix
7219 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007220 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007221 qf_win_pos_update(qi, old_qfidx);
7222
7223 return OK;
7224}
7225
7226/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007227 * Set the current index in the specified quickfix list
7228 */
7229 static int
7230qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7231{
7232 VIM_CLEAR(qfl->qf_qftf);
7233 if (di->di_tv.v_type == VAR_STRING
7234 && di->di_tv.vval.v_string != NULL)
7235 qfl->qf_qftf = vim_strsave(di->di_tv.vval.v_string);
7236
7237 return OK;
7238}
7239
7240/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007241 * Set quickfix/location list properties (title, items, context).
7242 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007243 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007244 */
7245 static int
7246qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7247{
7248 dictitem_T *di;
7249 int retval = FAIL;
7250 int qf_idx;
7251 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007252 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007253
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007254 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007255 newlist = TRUE;
7256
7257 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007258 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007259 return FAIL;
7260
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007261 if (newlist)
7262 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007263 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007264 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007265 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007266 }
7267
Bram Moolenaar0398e002019-03-21 21:12:49 +01007268 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007269 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007270 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007271 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007272 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007273 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007274 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007275 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007276 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007277 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7278 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007279 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7280 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007281
Bram Moolenaarb254af32017-12-18 19:48:58 +01007282 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007283 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007284
Bram Moolenaard823fa92016-08-12 16:29:27 +02007285 return retval;
7286}
7287
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007288/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007289 * Free the entire quickfix/location list stack.
7290 * If the quickfix/location list window is open, then clear it.
7291 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007292 static void
7293qf_free_stack(win_T *wp, qf_info_T *qi)
7294{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007295 win_T *qfwin = qf_find_win(qi);
7296 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007297
7298 if (qfwin != NULL)
7299 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007300 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007301 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007302 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007303 qf_update_buffer(qi, NULL);
7304 }
7305
7306 if (wp != NULL && IS_LL_WINDOW(wp))
7307 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007308 // If in the location list window, then use the non-location list
7309 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007310 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007311 if (llwin != NULL)
7312 wp = llwin;
7313 }
7314
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007315 qf_free_all(wp);
7316 if (wp == NULL)
7317 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007318 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007319 qi->qf_curlist = 0;
7320 qi->qf_listcount = 0;
7321 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007322 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007323 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007324 // If the location list window is open, then create a new empty
7325 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007326 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007327
Bram Moolenaar95946f12019-03-31 15:31:59 +02007328 if (new_ll != NULL)
7329 {
7330 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007331
Bram Moolenaar95946f12019-03-31 15:31:59 +02007332 // first free the list reference in the location list window
7333 ll_free_all(&qfwin->w_llist_ref);
7334
7335 qfwin->w_llist_ref = new_ll;
7336 if (wp != qfwin)
7337 win_set_loclist(wp, new_ll);
7338 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007339 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007340}
7341
Bram Moolenaard823fa92016-08-12 16:29:27 +02007342/*
7343 * Populate the quickfix list with the items supplied in the list
7344 * of dictionaries. "title" will be copied to w:quickfix_title.
7345 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
7346 */
7347 int
7348set_errorlist(
7349 win_T *wp,
7350 list_T *list,
7351 int action,
7352 char_u *title,
7353 dict_T *what)
7354{
7355 qf_info_T *qi = &ql_info;
7356 int retval = OK;
7357
7358 if (wp != NULL)
7359 {
7360 qi = ll_get_or_alloc_list(wp);
7361 if (qi == NULL)
7362 return FAIL;
7363 }
7364
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007365 if (action == 'f')
7366 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007367 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007368 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007369 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007370 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007371
7372 incr_quickfix_busy();
7373
7374 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007375 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007376 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007377 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007378 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007379 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007380 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007381 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007382
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007383 decr_quickfix_busy();
7384
Bram Moolenaard823fa92016-08-12 16:29:27 +02007385 return retval;
7386}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007387
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007388/*
7389 * Mark the context as in use for all the lists in a quickfix stack.
7390 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007391 static int
7392mark_quickfix_ctx(qf_info_T *qi, int copyID)
7393{
7394 int i;
7395 int abort = FALSE;
7396 typval_T *ctx;
7397
7398 for (i = 0; i < LISTCOUNT && !abort; ++i)
7399 {
7400 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007401 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7402 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007403 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
7404 }
7405
7406 return abort;
7407}
7408
7409/*
7410 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007411 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007412 */
7413 int
7414set_ref_in_quickfix(int copyID)
7415{
7416 int abort = FALSE;
7417 tabpage_T *tp;
7418 win_T *win;
7419
7420 abort = mark_quickfix_ctx(&ql_info, copyID);
7421 if (abort)
7422 return abort;
7423
7424 FOR_ALL_TAB_WINDOWS(tp, win)
7425 {
7426 if (win->w_llist != NULL)
7427 {
7428 abort = mark_quickfix_ctx(win->w_llist, copyID);
7429 if (abort)
7430 return abort;
7431 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007432 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7433 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007434 // In a location list window and none of the other windows is
7435 // referring to this location list. Mark the location list
7436 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007437 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7438 if (abort)
7439 return abort;
7440 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007441 }
7442
7443 return abort;
7444}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007445#endif
7446
Bram Moolenaar81695252004-12-29 20:58:21 +00007447/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007448 * Return the autocmd name for the :cbuffer Ex commands
7449 */
7450 static char_u *
7451cbuffer_get_auname(cmdidx_T cmdidx)
7452{
7453 switch (cmdidx)
7454 {
7455 case CMD_cbuffer: return (char_u *)"cbuffer";
7456 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7457 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7458 case CMD_lbuffer: return (char_u *)"lbuffer";
7459 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7460 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7461 default: return NULL;
7462 }
7463}
7464
7465/*
7466 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7467 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7468 */
7469 static int
7470cbuffer_process_args(
7471 exarg_T *eap,
7472 buf_T **bufp,
7473 linenr_T *line1,
7474 linenr_T *line2)
7475{
7476 buf_T *buf = NULL;
7477
7478 if (*eap->arg == NUL)
7479 buf = curbuf;
7480 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7481 buf = buflist_findnr(atoi((char *)eap->arg));
7482
7483 if (buf == NULL)
7484 {
7485 emsg(_(e_invarg));
7486 return FAIL;
7487 }
7488
7489 if (buf->b_ml.ml_mfp == NULL)
7490 {
7491 emsg(_("E681: Buffer is not loaded"));
7492 return FAIL;
7493 }
7494
7495 if (eap->addr_count == 0)
7496 {
7497 eap->line1 = 1;
7498 eap->line2 = buf->b_ml.ml_line_count;
7499 }
7500
7501 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7502 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7503 {
7504 emsg(_(e_invrange));
7505 return FAIL;
7506 }
7507
7508 *line1 = eap->line1;
7509 *line2 = eap->line2;
7510 *bufp = buf;
7511
7512 return OK;
7513}
7514
7515/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007516 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007517 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007518 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007519 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007520 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007521 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007522 */
7523 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007524ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007525{
7526 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007527 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007528 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007529 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007530 int_u save_qfid;
7531 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007532 char_u *qf_title;
7533 linenr_T line1;
7534 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007535
Bram Moolenaara16123a2019-03-28 20:31:07 +01007536 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007537 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007538 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007539 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007540#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007541 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007542 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007543#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007544 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007545
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007546 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007547 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7548 if (qi == NULL)
7549 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007550
Bram Moolenaara16123a2019-03-28 20:31:07 +01007551 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7552 return;
7553
7554 qf_title = qf_cmdtitle(*eap->cmdlinep);
7555
7556 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007557 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007558 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7559 (char *)qf_title, (char *)buf->b_sfname);
7560 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007561 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007562
7563 incr_quickfix_busy();
7564
7565 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7566 (eap->cmdidx != CMD_caddbuffer
7567 && eap->cmdidx != CMD_laddbuffer),
7568 line1, line2,
7569 qf_title, NULL);
7570 if (qf_stack_empty(qi))
7571 {
7572 decr_quickfix_busy();
7573 return;
7574 }
7575 if (res >= 0)
7576 qf_list_changed(qf_get_curlist(qi));
7577
7578 // Remember the current quickfix list identifier, so that we can
7579 // check for autocommands changing the current quickfix list.
7580 save_qfid = qf_get_curlist(qi)->qf_id;
7581 if (au_name != NULL)
7582 {
7583 buf_T *curbuf_old = curbuf;
7584
7585 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7586 TRUE, curbuf);
7587 if (curbuf != curbuf_old)
7588 // Autocommands changed buffer, don't jump now, "qi" may
7589 // be invalid.
7590 res = 0;
7591 }
7592 // Jump to the first error for a new list and if autocmds didn't
7593 // free the list.
7594 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7595 eap->cmdidx == CMD_lbuffer)
7596 && qflist_valid(wp, save_qfid))
7597 // display the first error
7598 qf_jump_first(qi, save_qfid, eap->forceit);
7599
7600 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007601}
7602
Bram Moolenaar1e015462005-09-25 22:16:38 +00007603#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007604/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007605 * Return the autocmd name for the :cexpr Ex commands.
7606 */
7607 static char_u *
7608cexpr_get_auname(cmdidx_T cmdidx)
7609{
7610 switch (cmdidx)
7611 {
7612 case CMD_cexpr: return (char_u *)"cexpr";
7613 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7614 case CMD_caddexpr: return (char_u *)"caddexpr";
7615 case CMD_lexpr: return (char_u *)"lexpr";
7616 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7617 case CMD_laddexpr: return (char_u *)"laddexpr";
7618 default: return NULL;
7619 }
7620}
7621
7622/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007623 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7624 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007625 */
7626 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007627ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007628{
7629 typval_T *tv;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007630 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007631 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007632 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007633 int_u save_qfid;
7634 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007635
Bram Moolenaara16123a2019-03-28 20:31:07 +01007636 au_name = cexpr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007637 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7638 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007639 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007640#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007641 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007642 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007643#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007644 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007645
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007646 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7647 if (qi == NULL)
7648 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007649
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007650 // Evaluate the expression. When the result is a string or a list we can
7651 // use it to fill the errorlist.
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01007652 tv = eval_expr(eap->arg, &eap->nextcmd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007653 if (tv != NULL)
7654 {
7655 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7656 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7657 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007658 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007659 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00007660 (eap->cmdidx != CMD_caddexpr
7661 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007662 (linenr_T)0, (linenr_T)0,
7663 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007664 if (qf_stack_empty(qi))
7665 {
7666 decr_quickfix_busy();
7667 goto cleanup;
7668 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01007669 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007670 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007671
7672 // Remember the current quickfix list identifier, so that we can
7673 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007674 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007675 if (au_name != NULL)
7676 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7677 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007678
7679 // Jump to the first error for a new list and if autocmds didn't
7680 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02007681 if (res > 0 && (eap->cmdidx == CMD_cexpr
7682 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007683 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02007684 // display the first error
7685 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007686 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00007687 }
7688 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007689 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007690cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00007691 free_tv(tv);
7692 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007693}
Bram Moolenaar1e015462005-09-25 22:16:38 +00007694#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007695
7696/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007697 * Get the location list for ":lhelpgrep"
7698 */
7699 static qf_info_T *
7700hgr_get_ll(int *new_ll)
7701{
7702 win_T *wp;
7703 qf_info_T *qi;
7704
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007705 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007706 if (bt_help(curwin->w_buffer))
7707 wp = curwin;
7708 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007709 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007710 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007711
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007712 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007713 qi = NULL;
7714 else
7715 qi = wp->w_llist;
7716
7717 if (qi == NULL)
7718 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007719 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007720 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007721 return NULL;
7722 *new_ll = TRUE;
7723 }
7724
7725 return qi;
7726}
7727
7728/*
7729 * Search for a pattern in a help file.
7730 */
7731 static void
7732hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007733 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007734 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007735 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007736 regmatch_T *p_regmatch)
7737{
7738 FILE *fd;
7739 long lnum;
7740
7741 fd = mch_fopen((char *)fname, "r");
7742 if (fd == NULL)
7743 return;
7744
7745 lnum = 1;
7746 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7747 {
7748 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007749
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007750 // Convert a line if 'encoding' is not utf-8 and
7751 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007752 if (p_vc->vc_type != CONV_NONE
7753 && has_non_ascii(IObuff))
7754 {
7755 line = string_convert(p_vc, IObuff, NULL);
7756 if (line == NULL)
7757 line = IObuff;
7758 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007759
7760 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7761 {
7762 int l = (int)STRLEN(line);
7763
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007764 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007765 while (l > 0 && line[l - 1] <= ' ')
7766 line[--l] = NUL;
7767
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007768 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007769 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007770 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007771 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007772 0,
7773 line,
7774 lnum,
7775 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007776 + 1, // col
7777 FALSE, // vis_col
7778 NULL, // search pattern
7779 0, // nr
7780 1, // type
7781 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02007782 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007783 {
7784 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007785 if (line != IObuff)
7786 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007787 break;
7788 }
7789 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007790 if (line != IObuff)
7791 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007792 ++lnum;
7793 line_breakcheck();
7794 }
7795 fclose(fd);
7796}
7797
7798/*
7799 * Search for a pattern in all the help files in the doc directory under
7800 * the given directory.
7801 */
7802 static void
7803hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007804 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007805 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007806 regmatch_T *p_regmatch,
7807 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007808#ifdef FEAT_MULTI_LANG
7809 , char_u *lang
7810#endif
7811 )
7812{
7813 int fcount;
7814 char_u **fnames;
7815 int fi;
7816
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007817 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007818 add_pathsep(dirname);
7819 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7820 if (gen_expand_wildcards(1, &dirname, &fcount,
7821 &fnames, EW_FILE|EW_SILENT) == OK
7822 && fcount > 0)
7823 {
7824 for (fi = 0; fi < fcount && !got_int; ++fi)
7825 {
7826#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007827 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007828 if (lang != NULL
7829 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007830 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007831 && !(STRNICMP(lang, "en", 2) == 0
7832 && STRNICMP("txt", fnames[fi]
7833 + STRLEN(fnames[fi]) - 3, 3) == 0))
7834 continue;
7835#endif
7836
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007837 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007838 }
7839 FreeWild(fcount, fnames);
7840 }
7841}
7842
7843/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007844 * Search for a pattern in all the help files in the 'runtimepath'
7845 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007846 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007847 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007848 */
7849 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007850hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007851{
7852 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007853
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007854 vimconv_T vc;
7855
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007856 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7857 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007858 vc.vc_type = CONV_NONE;
7859 if (!enc_utf8)
7860 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007861
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007862 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007863 p = p_rtp;
7864 while (*p != NUL && !got_int)
7865 {
7866 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7867
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007868 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007869#ifdef FEAT_MULTI_LANG
7870 , lang
7871#endif
7872 );
7873 }
7874
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007875 if (vc.vc_type != CONV_NONE)
7876 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007877}
7878
7879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 * ":helpgrep {pattern}"
7881 */
7882 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007883ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884{
7885 regmatch_T regmatch;
7886 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007887 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007888 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007889 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007890 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891
Bram Moolenaar73633f82012-01-20 13:39:07 +01007892 switch (eap->cmdidx)
7893 {
7894 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7895 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7896 default: break;
7897 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007898 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7899 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007900 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007901#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007902 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007903 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007904#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007905 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007906
Bram Moolenaar39665952018-08-15 20:59:48 +02007907 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007908 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007909 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007910 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007911 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007912 }
7913
Bram Moolenaara16123a2019-03-28 20:31:07 +01007914 // Make 'cpoptions' empty, the 'l' flag should not be used here.
7915 save_cpo = p_cpo;
7916 p_cpo = empty_option;
7917
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007918 incr_quickfix_busy();
7919
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007920#ifdef FEAT_MULTI_LANG
7921 // Check for a specified language
7922 lang = check_help_lang(eap->arg);
7923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7925 regmatch.rm_ic = FALSE;
7926 if (regmatch.regprog != NULL)
7927 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007928 qf_list_T *qfl;
7929
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007930 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007931 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007932 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007934 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007935
Bram Moolenaar473de612013-06-08 18:19:48 +02007936 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007938 qfl->qf_nonevalid = FALSE;
7939 qfl->qf_ptr = qfl->qf_start;
7940 qfl->qf_index = 1;
7941 qf_list_changed(qfl);
7942 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 }
7944
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007945 if (p_cpo == empty_option)
7946 p_cpo = save_cpo;
7947 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007948 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007949 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950
Bram Moolenaar73633f82012-01-20 13:39:07 +01007951 if (au_name != NULL)
7952 {
7953 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7954 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007955 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007956 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007957 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007958 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007959 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007960 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007961 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007962
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007963 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007964 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007965 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007966 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007967 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007968
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007969 decr_quickfix_busy();
7970
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007971 if (eap->cmdidx == CMD_lhelpgrep)
7972 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007973 // If the help window is not opened or if it already points to the
7974 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007975 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007976 {
7977 if (new_qi)
7978 ll_free_all(&qi);
7979 }
7980 else if (curwin->w_llist == NULL)
7981 curwin->w_llist = qi;
7982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007984#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02007985
7986#if defined(FEAT_EVAL) || defined(PROTO)
7987# ifdef FEAT_QUICKFIX
7988 static void
7989get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
7990{
7991 if (what_arg->v_type == VAR_UNKNOWN)
7992 {
7993 if (rettv_list_alloc(rettv) == OK)
7994 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02007995 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02007996 }
7997 else
7998 {
7999 if (rettv_dict_alloc(rettv) == OK)
8000 if (is_qf || (wp != NULL))
8001 {
8002 if (what_arg->v_type == VAR_DICT)
8003 {
8004 dict_T *d = what_arg->vval.v_dict;
8005
8006 if (d != NULL)
8007 qf_get_properties(wp, d, rettv->vval.v_dict);
8008 }
8009 else
8010 emsg(_(e_dictreq));
8011 }
8012 }
8013}
8014# endif
8015
8016/*
8017 * "getloclist()" function
8018 */
8019 void
8020f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8021{
8022# ifdef FEAT_QUICKFIX
8023 win_T *wp;
8024
8025 wp = find_win_by_nr_or_id(&argvars[0]);
8026 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8027# endif
8028}
8029
8030/*
8031 * "getqflist()" function
8032 */
8033 void
8034f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8035{
8036# ifdef FEAT_QUICKFIX
8037 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8038# endif
8039}
8040
8041/*
8042 * Used by "setqflist()" and "setloclist()" functions
8043 */
8044 static void
8045set_qf_ll_list(
8046 win_T *wp UNUSED,
8047 typval_T *list_arg UNUSED,
8048 typval_T *action_arg UNUSED,
8049 typval_T *what_arg UNUSED,
8050 typval_T *rettv)
8051{
8052# ifdef FEAT_QUICKFIX
8053 static char *e_invact = N_("E927: Invalid action: '%s'");
8054 char_u *act;
8055 int action = 0;
8056 static int recursive = 0;
8057# endif
8058
8059 rettv->vval.v_number = -1;
8060
8061# ifdef FEAT_QUICKFIX
8062 if (list_arg->v_type != VAR_LIST)
8063 emsg(_(e_listreq));
8064 else if (recursive != 0)
8065 emsg(_(e_au_recursive));
8066 else
8067 {
8068 list_T *l = list_arg->vval.v_list;
8069 dict_T *d = NULL;
8070 int valid_dict = TRUE;
8071
8072 if (action_arg->v_type == VAR_STRING)
8073 {
8074 act = tv_get_string_chk(action_arg);
8075 if (act == NULL)
8076 return; // type error; errmsg already given
8077 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8078 act[1] == NUL)
8079 action = *act;
8080 else
8081 semsg(_(e_invact), act);
8082 }
8083 else if (action_arg->v_type == VAR_UNKNOWN)
8084 action = ' ';
8085 else
8086 emsg(_(e_stringreq));
8087
8088 if (action_arg->v_type != VAR_UNKNOWN
8089 && what_arg->v_type != VAR_UNKNOWN)
8090 {
8091 if (what_arg->v_type == VAR_DICT)
8092 d = what_arg->vval.v_dict;
8093 else
8094 {
8095 emsg(_(e_dictreq));
8096 valid_dict = FALSE;
8097 }
8098 }
8099
8100 ++recursive;
8101 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
8102 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
8103 d) == OK)
8104 rettv->vval.v_number = 0;
8105 --recursive;
8106 }
8107# endif
8108}
8109
8110/*
8111 * "setloclist()" function
8112 */
8113 void
8114f_setloclist(typval_T *argvars, typval_T *rettv)
8115{
8116 win_T *win;
8117
8118 rettv->vval.v_number = -1;
8119
8120 win = find_win_by_nr_or_id(&argvars[0]);
8121 if (win != NULL)
8122 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8123}
8124
8125/*
8126 * "setqflist()" function
8127 */
8128 void
8129f_setqflist(typval_T *argvars, typval_T *rettv)
8130{
8131 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8132}
8133#endif