blob: aa4e765908b64f1fb64c2ec209a6396f5b4c1144 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020030 qfline_T *qf_next; // pointer to next error in the list
31 qfline_T *qf_prev; // pointer to previous error in the list
32 linenr_T qf_lnum; // line number where the error occurred
33 int qf_fnum; // file number for the line
34 int qf_col; // column where the error occurred
35 int qf_nr; // error number
36 char_u *qf_module; // module name for this error
37 char_u *qf_pattern; // search pattern for the error
38 char_u *qf_text; // description of the error
39 char_u qf_viscol; // set to TRUE if qf_col is screen column
40 char_u qf_cleared; // set to TRUE if line has been deleted
41 char_u qf_type; // type of the error (mostly 'E'); 1 for
42 // :helpgrep
43 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000044};
45
46/*
47 * There is a stack of error lists.
48 */
49#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020050#define INVALID_QFIDX (-1)
Bram Moolenaaree8188f2019-02-05 21:23:04 +010051#define INVALID_QFBUFNR (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020053/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010054 * Quickfix list type.
55 */
56typedef enum
57{
58 QFLT_QUICKFIX, // Quickfix list - global list
59 QFLT_LOCATION, // Location list - per window list
60 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
61} qfltype_T;
62
63/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020064 * Quickfix/Location list definition
65 * Contains a list of entries (qfline_T). qf_start points to the first entry
66 * and qf_last points to the last entry. qf_count contains the list size.
67 *
68 * Usually the list contains one or more entries. But an empty list can be
69 * created using setqflist()/setloclist() with a title and/or user context
70 * information and entries can be added later using setqflist()/setloclist().
71 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000072typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000073{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020074 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010075 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020076 qfline_T *qf_start; // pointer to the first error
77 qfline_T *qf_last; // pointer to the last error
78 qfline_T *qf_ptr; // pointer to the current error
79 int qf_count; // number of errors (0 means empty list)
80 int qf_index; // current index in the error list
81 int qf_nonevalid; // TRUE if not a single valid entry found
82 char_u *qf_title; // title derived from the command that created
83 // the error list or set by setqflist
84 typval_T *qf_ctx; // context set by setqflist/setloclist
Bram Moolenaara7df8c72017-07-19 13:23:06 +020085
86 struct dir_stack_T *qf_dir_stack;
87 char_u *qf_directory;
88 struct dir_stack_T *qf_file_stack;
89 char_u *qf_currfile;
90 int qf_multiline;
91 int qf_multiignore;
92 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010093 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000094} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000095
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020096/*
97 * Quickfix/Location list stack definition
98 * Contains a list of quickfix/location lists (qf_list_T)
99 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000100struct qf_info_S
101{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200102 // Count of references to this list. Used only for location lists.
103 // When a location list window reference this list, qf_refcount
104 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
105 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000106 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200107 int qf_listcount; // current number of lists
108 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000109 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100110 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100111 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000112};
113
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200114static qf_info_T ql_info; // global quickfix list
115static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200117#define FMT_PATTERNS 11 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
120 * Structure used to hold the info of one part of 'errorformat'
121 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000122typedef struct efm_S efm_T;
123struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200125 regprog_T *prog; // pre-formatted part of 'errorformat'
126 efm_T *next; // pointer to next (NULL if last)
127 char_u addr[FMT_PATTERNS]; // indices of used % patterns
128 char_u prefix; // prefix of this format line:
129 // 'D' enter directory
130 // 'X' leave directory
131 // 'A' start of multi-line message
132 // 'E' error message
133 // 'W' warning message
134 // 'I' informational message
135 // 'C' continuation line
136 // 'Z' end of multi-line message
137 // 'G' general, unspecific message
138 // 'P' push file (partial) message
139 // 'Q' pop/quit file (partial) message
140 // 'O' overread (partial) message
141 char_u flags; // additional flags given in prefix
142 // '-' do not include this line
143 // '+' include whole line in message
144 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145};
146
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200147// List of location lists to be deleted.
148// Used to delay the deletion of locations lists by autocmds.
149typedef struct qf_delq_S
150{
151 struct qf_delq_S *next;
152 qf_info_T *qi;
153} qf_delq_T;
154static qf_delq_T *qf_delq_head = NULL;
155
156// Counter to prevent autocmds from freeing up location lists when they are
157// still being used.
158static int quickfix_busy = 0;
159
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200160static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100161
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100162static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100163static int qf_add_entry(qf_list_T *qfl, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200164static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100166static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200167static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200169static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200170static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100172static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static win_T *qf_find_win(qf_info_T *qi);
174static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200175static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +0100176static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
178static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
179static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
180static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar3ff33112019-05-03 21:56:35 +0200181static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200183// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000184#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200185// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000186#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200187
188// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100189#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
190#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
191#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
192#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200193
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000194/*
195 * Return location list for window 'wp'
196 * For location list window, return the referenced location list
197 */
198#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
199
Bram Moolenaar95946f12019-03-31 15:31:59 +0200200// Macro to loop through all the items in a quickfix list
201// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100202#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
Bram Moolenaar95946f12019-03-31 15:31:59 +0200203 for (i = 1, qfp = qfl->qf_start; \
204 !got_int && i <= qfl->qf_count && qfp != NULL; \
Bram Moolenaara16123a2019-03-28 20:31:07 +0100205 ++i, qfp = qfp->qf_next)
206
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200208 * Looking up a buffer can be slow if there are many. Remember the last one
209 * to make this a lot faster if there are multiple matches in the same file.
210 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200211static char_u *qf_last_bufname = NULL;
212static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200213
Bram Moolenaar3c097222017-12-21 20:54:49 +0100214static char *e_loc_list_changed =
215 N_("E926: Current location list was changed");
216
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200217/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200218 * Maximum number of bytes allowed per line while reading a errorfile.
219 */
220#define LINE_MAXLEN 4096
221
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200222static struct fmtpattern
223{
224 char_u convchar;
225 char *pattern;
226} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200227 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200228 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200229 {'n', "\\d\\+"},
230 {'l', "\\d\\+"},
231 {'c', "\\d\\+"},
232 {'t', "."},
233 {'m', ".\\+"},
234 {'r', ".*"},
235 {'p', "[- .]*"},
236 {'v', "\\d\\+"},
237 {'s', ".\\+"},
238 {'o', ".\\+"}
239 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200240
241/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200242 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200243 * See fmt_pat definition above for the list of supported patterns. The
244 * pattern specifier is supplied in "efmpat". The converted pattern is stored
245 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200246 */
247 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200248efmpat_to_regpat(
249 char_u *efmpat,
250 char_u *regpat,
251 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200252 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100253 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254{
255 char_u *srcptr;
256
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200257 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200258 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200259 // Each errorformat pattern can occur only once
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100260 semsg(_("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200261 return NULL;
262 }
263 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200264 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200265 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200266 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100268 semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200269 return NULL;
270 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200271 efminfo->addr[idx] = (char_u)++round;
272 *regpat++ = '\\';
273 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200274#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200275 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200276 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200277 // Also match "c:" in the file name, even when
278 // checking for a colon next: "%f:".
279 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200280 STRCPY(regpat, "\\%(\\a:\\)\\=");
281 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200282 }
283#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200284 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200285 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200286 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200287 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200288 // A file name may contain spaces, but this isn't
289 // in "\f". For "%f:%l:%m" there may be a ":" in
290 // the file name. Use ".\{-1,}x" instead (x is
291 // the next character), the requirement that :999:
292 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200293 STRCPY(regpat, ".\\{-1,}");
294 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200295 }
296 else
297 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200298 // File name followed by '\\' or '%': include as
299 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200300 STRCPY(regpat, "\\f\\+");
301 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200302 }
303 }
304 else
305 {
306 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200307 while ((*regpat = *srcptr++) != NUL)
308 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200309 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200310 *regpat++ = '\\';
311 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200312
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200313 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200314}
315
316/*
317 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200318 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200319 */
320 static char_u *
321scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200322 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200323 char_u *efm,
324 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100325 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200326{
327 char_u *efmp = *pefmp;
328
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200329 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200330 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200331 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200332 {
333 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200334 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200335 if (efmp < efm + len)
336 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200337 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200338 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200339 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200340 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200341 if (efmp == efm + len)
342 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100343 emsg(_("E374: Missing ] in format string"));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200344 return NULL;
345 }
346 }
347 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200348 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200349 *regpat++ = *++efmp;
350 *regpat++ = '\\';
351 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200352 }
353 else
354 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200355 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100356 semsg(_("E375: Unsupported %%%c in format string"), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200357 return NULL;
358 }
359
360 *pefmp = efmp;
361
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200362 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200363}
364
365/*
366 * Analyze/parse an errorformat prefix.
367 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200368 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100369efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200370{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200371 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200372 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200373 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200374 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200375 else
376 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100377 semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200378 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200379 }
380
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200381 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200382}
383
384/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200385 * Converts a 'errorformat' string part in 'efm' to a regular expression
386 * pattern. The resulting regex pattern is returned in "regpat". Additional
387 * information about the 'erroformat' pattern is returned in "fmt_ptr".
388 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200389 */
390 static int
391efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200392 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200393 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200394 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100395 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200396{
397 char_u *ptr;
398 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200399 int round;
400 int idx = 0;
401
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200402 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200403 ptr = regpat;
404 *ptr++ = '^';
405 round = 0;
406 for (efmp = efm; efmp < efm + len; ++efmp)
407 {
408 if (*efmp == '%')
409 {
410 ++efmp;
411 for (idx = 0; idx < FMT_PATTERNS; ++idx)
412 if (fmt_pat[idx].convchar == *efmp)
413 break;
414 if (idx < FMT_PATTERNS)
415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100416 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200417 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200418 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200419 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200420 }
421 else if (*efmp == '*')
422 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200423 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100424 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200425 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200426 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200427 }
428 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200429 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200430 else if (*efmp == '#')
431 *ptr++ = '*';
432 else if (*efmp == '>')
433 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200434 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200435 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200436 // prefix is allowed only at the beginning of the errorformat
437 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100438 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200439 if (efmp == NULL)
440 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200441 }
442 else
443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100444 semsg(_("E377: Invalid %%%c in format string"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200445 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200446 }
447 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200448 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200449 {
450 if (*efmp == '\\' && efmp + 1 < efm + len)
451 ++efmp;
452 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200453 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200454 if (*efmp)
455 *ptr++ = *efmp;
456 }
457 }
458 *ptr++ = '$';
459 *ptr = NUL;
460
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200461 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200462}
463
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200464/*
465 * Free the 'errorformat' information list
466 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200467 static void
468free_efm_list(efm_T **efm_first)
469{
470 efm_T *efm_ptr;
471
472 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
473 {
474 *efm_first = efm_ptr->next;
475 vim_regfree(efm_ptr->prog);
476 vim_free(efm_ptr);
477 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100478 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200479}
480
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200481/*
482 * Compute the size of the buffer used to convert a 'errorformat' pattern into
483 * a regular expression pattern.
484 */
485 static int
486efm_regpat_bufsz(char_u *efm)
487{
488 int sz;
489 int i;
490
491 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
492 for (i = FMT_PATTERNS; i > 0; )
493 sz += (int)STRLEN(fmt_pat[--i].pattern);
494#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200495 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200496#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200497 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200498#endif
499
500 return sz;
501}
502
503/*
504 * Return the length of a 'errorformat' option part (separated by ",").
505 */
506 static int
507efm_option_part_len(char_u *efm)
508{
509 int len;
510
511 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
512 if (efm[len] == '\\' && efm[len + 1] != NUL)
513 ++len;
514
515 return len;
516}
517
518/*
519 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
520 * are parsed and converted to regular expressions. Returns information about
521 * the parsed 'errorformat' option.
522 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200523 static efm_T *
524parse_efm_option(char_u *efm)
525{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200526 efm_T *fmt_ptr = NULL;
527 efm_T *fmt_first = NULL;
528 efm_T *fmt_last = NULL;
529 char_u *fmtstr = NULL;
530 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200531 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200532
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200533 // Each part of the format string is copied and modified from errorformat
534 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200535
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200536 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200537 sz = efm_regpat_bufsz(efm);
538 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200539 goto parse_efm_error;
540
541 while (efm[0] != NUL)
542 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200543 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200544 fmt_ptr = ALLOC_CLEAR_ONE(efm_T);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200545 if (fmt_ptr == NULL)
546 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200547 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200548 fmt_first = fmt_ptr;
549 else
550 fmt_last->next = fmt_ptr;
551 fmt_last = fmt_ptr;
552
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200553 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200554 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200555
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100556 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200557 goto parse_efm_error;
558 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
559 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200560 // Advance to next part
561 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200562 }
563
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200564 if (fmt_first == NULL) // nothing found
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100565 emsg(_("E378: 'errorformat' contains no pattern"));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200566
567 goto parse_efm_end;
568
569parse_efm_error:
570 free_efm_list(&fmt_first);
571
572parse_efm_end:
573 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200574
575 return fmt_first;
576}
577
Bram Moolenaare0d37972016-07-15 22:36:01 +0200578enum {
579 QF_FAIL = 0,
580 QF_OK = 1,
581 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200582 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200583 QF_IGNORE_LINE = 4,
584 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200585};
586
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200587/*
588 * State information used to parse lines and add entries to a quickfix/location
589 * list.
590 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200591typedef struct {
592 char_u *linebuf;
593 int linelen;
594 char_u *growbuf;
595 int growbufsiz;
596 FILE *fd;
597 typval_T *tv;
598 char_u *p_str;
599 listitem_T *p_li;
600 buf_T *buf;
601 linenr_T buflnum;
602 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100603 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200604} qfstate_T;
605
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200606/*
607 * Allocate more memory for the line buffer used for parsing lines.
608 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200609 static char_u *
610qf_grow_linebuf(qfstate_T *state, int newsz)
611{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200612 char_u *p;
613
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200614 // If the line exceeds LINE_MAXLEN exclude the last
615 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200616 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
617 if (state->growbuf == NULL)
618 {
619 state->growbuf = alloc(state->linelen + 1);
620 if (state->growbuf == NULL)
621 return NULL;
622 state->growbufsiz = state->linelen;
623 }
624 else if (state->linelen > state->growbufsiz)
625 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200626 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200627 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200628 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200629 state->growbufsiz = state->linelen;
630 }
631 return state->growbuf;
632}
633
634/*
635 * Get the next string (separated by newline) from state->p_str.
636 */
637 static int
638qf_get_next_str_line(qfstate_T *state)
639{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200640 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200641 char_u *p_str = state->p_str;
642 char_u *p;
643 int len;
644
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200645 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200646 return QF_END_OF_INPUT;
647
648 p = vim_strchr(p_str, '\n');
649 if (p != NULL)
650 len = (int)(p - p_str) + 1;
651 else
652 len = (int)STRLEN(p_str);
653
654 if (len > IOSIZE - 2)
655 {
656 state->linebuf = qf_grow_linebuf(state, len);
657 if (state->linebuf == NULL)
658 return QF_NOMEM;
659 }
660 else
661 {
662 state->linebuf = IObuff;
663 state->linelen = len;
664 }
665 vim_strncpy(state->linebuf, p_str, state->linelen);
666
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200667 // Increment using len in order to discard the rest of the
668 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200669 p_str += len;
670 state->p_str = p_str;
671
672 return QF_OK;
673}
674
675/*
676 * Get the next string from state->p_Li.
677 */
678 static int
679qf_get_next_list_line(qfstate_T *state)
680{
681 listitem_T *p_li = state->p_li;
682 int len;
683
684 while (p_li != NULL
685 && (p_li->li_tv.v_type != VAR_STRING
686 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200687 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200688
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200689 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200690 {
691 state->p_li = NULL;
692 return QF_END_OF_INPUT;
693 }
694
695 len = (int)STRLEN(p_li->li_tv.vval.v_string);
696 if (len > IOSIZE - 2)
697 {
698 state->linebuf = qf_grow_linebuf(state, len);
699 if (state->linebuf == NULL)
700 return QF_NOMEM;
701 }
702 else
703 {
704 state->linebuf = IObuff;
705 state->linelen = len;
706 }
707
708 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
709
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200710 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200711 return QF_OK;
712}
713
714/*
715 * Get the next string from state->buf.
716 */
717 static int
718qf_get_next_buf_line(qfstate_T *state)
719{
720 char_u *p_buf = NULL;
721 int len;
722
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200723 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200724 if (state->buflnum > state->lnumlast)
725 return QF_END_OF_INPUT;
726
727 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
728 state->buflnum += 1;
729
730 len = (int)STRLEN(p_buf);
731 if (len > IOSIZE - 2)
732 {
733 state->linebuf = qf_grow_linebuf(state, len);
734 if (state->linebuf == NULL)
735 return QF_NOMEM;
736 }
737 else
738 {
739 state->linebuf = IObuff;
740 state->linelen = len;
741 }
742 vim_strncpy(state->linebuf, p_buf, state->linelen);
743
744 return QF_OK;
745}
746
747/*
748 * Get the next string from file state->fd.
749 */
750 static int
751qf_get_next_file_line(qfstate_T *state)
752{
753 int discard;
754 int growbuflen;
755
756 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
757 return QF_END_OF_INPUT;
758
759 discard = FALSE;
760 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200761 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200762 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200763 // The current line exceeds IObuff, continue reading using
764 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200765 if (state->growbuf == NULL)
766 {
767 state->growbufsiz = 2 * (IOSIZE - 1);
768 state->growbuf = alloc(state->growbufsiz);
769 if (state->growbuf == NULL)
770 return QF_NOMEM;
771 }
772
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200773 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200774 memcpy(state->growbuf, IObuff, IOSIZE - 1);
775 growbuflen = state->linelen;
776
777 for (;;)
778 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200779 char_u *p;
780
Bram Moolenaare0d37972016-07-15 22:36:01 +0200781 if (fgets((char *)state->growbuf + growbuflen,
782 state->growbufsiz - growbuflen, state->fd) == NULL)
783 break;
784 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
785 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200786 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200787 break;
788 if (state->growbufsiz == LINE_MAXLEN)
789 {
790 discard = TRUE;
791 break;
792 }
793
794 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
795 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200796 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200797 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200798 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200799 }
800
801 while (discard)
802 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200803 // The current line is longer than LINE_MAXLEN, continue
804 // reading but discard everything until EOL or EOF is
805 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200806 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
807 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200808 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200809 break;
810 }
811
812 state->linebuf = state->growbuf;
813 state->linelen = growbuflen;
814 }
815 else
816 state->linebuf = IObuff;
817
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200818 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200819 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
820 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100821 char_u *line;
822
823 line = string_convert(&state->vc, state->linebuf, &state->linelen);
824 if (line != NULL)
825 {
826 if (state->linelen < IOSIZE)
827 {
828 STRCPY(state->linebuf, line);
829 vim_free(line);
830 }
831 else
832 {
833 vim_free(state->growbuf);
834 state->linebuf = state->growbuf = line;
835 state->growbufsiz = state->linelen < LINE_MAXLEN
836 ? state->linelen : LINE_MAXLEN;
837 }
838 }
839 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100840
Bram Moolenaare0d37972016-07-15 22:36:01 +0200841 return QF_OK;
842}
843
844/*
845 * Get the next string from a file/buffer/list/string.
846 */
847 static int
848qf_get_nextline(qfstate_T *state)
849{
850 int status = QF_FAIL;
851
852 if (state->fd == NULL)
853 {
854 if (state->tv != NULL)
855 {
856 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200857 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200858 status = qf_get_next_str_line(state);
859 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200860 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200861 status = qf_get_next_list_line(state);
862 }
863 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200864 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200865 status = qf_get_next_buf_line(state);
866 }
867 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200868 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200869 status = qf_get_next_file_line(state);
870
871 if (status != QF_OK)
872 return status;
873
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200874 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200875 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200876 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200877 state->linebuf[state->linelen - 1] = NUL;
878#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200879 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
880 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200881#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200882 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200883
Bram Moolenaare0d37972016-07-15 22:36:01 +0200884 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200885
886 return QF_OK;
887}
888
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200889typedef struct {
890 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200891 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200892 char_u *errmsg;
893 int errmsglen;
894 long lnum;
895 int col;
896 char_u use_viscol;
897 char_u *pattern;
898 int enr;
899 int type;
900 int valid;
901} qffields_T;
902
903/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200904 * Parse the match for filename ('%f') pattern in regmatch.
905 * Return the matched value in "fields->namebuf".
906 */
907 static int
908qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
909{
910 int c;
911
912 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
913 return QF_FAIL;
914
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200915 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200916 c = *rmp->endp[midx];
917 *rmp->endp[midx] = NUL;
918 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
919 *rmp->endp[midx] = c;
920
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200921 // For separate filename patterns (%O, %P and %Q), the specified file
922 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200923 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
924 && mch_getperm(fields->namebuf) == -1)
925 return QF_FAIL;
926
927 return QF_OK;
928}
929
930/*
931 * Parse the match for error number ('%n') pattern in regmatch.
932 * Return the matched value in "fields->enr".
933 */
934 static int
935qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
936{
937 if (rmp->startp[midx] == NULL)
938 return QF_FAIL;
939 fields->enr = (int)atol((char *)rmp->startp[midx]);
940 return QF_OK;
941}
942
943/*
944 * Parse the match for line number (%l') pattern in regmatch.
945 * Return the matched value in "fields->lnum".
946 */
947 static int
948qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
949{
950 if (rmp->startp[midx] == NULL)
951 return QF_FAIL;
952 fields->lnum = atol((char *)rmp->startp[midx]);
953 return QF_OK;
954}
955
956/*
957 * Parse the match for column number ('%c') pattern in regmatch.
958 * Return the matched value in "fields->col".
959 */
960 static int
961qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
962{
963 if (rmp->startp[midx] == NULL)
964 return QF_FAIL;
965 fields->col = (int)atol((char *)rmp->startp[midx]);
966 return QF_OK;
967}
968
969/*
970 * Parse the match for error type ('%t') pattern in regmatch.
971 * Return the matched value in "fields->type".
972 */
973 static int
974qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
975{
976 if (rmp->startp[midx] == NULL)
977 return QF_FAIL;
978 fields->type = *rmp->startp[midx];
979 return QF_OK;
980}
981
982/*
983 * Parse the match for '%+' format pattern. The whole matching line is included
984 * in the error string. Return the matched line in "fields->errmsg".
985 */
986 static int
987qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
988{
989 char_u *p;
990
991 if (linelen >= fields->errmsglen)
992 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200993 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200994 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
995 return QF_NOMEM;
996 fields->errmsg = p;
997 fields->errmsglen = linelen + 1;
998 }
999 vim_strncpy(fields->errmsg, linebuf, linelen);
1000 return QF_OK;
1001}
1002
1003/*
1004 * Parse the match for error message ('%m') pattern in regmatch.
1005 * Return the matched value in "fields->errmsg".
1006 */
1007 static int
1008qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1009{
1010 char_u *p;
1011 int len;
1012
1013 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1014 return QF_FAIL;
1015 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1016 if (len >= fields->errmsglen)
1017 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001018 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001019 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1020 return QF_NOMEM;
1021 fields->errmsg = p;
1022 fields->errmsglen = len + 1;
1023 }
1024 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1025 return QF_OK;
1026}
1027
1028/*
1029 * Parse the match for rest of a single-line file message ('%r') pattern.
1030 * Return the matched value in "tail".
1031 */
1032 static int
1033qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1034{
1035 if (rmp->startp[midx] == NULL)
1036 return QF_FAIL;
1037 *tail = rmp->startp[midx];
1038 return QF_OK;
1039}
1040
1041/*
1042 * Parse the match for the pointer line ('%p') pattern in regmatch.
1043 * Return the matched value in "fields->col".
1044 */
1045 static int
1046qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1047{
1048 char_u *match_ptr;
1049
1050 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1051 return QF_FAIL;
1052 fields->col = 0;
1053 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1054 ++match_ptr)
1055 {
1056 ++fields->col;
1057 if (*match_ptr == TAB)
1058 {
1059 fields->col += 7;
1060 fields->col -= fields->col % 8;
1061 }
1062 }
1063 ++fields->col;
1064 fields->use_viscol = TRUE;
1065 return QF_OK;
1066}
1067
1068/*
1069 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1070 * Return the matched value in "fields->col".
1071 */
1072 static int
1073qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1074{
1075 if (rmp->startp[midx] == NULL)
1076 return QF_FAIL;
1077 fields->col = (int)atol((char *)rmp->startp[midx]);
1078 fields->use_viscol = TRUE;
1079 return QF_OK;
1080}
1081
1082/*
1083 * Parse the match for the search text ('%s') pattern in regmatch.
1084 * Return the matched value in "fields->pattern".
1085 */
1086 static int
1087qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1088{
1089 int len;
1090
1091 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1092 return QF_FAIL;
1093 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1094 if (len > CMDBUFFSIZE - 5)
1095 len = CMDBUFFSIZE - 5;
1096 STRCPY(fields->pattern, "^\\V");
1097 STRNCAT(fields->pattern, rmp->startp[midx], len);
1098 fields->pattern[len + 3] = '\\';
1099 fields->pattern[len + 4] = '$';
1100 fields->pattern[len + 5] = NUL;
1101 return QF_OK;
1102}
1103
1104/*
1105 * Parse the match for the module ('%o') pattern in regmatch.
1106 * Return the matched value in "fields->module".
1107 */
1108 static int
1109qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1110{
1111 int len;
1112
1113 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1114 return QF_FAIL;
1115 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1116 if (len > CMDBUFFSIZE)
1117 len = CMDBUFFSIZE;
1118 STRNCAT(fields->module, rmp->startp[midx], len);
1119 return QF_OK;
1120}
1121
1122/*
1123 * 'errorformat' format pattern parser functions.
1124 * The '%f' and '%r' formats are parsed differently from other formats.
1125 * See qf_parse_match() for details.
1126 */
1127static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1128{
1129 NULL,
1130 qf_parse_fmt_n,
1131 qf_parse_fmt_l,
1132 qf_parse_fmt_c,
1133 qf_parse_fmt_t,
1134 qf_parse_fmt_m,
1135 NULL,
1136 qf_parse_fmt_p,
1137 qf_parse_fmt_v,
1138 qf_parse_fmt_s,
1139 qf_parse_fmt_o
1140};
1141
1142/*
1143 * Parse the error format pattern matches in "regmatch" and set the values in
1144 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1145 * match. Returns QF_OK if all the matches are successfully parsed. On
1146 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001147 */
1148 static int
1149qf_parse_match(
1150 char_u *linebuf,
1151 int linelen,
1152 efm_T *fmt_ptr,
1153 regmatch_T *regmatch,
1154 qffields_T *fields,
1155 int qf_multiline,
1156 int qf_multiscan,
1157 char_u **tail)
1158{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001159 int idx = fmt_ptr->prefix;
1160 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001161 int midx;
1162 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001163
1164 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1165 return QF_FAIL;
1166 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1167 fields->type = idx;
1168 else
1169 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001170
1171 // Extract error message data from matched line.
1172 // We check for an actual submatch, because "\[" and "\]" in
1173 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001174 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001175 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001176 status = QF_OK;
1177 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001178 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001179 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1180 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001181 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001182 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001183 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001184 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001185 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001186 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001187 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001188 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001189 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001190 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001191
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001192 if (status != QF_OK)
1193 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001194 }
1195
1196 return QF_OK;
1197}
1198
1199/*
1200 * Parse an error line in 'linebuf' using a single error format string in
1201 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1202 * Returns QF_OK if the efm format matches completely and the fields are
1203 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1204 */
1205 static int
1206qf_parse_get_fields(
1207 char_u *linebuf,
1208 int linelen,
1209 efm_T *fmt_ptr,
1210 qffields_T *fields,
1211 int qf_multiline,
1212 int qf_multiscan,
1213 char_u **tail)
1214{
1215 regmatch_T regmatch;
1216 int status = QF_FAIL;
1217 int r;
1218
1219 if (qf_multiscan &&
1220 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1221 return QF_FAIL;
1222
1223 fields->namebuf[0] = NUL;
1224 fields->module[0] = NUL;
1225 fields->pattern[0] = NUL;
1226 if (!qf_multiscan)
1227 fields->errmsg[0] = NUL;
1228 fields->lnum = 0;
1229 fields->col = 0;
1230 fields->use_viscol = FALSE;
1231 fields->enr = -1;
1232 fields->type = 0;
1233 *tail = NULL;
1234
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001235 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001236 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001237 regmatch.regprog = fmt_ptr->prog;
1238 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1239 fmt_ptr->prog = regmatch.regprog;
1240 if (r)
1241 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1242 fields, qf_multiline, qf_multiscan, tail);
1243
1244 return status;
1245}
1246
1247/*
1248 * Parse directory error format prefixes (%D and %X).
1249 * Push and pop directories from the directory stack when scanning directory
1250 * names.
1251 */
1252 static int
1253qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1254{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001255 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001256 {
1257 if (*fields->namebuf == NUL)
1258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001259 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001260 return QF_FAIL;
1261 }
1262 qfl->qf_directory =
1263 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1264 if (qfl->qf_directory == NULL)
1265 return QF_FAIL;
1266 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001267 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001268 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1269
1270 return QF_OK;
1271}
1272
1273/*
1274 * Parse global file name error format prefixes (%O, %P and %Q).
1275 */
1276 static int
1277qf_parse_file_pfx(
1278 int idx,
1279 qffields_T *fields,
1280 qf_list_T *qfl,
1281 char_u *tail)
1282{
1283 fields->valid = FALSE;
1284 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1285 {
1286 if (*fields->namebuf && idx == 'P')
1287 qfl->qf_currfile =
1288 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1289 else if (idx == 'Q')
1290 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1291 *fields->namebuf = NUL;
1292 if (tail && *tail)
1293 {
1294 STRMOVE(IObuff, skipwhite(tail));
1295 qfl->qf_multiscan = TRUE;
1296 return QF_MULTISCAN;
1297 }
1298 }
1299
1300 return QF_OK;
1301}
1302
1303/*
1304 * Parse a non-error line (a line which doesn't match any of the error
1305 * format in 'efm').
1306 */
1307 static int
1308qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1309{
1310 char_u *p;
1311
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001312 fields->namebuf[0] = NUL; // no match found, remove file name
1313 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001314 fields->valid = FALSE;
1315 if (linelen >= fields->errmsglen)
1316 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001317 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001318 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1319 return QF_NOMEM;
1320 fields->errmsg = p;
1321 fields->errmsglen = linelen + 1;
1322 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001323 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001324 vim_strncpy(fields->errmsg, linebuf, linelen);
1325
1326 return QF_OK;
1327}
1328
1329/*
1330 * Parse multi-line error format prefixes (%C and %Z)
1331 */
1332 static int
1333qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001334 int idx,
1335 qf_list_T *qfl,
1336 qffields_T *fields)
1337{
1338 char_u *ptr;
1339 int len;
1340
1341 if (!qfl->qf_multiignore)
1342 {
1343 qfline_T *qfprev = qfl->qf_last;
1344
1345 if (qfprev == NULL)
1346 return QF_FAIL;
1347 if (*fields->errmsg && !qfl->qf_multiignore)
1348 {
1349 len = (int)STRLEN(qfprev->qf_text);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001350 if ((ptr = alloc(len + STRLEN(fields->errmsg) + 2))
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001351 == NULL)
1352 return QF_FAIL;
1353 STRCPY(ptr, qfprev->qf_text);
1354 vim_free(qfprev->qf_text);
1355 qfprev->qf_text = ptr;
1356 *(ptr += len) = '\n';
1357 STRCPY(++ptr, fields->errmsg);
1358 }
1359 if (qfprev->qf_nr == -1)
1360 qfprev->qf_nr = fields->enr;
1361 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001362 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001363 qfprev->qf_type = fields->type;
1364
1365 if (!qfprev->qf_lnum)
1366 qfprev->qf_lnum = fields->lnum;
1367 if (!qfprev->qf_col)
1368 qfprev->qf_col = fields->col;
1369 qfprev->qf_viscol = fields->use_viscol;
1370 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001371 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001372 qfl->qf_directory,
1373 *fields->namebuf || qfl->qf_directory != NULL
1374 ? fields->namebuf
1375 : qfl->qf_currfile != NULL && fields->valid
1376 ? qfl->qf_currfile : 0);
1377 }
1378 if (idx == 'Z')
1379 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1380 line_breakcheck();
1381
1382 return QF_IGNORE_LINE;
1383}
1384
1385/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001386 * Parse a line and get the quickfix fields.
1387 * Return the QF_ status.
1388 */
1389 static int
1390qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001391 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001392 char_u *linebuf,
1393 int linelen,
1394 efm_T *fmt_first,
1395 qffields_T *fields)
1396{
1397 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001398 int idx = 0;
1399 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001400 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001401
Bram Moolenaare333e792018-04-08 13:27:39 +02001402restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001403 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001404 if (fmt_start == NULL)
1405 fmt_ptr = fmt_first;
1406 else
1407 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001408 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001409 fmt_ptr = fmt_start;
1410 fmt_start = NULL;
1411 }
1412
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001413 // Try to match each part of 'errorformat' until we find a complete
1414 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001415 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001416 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1417 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001418 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001419 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1420 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1421 if (status == QF_NOMEM)
1422 return status;
1423 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001424 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001425 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001426 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001427
1428 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1429 {
1430 if (fmt_ptr != NULL)
1431 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001432 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001433 status = qf_parse_dir_pfx(idx, fields, qfl);
1434 if (status != QF_OK)
1435 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001436 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001437
1438 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1439 if (status != QF_OK)
1440 return status;
1441
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001442 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001443 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001444 }
1445 else if (fmt_ptr != NULL)
1446 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001447 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001448 if (fmt_ptr->conthere)
1449 fmt_start = fmt_ptr;
1450
1451 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1452 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001453 qfl->qf_multiline = TRUE; // start of a multi-line message
1454 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001455 }
1456 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001457 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001458 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001459 if (status != QF_OK)
1460 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001461 }
1462 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001463 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001464 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1465 if (status == QF_MULTISCAN)
1466 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001467 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001468 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001469 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001470 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001471 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001472 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001473 return QF_IGNORE_LINE;
1474 }
1475 }
1476
1477 return QF_OK;
1478}
1479
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001480/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001481 * Returns TRUE if the specified quickfix/location stack is empty
1482 */
1483 static int
1484qf_stack_empty(qf_info_T *qi)
1485{
1486 return qi == NULL || qi->qf_listcount <= 0;
1487}
1488
1489/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001490 * Returns TRUE if the specified quickfix/location list is empty.
1491 */
1492 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001493qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001494{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001495 return qfl == NULL || qfl->qf_count <= 0;
1496}
1497
1498/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001499 * Returns TRUE if the specified quickfix/location list is not empty and
1500 * has valid entries.
1501 */
1502 static int
1503qf_list_has_valid_entries(qf_list_T *qfl)
1504{
1505 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1506}
1507
1508/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001509 * Return a pointer to a list in the specified quickfix stack
1510 */
1511 static qf_list_T *
1512qf_get_list(qf_info_T *qi, int idx)
1513{
1514 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001515}
1516
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001517/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001518 * Allocate the fields used for parsing lines and populating a quickfix list.
1519 */
1520 static int
1521qf_alloc_fields(qffields_T *pfields)
1522{
1523 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1524 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1525 pfields->errmsglen = CMDBUFFSIZE + 1;
1526 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1527 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1528 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1529 || pfields->pattern == NULL || pfields->module == NULL)
1530 return FAIL;
1531
1532 return OK;
1533}
1534
1535/*
1536 * Free the fields used for parsing lines and populating a quickfix list.
1537 */
1538 static void
1539qf_free_fields(qffields_T *pfields)
1540{
1541 vim_free(pfields->namebuf);
1542 vim_free(pfields->module);
1543 vim_free(pfields->errmsg);
1544 vim_free(pfields->pattern);
1545}
1546
1547/*
1548 * Setup the state information used for parsing lines and populating a
1549 * quickfix list.
1550 */
1551 static int
1552qf_setup_state(
1553 qfstate_T *pstate,
1554 char_u *enc,
1555 char_u *efile,
1556 typval_T *tv,
1557 buf_T *buf,
1558 linenr_T lnumfirst,
1559 linenr_T lnumlast)
1560{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001561 pstate->vc.vc_type = CONV_NONE;
1562 if (enc != NULL && *enc != NUL)
1563 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001564
1565 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001567 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001568 return FAIL;
1569 }
1570
1571 if (tv != NULL)
1572 {
1573 if (tv->v_type == VAR_STRING)
1574 pstate->p_str = tv->vval.v_string;
1575 else if (tv->v_type == VAR_LIST)
1576 pstate->p_li = tv->vval.v_list->lv_first;
1577 pstate->tv = tv;
1578 }
1579 pstate->buf = buf;
1580 pstate->buflnum = lnumfirst;
1581 pstate->lnumlast = lnumlast;
1582
1583 return OK;
1584}
1585
1586/*
1587 * Cleanup the state information used for parsing lines and populating a
1588 * quickfix list.
1589 */
1590 static void
1591qf_cleanup_state(qfstate_T *pstate)
1592{
1593 if (pstate->fd != NULL)
1594 fclose(pstate->fd);
1595
1596 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001597 if (pstate->vc.vc_type != CONV_NONE)
1598 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001599}
1600
1601/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001602 * Process the next line from a file/buffer/list/string and add it
1603 * to the quickfix list 'qfl'.
1604 */
1605 static int
1606qf_init_process_nextline(
1607 qf_list_T *qfl,
1608 efm_T *fmt_first,
1609 qfstate_T *state,
1610 qffields_T *fields)
1611{
1612 int status;
1613
1614 // Get the next line from a file/buffer/list/string
1615 status = qf_get_nextline(state);
1616 if (status != QF_OK)
1617 return status;
1618
1619 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1620 fmt_first, fields);
1621 if (status != QF_OK)
1622 return status;
1623
1624 return qf_add_entry(qfl,
1625 qfl->qf_directory,
1626 (*fields->namebuf || qfl->qf_directory != NULL)
1627 ? fields->namebuf
1628 : ((qfl->qf_currfile != NULL && fields->valid)
1629 ? qfl->qf_currfile : (char_u *)NULL),
1630 fields->module,
1631 0,
1632 fields->errmsg,
1633 fields->lnum,
1634 fields->col,
1635 fields->use_viscol,
1636 fields->pattern,
1637 fields->enr,
1638 fields->type,
1639 fields->valid);
1640}
1641
1642/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001643 * Read the errorfile "efile" into memory, line by line, building the error
1644 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001645 * Alternative: when "efile" is NULL read errors from buffer "buf".
1646 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001647 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001648 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1649 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001650 * Return -1 for error, number of errors for success.
1651 */
1652 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001653qf_init_ext(
1654 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001655 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001656 char_u *efile,
1657 buf_T *buf,
1658 typval_T *tv,
1659 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001660 int newlist, // TRUE: start a new error list
1661 linenr_T lnumfirst, // first line number to use
1662 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001663 char_u *qf_title,
1664 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001665{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001666 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001667 qfstate_T state;
1668 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001669 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001670 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001671 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001673 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001674 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001675 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001677 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001678 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001679
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001680 vim_memset(&state, 0, sizeof(state));
1681 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001682 if ((qf_alloc_fields(&fields) == FAIL) ||
1683 (qf_setup_state(&state, enc, efile, tv, buf,
1684 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 goto qf_init_end;
1686
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001687 if (newlist || qf_idx == qi->qf_listcount)
1688 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001689 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001690 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001691 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001692 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001693 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001694 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001695 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001696 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001697 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001698 qfl = qf_get_list(qi, qf_idx);
1699 if (!qf_list_empty(qfl))
1700 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001703 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001704 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001705 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 else
1707 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001709 // If the errorformat didn't change between calls, then reuse the
1710 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001711 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1712 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001713 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001714 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001715 free_efm_list(&fmt_first);
1716
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001717 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001718 fmt_first = parse_efm_option(efm);
1719 if (fmt_first != NULL)
1720 last_efm = vim_strsave(efm);
1721 }
1722
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001723 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001726 // got_int is reset here, because it was probably set when killing the
1727 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 got_int = FALSE;
1729
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001730 // Read the lines in the error file one by one.
1731 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001732 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001734 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001735 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001736 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001737 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001738 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001739 if (status == QF_FAIL)
1740 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 line_breakcheck();
1743 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001744 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001746 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001748 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001749 qfl->qf_ptr = qfl->qf_start;
1750 qfl->qf_index = 1;
1751 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 }
1753 else
1754 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001755 qfl->qf_nonevalid = FALSE;
1756 if (qfl->qf_ptr == NULL)
1757 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001759 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001760 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001761 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001763 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001765 if (!adding)
1766 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001767 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001768 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001769 qi->qf_listcount--;
1770 if (qi->qf_curlist > 0)
1771 --qi->qf_curlist;
1772 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001773qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001774 if (qf_idx == qi->qf_curlist)
1775 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001776 qf_cleanup_state(&state);
1777 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
1779 return retval;
1780}
1781
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001782/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001783 * Read the errorfile "efile" into memory, line by line, building the error
1784 * list. Set the error list's title to qf_title.
1785 * Return -1 for error, number of errors for success.
1786 */
1787 int
1788qf_init(win_T *wp,
1789 char_u *efile,
1790 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001791 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001792 char_u *qf_title,
1793 char_u *enc)
1794{
1795 qf_info_T *qi = &ql_info;
1796
1797 if (wp != NULL)
1798 {
1799 qi = ll_get_or_alloc_list(wp);
1800 if (qi == NULL)
1801 return FAIL;
1802 }
1803
1804 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1805 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1806}
1807
1808/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001809 * Set the title of the specified quickfix list. Frees the previous title.
1810 * Prepends ':' to the title.
1811 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001812 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001813qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001814{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001815 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001816
Bram Moolenaarfb604092014-07-23 15:55:00 +02001817 if (title != NULL)
1818 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02001819 char_u *p = alloc(STRLEN(title) + 2);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001820
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001821 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001822 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001823 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001824 }
1825}
1826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001828 * The title of a quickfix/location list is set, by default, to the command
1829 * that created the quickfix list with the ":" prefix.
1830 * Create a quickfix list title string by prepending ":" to a user command.
1831 * Returns a pointer to a static buffer with the title.
1832 */
1833 static char_u *
1834qf_cmdtitle(char_u *cmd)
1835{
1836 static char_u qftitle_str[IOSIZE];
1837
1838 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1839 return qftitle_str;
1840}
1841
1842/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001843 * Return a pointer to the current list in the specified quickfix stack
1844 */
1845 static qf_list_T *
1846qf_get_curlist(qf_info_T *qi)
1847{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001848 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001849}
1850
1851/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001852 * Prepare for adding a new quickfix list. If the current list is in the
1853 * middle of the stack, then all the following lists are freed and then
1854 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 */
1856 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001857qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858{
1859 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001860 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001862 // If the current entry is not the last entry, delete entries beyond
1863 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001864 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001865 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001866 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001868 // When the stack is full, remove to oldest entry
1869 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001870 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001872 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001874 qi->qf_lists[i - 1] = qi->qf_lists[i];
1875 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
1877 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001878 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001879 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001880 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1881 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001882 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001883 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884}
1885
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001886/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001887 * Queue location list stack delete request.
1888 */
1889 static void
1890locstack_queue_delreq(qf_info_T *qi)
1891{
1892 qf_delq_T *q;
1893
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001894 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001895 if (q != NULL)
1896 {
1897 q->qi = qi;
1898 q->next = qf_delq_head;
1899 qf_delq_head = q;
1900 }
1901}
1902
1903/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001904 * Return the global quickfix stack window buffer number.
1905 */
1906 int
1907qf_stack_get_bufnr(void)
1908{
1909 return ql_info.qf_bufnr;
1910}
1911
1912/*
1913 * Wipe the quickfix window buffer (if present) for the specified
1914 * quickfix/location list.
1915 */
1916 static void
1917wipe_qf_buffer(qf_info_T *qi)
1918{
1919 buf_T *qfbuf;
1920
1921 if (qi->qf_bufnr != INVALID_QFBUFNR)
1922 {
1923 qfbuf = buflist_findnr(qi->qf_bufnr);
1924 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1925 {
1926 // If the quickfix buffer is not loaded in any window, then
1927 // wipe the buffer.
1928 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE);
1929 qi->qf_bufnr = INVALID_QFBUFNR;
1930 }
1931 }
1932}
1933
1934/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001935 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001936 */
1937 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001938ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001939{
1940 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001941 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001942
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001943 qi = *pqi;
1944 if (qi == NULL)
1945 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001946 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001947
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001948 // If the location list is still in use, then queue the delete request
1949 // to be processed later.
1950 if (quickfix_busy > 0)
1951 {
1952 locstack_queue_delreq(qi);
1953 return;
1954 }
1955
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001956 qi->qf_refcount--;
1957 if (qi->qf_refcount < 1)
1958 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001959 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001960 // If the quickfix window buffer is loaded, then wipe it
1961 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001962
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001963 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001964 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001965 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001966 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001967}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001968
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001969/*
1970 * Free all the quickfix/location lists in the stack.
1971 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001972 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001973qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001974{
1975 int i;
1976 qf_info_T *qi = &ql_info;
1977
1978 if (wp != NULL)
1979 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001980 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001981 ll_free_all(&wp->w_llist);
1982 ll_free_all(&wp->w_llist_ref);
1983 }
1984 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001985 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001986 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001987 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001988}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001991 * Delay freeing of location list stacks when the quickfix code is running.
1992 * Used to avoid problems with autocmds freeing location list stacks when the
1993 * quickfix code is still referencing the stack.
1994 * Must always call decr_quickfix_busy() exactly once after this.
1995 */
1996 static void
1997incr_quickfix_busy(void)
1998{
1999 quickfix_busy++;
2000}
2001
2002/*
2003 * Safe to free location list stacks. Process any delayed delete requests.
2004 */
2005 static void
2006decr_quickfix_busy(void)
2007{
2008 if (--quickfix_busy == 0)
2009 {
2010 // No longer referencing the location lists. Process all the pending
2011 // delete requests.
2012 while (qf_delq_head != NULL)
2013 {
2014 qf_delq_T *q = qf_delq_head;
2015
2016 qf_delq_head = q->next;
2017 ll_free_all(&q->qi);
2018 vim_free(q);
2019 }
2020 }
2021#ifdef ABORT_ON_INTERNAL_ERROR
2022 if (quickfix_busy < 0)
2023 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002024 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002025 abort();
2026 }
2027#endif
2028}
2029
2030#if defined(EXITFREE) || defined(PROTO)
2031 void
2032check_quickfix_busy(void)
2033{
2034 if (quickfix_busy != 0)
2035 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002036 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002037# ifdef ABORT_ON_INTERNAL_ERROR
2038 abort();
2039# endif
2040 }
2041}
2042#endif
2043
2044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 * Add an entry to the end of the list of errors.
Bram Moolenaar95946f12019-03-31 15:31:59 +02002046 * Returns QF_OK or QF_FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 */
2048 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002049qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002050 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002051 char_u *dir, // optional directory name
2052 char_u *fname, // file name or NULL
2053 char_u *module, // module name or NULL
2054 int bufnum, // buffer number or zero
2055 char_u *mesg, // message
2056 long lnum, // line number
2057 int col, // column
2058 int vis_col, // using visual column
2059 char_u *pattern, // search pattern
2060 int nr, // error number
2061 int type, // type character
2062 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002064 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002065 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002067 if ((qfp = ALLOC_ONE(qfline_T)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002068 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002069 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002070 {
2071 buf_T *buf = buflist_findnr(bufnum);
2072
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002073 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002074 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002075 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002076 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002077 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002078 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002079 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2081 {
2082 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002083 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
2085 qfp->qf_lnum = lnum;
2086 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002087 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002088 if (pattern == NULL || *pattern == NUL)
2089 qfp->qf_pattern = NULL;
2090 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2091 {
2092 vim_free(qfp->qf_text);
2093 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002094 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002095 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002096 if (module == NULL || *module == NUL)
2097 qfp->qf_module = NULL;
2098 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2099 {
2100 vim_free(qfp->qf_text);
2101 vim_free(qfp->qf_pattern);
2102 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002103 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002106 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 type = 0;
2108 qfp->qf_type = type;
2109 qfp->qf_valid = valid;
2110
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002111 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002112 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002114 qfl->qf_start = qfp;
2115 qfl->qf_ptr = qfp;
2116 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002117 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 }
2119 else
2120 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002121 qfp->qf_prev = *lastp;
2122 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002124 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002126 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002127 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002128 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002130 qfl->qf_index = qfl->qf_count;
2131 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
2133
Bram Moolenaar95946f12019-03-31 15:31:59 +02002134 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135}
2136
2137/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002138 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002139 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002140 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002141qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002142{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002143 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002144
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002145 qi = ALLOC_CLEAR_ONE(qf_info_T);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002146 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002147 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002148 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002149 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002150 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002151 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002152 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002153}
2154
2155/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002156 * Return the location list stack for window 'wp'.
2157 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002158 */
2159 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002160ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002161{
2162 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002163 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002164 return wp->w_llist_ref;
2165
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002166 // For a non-location list window, w_llist_ref should not point to a
2167 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002168 ll_free_all(&wp->w_llist_ref);
2169
2170 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002171 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002172 return wp->w_llist;
2173}
2174
2175/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002176 * Get the quickfix/location list stack to use for the specified Ex command.
2177 * For a location list command, returns the stack for the current window. If
2178 * the location list is not found, then returns NULL and prints an error
2179 * message if 'print_emsg' is TRUE.
2180 */
2181 static qf_info_T *
2182qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2183{
2184 qf_info_T *qi = &ql_info;
2185
2186 if (is_loclist_cmd(eap->cmdidx))
2187 {
2188 qi = GET_LOC_LIST(curwin);
2189 if (qi == NULL)
2190 {
2191 if (print_emsg)
2192 emsg(_(e_loclist));
2193 return NULL;
2194 }
2195 }
2196
2197 return qi;
2198}
2199
2200/*
2201 * Get the quickfix/location list stack to use for the specified Ex command.
2202 * For a location list command, returns the stack for the current window.
2203 * If the location list is not present, then allocates a new one.
2204 * Returns NULL if the allocation fails. For a location list command, sets
2205 * 'pwinp' to curwin.
2206 */
2207 static qf_info_T *
2208qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2209{
2210 qf_info_T *qi = &ql_info;
2211
2212 if (is_loclist_cmd(eap->cmdidx))
2213 {
2214 qi = ll_get_or_alloc_list(curwin);
2215 if (qi == NULL)
2216 return NULL;
2217 *pwinp = curwin;
2218 }
2219
2220 return qi;
2221}
2222
2223/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002224 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2225 */
2226 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002227copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002228{
2229 int i;
2230 qfline_T *from_qfp;
2231 qfline_T *prevp;
2232
2233 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002234 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002235 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002236 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002237 NULL,
2238 NULL,
2239 from_qfp->qf_module,
2240 0,
2241 from_qfp->qf_text,
2242 from_qfp->qf_lnum,
2243 from_qfp->qf_col,
2244 from_qfp->qf_viscol,
2245 from_qfp->qf_pattern,
2246 from_qfp->qf_nr,
2247 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002248 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002249 return FAIL;
2250
2251 // qf_add_entry() will not set the qf_num field, as the
2252 // directory and file names are not supplied. So the qf_fnum
2253 // field is copied here.
2254 prevp = to_qfl->qf_last;
2255 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2256 prevp->qf_type = from_qfp->qf_type; // error type
2257 if (from_qfl->qf_ptr == from_qfp)
2258 to_qfl->qf_ptr = prevp; // current location
2259 }
2260
2261 return OK;
2262}
2263
2264/*
2265 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2266 */
2267 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002268copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002269{
2270 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002271 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002272 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2273 to_qfl->qf_count = 0;
2274 to_qfl->qf_index = 0;
2275 to_qfl->qf_start = NULL;
2276 to_qfl->qf_last = NULL;
2277 to_qfl->qf_ptr = NULL;
2278 if (from_qfl->qf_title != NULL)
2279 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2280 else
2281 to_qfl->qf_title = NULL;
2282 if (from_qfl->qf_ctx != NULL)
2283 {
2284 to_qfl->qf_ctx = alloc_tv();
2285 if (to_qfl->qf_ctx != NULL)
2286 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2287 }
2288 else
2289 to_qfl->qf_ctx = NULL;
2290
2291 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002292 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002293 return FAIL;
2294
2295 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2296
2297 // Assign a new ID for the location list
2298 to_qfl->qf_id = ++last_qf_id;
2299 to_qfl->qf_changedtick = 0L;
2300
2301 // When no valid entries are present in the list, qf_ptr points to
2302 // the first item in the list
2303 if (to_qfl->qf_nonevalid)
2304 {
2305 to_qfl->qf_ptr = to_qfl->qf_start;
2306 to_qfl->qf_index = 1;
2307 }
2308
2309 return OK;
2310}
2311
2312/*
2313 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002314 */
2315 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002316copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002317{
2318 qf_info_T *qi;
2319 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002320
Bram Moolenaar09037502018-09-25 22:08:14 +02002321 // When copying from a location list window, copy the referenced
2322 // location list. For other windows, copy the location list for
2323 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002324 if (IS_LL_WINDOW(from))
2325 qi = from->w_llist_ref;
2326 else
2327 qi = from->w_llist;
2328
Bram Moolenaar09037502018-09-25 22:08:14 +02002329 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002330 return;
2331
Bram Moolenaar09037502018-09-25 22:08:14 +02002332 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002333 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002334 return;
2335
2336 to->w_llist->qf_listcount = qi->qf_listcount;
2337
Bram Moolenaar09037502018-09-25 22:08:14 +02002338 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002339 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002340 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002341 to->w_llist->qf_curlist = idx;
2342
Bram Moolenaar0398e002019-03-21 21:12:49 +01002343 if (copy_loclist(qf_get_list(qi, idx),
2344 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002345 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002346 qf_free_all(to);
2347 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002348 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002349 }
2350
Bram Moolenaar09037502018-09-25 22:08:14 +02002351 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002352}
2353
2354/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002355 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002356 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 */
2358 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002359qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
Bram Moolenaar82404332016-07-10 17:00:38 +02002361 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002362 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002363 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002364
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002365 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
Bram Moolenaare60acc12011-05-10 16:41:25 +02002368#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002369 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002370#endif
2371#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002372 if (directory != NULL)
2373 slash_adjust(directory);
2374 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002375#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002376 if (directory != NULL && !vim_isAbsName(fname)
2377 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2378 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002379 // Here we check if the file really exists.
2380 // This should normally be true, but if make works without
2381 // "leaving directory"-messages we might have missed a
2382 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002383 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002386 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002387 if (directory)
2388 ptr = concat_fnames(directory, fname, TRUE);
2389 else
2390 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002392 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002393 bufname = ptr;
2394 }
2395 else
2396 bufname = fname;
2397
2398 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002399 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002400 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002401 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002402 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002404 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002405 {
2406 vim_free(qf_last_bufname);
2407 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2408 if (bufname == ptr)
2409 qf_last_bufname = bufname;
2410 else
2411 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002412 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002413 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002414 if (buf == NULL)
2415 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002416
Bram Moolenaarc1542742016-07-20 21:44:37 +02002417 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002418 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002419 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420}
2421
2422/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002423 * Push dirbuf onto the directory stack and return pointer to actual dir or
2424 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 */
2426 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002427qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428{
2429 struct dir_stack_T *ds_new;
2430 struct dir_stack_T *ds_ptr;
2431
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002432 // allocate new stack element and hook it in
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002433 ds_new = ALLOC_ONE(struct dir_stack_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 if (ds_new == NULL)
2435 return NULL;
2436
2437 ds_new->next = *stackptr;
2438 *stackptr = ds_new;
2439
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002440 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 if (vim_isAbsName(dirbuf)
2442 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002443 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 (*stackptr)->dirname = vim_strsave(dirbuf);
2445 else
2446 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002447 // Okay we don't have an absolute path.
2448 // dirbuf must be a subdir of one of the directories on the stack.
2449 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 ds_new = (*stackptr)->next;
2451 (*stackptr)->dirname = NULL;
2452 while (ds_new)
2453 {
2454 vim_free((*stackptr)->dirname);
2455 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2456 TRUE);
2457 if (mch_isdir((*stackptr)->dirname) == TRUE)
2458 break;
2459
2460 ds_new = ds_new->next;
2461 }
2462
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002463 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 while ((*stackptr)->next != ds_new)
2465 {
2466 ds_ptr = (*stackptr)->next;
2467 (*stackptr)->next = (*stackptr)->next->next;
2468 vim_free(ds_ptr->dirname);
2469 vim_free(ds_ptr);
2470 }
2471
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002472 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 if (ds_new == NULL)
2474 {
2475 vim_free((*stackptr)->dirname);
2476 (*stackptr)->dirname = vim_strsave(dirbuf);
2477 }
2478 }
2479
2480 if ((*stackptr)->dirname != NULL)
2481 return (*stackptr)->dirname;
2482 else
2483 {
2484 ds_ptr = *stackptr;
2485 *stackptr = (*stackptr)->next;
2486 vim_free(ds_ptr);
2487 return NULL;
2488 }
2489}
2490
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491/*
2492 * pop dirbuf from the directory stack and return previous directory or NULL if
2493 * stack is empty
2494 */
2495 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002496qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 struct dir_stack_T *ds_ptr;
2499
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002500 // TODO: Should we check if dirbuf is the directory on top of the stack?
2501 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002503 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 if (*stackptr != NULL)
2505 {
2506 ds_ptr = *stackptr;
2507 *stackptr = (*stackptr)->next;
2508 vim_free(ds_ptr->dirname);
2509 vim_free(ds_ptr);
2510 }
2511
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002512 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 return *stackptr ? (*stackptr)->dirname : NULL;
2514}
2515
2516/*
2517 * clean up directory stack
2518 */
2519 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002520qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521{
2522 struct dir_stack_T *ds_ptr;
2523
2524 while ((ds_ptr = *stackptr) != NULL)
2525 {
2526 *stackptr = (*stackptr)->next;
2527 vim_free(ds_ptr->dirname);
2528 vim_free(ds_ptr);
2529 }
2530}
2531
2532/*
2533 * Check in which directory of the directory stack the given file can be
2534 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002535 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 * Cleans up intermediate directory entries.
2537 *
2538 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002539 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 * ./
2541 * ./aa
2542 * ./aa/bb
2543 * ./bb
2544 * ./bb/x.c
2545 * and make says:
2546 * making all in aa
2547 * making all in bb
2548 * x.c:9: Error
2549 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2550 * qf_guess_filepath will return NULL.
2551 */
2552 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002553qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 struct dir_stack_T *ds_ptr;
2556 struct dir_stack_T *ds_tmp;
2557 char_u *fullname;
2558
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002559 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002560 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 return NULL;
2562
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002563 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 fullname = NULL;
2565 while (ds_ptr)
2566 {
2567 vim_free(fullname);
2568 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2569
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002570 // If concat_fnames failed, just go on. The worst thing that can happen
2571 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2573 break;
2574
2575 ds_ptr = ds_ptr->next;
2576 }
2577
2578 vim_free(fullname);
2579
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002580 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002581 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002583 ds_tmp = qfl->qf_dir_stack->next;
2584 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 vim_free(ds_tmp->dirname);
2586 vim_free(ds_tmp);
2587 }
2588
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002589 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590}
2591
2592/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002593 * Returns TRUE if a quickfix/location list with the given identifier exists.
2594 */
2595 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002596qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002597{
2598 qf_info_T *qi = &ql_info;
2599 int i;
2600
2601 if (wp != NULL)
2602 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002603 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002604 if (qi == NULL)
2605 return FALSE;
2606 }
2607
2608 for (i = 0; i < qi->qf_listcount; ++i)
2609 if (qi->qf_lists[i].qf_id == qf_id)
2610 return TRUE;
2611
2612 return FALSE;
2613}
2614
2615/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002616 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002617 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002618 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002619 * Similar to location list.
2620 */
2621 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002622is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002623{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002624 qfline_T *qfp;
2625 int i;
2626
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002627 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002628 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2629 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002630 break;
2631
Bram Moolenaar95946f12019-03-31 15:31:59 +02002632 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002633 return FALSE;
2634
2635 return TRUE;
2636}
2637
2638/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002639 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002640 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002641 */
2642 static qfline_T *
2643get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002644 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002645 qfline_T *qf_ptr,
2646 int *qf_index,
2647 int dir)
2648{
2649 int idx;
2650 int old_qf_fnum;
2651
2652 idx = *qf_index;
2653 old_qf_fnum = qf_ptr->qf_fnum;
2654
2655 do
2656 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002657 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002658 return NULL;
2659 ++idx;
2660 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002661 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002662 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2663
2664 *qf_index = idx;
2665 return qf_ptr;
2666}
2667
2668/*
2669 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002670 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002671 */
2672 static qfline_T *
2673get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002674 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002675 qfline_T *qf_ptr,
2676 int *qf_index,
2677 int dir)
2678{
2679 int idx;
2680 int old_qf_fnum;
2681
2682 idx = *qf_index;
2683 old_qf_fnum = qf_ptr->qf_fnum;
2684
2685 do
2686 {
2687 if (idx == 1 || qf_ptr->qf_prev == NULL)
2688 return NULL;
2689 --idx;
2690 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002691 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002692 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2693
2694 *qf_index = idx;
2695 return qf_ptr;
2696}
2697
2698/*
2699 * Get the n'th (errornr) previous/next valid entry from the current entry in
2700 * the quickfix list.
2701 * dir == FORWARD or FORWARD_FILE: next valid entry
2702 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2703 */
2704 static qfline_T *
2705get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002706 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002707 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002708 int dir,
2709 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002710{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002711 qfline_T *qf_ptr = qfl->qf_ptr;
2712 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002713 qfline_T *prev_qf_ptr;
2714 int prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002715 char_u *err = e_no_more_items;
2716
2717 while (errornr--)
2718 {
2719 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002720 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002721
2722 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002723 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002724 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002725 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002726 if (qf_ptr == NULL)
2727 {
2728 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002729 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002730 if (err != NULL)
2731 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002732 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002733 return NULL;
2734 }
2735 break;
2736 }
2737
2738 err = NULL;
2739 }
2740
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002741 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002742 return qf_ptr;
2743}
2744
2745/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002746 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2747 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002748 */
2749 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002750get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002751{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002752 qfline_T *qf_ptr = qfl->qf_ptr;
2753 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002754
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002755 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002756 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2757 {
2758 --qf_idx;
2759 qf_ptr = qf_ptr->qf_prev;
2760 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002761 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002762 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2763 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002764 {
2765 ++qf_idx;
2766 qf_ptr = qf_ptr->qf_next;
2767 }
2768
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002769 *new_qfidx = qf_idx;
2770 return qf_ptr;
2771}
2772
2773/*
2774 * Get a entry specied by 'errornr' and 'dir' from the current
2775 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2776 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2777 * Returns a pointer to the entry and the index of the new entry is stored in
2778 * 'new_qfidx'.
2779 */
2780 static qfline_T *
2781qf_get_entry(
2782 qf_list_T *qfl,
2783 int errornr,
2784 int dir,
2785 int *new_qfidx)
2786{
2787 qfline_T *qf_ptr = qfl->qf_ptr;
2788 int qfidx = qfl->qf_index;
2789
2790 if (dir != 0) // next/prev valid entry
2791 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2792 else if (errornr != 0) // go to specified number
2793 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2794
2795 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002796 return qf_ptr;
2797}
2798
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002799/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002800 * Find a window displaying a Vim help file.
2801 */
2802 static win_T *
2803qf_find_help_win(void)
2804{
2805 win_T *wp;
2806
2807 FOR_ALL_WINDOWS(wp)
2808 if (bt_help(wp->w_buffer))
2809 return wp;
2810
2811 return NULL;
2812}
2813
2814/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002815 * Set the location list for the specified window to 'qi'.
2816 */
2817 static void
2818win_set_loclist(win_T *wp, qf_info_T *qi)
2819{
2820 wp->w_llist = qi;
2821 qi->qf_refcount++;
2822}
2823
2824/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002825 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2826 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002827 */
2828 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002829jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002830{
2831 win_T *wp;
2832 int flags;
2833
Bram Moolenaarb2443732018-11-11 22:50:27 +01002834 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002835 wp = NULL;
2836 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002837 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002838 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2839 win_enter(wp, TRUE);
2840 else
2841 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002842 // Split off help window; put it at far top if no position
2843 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002844 flags = WSP_HELP;
2845 if (cmdmod.split == 0 && curwin->w_width != Columns
2846 && curwin->w_width < 80)
2847 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002848 // If the user asks to open a new window, then copy the location list.
2849 // Otherwise, don't copy the location list.
2850 if (IS_LL_STACK(qi) && !newwin)
2851 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002852
2853 if (win_split(0, flags) == FAIL)
2854 return FAIL;
2855
2856 *opened_window = TRUE;
2857
2858 if (curwin->w_height < p_hh)
2859 win_setheight((int)p_hh);
2860
Bram Moolenaarb2443732018-11-11 22:50:27 +01002861 // When using location list, the new window should use the supplied
2862 // location list. If the user asks to open a new window, then the new
2863 // window will get a copy of the location list.
2864 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002865 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002866 }
2867
2868 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002869 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002870
2871 return OK;
2872}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002873
2874/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002875 * Find a non-quickfix window in the current tabpage using the given location
2876 * list stack.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002877 * Returns NULL if a matching window is not found.
2878 */
2879 static win_T *
2880qf_find_win_with_loclist(qf_info_T *ll)
2881{
2882 win_T *wp;
2883
2884 FOR_ALL_WINDOWS(wp)
2885 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2886 return wp;
2887
2888 return NULL;
2889}
2890
2891/*
2892 * Find a window containing a normal buffer
2893 */
2894 static win_T *
2895qf_find_win_with_normal_buf(void)
2896{
2897 win_T *wp;
2898
2899 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002900 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002901 return wp;
2902
2903 return NULL;
2904}
2905
2906/*
2907 * Go to a window in any tabpage containing the specified file. Returns TRUE
2908 * if successfully jumped to the window. Otherwise returns FALSE.
2909 */
2910 static int
2911qf_goto_tabwin_with_file(int fnum)
2912{
2913 tabpage_T *tp;
2914 win_T *wp;
2915
2916 FOR_ALL_TAB_WINDOWS(tp, wp)
2917 if (wp->w_buffer->b_fnum == fnum)
2918 {
2919 goto_tabpage_win(tp, wp);
2920 return TRUE;
2921 }
2922
2923 return FALSE;
2924}
2925
2926/*
2927 * Create a new window to show a file above the quickfix window. Called when
2928 * only the quickfix window is present.
2929 */
2930 static int
2931qf_open_new_file_win(qf_info_T *ll_ref)
2932{
2933 int flags;
2934
2935 flags = WSP_ABOVE;
2936 if (ll_ref != NULL)
2937 flags |= WSP_NEWLOC;
2938 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002939 return FAIL; // not enough room for window
2940 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002941 swb_flags = 0;
2942 RESET_BINDING(curwin);
2943 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002944 // The new window should use the location list from the
2945 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002946 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002947 return OK;
2948}
2949
2950/*
2951 * Go to a window that shows the right buffer. If the window is not found, go
2952 * to the window just above the location list window. This is used for opening
2953 * a file from a location window and not from a quickfix window. If some usable
2954 * window is previously found, then it is supplied in 'use_win'.
2955 */
2956 static void
2957qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2958{
2959 win_T *win = use_win;
2960
2961 if (win == NULL)
2962 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002963 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002964 FOR_ALL_WINDOWS(win)
2965 if (win->w_buffer->b_fnum == qf_fnum)
2966 break;
2967 if (win == NULL)
2968 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002969 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002970 win = curwin;
2971 do
2972 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002973 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002974 break;
2975 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002976 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002977 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002978 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002979 } while (win != curwin);
2980 }
2981 }
2982 win_goto(win);
2983
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002984 // If the location list for the window is not set, then set it
2985 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01002986 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002987 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002988}
2989
2990/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002991 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2992 * not found, then go to the window just above the quickfix window. This is
2993 * used for opening a file from a quickfix window and not from a location
2994 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002995 */
2996 static void
2997qf_goto_win_with_qfl_file(int qf_fnum)
2998{
2999 win_T *win;
3000 win_T *altwin;
3001
3002 win = curwin;
3003 altwin = NULL;
3004 for (;;)
3005 {
3006 if (win->w_buffer->b_fnum == qf_fnum)
3007 break;
3008 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003009 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003010 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003011 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003012
3013 if (IS_QF_WINDOW(win))
3014 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003015 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003016 // window, unless 'switchbuf' contains 'uselast': in this case we
3017 // try to jump to the previously used window first.
3018 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3019 win = prevwin;
3020 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003021 win = altwin;
3022 else if (curwin->w_prev != NULL)
3023 win = curwin->w_prev;
3024 else
3025 win = curwin->w_next;
3026 break;
3027 }
3028
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003029 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003030 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003031 altwin = win;
3032 }
3033
3034 win_goto(win);
3035}
3036
3037/*
3038 * Find a suitable window for opening a file (qf_fnum) from the
3039 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003040 * window, jump to it. Otherwise open a new window to display the file. If
3041 * 'newwin' is TRUE, then always open a new window. This is called from either
3042 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003043 */
3044 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003045qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003046{
3047 win_T *usable_win_ptr = NULL;
3048 int usable_win;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003049 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003050 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003051
3052 usable_win = 0;
3053
Bram Moolenaarb2443732018-11-11 22:50:27 +01003054 // If opening a new window, then don't use the location list referred by
3055 // the current window. Otherwise two windows will refer to the same
3056 // location list.
3057 if (!newwin)
3058 ll_ref = curwin->w_llist_ref;
3059
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003060 if (ll_ref != NULL)
3061 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003062 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003063 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
3064 if (usable_win_ptr != NULL)
3065 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003066 }
3067
3068 if (!usable_win)
3069 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003070 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003071 win = qf_find_win_with_normal_buf();
3072 if (win != NULL)
3073 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003074 }
3075
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003076 // If no usable window is found and 'switchbuf' contains "usetab"
3077 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003078 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003079 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003080
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003081 // If there is only one window and it is the quickfix window, create a
3082 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003083 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003084 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003085 if (qf_open_new_file_win(ll_ref) != OK)
3086 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003087 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003088 }
3089 else
3090 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003091 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003092 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003093 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003094 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003095 }
3096
3097 return OK;
3098}
3099
3100/*
3101 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003102 * Returns OK if successfully edited the file, FAIL on failing to open the
3103 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3104 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003105 */
3106 static int
3107qf_jump_edit_buffer(
3108 qf_info_T *qi,
3109 qfline_T *qf_ptr,
3110 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003111 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003112 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003113{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003114 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003115 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003116 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003117 int old_qf_curlist = qi->qf_curlist;
3118 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003119
3120 if (qf_ptr->qf_type == 1)
3121 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003122 // Open help file (do_ecmd() will set b_help flag, readfile() will
3123 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003124 if (!can_abandon(curbuf, forceit))
3125 {
3126 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003127 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003128 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003129
3130 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3131 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003132 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003133 }
3134 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003135 retval = buflist_getfile(qf_ptr->qf_fnum,
3136 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003137
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003138 // If a location list, check whether the associated window is still
3139 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003140 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003141 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003142 win_T *wp = win_id2wp(prev_winid);
3143 if (wp == NULL && curwin->w_llist != qi)
3144 {
3145 emsg(_("E924: Current window was closed"));
3146 *opened_window = FALSE;
3147 return NOTDONE;
3148 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003149 }
3150
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003151 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003152 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003153 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003154 return NOTDONE;
3155 }
3156
3157 if (old_qf_curlist != qi->qf_curlist
3158 || !is_qf_entry_present(qfl, qf_ptr))
3159 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003160 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003161 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003162 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003163 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003164 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003165 }
3166
3167 return retval;
3168}
3169
3170/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003171 * Go to the error line in the current file using either line/column number or
3172 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003173 */
3174 static void
3175qf_jump_goto_line(
3176 linenr_T qf_lnum,
3177 int qf_col,
3178 char_u qf_viscol,
3179 char_u *qf_pattern)
3180{
3181 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003182
3183 if (qf_pattern == NULL)
3184 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003185 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003186 i = qf_lnum;
3187 if (i > 0)
3188 {
3189 if (i > curbuf->b_ml.ml_line_count)
3190 i = curbuf->b_ml.ml_line_count;
3191 curwin->w_cursor.lnum = i;
3192 }
3193 if (qf_col > 0)
3194 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003195 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003196 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003197 coladvance(qf_col - 1);
3198 else
3199 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003200 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003201 check_cursor();
3202 }
3203 else
3204 beginline(BL_WHITE | BL_FIX);
3205 }
3206 else
3207 {
3208 pos_T save_cursor;
3209
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003210 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003211 save_cursor = curwin->w_cursor;
3212 curwin->w_cursor.lnum = 0;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003213 if (!do_search(NULL, '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003214 curwin->w_cursor = save_cursor;
3215 }
3216}
3217
3218/*
3219 * Display quickfix list index and size message
3220 */
3221 static void
3222qf_jump_print_msg(
3223 qf_info_T *qi,
3224 int qf_index,
3225 qfline_T *qf_ptr,
3226 buf_T *old_curbuf,
3227 linenr_T old_lnum)
3228{
3229 linenr_T i;
3230 int len;
3231
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003232 // Update the screen before showing the message, unless the screen
3233 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003234 if (!msg_scrolled)
3235 update_topline_redraw();
3236 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003237 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003238 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3239 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003240 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003241 len = (int)STRLEN(IObuff);
3242 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3243
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003244 // Output the message. Overwrite to avoid scrolling when the 'O'
3245 // flag is present in 'shortmess'; But when not jumping, print the
3246 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003247 i = msg_scroll;
3248 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3249 msg_scroll = TRUE;
3250 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3251 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003252 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003253 msg_scroll = i;
3254}
3255
3256/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003257 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003258 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3259 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003260 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3261 * able to jump/open a window. Returns NOTDONE if a file is not associated
3262 * with the entry.
3263 */
3264 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003265qf_jump_open_window(
3266 qf_info_T *qi,
3267 qfline_T *qf_ptr,
3268 int newwin,
3269 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003270{
3271 // For ":helpgrep" find a help window or open one.
3272 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003273 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003274 return FAIL;
3275
3276 // If currently in the quickfix window, find another window to show the
3277 // file in.
3278 if (bt_quickfix(curbuf) && !*opened_window)
3279 {
3280 // If there is no file specified, we don't know where to go.
3281 // But do advance, otherwise ":cn" gets stuck.
3282 if (qf_ptr->qf_fnum == 0)
3283 return NOTDONE;
3284
Bram Moolenaarb2443732018-11-11 22:50:27 +01003285 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3286 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003287 return FAIL;
3288 }
3289
3290 return OK;
3291}
3292
3293/*
3294 * Edit a selected file from the quickfix/location list and jump to a
3295 * particular line/column, adjust the folds and display a message about the
3296 * jump.
3297 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3298 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3299 * the file.
3300 */
3301 static int
3302qf_jump_to_buffer(
3303 qf_info_T *qi,
3304 int qf_index,
3305 qfline_T *qf_ptr,
3306 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003307 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003308 int *opened_window,
3309 int openfold,
3310 int print_message)
3311{
3312 buf_T *old_curbuf;
3313 linenr_T old_lnum;
3314 int retval = OK;
3315
3316 // If there is a file name, read the wanted file if needed, and check
3317 // autowrite etc.
3318 old_curbuf = curbuf;
3319 old_lnum = curwin->w_cursor.lnum;
3320
3321 if (qf_ptr->qf_fnum != 0)
3322 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003323 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003324 opened_window);
3325 if (retval != OK)
3326 return retval;
3327 }
3328
3329 // When not switched to another buffer, still need to set pc mark
3330 if (curbuf == old_curbuf)
3331 setpcmark();
3332
3333 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3334 qf_ptr->qf_pattern);
3335
3336#ifdef FEAT_FOLDING
3337 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3338 foldOpenCursor();
3339#endif
3340 if (print_message)
3341 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3342
3343 return retval;
3344}
3345
3346/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003347 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 */
3349 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003350qf_jump(qf_info_T *qi,
3351 int dir,
3352 int errornr,
3353 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003355 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3356}
3357
3358/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003359 * Jump to a quickfix line.
3360 * If dir == 0 go to entry "errornr".
3361 * If dir == FORWARD go "errornr" valid entries forward.
3362 * If dir == BACKWARD go "errornr" valid entries backward.
3363 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3364 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3365 * else if "errornr" is zero, redisplay the same line
3366 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003367 * If 'newwin' is TRUE, then open the file in a new window.
3368 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003369 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003370qf_jump_newwin(qf_info_T *qi,
3371 int dir,
3372 int errornr,
3373 int forceit,
3374 int newwin)
3375{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003376 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003377 qfline_T *qf_ptr;
3378 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003381 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003382 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003383 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003386 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003387 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003389 if (qi == NULL)
3390 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003391
Bram Moolenaar0398e002019-03-21 21:12:49 +01003392 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003394 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 return;
3396 }
3397
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003398 incr_quickfix_busy();
3399
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003400 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003401
3402 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003404 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003406
3407 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3408 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003410 qf_ptr = old_qf_ptr;
3411 qf_index = old_qf_index;
3412 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003415 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003416 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003417 // No need to print the error message if it's visible in the error
3418 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 print_message = FALSE;
3420
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003421 prev_winid = curwin->w_id;
3422
Bram Moolenaarb2443732018-11-11 22:50:27 +01003423 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003424 if (retval == FAIL)
3425 goto failed;
3426 if (retval == NOTDONE)
3427 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003428
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003429 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003430 &opened_window, old_KeyTyped, print_message);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003431 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003433 // Quickfix/location list is freed by an autocmd
3434 qi = NULL;
3435 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003438 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003441 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003442 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003444 // Couldn't open file, so put index back where it was. This could
3445 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 qf_ptr = old_qf_ptr;
3448 qf_index = old_qf_index;
3449 }
3450 }
3451theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003452 if (qi != NULL)
3453 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003454 qfl->qf_ptr = qf_ptr;
3455 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003456 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003457 if (p_swb != old_swb)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003459 // Restore old 'switchbuf' value, but not when an autocommand or
3460 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003464 swb_flags = old_swb_flags;
3465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 else
3467 free_string_option(old_swb);
3468 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003469 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470}
3471
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003472// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003473static int qfFileAttr;
3474static int qfSepAttr;
3475static int qfLineAttr;
3476
3477/*
3478 * Display information about a single entry from the quickfix/location list.
3479 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003480 * 'cursel' will be set to TRUE for the currently selected entry in the
3481 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003482 */
3483 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003484qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003485{
3486 char_u *fname;
3487 buf_T *buf;
3488 int filter_entry;
3489
3490 fname = NULL;
3491 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3492 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3493 (char *)qfp->qf_module);
3494 else {
3495 if (qfp->qf_fnum != 0
3496 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3497 {
3498 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003499 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003500 fname = gettail(fname);
3501 }
3502 if (fname == NULL)
3503 sprintf((char *)IObuff, "%2d", qf_idx);
3504 else
3505 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3506 qf_idx, (char *)fname);
3507 }
3508
3509 // Support for filtering entries using :filter /pat/ clist
3510 // Match against the module name, file name, search pattern and
3511 // text of the entry.
3512 filter_entry = TRUE;
3513 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3514 filter_entry &= message_filtered(qfp->qf_module);
3515 if (filter_entry && fname != NULL)
3516 filter_entry &= message_filtered(fname);
3517 if (filter_entry && qfp->qf_pattern != NULL)
3518 filter_entry &= message_filtered(qfp->qf_pattern);
3519 if (filter_entry)
3520 filter_entry &= message_filtered(qfp->qf_text);
3521 if (filter_entry)
3522 return;
3523
3524 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003525 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003526
3527 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003528 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003529 if (qfp->qf_lnum == 0)
3530 IObuff[0] = NUL;
3531 else if (qfp->qf_col == 0)
3532 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3533 else
3534 sprintf((char *)IObuff, "%ld col %d",
3535 qfp->qf_lnum, qfp->qf_col);
3536 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3537 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003538 msg_puts_attr((char *)IObuff, qfLineAttr);
3539 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003540 if (qfp->qf_pattern != NULL)
3541 {
3542 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003543 msg_puts((char *)IObuff);
3544 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003545 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003546 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003547
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003548 // Remove newlines and leading whitespace from the text. For an
3549 // unrecognized line keep the indent, the compiler may mark a word
3550 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003551 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3552 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3553 IObuff, IOSIZE);
3554 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003555 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003556}
3557
3558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003560 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 */
3562 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003563qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003565 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003566 qfline_T *qfp;
3567 int i;
3568 int idx1 = 1;
3569 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003570 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003571 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003572 int all = eap->forceit; // if not :cl!, only show
3573 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003574 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003576 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3577 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003578
Bram Moolenaar0398e002019-03-21 21:12:49 +01003579 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003581 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 return;
3583 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003584 if (*arg == '+')
3585 {
3586 ++arg;
3587 plus = TRUE;
3588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003591 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 return;
3593 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003594 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003595 if (plus)
3596 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003597 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003598 idx2 = i + idx1;
3599 idx1 = i;
3600 }
3601 else
3602 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003603 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003604 if (idx1 < 0)
3605 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3606 if (idx2 < 0)
3607 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003610 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003611 shorten_fnames(FALSE);
3612
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003613 // Get the attributes for the different quickfix highlight items. Note
3614 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003615 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3616 if (qfFileAttr == 0)
3617 qfFileAttr = HL_ATTR(HLF_D);
3618 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3619 if (qfSepAttr == 0)
3620 qfSepAttr = HL_ATTR(HLF_D);
3621 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3622 if (qfLineAttr == 0)
3623 qfLineAttr = HL_ATTR(HLF_N);
3624
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003625 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003627 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 {
3629 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003630 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003631
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 ui_breakcheck();
3633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634}
3635
3636/*
3637 * Remove newlines and leading whitespace from an error message.
3638 * Put the result in "buf[bufsize]".
3639 */
3640 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003641qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
3643 int i;
3644 char_u *p = text;
3645
3646 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3647 {
3648 if (*p == '\n')
3649 {
3650 buf[i] = ' ';
3651 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003652 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 break;
3654 }
3655 else
3656 buf[i] = *p++;
3657 }
3658 buf[i] = NUL;
3659}
3660
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003661/*
3662 * Display information (list number, list size and the title) about a
3663 * quickfix/location list.
3664 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003665 static void
3666qf_msg(qf_info_T *qi, int which, char *lead)
3667{
3668 char *title = (char *)qi->qf_lists[which].qf_title;
3669 int count = qi->qf_lists[which].qf_count;
3670 char_u buf[IOSIZE];
3671
3672 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3673 lead,
3674 which + 1,
3675 qi->qf_listcount,
3676 count);
3677
3678 if (title != NULL)
3679 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003680 size_t len = STRLEN(buf);
3681
3682 if (len < 34)
3683 {
3684 vim_memset(buf + len, ' ', 34 - len);
3685 buf[34] = NUL;
3686 }
3687 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003688 }
3689 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003690 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003691}
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693/*
3694 * ":colder [count]": Up in the quickfix stack.
3695 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003696 * ":lolder [count]": Up in the location list stack.
3697 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 */
3699 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003700qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003702 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 int count;
3704
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003705 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3706 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 if (eap->addr_count != 0)
3709 count = eap->line2;
3710 else
3711 count = 1;
3712 while (count--)
3713 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003714 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003716 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003718 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003719 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003721 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 }
3723 else
3724 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003725 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003727 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003728 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003730 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003733 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003734 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735}
3736
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003737/*
3738 * Display the information about all the quickfix/location lists in the stack
3739 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003740 void
3741qf_history(exarg_T *eap)
3742{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003743 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003744 int i;
3745
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003746 if (eap->addr_count > 0)
3747 {
3748 if (qi == NULL)
3749 {
3750 emsg(_(e_loclist));
3751 return;
3752 }
3753
3754 // Jump to the specified quickfix list
3755 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3756 {
3757 qi->qf_curlist = eap->line2 - 1;
3758 qf_msg(qi, qi->qf_curlist, "");
3759 qf_update_buffer(qi, NULL);
3760 }
3761 else
3762 emsg(_(e_invrange));
3763
3764 return;
3765 }
3766
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003767 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003768 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003769 else
3770 for (i = 0; i < qi->qf_listcount; ++i)
3771 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3772}
3773
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003775 * Free all the entries in the error list "idx". Note that other information
3776 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 */
3778 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003779qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003781 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003782 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003783 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003785 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003787 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003788 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003789 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003790 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003791 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003792 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003793 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003794 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003795 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003796 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003797 // Somehow qf_count may have an incorrect value, set it to 1
3798 // to avoid crashing when it's wrong.
3799 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003800 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003801 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003802 qfl->qf_start = qfpnext;
3803 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003805
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003806 qfl->qf_index = 0;
3807 qfl->qf_start = NULL;
3808 qfl->qf_last = NULL;
3809 qfl->qf_ptr = NULL;
3810 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003811
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003812 qf_clean_dir_stack(&qfl->qf_dir_stack);
3813 qfl->qf_directory = NULL;
3814 qf_clean_dir_stack(&qfl->qf_file_stack);
3815 qfl->qf_currfile = NULL;
3816 qfl->qf_multiline = FALSE;
3817 qfl->qf_multiignore = FALSE;
3818 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819}
3820
3821/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003822 * Free error list "idx". Frees all the entries in the quickfix list,
3823 * associated context information and the title.
3824 */
3825 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003826qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003827{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003828 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003829
Bram Moolenaard23a8232018-02-10 18:45:26 +01003830 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003831 free_tv(qfl->qf_ctx);
3832 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003833 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003834 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003835}
3836
3837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 * qf_mark_adjust: adjust marks
3839 */
3840 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003841qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003842 win_T *wp,
3843 linenr_T line1,
3844 linenr_T line2,
3845 long amount,
3846 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003848 int i;
3849 qfline_T *qfp;
3850 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003851 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003852 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003853 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
Bram Moolenaarc1542742016-07-20 21:44:37 +02003855 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003856 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003857 if (wp != NULL)
3858 {
3859 if (wp->w_llist == NULL)
3860 return;
3861 qi = wp->w_llist;
3862 }
3863
3864 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003865 {
3866 qf_list_T *qfl = qf_get_list(qi, idx);
3867
3868 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003869 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 if (qfp->qf_fnum == curbuf->b_fnum)
3871 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003872 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3874 {
3875 if (amount == MAXLNUM)
3876 qfp->qf_cleared = TRUE;
3877 else
3878 qfp->qf_lnum += amount;
3879 }
3880 else if (amount_after && qfp->qf_lnum > line2)
3881 qfp->qf_lnum += amount_after;
3882 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003883 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003884
3885 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003886 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887}
3888
3889/*
3890 * Make a nice message out of the error character and the error number:
3891 * char number message
3892 * e or E 0 " error"
3893 * w or W 0 " warning"
3894 * i or I 0 " info"
3895 * 0 0 ""
3896 * other 0 " c"
3897 * e or E n " error n"
3898 * w or W n " warning n"
3899 * i or I n " info n"
3900 * 0 n " error n"
3901 * other n " c n"
3902 * 1 x "" :helpgrep
3903 */
3904 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003905qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906{
3907 static char_u buf[20];
3908 static char_u cc[3];
3909 char_u *p;
3910
3911 if (c == 'W' || c == 'w')
3912 p = (char_u *)" warning";
3913 else if (c == 'I' || c == 'i')
3914 p = (char_u *)" info";
3915 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3916 p = (char_u *)" error";
3917 else if (c == 0 || c == 1)
3918 p = (char_u *)"";
3919 else
3920 {
3921 cc[0] = ' ';
3922 cc[1] = c;
3923 cc[2] = NUL;
3924 p = cc;
3925 }
3926
3927 if (nr <= 0)
3928 return p;
3929
3930 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3931 return buf;
3932}
3933
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003935 * When "split" is FALSE: Open the entry/result under the cursor.
3936 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3937 */
3938 void
3939qf_view_result(int split)
3940{
3941 qf_info_T *qi = &ql_info;
3942
3943 if (!bt_quickfix(curbuf))
3944 return;
3945
3946 if (IS_LL_WINDOW(curwin))
3947 qi = GET_LOC_LIST(curwin);
3948
Bram Moolenaar0398e002019-03-21 21:12:49 +01003949 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003950 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003951 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003952 return;
3953 }
3954
3955 if (split)
3956 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003957 // Open the selected entry in a new window
3958 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3959 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003960 return;
3961 }
3962
3963 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3964}
3965
3966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 * ":cwindow": open the quickfix window if we have errors to display,
3968 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003969 * ":lwindow": open the location list window if we have locations to display,
3970 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 */
3972 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003973ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003975 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003976 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 win_T *win;
3978
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003979 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3980 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003981
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003982 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003983
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003984 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003985 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003987 // If a quickfix window is open but we have no errors to display,
3988 // close the window. If a quickfix window is not open, then open
3989 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003990 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003991 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01003992 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 {
3994 if (win != NULL)
3995 ex_cclose(eap);
3996 }
3997 else if (win == NULL)
3998 ex_copen(eap);
3999}
4000
4001/*
4002 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004003 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004006ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004008 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004009 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004011 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4012 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004014 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004015 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 if (win != NULL)
4017 win_close(win, FALSE);
4018}
4019
4020/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004021 * Set "w:quickfix_title" if "qi" has a title.
4022 */
4023 static void
4024qf_set_title_var(qf_list_T *qfl)
4025{
4026 if (qfl->qf_title != NULL)
4027 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4028}
4029
4030/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004031 * Goto a quickfix or location list window (if present).
4032 * Returns OK if the window is found, FAIL otherwise.
4033 */
4034 static int
4035qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4036{
4037 win_T *win;
4038
4039 win = qf_find_win(qi);
4040 if (win == NULL)
4041 return FAIL;
4042
4043 win_goto(win);
4044 if (resize)
4045 {
4046 if (vertsplit)
4047 {
4048 if (sz != win->w_width)
4049 win_setwidth(sz);
4050 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004051 else if (sz != win->w_height && win->w_height
4052 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004053 win_setheight(sz);
4054 }
4055
4056 return OK;
4057}
4058
4059/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004060 * Set options for the buffer in the quickfix or location list window.
4061 */
4062 static void
4063qf_set_cwindow_options(void)
4064{
4065 // switch off 'swapfile'
4066 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4067 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4068 OPT_LOCAL);
4069 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4070 RESET_BINDING(curwin);
4071#ifdef FEAT_DIFF
4072 curwin->w_p_diff = FALSE;
4073#endif
4074#ifdef FEAT_FOLDING
4075 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4076 OPT_LOCAL);
4077#endif
4078}
4079
4080/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004081 * Open a new quickfix or location list window, load the quickfix buffer and
4082 * set the appropriate options for the window.
4083 * Returns FAIL if the window could not be opened.
4084 */
4085 static int
4086qf_open_new_cwindow(qf_info_T *qi, int height)
4087{
4088 buf_T *qf_buf;
4089 win_T *oldwin = curwin;
4090 tabpage_T *prevtab = curtab;
4091 int flags = 0;
4092 win_T *win;
4093
4094 qf_buf = qf_find_buf(qi);
4095
4096 // The current window becomes the previous window afterwards.
4097 win = curwin;
4098
4099 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4100 // Create the new quickfix window at the very bottom, except when
4101 // :belowright or :aboveleft is used.
4102 win_goto(lastwin);
4103 // Default is to open the window below the current window
4104 if (cmdmod.split == 0)
4105 flags = WSP_BELOW;
4106 flags |= WSP_NEWLOC;
4107 if (win_split(height, flags) == FAIL)
4108 return FAIL; // not enough room for window
4109 RESET_BINDING(curwin);
4110
4111 if (IS_LL_STACK(qi))
4112 {
4113 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004114 // location list stack from the window 'win'.
4115 curwin->w_llist_ref = qi;
4116 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004117 }
4118
4119 if (oldwin != curwin)
4120 oldwin = NULL; // don't store info when in another window
4121 if (qf_buf != NULL)
4122 {
4123 // Use the existing quickfix buffer
4124 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4125 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4126 }
4127 else
4128 {
4129 // Create a new quickfix buffer
4130 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4131
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004132 // save the number of the new buffer
4133 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004134 }
4135
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004136 // Set the options for the quickfix buffer/window (if not already done)
4137 // Do this even if the quickfix buffer was already present, as an autocmd
4138 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004139 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004140 qf_set_cwindow_options();
4141
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004142 // Only set the height when still in the same tab page and there is no
4143 // window to the side.
4144 if (curtab == prevtab && curwin->w_width == Columns)
4145 win_setheight(height);
4146 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4147 if (win_valid(win))
4148 prevwin = win;
4149
4150 return OK;
4151}
4152
4153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004155 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 */
4157 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004158ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004160 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004161 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004163 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004164 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004166 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4167 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004168
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004169 incr_quickfix_busy();
4170
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 if (eap->addr_count != 0)
4172 height = eap->line2;
4173 else
4174 height = QF_WINHEIGHT;
4175
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004176 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177#ifdef FEAT_GUI
4178 need_mouse_correct = TRUE;
4179#endif
4180
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004181 // Find an existing quickfix window, or open a new one.
4182 if (cmdmod.tab == 0)
4183 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4184 cmdmod.split & WSP_VERT);
4185 if (status == FAIL)
4186 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004187 {
4188 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004189 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004192 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004193 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004194 // Save the current index here, as updating the quickfix buffer may free
4195 // the quickfix list
4196 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004197
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004198 // Fill the buffer with the quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004199 qf_fill_buffer(qfl, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004201 decr_quickfix_busy();
4202
4203 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 curwin->w_cursor.col = 0;
4205 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004206 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207}
4208
4209/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004210 * Move the cursor in the quickfix window to "lnum".
4211 */
4212 static void
4213qf_win_goto(win_T *win, linenr_T lnum)
4214{
4215 win_T *old_curwin = curwin;
4216
4217 curwin = win;
4218 curbuf = win->w_buffer;
4219 curwin->w_cursor.lnum = lnum;
4220 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004221 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004222 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004223 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004224 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004225 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004226 curwin = old_curwin;
4227 curbuf = curwin->w_buffer;
4228}
4229
4230/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004231 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004232 */
4233 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004234ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004235{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004236 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004237 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004238
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004239 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4240 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004241
4242 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004243 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4244 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4245}
4246
4247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 * Return the number of the current entry (line number in the quickfix
4249 * window).
4250 */
4251 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004252qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004254 qf_info_T *qi = &ql_info;
4255
4256 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004257 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004258 qi = wp->w_llist_ref;
4259
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004260 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261}
4262
4263/*
4264 * Update the cursor position in the quickfix window to the current error.
4265 * Return TRUE if there is a quickfix window.
4266 */
4267 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004268qf_win_pos_update(
4269 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004270 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271{
4272 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004273 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004275 // Put the cursor on the current error in the quickfix window, so that
4276 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004277 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 if (win != NULL
4279 && qf_index <= win->w_buffer->b_ml.ml_line_count
4280 && old_qf_index != qf_index)
4281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 if (qf_index > old_qf_index)
4283 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004284 win->w_redraw_top = old_qf_index;
4285 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 else
4288 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004289 win->w_redraw_top = qf_index;
4290 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004292 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294 return win != NULL;
4295}
4296
4297/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004298 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004299 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004300 */
4301 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004302is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004303{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004304 // A window displaying the quickfix buffer will have the w_llist_ref field
4305 // set to NULL.
4306 // A window displaying a location list buffer will have the w_llist_ref
4307 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004308 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004309 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4310 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004311 return TRUE;
4312
4313 return FALSE;
4314}
4315
4316/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004317 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004318 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004319 */
4320 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004321qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004322{
4323 win_T *win;
4324
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004325 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004326 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004327 return win;
4328 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004329}
4330
4331/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004332 * Find a quickfix buffer.
4333 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 */
4335 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004336qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004338 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004339 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004341 if (qi->qf_bufnr != INVALID_QFBUFNR)
4342 {
4343 buf_T *qfbuf;
4344 qfbuf = buflist_findnr(qi->qf_bufnr);
4345 if (qfbuf != NULL)
4346 return qfbuf;
4347 // buffer is no longer present
4348 qi->qf_bufnr = INVALID_QFBUFNR;
4349 }
4350
Bram Moolenaar9c102382006-05-03 21:26:49 +00004351 FOR_ALL_TAB_WINDOWS(tp, win)
4352 if (is_qf_win(win, qi))
4353 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004354
Bram Moolenaar9c102382006-05-03 21:26:49 +00004355 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356}
4357
4358/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004359 * Update the w:quickfix_title variable in the quickfix/location list window
4360 */
4361 static void
4362qf_update_win_titlevar(qf_info_T *qi)
4363{
4364 win_T *win;
4365 win_T *curwin_save;
4366
4367 if ((win = qf_find_win(qi)) != NULL)
4368 {
4369 curwin_save = curwin;
4370 curwin = win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004371 qf_set_title_var(qf_get_curlist(qi));
Bram Moolenaard823fa92016-08-12 16:29:27 +02004372 curwin = curwin_save;
4373 }
4374}
4375
4376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 * Find the quickfix buffer. If it exists, update the contents.
4378 */
4379 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004380qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
4382 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004383 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004386 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004387 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 if (buf != NULL)
4389 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004390 linenr_T old_line_count = buf->b_ml.ml_line_count;
4391
4392 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004393 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004394 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
Bram Moolenaard823fa92016-08-12 16:29:27 +02004396 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004397
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004398 qf_fill_buffer(qf_get_curlist(qi), buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004399 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004400
Bram Moolenaar864293a2016-06-02 13:40:04 +02004401 if (old_last == NULL)
4402 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004403 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004404
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004405 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004406 aucmd_restbuf(&aco);
4407 }
4408
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004409 // Only redraw when added lines are visible. This avoids flickering
4410 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004411 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4412 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414}
4415
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004416/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004417 * Add an error line to the quickfix buffer.
4418 */
4419 static int
4420qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4421{
4422 int len;
4423 buf_T *errbuf;
4424
4425 if (qfp->qf_module != NULL)
4426 {
4427 STRCPY(IObuff, qfp->qf_module);
4428 len = (int)STRLEN(IObuff);
4429 }
4430 else if (qfp->qf_fnum != 0
4431 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4432 && errbuf->b_fname != NULL)
4433 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004434 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004435 STRCPY(IObuff, gettail(errbuf->b_fname));
4436 else
4437 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004438 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004439 if (errbuf->b_sfname == NULL
4440 || mch_isFullName(errbuf->b_sfname))
4441 {
4442 if (*dirname == NUL)
4443 mch_dirname(dirname, MAXPATHL);
4444 shorten_buf_fname(errbuf, dirname, FALSE);
4445 }
4446 STRCPY(IObuff, errbuf->b_fname);
4447 }
4448 len = (int)STRLEN(IObuff);
4449 }
4450 else
4451 len = 0;
4452 IObuff[len++] = '|';
4453
4454 if (qfp->qf_lnum > 0)
4455 {
4456 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4457 len += (int)STRLEN(IObuff + len);
4458
4459 if (qfp->qf_col > 0)
4460 {
4461 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4462 len += (int)STRLEN(IObuff + len);
4463 }
4464
4465 sprintf((char *)IObuff + len, "%s",
4466 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4467 len += (int)STRLEN(IObuff + len);
4468 }
4469 else if (qfp->qf_pattern != NULL)
4470 {
4471 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4472 len += (int)STRLEN(IObuff + len);
4473 }
4474 IObuff[len++] = '|';
4475 IObuff[len++] = ' ';
4476
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004477 // Remove newlines and leading whitespace from the text.
4478 // For an unrecognized line keep the indent, the compiler may
4479 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004480 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4481 IObuff + len, IOSIZE - len);
4482
4483 if (ml_append_buf(buf, lnum, IObuff,
4484 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4485 return FAIL;
4486
4487 return OK;
4488}
4489
4490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 * Fill current buffer with quickfix errors, replacing any previous contents.
4492 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004493 * If "old_last" is not NULL append the items after this one.
4494 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4495 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 */
4497 static void
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004498qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004500 linenr_T lnum;
4501 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004502 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
Bram Moolenaar864293a2016-06-02 13:40:04 +02004504 if (old_last == NULL)
4505 {
4506 if (buf != curbuf)
4507 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004508 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004509 return;
4510 }
4511
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004512 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004513 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4514 (void)ml_delete((linenr_T)1, FALSE);
4515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004517 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004518 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004520 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004521
4522 *dirname = NUL;
4523
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004524 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004525 if (old_last == NULL)
4526 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004527 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004528 lnum = 0;
4529 }
4530 else
4531 {
4532 qfp = old_last->qf_next;
4533 lnum = buf->b_ml.ml_line_count;
4534 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004535 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004537 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004539
Bram Moolenaar864293a2016-06-02 13:40:04 +02004540 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004542 if (qfp == NULL)
4543 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004545
4546 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004547 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004548 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 }
4550
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004551 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 check_lnums(TRUE);
4553
Bram Moolenaar864293a2016-06-02 13:40:04 +02004554 if (old_last == NULL)
4555 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004556 // Set the 'filetype' to "qf" each time after filling the buffer.
4557 // This resembles reading a file into a buffer, it's more logical when
4558 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004559 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004560 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4561 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004563 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004564 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004566 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004568 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004569 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004570
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004571 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004572 redraw_curbuf_later(NOT_VALID);
4573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004575 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 KeyTyped = old_KeyTyped;
4577}
4578
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004579/*
4580 * For every change made to the quickfix list, update the changed tick.
4581 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004582 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004583qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004584{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004585 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004586}
4587
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004589 * Return the quickfix/location list number with the given identifier.
4590 * Returns -1 if list is not found.
4591 */
4592 static int
4593qf_id2nr(qf_info_T *qi, int_u qfid)
4594{
4595 int qf_idx;
4596
4597 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4598 if (qi->qf_lists[qf_idx].qf_id == qfid)
4599 return qf_idx;
4600 return INVALID_QFIDX;
4601}
4602
4603/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004604 * If the current list is not "save_qfid" and we can find the list with that ID
4605 * then make it the current list.
4606 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004607 * Returns OK if successfully restored the list. Returns FAIL if the list with
4608 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004609 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004610 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004611qf_restore_list(qf_info_T *qi, int_u save_qfid)
4612{
4613 int curlist;
4614
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004615 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004616 {
4617 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004618 if (curlist < 0)
4619 // list is not present
4620 return FAIL;
4621 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004622 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004623 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004624}
4625
4626/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004627 * Jump to the first entry if there is one.
4628 */
4629 static void
4630qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4631{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004632 if (qf_restore_list(qi, save_qfid) == FAIL)
4633 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004634
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004635 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004636 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004637 qf_jump(qi, 0, 0, forceit);
4638}
4639
4640/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004641 * Return TRUE when using ":vimgrep" for ":grep".
4642 */
4643 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004644grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004645{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004646 return ((cmdidx == CMD_grep
4647 || cmdidx == CMD_lgrep
4648 || cmdidx == CMD_grepadd
4649 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004650 && STRCMP("internal",
4651 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4652}
4653
4654/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004655 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004657 static char_u *
4658make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004660 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004661 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004662 case CMD_make: return (char_u *)"make";
4663 case CMD_lmake: return (char_u *)"lmake";
4664 case CMD_grep: return (char_u *)"grep";
4665 case CMD_lgrep: return (char_u *)"lgrep";
4666 case CMD_grepadd: return (char_u *)"grepadd";
4667 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4668 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670}
4671
4672/*
4673 * Return the name for the errorfile, in allocated memory.
4674 * Find a new unique name when 'makeef' contains "##".
4675 * Returns NULL for error.
4676 */
4677 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004678get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
4680 char_u *p;
4681 char_u *name;
4682 static int start = -1;
4683 static int off = 0;
4684#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004685 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686#endif
4687
4688 if (*p_mef == NUL)
4689 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004690 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004692 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 return name;
4694 }
4695
4696 for (p = p_mef; *p; ++p)
4697 if (p[0] == '#' && p[1] == '#')
4698 break;
4699
4700 if (*p == NUL)
4701 return vim_strsave(p_mef);
4702
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004703 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 for (;;)
4705 {
4706 if (start == -1)
4707 start = mch_get_pid();
4708 else
4709 off += 19;
4710
Bram Moolenaar964b3742019-05-24 18:54:09 +02004711 name = alloc(STRLEN(p_mef) + 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 if (name == NULL)
4713 break;
4714 STRCPY(name, p_mef);
4715 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4716 STRCAT(name, p + 2);
4717 if (mch_getperm(name) < 0
4718#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004719 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 && mch_lstat((char *)name, &sb) < 0
4721#endif
4722 )
4723 break;
4724 vim_free(name);
4725 }
4726 return name;
4727}
4728
4729/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004730 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4731 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4732 */
4733 static char_u *
4734make_get_fullcmd(char_u *makecmd, char_u *fname)
4735{
4736 char_u *cmd;
4737 unsigned len;
4738
4739 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4740 if (*p_sp != NUL)
4741 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4742 cmd = alloc(len);
4743 if (cmd == NULL)
4744 return NULL;
4745 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4746 (char *)p_shq);
4747
4748 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4749 if (*p_sp != NUL)
4750 append_redir(cmd, len, p_sp, fname);
4751
4752 // Display the fully formed command. Output a newline if there's something
4753 // else than the :make command that was typed (in which case the cursor is
4754 // in column 0).
4755 if (msg_col == 0)
4756 msg_didout = FALSE;
4757 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004758 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004759 msg_outtrans(cmd); // show what we are doing
4760
4761 return cmd;
4762}
4763
4764/*
4765 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4766 */
4767 void
4768ex_make(exarg_T *eap)
4769{
4770 char_u *fname;
4771 char_u *cmd;
4772 char_u *enc = NULL;
4773 win_T *wp = NULL;
4774 qf_info_T *qi = &ql_info;
4775 int res;
4776 char_u *au_name = NULL;
4777 int_u save_qfid;
4778
4779 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4780 if (grep_internal(eap->cmdidx))
4781 {
4782 ex_vimgrep(eap);
4783 return;
4784 }
4785
4786 au_name = make_get_auname(eap->cmdidx);
4787 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4788 curbuf->b_fname, TRUE, curbuf))
4789 {
4790#ifdef FEAT_EVAL
4791 if (aborting())
4792 return;
4793#endif
4794 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004795 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004796
4797 if (is_loclist_cmd(eap->cmdidx))
4798 wp = curwin;
4799
4800 autowrite_all();
4801 fname = get_mef_name();
4802 if (fname == NULL)
4803 return;
4804 mch_remove(fname); // in case it's not unique
4805
4806 cmd = make_get_fullcmd(eap->arg, fname);
4807 if (cmd == NULL)
4808 return;
4809
4810 // let the shell know if we are redirecting output or not
4811 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4812
4813#ifdef AMIGA
4814 out_flush();
4815 // read window status report and redraw before message
4816 (void)char_avail();
4817#endif
4818
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004819 incr_quickfix_busy();
4820
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004821 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4822 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4823 (eap->cmdidx != CMD_grepadd
4824 && eap->cmdidx != CMD_lgrepadd),
4825 qf_cmdtitle(*eap->cmdlinep), enc);
4826 if (wp != NULL)
4827 {
4828 qi = GET_LOC_LIST(wp);
4829 if (qi == NULL)
4830 goto cleanup;
4831 }
4832 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004833 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004834
4835 // Remember the current quickfix list identifier, so that we can
4836 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004837 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004838 if (au_name != NULL)
4839 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4840 curbuf->b_fname, TRUE, curbuf);
4841 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4842 // display the first error
4843 qf_jump_first(qi, save_qfid, FALSE);
4844
4845cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004846 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004847 mch_remove(fname);
4848 vim_free(fname);
4849 vim_free(cmd);
4850}
4851
4852/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02004853 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004854 */
4855 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004856qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004857{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004858 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004859
4860 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4861 return 0;
4862 return qf_get_curlist(qi)->qf_count;
4863}
4864
4865/*
4866 * Returns the number of valid entries in the current quickfix/location list.
4867 */
4868 int
4869qf_get_valid_size(exarg_T *eap)
4870{
4871 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004872 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004873 qfline_T *qfp;
4874 int i, sz = 0;
4875 int prev_fnum = 0;
4876
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004877 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4878 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004879
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004880 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01004881 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004882 {
4883 if (qfp->qf_valid)
4884 {
4885 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004886 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004887 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4888 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004889 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004890 sz++;
4891 prev_fnum = qfp->qf_fnum;
4892 }
4893 }
4894 }
4895
4896 return sz;
4897}
4898
4899/*
4900 * Returns the current index of the quickfix/location list.
4901 * Returns 0 if there is an error.
4902 */
4903 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004904qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004905{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004906 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004907
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004908 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4909 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004910
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004911 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004912}
4913
4914/*
4915 * Returns the current index in the quickfix/location list (counting only valid
4916 * entries). If no valid entries are in the list, then returns 1.
4917 */
4918 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004919qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004920{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004921 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004922 qf_list_T *qfl;
4923 qfline_T *qfp;
4924 int i, eidx = 0;
4925 int prev_fnum = 0;
4926
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004927 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4928 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004929
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004930 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004931 qfp = qfl->qf_start;
4932
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004933 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004934 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004935 return 1;
4936
4937 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4938 {
4939 if (qfp->qf_valid)
4940 {
4941 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4942 {
4943 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4944 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004945 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004946 eidx++;
4947 prev_fnum = qfp->qf_fnum;
4948 }
4949 }
4950 else
4951 eidx++;
4952 }
4953 }
4954
4955 return eidx ? eidx : 1;
4956}
4957
4958/*
4959 * Get the 'n'th valid error entry in the quickfix or location list.
4960 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4961 * For :cdo and :ldo returns the 'n'th valid error entry.
4962 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4963 */
4964 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004965qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004966{
Bram Moolenaar95946f12019-03-31 15:31:59 +02004967 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004968 int i, eidx;
4969 int prev_fnum = 0;
4970
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004971 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004972 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004973 return 1;
4974
Bram Moolenaar95946f12019-03-31 15:31:59 +02004975 eidx = 0;
4976 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004977 {
4978 if (qfp->qf_valid)
4979 {
4980 if (fdo)
4981 {
4982 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4983 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004984 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004985 eidx++;
4986 prev_fnum = qfp->qf_fnum;
4987 }
4988 }
4989 else
4990 eidx++;
4991 }
4992
4993 if (eidx == n)
4994 break;
4995 }
4996
4997 if (i <= qfl->qf_count)
4998 return i;
4999 else
5000 return 1;
5001}
5002
5003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005005 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005006 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 */
5008 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005009ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005011 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005012 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005013
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005014 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5015 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005016
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005017 if (eap->addr_count > 0)
5018 errornr = (int)eap->line2;
5019 else
5020 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005021 switch (eap->cmdidx)
5022 {
5023 case CMD_cc: case CMD_ll:
5024 errornr = 0;
5025 break;
5026 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5027 case CMD_lfirst:
5028 errornr = 1;
5029 break;
5030 default:
5031 errornr = 32767;
5032 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005033 }
5034
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005035 // For cdo and ldo commands, jump to the nth valid error.
5036 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005037 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5038 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005039 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005040 eap->addr_count > 0 ? (int)eap->line1 : 1,
5041 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5042
5043 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044}
5045
5046/*
5047 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005048 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005049 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 */
5051 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005052ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005054 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005055 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005056 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005057
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005058 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5059 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005060
Bram Moolenaar55b69262017-08-13 13:42:01 +02005061 if (eap->addr_count > 0
5062 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5063 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005064 errornr = (int)eap->line2;
5065 else
5066 errornr = 1;
5067
Bram Moolenaar39665952018-08-15 20:59:48 +02005068 // Depending on the command jump to either next or previous entry/file.
5069 switch (eap->cmdidx)
5070 {
5071 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5072 dir = FORWARD;
5073 break;
5074 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5075 case CMD_lNext:
5076 dir = BACKWARD;
5077 break;
5078 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5079 dir = FORWARD_FILE;
5080 break;
5081 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5082 dir = BACKWARD_FILE;
5083 break;
5084 default:
5085 dir = FORWARD;
5086 break;
5087 }
5088
5089 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090}
5091
5092/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005093 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5094 * The index of the entry is stored in 'errornr'.
5095 * Returns NULL if an entry is not found.
5096 */
5097 static qfline_T *
5098qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5099{
5100 qfline_T *qfp = NULL;
5101 int idx = 0;
5102
5103 // Find the first entry in this file
5104 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5105 if (qfp->qf_fnum == bnr)
5106 break;
5107
5108 *errornr = idx;
5109 return qfp;
5110}
5111
5112/*
5113 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5114 * with the error number for the first entry. Assumes the entries are sorted in
5115 * the quickfix list by line number.
5116 */
5117 static qfline_T *
5118qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5119{
5120 while (!got_int
5121 && entry->qf_prev != NULL
5122 && entry->qf_fnum == entry->qf_prev->qf_fnum
5123 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5124 {
5125 entry = entry->qf_prev;
5126 --*errornr;
5127 }
5128
5129 return entry;
5130}
5131
5132/*
5133 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5134 * with the error number for the last entry. Assumes the entries are sorted in
5135 * the quickfix list by line number.
5136 */
5137 static qfline_T *
5138qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5139{
5140 while (!got_int &&
5141 entry->qf_next != NULL
5142 && entry->qf_fnum == entry->qf_next->qf_fnum
5143 && entry->qf_lnum == entry->qf_next->qf_lnum)
5144 {
5145 entry = entry->qf_next;
5146 ++*errornr;
5147 }
5148
5149 return entry;
5150}
5151
5152/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005153 * Returns TRUE if the specified quickfix entry is
5154 * after the given line (linewise is TRUE)
5155 * or after the line and column.
5156 */
5157 static int
5158qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5159{
5160 if (linewise)
5161 return qfp->qf_lnum > pos->lnum;
5162 else
5163 return (qfp->qf_lnum > pos->lnum ||
5164 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5165}
5166
5167/*
5168 * Returns TRUE if the specified quickfix entry is
5169 * before the given line (linewise is TRUE)
5170 * or before the line and column.
5171 */
5172 static int
5173qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5174{
5175 if (linewise)
5176 return qfp->qf_lnum < pos->lnum;
5177 else
5178 return (qfp->qf_lnum < pos->lnum ||
5179 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5180}
5181
5182/*
5183 * Returns TRUE if the specified quickfix entry is
5184 * on or after the given line (linewise is TRUE)
5185 * or on or after the line and column.
5186 */
5187 static int
5188qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5189{
5190 if (linewise)
5191 return qfp->qf_lnum >= pos->lnum;
5192 else
5193 return (qfp->qf_lnum > pos->lnum ||
5194 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5195}
5196
5197/*
5198 * Returns TRUE if the specified quickfix entry is
5199 * on or before the given line (linewise is TRUE)
5200 * or on or before the line and column.
5201 */
5202 static int
5203qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5204{
5205 if (linewise)
5206 return qfp->qf_lnum <= pos->lnum;
5207 else
5208 return (qfp->qf_lnum < pos->lnum ||
5209 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5210}
5211
5212/*
5213 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5214 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5215 * multiple entries on a single line as one. Otherwise returns the entry after
5216 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005217 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5218 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005219 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005220 */
5221 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005222qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005223 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005224 pos_T *pos,
5225 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005226 qfline_T *qfp,
5227 int *errornr)
5228{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005229 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005230 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005231 return qfp;
5232
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005233 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005234 while (qfp->qf_next != NULL
5235 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005236 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005237 {
5238 qfp = qfp->qf_next;
5239 ++*errornr;
5240 }
5241
5242 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005243 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005244 return NULL;
5245
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005246 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005247 qfp = qfp->qf_next;
5248 ++*errornr;
5249
5250 return qfp;
5251}
5252
5253/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005254 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5255 * If 'linewise' is TRUE, returns the entry before the specified line and
5256 * treats multiple entries on a single line as one. Otherwise returns the entry
5257 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005258 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5259 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005260 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005261 */
5262 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005263qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005264 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005265 pos_T *pos,
5266 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005267 qfline_T *qfp,
5268 int *errornr)
5269{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005270 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005271 while (qfp->qf_next != NULL
5272 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005273 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005274 {
5275 qfp = qfp->qf_next;
5276 ++*errornr;
5277 }
5278
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005279 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005280 return NULL;
5281
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005282 if (linewise)
5283 // If multiple entries are on the same line, then use the first entry
5284 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005285
5286 return qfp;
5287}
5288
5289/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005290 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005291 * the direction 'dir'.
5292 */
5293 static qfline_T *
5294qf_find_closest_entry(
5295 qf_list_T *qfl,
5296 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005297 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005298 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005299 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005300 int *errornr)
5301{
5302 qfline_T *qfp;
5303
5304 *errornr = 0;
5305
5306 // Find the first entry in this file
5307 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5308 if (qfp == NULL)
5309 return NULL; // no entry in this file
5310
5311 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005312 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005313 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005314 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005315
5316 return qfp;
5317}
5318
5319/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005320 * Get the nth quickfix entry below the specified entry. Searches forward in
5321 * the list. If linewise is TRUE, then treat multiple entries on a single line
5322 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005323 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005324 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005325qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005326{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005327 qfline_T *entry = entry_arg;
5328
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005329 while (n-- > 0 && !got_int)
5330 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005331 int first_errornr = *errornr;
5332
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005333 if (linewise)
5334 // Treat all the entries on the same line in this file as one
5335 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005336
5337 if (entry->qf_next == NULL
5338 || entry->qf_next->qf_fnum != entry->qf_fnum)
5339 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005340 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005341 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005342 break;
5343 }
5344
5345 entry = entry->qf_next;
5346 ++*errornr;
5347 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005348}
5349
5350/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005351 * Get the nth quickfix entry above the specified entry. Searches backwards in
5352 * the list. If linewise is TRUE, then treat multiple entries on a single line
5353 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005354 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005355 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005356qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005357{
5358 while (n-- > 0 && !got_int)
5359 {
5360 if (entry->qf_prev == NULL
5361 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5362 break;
5363
5364 entry = entry->qf_prev;
5365 --*errornr;
5366
5367 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005368 if (linewise)
5369 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005370 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005371}
5372
5373/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005374 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5375 * the specified direction. Returns the error number in the quickfix list or 0
5376 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005377 */
5378 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005379qf_find_nth_adj_entry(
5380 qf_list_T *qfl,
5381 int bnr,
5382 pos_T *pos,
5383 int n,
5384 int dir,
5385 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005386{
5387 qfline_T *adj_entry;
5388 int errornr;
5389
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005390 // Find an entry closest to the specified position
5391 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005392 if (adj_entry == NULL)
5393 return 0;
5394
5395 if (--n > 0)
5396 {
5397 // Go to the n'th entry in the current buffer
5398 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005399 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005400 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005401 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005402 }
5403
5404 return errornr;
5405}
5406
5407/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005408 * Jump to a quickfix entry in the current file nearest to the current line or
5409 * current line/col.
5410 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5411 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005412 */
5413 void
5414ex_cbelow(exarg_T *eap)
5415{
5416 qf_info_T *qi;
5417 qf_list_T *qfl;
5418 int dir;
5419 int buf_has_flag;
5420 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005421 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005422
5423 if (eap->addr_count > 0 && eap->line2 <= 0)
5424 {
5425 emsg(_(e_invrange));
5426 return;
5427 }
5428
5429 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005430 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5431 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005432 buf_has_flag = BUF_HAS_QF_ENTRY;
5433 else
5434 buf_has_flag = BUF_HAS_LL_ENTRY;
5435 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5436 {
5437 emsg(_(e_quickfix));
5438 return;
5439 }
5440
5441 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5442 return;
5443
5444 qfl = qf_get_curlist(qi);
5445 // check if the list has valid errors
5446 if (!qf_list_has_valid_entries(qfl))
5447 {
5448 emsg(_(e_quickfix));
5449 return;
5450 }
5451
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005452 if (eap->cmdidx == CMD_cbelow
5453 || eap->cmdidx == CMD_lbelow
5454 || eap->cmdidx == CMD_cafter
5455 || eap->cmdidx == CMD_lafter)
5456 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005457 dir = FORWARD;
5458 else
5459 dir = BACKWARD;
5460
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005461 pos = curwin->w_cursor;
5462 // A quickfix entry column number is 1 based whereas cursor column
5463 // number is 0 based. Adjust the column number.
5464 pos.col++;
5465 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5466 eap->addr_count > 0 ? eap->line2 : 0, dir,
5467 eap->cmdidx == CMD_cbelow
5468 || eap->cmdidx == CMD_lbelow
5469 || eap->cmdidx == CMD_cabove
5470 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005471
5472 if (errornr > 0)
5473 qf_jump(qi, 0, errornr, FALSE);
5474 else
5475 emsg(_(e_no_more_items));
5476}
5477
5478/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005479 * Return the autocmd name for the :cfile Ex commands
5480 */
5481 static char_u *
5482cfile_get_auname(cmdidx_T cmdidx)
5483{
5484 switch (cmdidx)
5485 {
5486 case CMD_cfile: return (char_u *)"cfile";
5487 case CMD_cgetfile: return (char_u *)"cgetfile";
5488 case CMD_caddfile: return (char_u *)"caddfile";
5489 case CMD_lfile: return (char_u *)"lfile";
5490 case CMD_lgetfile: return (char_u *)"lgetfile";
5491 case CMD_laddfile: return (char_u *)"laddfile";
5492 default: return NULL;
5493 }
5494}
5495
5496/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005497 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005498 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 */
5500 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005501ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005503 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005504 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005505 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005506 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005507 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005508 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005509
Bram Moolenaara16123a2019-03-28 20:31:07 +01005510 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005511 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5512 NULL, FALSE, curbuf))
5513 {
5514#ifdef FEAT_EVAL
5515 if (aborting())
5516 return;
5517#endif
5518 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005519
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005520 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005521#ifdef FEAT_BROWSE
5522 if (cmdmod.browse)
5523 {
5524 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005525 NULL, NULL,
5526 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005527 if (browse_file == NULL)
5528 return;
5529 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5530 vim_free(browse_file);
5531 }
5532 else
5533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005535 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005536
Bram Moolenaar39665952018-08-15 20:59:48 +02005537 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005538 wp = curwin;
5539
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005540 incr_quickfix_busy();
5541
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005542 // This function is used by the :cfile, :cgetfile and :caddfile
5543 // commands.
5544 // :cfile always creates a new quickfix list and jumps to the
5545 // first error.
5546 // :cgetfile creates a new quickfix list but doesn't jump to the
5547 // first error.
5548 // :caddfile adds to an existing quickfix list. If there is no
5549 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005550 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005551 && eap->cmdidx != CMD_laddfile),
5552 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005553 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005554 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005555 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005556 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005557 {
5558 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005559 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005560 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005561 }
5562 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005563 qf_list_changed(qf_get_curlist(qi));
5564 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005565 if (au_name != NULL)
5566 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005567
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005568 // Jump to the first error for a new list and if autocmds didn't
5569 // free the list.
5570 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5571 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005572 // display the first error
5573 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005574
5575 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005576}
5577
5578/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005579 * Return the vimgrep autocmd name.
5580 */
5581 static char_u *
5582vgr_get_auname(cmdidx_T cmdidx)
5583{
5584 switch (cmdidx)
5585 {
5586 case CMD_vimgrep: return (char_u *)"vimgrep";
5587 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5588 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5589 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5590 case CMD_grep: return (char_u *)"grep";
5591 case CMD_lgrep: return (char_u *)"lgrep";
5592 case CMD_grepadd: return (char_u *)"grepadd";
5593 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5594 default: return NULL;
5595 }
5596}
5597
5598/*
5599 * Initialize the regmatch used by vimgrep for pattern "s".
5600 */
5601 static void
5602vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5603{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005604 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005605 regmatch->regprog = NULL;
5606
5607 if (s == NULL || *s == NUL)
5608 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005609 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005610 if (last_search_pat() == NULL)
5611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005612 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005613 return;
5614 }
5615 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5616 }
5617 else
5618 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5619
5620 regmatch->rmm_ic = p_ic;
5621 regmatch->rmm_maxcol = 0;
5622}
5623
5624/*
5625 * Display a file name when vimgrep is running.
5626 */
5627 static void
5628vgr_display_fname(char_u *fname)
5629{
5630 char_u *p;
5631
5632 msg_start();
5633 p = msg_strtrunc(fname, TRUE);
5634 if (p == NULL)
5635 msg_outtrans(fname);
5636 else
5637 {
5638 msg_outtrans(p);
5639 vim_free(p);
5640 }
5641 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005642 msg_didout = FALSE; // overwrite this message
5643 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005644 msg_col = 0;
5645 out_flush();
5646}
5647
5648/*
5649 * Load a dummy buffer to search for a pattern using vimgrep.
5650 */
5651 static buf_T *
5652vgr_load_dummy_buf(
5653 char_u *fname,
5654 char_u *dirname_start,
5655 char_u *dirname_now)
5656{
5657 int save_mls;
5658#if defined(FEAT_SYN_HL)
5659 char_u *save_ei = NULL;
5660#endif
5661 buf_T *buf;
5662
5663#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005664 // Don't do Filetype autocommands to avoid loading syntax and
5665 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005666 save_ei = au_event_disable(",Filetype");
5667#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005668 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005669 save_mls = p_mls;
5670 p_mls = 0;
5671
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005672 // Load file into a buffer, so that 'fileencoding' is detected,
5673 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005674 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5675
5676 p_mls = save_mls;
5677#if defined(FEAT_SYN_HL)
5678 au_event_restore(save_ei);
5679#endif
5680
5681 return buf;
5682}
5683
5684/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005685 * Check whether a quickfix/location list is valid. Autocmds may remove or
5686 * change a quickfix list when vimgrep is running. If the list is not found,
5687 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005688 */
5689 static int
5690vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005691 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005692 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005693 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005694 char_u *title)
5695{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005696 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005697 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005698 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005699 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005700 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005701 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005702 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005703 return FALSE;
5704 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005705 else
5706 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005707 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005708 qf_new_list(qi, title);
5709 return TRUE;
5710 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005711 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005712
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005713 if (qf_restore_list(qi, qfid) == FAIL)
5714 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005715
5716 return TRUE;
5717}
5718
5719/*
5720 * Search for a pattern in all the lines in a buffer and add the matching lines
5721 * to a quickfix list.
5722 */
5723 static int
5724vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005725 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005726 char_u *fname,
5727 buf_T *buf,
5728 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005729 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005730 int duplicate_name,
5731 int flags)
5732{
5733 int found_match = FALSE;
5734 long lnum;
5735 colnr_T col;
5736
Bram Moolenaar1c299432018-10-28 14:36:09 +01005737 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005738 {
5739 col = 0;
5740 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5741 col, NULL, NULL) > 0)
5742 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005743 // Pass the buffer number so that it gets used even for a
5744 // dummy buffer, unless duplicate_name is set, then the
5745 // buffer will be wiped out below.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005746 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005747 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005748 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005749 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005750 duplicate_name ? 0 : buf->b_fnum,
5751 ml_get_buf(buf,
5752 regmatch->startpos[0].lnum + lnum, FALSE),
5753 regmatch->startpos[0].lnum + lnum,
5754 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005755 FALSE, // vis_col
5756 NULL, // search pattern
5757 0, // nr
5758 0, // type
5759 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02005760 ) == QF_FAIL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005761 {
5762 got_int = TRUE;
5763 break;
5764 }
5765 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005766 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005767 break;
5768 if ((flags & VGR_GLOBAL) == 0
5769 || regmatch->endpos[0].lnum > 0)
5770 break;
5771 col = regmatch->endpos[0].col
5772 + (col == regmatch->endpos[0].col);
5773 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5774 break;
5775 }
5776 line_breakcheck();
5777 if (got_int)
5778 break;
5779 }
5780
5781 return found_match;
5782}
5783
5784/*
5785 * Jump to the first match and update the directory.
5786 */
5787 static void
5788vgr_jump_to_match(
5789 qf_info_T *qi,
5790 int forceit,
5791 int *redraw_for_dummy,
5792 buf_T *first_match_buf,
5793 char_u *target_dir)
5794{
5795 buf_T *buf;
5796
5797 buf = curbuf;
5798 qf_jump(qi, 0, 0, forceit);
5799 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005800 // If we jumped to another buffer redrawing will already be
5801 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005802 *redraw_for_dummy = FALSE;
5803
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005804 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005805 if (curbuf == first_match_buf && target_dir != NULL)
5806 {
5807 exarg_T ea;
5808
Bram Moolenaar4ca41532019-05-09 21:48:37 +02005809 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005810 ea.arg = target_dir;
5811 ea.cmdidx = CMD_lcd;
5812 ex_cd(&ea);
5813 }
5814}
5815
5816/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005817 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00005818 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005819typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00005820{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005821 long tomatch; // maximum number of matches to find
5822 char_u *spat; // search pattern
5823 int flags; // search modifier
5824 char_u **fnames; // list of files to search
5825 int fcount; // number of files
5826 regmmatch_T regmatch; // compiled search pattern
5827 char_u *qf_title; // quickfix list title
5828} vgr_args_T;
5829
5830/*
5831 * Process :vimgrep command arguments. The command syntax is:
5832 *
5833 * :{count}vimgrep /{pattern}/[g][j] {file} ...
5834 */
5835 static int
5836vgr_process_args(
5837 exarg_T *eap,
5838 vgr_args_T *args)
5839{
Bram Moolenaar748bf032005-02-02 23:04:36 +00005840 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005841
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005842 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005843
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005844 args->regmatch.regprog = NULL;
5845 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00005846
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005847 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005848 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005849 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005850 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005851
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005852 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005853 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005854 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005855 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005856 emsg(_(e_invalpat));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005857 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005858 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005859
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005860 vgr_init_regmatch(&args->regmatch, args->spat);
5861 if (args->regmatch.regprog == NULL)
5862 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005863
5864 p = skipwhite(p);
5865 if (*p == NUL)
5866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005867 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005868 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005869 }
5870
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005871 // parse the list of arguments
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005872 if (get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL)
5873 return FAIL;
5874 if (args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005875 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005876 emsg(_(e_nomatch));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005877 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005878 }
5879
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005880 return OK;
5881}
5882
5883/*
5884 * Search for a pattern in a list of files and populate the quickfix list with
5885 * the matches.
5886 */
5887 static int
5888vgr_process_files(
5889 win_T *wp,
5890 qf_info_T *qi,
5891 vgr_args_T *cmd_args,
5892 int *redraw_for_dummy,
5893 buf_T **first_match_buf,
5894 char_u **target_dir)
5895{
5896 int status = FAIL;
5897 int_u save_qfid = qf_get_curlist(qi)->qf_id;
5898 time_t seconds = 0;
5899 char_u *fname;
5900 int fi;
5901 buf_T *buf;
5902 int duplicate_name = FALSE;
5903 int using_dummy;
5904 char_u *dirname_start = NULL;
5905 char_u *dirname_now = NULL;
5906 int found_match;
5907 aco_save_T aco;
5908
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005909 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5910 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005911 if (dirname_start == NULL || dirname_now == NULL)
5912 goto theend;
5913
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005914 // Remember the current directory, because a BufRead autocommand that does
5915 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005916 mch_dirname(dirname_start, MAXPATHL);
5917
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005918 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005919 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
5920 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005921 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005922 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005923 if (time(NULL) > seconds)
5924 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005925 // Display the file name every second or so, show the user we are
5926 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005927 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005928 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005929 }
5930
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005931 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00005932 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5933 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005934 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005935 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005936 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005937 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005938
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005939 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005940 }
5941 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005942 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005943 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005944
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005945 // Check whether the quickfix list is still valid. When loading a
5946 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005947 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005948 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005949
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005950 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005951
Bram Moolenaar81695252004-12-29 20:58:21 +00005952 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005953 {
5954 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005955 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005956 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005957 else
5958 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005959 // Try for a match in all lines of the buffer.
5960 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005961 found_match = vgr_match_buflines(qf_get_curlist(qi),
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005962 fname, buf, &cmd_args->regmatch,
5963 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005964
Bram Moolenaar81695252004-12-29 20:58:21 +00005965 if (using_dummy)
5966 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005967 if (found_match && *first_match_buf == NULL)
5968 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005969 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005970 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005971 // Never keep a dummy buffer if there is another buffer
5972 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005973 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005974 buf = NULL;
5975 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005976 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005977 || buf->b_p_bh[0] == 'u' // "unload"
5978 || buf->b_p_bh[0] == 'w' // "wipe"
5979 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005980 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005981 // When no match was found we don't need to remember the
5982 // buffer, wipe it out. If there was a match and it
5983 // wasn't the first one or we won't jump there: only
5984 // unload the buffer.
5985 // Ignore 'hidden' here, because it may lead to having too
5986 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005987 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005988 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005989 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005990 buf = NULL;
5991 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005992 else if (buf != *first_match_buf
5993 || (cmd_args->flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005994 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005995 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005996 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005997 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005998 buf = NULL;
5999 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006000 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006001
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006002 if (buf != NULL)
6003 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006004 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006005 buf->b_flags &= ~BF_DUMMY;
6006
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006007 // If the buffer is still loaded we need to use the
6008 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006009 if (buf == *first_match_buf
6010 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006011 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006012 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006013
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006014 // The buffer is still loaded, the Filetype autocommands
6015 // need to be done now, in that buffer. And the modelines
6016 // need to be done (again). But not the window-local
6017 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006018 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006019#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006020 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6021 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006022#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006023 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006024 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006025 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006026 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006027 }
6028 }
6029
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006030 status = OK;
6031
6032theend:
6033 vim_free(dirname_now);
6034 vim_free(dirname_start);
6035 return status;
6036}
6037
6038/*
6039 * ":vimgrep {pattern} file(s)"
6040 * ":vimgrepadd {pattern} file(s)"
6041 * ":lvimgrep {pattern} file(s)"
6042 * ":lvimgrepadd {pattern} file(s)"
6043 */
6044 void
6045ex_vimgrep(exarg_T *eap)
6046{
6047 vgr_args_T args;
6048 qf_info_T *qi;
6049 qf_list_T *qfl;
6050 int_u save_qfid;
6051 win_T *wp = NULL;
6052 int redraw_for_dummy = FALSE;
6053 buf_T *first_match_buf = NULL;
6054 char_u *target_dir = NULL;
6055 char_u *au_name = NULL;
6056 int status;
6057
6058 au_name = vgr_get_auname(eap->cmdidx);
6059 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6060 curbuf->b_fname, TRUE, curbuf))
6061 {
6062#ifdef FEAT_EVAL
6063 if (aborting())
6064 return;
6065#endif
6066 }
6067
6068 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6069 if (qi == NULL)
6070 return;
6071
6072 if (vgr_process_args(eap, &args) == FAIL)
6073 goto theend;
6074
6075 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6076 && eap->cmdidx != CMD_vimgrepadd
6077 && eap->cmdidx != CMD_lvimgrepadd)
6078 || qf_stack_empty(qi))
6079 // make place for a new list
6080 qf_new_list(qi, args.qf_title);
6081
6082 incr_quickfix_busy();
6083
6084 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6085 &first_match_buf, &target_dir);
6086 if (status != OK)
6087 {
6088 FreeWild(args.fcount, args.fnames);
6089 decr_quickfix_busy();
6090 goto theend;
6091 }
6092
6093 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006094
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006095 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006096 qfl->qf_nonevalid = FALSE;
6097 qfl->qf_ptr = qfl->qf_start;
6098 qfl->qf_index = 1;
6099 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006100
Bram Moolenaar864293a2016-06-02 13:40:04 +02006101 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006102
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006103 // Remember the current quickfix list identifier, so that we can check for
6104 // autocommands changing the current quickfix list.
6105 save_qfid = qf_get_curlist(qi)->qf_id;
6106
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006107 if (au_name != NULL)
6108 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6109 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006110 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6111 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006112 if (!qflist_valid(wp, save_qfid)
6113 || qf_restore_list(qi, save_qfid) == FAIL)
6114 {
6115 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006116 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006117 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006118
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006119 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006120 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006121 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006122 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006123 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6124 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006125 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006126 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006127 semsg(_(e_nomatch2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006128
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006129 decr_quickfix_busy();
6130
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006131 // If we loaded a dummy buffer into the current window, the autocommands
6132 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006133 if (redraw_for_dummy)
6134 {
6135#ifdef FEAT_FOLDING
6136 foldUpdateAll(curwin);
6137#else
6138 redraw_later(NOT_VALID);
6139#endif
6140 }
6141
Bram Moolenaar86b68352004-12-27 21:59:20 +00006142theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006143 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006144 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006145 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006146}
6147
6148/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006149 * Restore current working directory to "dirname_start" if they differ, taking
6150 * into account whether it is set locally or globally.
6151 */
6152 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006153restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006154{
6155 char_u *dirname_now = alloc(MAXPATHL);
6156
6157 if (NULL != dirname_now)
6158 {
6159 mch_dirname(dirname_now, MAXPATHL);
6160 if (STRCMP(dirname_start, dirname_now) != 0)
6161 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006162 // If the directory has changed, change it back by building up an
6163 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006164 exarg_T ea;
6165
Bram Moolenaar4ca41532019-05-09 21:48:37 +02006166 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006167 ea.arg = dirname_start;
6168 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6169 ex_cd(&ea);
6170 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006171 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006172 }
6173}
6174
6175/*
6176 * Load file "fname" into a dummy buffer and return the buffer pointer,
6177 * placing the directory resulting from the buffer load into the
6178 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6179 * prior to calling this function. Restores directory to "dirname_start" prior
6180 * to returning, if autocmds or the 'autochdir' option have changed it.
6181 *
6182 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6183 * or wipe_dummy_buffer() later!
6184 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006185 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006186 */
6187 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006188load_dummy_buffer(
6189 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006190 char_u *dirname_start, // in: old directory
6191 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006192{
6193 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006194 bufref_T newbufref;
6195 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006196 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006197 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006198 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006199
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006200 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006201 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6202 if (newbuf == NULL)
6203 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006204 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006205
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006206 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006207 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6208
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006209 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006210 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006211 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006212 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006213 ++newbuf->b_locked;
6214
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006215 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006216 aucmd_prepbuf(&aco, newbuf);
6217
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006218 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006219 (void)setfname(curbuf, fname, NULL, FALSE);
6220
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006221 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006222 check_need_swap(TRUE);
6223
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006224 // Remove the "dummy" flag, otherwise autocommands may not
6225 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006226 curbuf->b_flags &= ~BF_DUMMY;
6227
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006228 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006229 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006230 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006231 NULL, READ_NEW | READ_DUMMY);
6232 --newbuf->b_locked;
6233 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006234 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006235 && !(curbuf->b_flags & BF_NEW))
6236 {
6237 failed = FALSE;
6238 if (curbuf != newbuf)
6239 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006240 // Bloody autocommands changed the buffer! Can happen when
6241 // using netrw and editing a remote file. Use the current
6242 // buffer instead, delete the dummy one after restoring the
6243 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006244 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006245 newbuf = curbuf;
6246 }
6247 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006248
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006249 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006250 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006251 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6252 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006253
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006254 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6255 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006256 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006257 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006258
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006259 // When autocommands/'autochdir' option changed directory: go back.
6260 // Let the caller know what the resulting dir was first, in case it is
6261 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006262 mch_dirname(resulting_dir, MAXPATHL);
6263 restore_start_dir(dirname_start);
6264
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006265 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006266 return NULL;
6267 if (failed)
6268 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006269 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006270 return NULL;
6271 }
6272 return newbuf;
6273}
6274
6275/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006276 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6277 * directory to "dirname_start" prior to returning, if autocmds or the
6278 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006279 */
6280 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006281wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006282{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006283 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006284 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006285#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006286 cleanup_T cs;
6287
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006288 // Reset the error/interrupt/exception state here so that aborting()
6289 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6290 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006291 enter_cleanup(&cs);
6292#endif
6293
Bram Moolenaar81695252004-12-29 20:58:21 +00006294 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006295
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006296#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006297 // Restore the error/interrupt/exception state if not discarded by a
6298 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006299 leave_cleanup(&cs);
6300#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006301 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006302 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006303 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006304}
6305
6306/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006307 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6308 * directory to "dirname_start" prior to returning, if autocmds or the
6309 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006310 */
6311 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006312unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006313{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006314 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006315 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006316 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006317
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006318 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006319 restore_start_dir(dirname_start);
6320 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006321}
6322
Bram Moolenaar05159a02005-02-26 23:04:13 +00006323#if defined(FEAT_EVAL) || defined(PROTO)
6324/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01006325 * Copy the specified quickfix entry items into a new dict and appened the dict
6326 * to 'list'. Returns OK on success.
6327 */
6328 static int
6329get_qfline_items(qfline_T *qfp, list_T *list)
6330{
6331 int bufnum;
6332 dict_T *dict;
6333 char_u buf[2];
6334
6335 // Handle entries with a non-existing buffer number.
6336 bufnum = qfp->qf_fnum;
6337 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6338 bufnum = 0;
6339
6340 if ((dict = dict_alloc()) == NULL)
6341 return FAIL;
6342 if (list_append_dict(list, dict) == FAIL)
6343 return FAIL;
6344
6345 buf[0] = qfp->qf_type;
6346 buf[1] = NUL;
6347 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
6348 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6349 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6350 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6351 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
6352 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6353 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6354 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6355 || dict_add_string(dict, "type", buf) == FAIL
6356 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6357 return FAIL;
6358
6359 return OK;
6360}
6361
6362/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006363 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006364 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006365 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006366 static int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006367get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006368{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006369 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006370 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006371 qfline_T *qfp;
6372 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006373
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006374 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006375 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006376 qi = &ql_info;
6377 if (wp != NULL)
6378 {
6379 qi = GET_LOC_LIST(wp);
6380 if (qi == NULL)
6381 return FAIL;
6382 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006383 }
6384
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006385 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006386 qf_idx = qi->qf_curlist;
6387
Bram Moolenaar0398e002019-03-21 21:12:49 +01006388 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006389 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006390
Bram Moolenaar0398e002019-03-21 21:12:49 +01006391 qfl = qf_get_list(qi, qf_idx);
6392 if (qf_list_empty(qfl))
6393 return FAIL;
6394
Bram Moolenaara16123a2019-03-28 20:31:07 +01006395 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006396 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01006397 if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006398 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006399 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006400
Bram Moolenaar05159a02005-02-26 23:04:13 +00006401 return OK;
6402}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006403
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006404// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006405enum {
6406 QF_GETLIST_NONE = 0x0,
6407 QF_GETLIST_TITLE = 0x1,
6408 QF_GETLIST_ITEMS = 0x2,
6409 QF_GETLIST_NR = 0x4,
6410 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006411 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006412 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006413 QF_GETLIST_IDX = 0x40,
6414 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006415 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006416 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006417 QF_GETLIST_QFBUFNR = 0x400,
6418 QF_GETLIST_ALL = 0x7FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006419};
6420
6421/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006422 * Parse text from 'di' and return the quickfix list items.
6423 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006424 */
6425 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006426qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006427{
6428 int status = FAIL;
6429 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006430 char_u *errorformat = p_efm;
6431 dictitem_T *efm_di;
6432 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006433
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006434 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006435 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006436 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006437 // If errorformat is supplied then use it, otherwise use the 'efm'
6438 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006439 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6440 {
6441 if (efm_di->di_tv.v_type != VAR_STRING ||
6442 efm_di->di_tv.vval.v_string == NULL)
6443 return FAIL;
6444 errorformat = efm_di->di_tv.vval.v_string;
6445 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006446
Bram Moolenaar36538222017-09-02 19:51:44 +02006447 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006448 if (l == NULL)
6449 return FAIL;
6450
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006451 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006452 if (qi != NULL)
6453 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006454 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006455 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6456 {
Bram Moolenaarda732532017-08-31 20:58:02 +02006457 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006458 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006459 }
6460 free(qi);
6461 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006462 dict_add_list(retdict, "items", l);
6463 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006464 }
6465
6466 return status;
6467}
6468
6469/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006470 * Return the quickfix/location list window identifier in the current tabpage.
6471 */
6472 static int
6473qf_winid(qf_info_T *qi)
6474{
6475 win_T *win;
6476
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006477 // The quickfix window can be opened even if the quickfix list is not set
6478 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006479 if (qi == NULL)
6480 return 0;
6481 win = qf_find_win(qi);
6482 if (win != NULL)
6483 return win->w_id;
6484 return 0;
6485}
6486
6487/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006488 * Returns the number of the buffer displayed in the quickfix/location list
6489 * window. If there is no buffer associated with the list, then returns 0.
6490 */
6491 static int
6492qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6493{
6494 return dict_add_number(retdict, "qfbufnr",
6495 (qi == NULL) ? 0 : qi->qf_bufnr);
6496}
6497
6498/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006499 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006500 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006501 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006502qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006503{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006504 int flags = QF_GETLIST_NONE;
6505
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006506 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006507 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006508 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006509 if (!loclist)
6510 // File window ID is applicable only to location list windows
6511 flags &= ~ QF_GETLIST_FILEWINID;
6512 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006513
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006514 if (dict_find(what, (char_u *)"title", -1) != NULL)
6515 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006516
Bram Moolenaara6d48492017-12-12 22:45:31 +01006517 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6518 flags |= QF_GETLIST_NR;
6519
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006520 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6521 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006522
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006523 if (dict_find(what, (char_u *)"context", -1) != NULL)
6524 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006525
Bram Moolenaara6d48492017-12-12 22:45:31 +01006526 if (dict_find(what, (char_u *)"id", -1) != NULL)
6527 flags |= QF_GETLIST_ID;
6528
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006529 if (dict_find(what, (char_u *)"items", -1) != NULL)
6530 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006531
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006532 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6533 flags |= QF_GETLIST_IDX;
6534
6535 if (dict_find(what, (char_u *)"size", -1) != NULL)
6536 flags |= QF_GETLIST_SIZE;
6537
Bram Moolenaarb254af32017-12-18 19:48:58 +01006538 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6539 flags |= QF_GETLIST_TICK;
6540
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006541 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6542 flags |= QF_GETLIST_FILEWINID;
6543
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006544 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6545 flags |= QF_GETLIST_QFBUFNR;
6546
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006547 return flags;
6548}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006549
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006550/*
6551 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6552 * If 'nr' and 'id' are not present in 'what' then return the current
6553 * quickfix list index.
6554 * If 'nr' is zero then return the current quickfix list index.
6555 * If 'nr' is '$' then return the last quickfix list index.
6556 * If 'id' is present then return the index of the quickfix list with that id.
6557 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6558 * Return -1, if quickfix list is not present or if the stack is empty.
6559 */
6560 static int
6561qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6562{
6563 int qf_idx;
6564 dictitem_T *di;
6565
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006566 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006567 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6568 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006569 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006570 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006571 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006572 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006573 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006574 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006575 qf_idx = di->di_tv.vval.v_number - 1;
6576 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006577 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006578 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006579 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006580 else if (di->di_tv.v_type == VAR_STRING
6581 && di->di_tv.vval.v_string != NULL
6582 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006583 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006584 qf_idx = qi->qf_listcount - 1;
6585 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006586 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006587 }
6588
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006589 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6590 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006591 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006592 if (di->di_tv.v_type == VAR_NUMBER)
6593 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006594 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006595 if (di->di_tv.vval.v_number != 0)
6596 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6597 }
6598 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006599 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006600 }
6601
6602 return qf_idx;
6603}
6604
6605/*
6606 * Return default values for quickfix list properties in retdict.
6607 */
6608 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006609qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006610{
6611 int status = OK;
6612
6613 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006614 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006615 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6616 {
6617 list_T *l = list_alloc();
6618 if (l != NULL)
6619 status = dict_add_list(retdict, "items", l);
6620 else
6621 status = FAIL;
6622 }
6623 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006624 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006625 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006626 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006627 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006628 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006629 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006630 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006631 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006632 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006633 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006634 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006635 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006636 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006637 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006638 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006639 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6640 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006641
6642 return status;
6643}
6644
6645/*
6646 * Return the quickfix list title as 'title' in retdict
6647 */
6648 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006649qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006650{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006651 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006652}
6653
6654/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006655 * Returns the identifier of the window used to display files from a location
6656 * list. If there is no associated window, then returns 0. Useful only when
6657 * called from a location list window.
6658 */
6659 static int
6660qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6661{
6662 int winid = 0;
6663
6664 if (wp != NULL && IS_LL_WINDOW(wp))
6665 {
6666 win_T *ll_wp = qf_find_win_with_loclist(qi);
6667 if (ll_wp != NULL)
6668 winid = ll_wp->w_id;
6669 }
6670
6671 return dict_add_number(retdict, "filewinid", winid);
6672}
6673
6674/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006675 * Return the quickfix list items/entries as 'items' in retdict
6676 */
6677 static int
6678qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6679{
6680 int status = OK;
6681 list_T *l = list_alloc();
6682 if (l != NULL)
6683 {
6684 (void)get_errorlist(qi, NULL, qf_idx, l);
6685 dict_add_list(retdict, "items", l);
6686 }
6687 else
6688 status = FAIL;
6689
6690 return status;
6691}
6692
6693/*
6694 * Return the quickfix list context (if any) as 'context' in retdict.
6695 */
6696 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006697qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006698{
6699 int status;
6700 dictitem_T *di;
6701
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006702 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006703 {
6704 di = dictitem_alloc((char_u *)"context");
6705 if (di != NULL)
6706 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006707 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006708 status = dict_add(retdict, di);
6709 if (status == FAIL)
6710 dictitem_free(di);
6711 }
6712 else
6713 status = FAIL;
6714 }
6715 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006716 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006717
6718 return status;
6719}
6720
6721/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006722 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006723 */
6724 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01006725qf_getprop_idx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006726{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006727 int curidx = qfl->qf_index;
6728 if (qf_list_empty(qfl))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006729 // For empty lists, current index is set to 0
6730 curidx = 0;
6731 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006732}
6733
6734/*
6735 * Return quickfix/location list details (title) as a
6736 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6737 * then current list is used. Otherwise the specified list is used.
6738 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006739 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006740qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6741{
6742 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006743 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006744 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006745 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006746 dictitem_T *di;
6747 int flags = QF_GETLIST_NONE;
6748
6749 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6750 return qf_get_list_from_lines(what, di, retdict);
6751
6752 if (wp != NULL)
6753 qi = GET_LOC_LIST(wp);
6754
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006755 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006756
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006757 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006758 qf_idx = qf_getprop_qfidx(qi, what);
6759
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006760 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006761 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006762 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006763
Bram Moolenaar0398e002019-03-21 21:12:49 +01006764 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006765
Bram Moolenaard823fa92016-08-12 16:29:27 +02006766 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006767 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006768 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006769 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006770 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006771 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006772 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006773 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006774 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006775 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006776 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006777 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006778 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar0398e002019-03-21 21:12:49 +01006779 status = qf_getprop_idx(qfl, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006780 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006781 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006782 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006783 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006784 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6785 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006786 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6787 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006788
Bram Moolenaard823fa92016-08-12 16:29:27 +02006789 return status;
6790}
6791
6792/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006793 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006794 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6795 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006796 */
6797 static int
6798qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01006799 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006800 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006801 int first_entry,
6802 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006803{
6804 static int did_bufnr_emsg;
6805 char_u *filename, *module, *pattern, *text, *type;
6806 int bufnum, valid, status, col, vcol, nr;
6807 long lnum;
6808
6809 if (first_entry)
6810 did_bufnr_emsg = FALSE;
6811
Bram Moolenaar8f667172018-12-14 15:38:31 +01006812 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6813 module = dict_get_string(d, (char_u *)"module", TRUE);
6814 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6815 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6816 col = (int)dict_get_number(d, (char_u *)"col");
6817 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6818 nr = (int)dict_get_number(d, (char_u *)"nr");
6819 type = dict_get_string(d, (char_u *)"type", TRUE);
6820 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6821 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006822 if (text == NULL)
6823 text = vim_strsave((char_u *)"");
6824
6825 valid = TRUE;
6826 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6827 valid = FALSE;
6828
6829 // Mark entries with non-existing buffer number as not valid. Give the
6830 // error message only once.
6831 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6832 {
6833 if (!did_bufnr_emsg)
6834 {
6835 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006836 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006837 }
6838 valid = FALSE;
6839 bufnum = 0;
6840 }
6841
6842 // If the 'valid' field is present it overrules the detected value.
6843 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006844 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006845
Bram Moolenaar0398e002019-03-21 21:12:49 +01006846 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006847 NULL, // dir
6848 filename,
6849 module,
6850 bufnum,
6851 text,
6852 lnum,
6853 col,
6854 vcol, // vis_col
6855 pattern, // search pattern
6856 nr,
6857 type == NULL ? NUL : *type,
6858 valid);
6859
6860 vim_free(filename);
6861 vim_free(module);
6862 vim_free(pattern);
6863 vim_free(text);
6864 vim_free(type);
6865
Bram Moolenaar9752c722018-12-22 16:49:34 +01006866 if (valid)
6867 *valid_entry = TRUE;
6868
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006869 return status;
6870}
6871
6872/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006873 * Add list of entries to quickfix/location list. Each list entry is
6874 * a dictionary with item information.
6875 */
6876 static int
6877qf_add_entries(
6878 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006879 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006880 list_T *list,
6881 char_u *title,
6882 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006883{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006884 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006885 listitem_T *li;
6886 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006887 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006888 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006889 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006890
Bram Moolenaara3921f42017-06-04 15:30:34 +02006891 if (action == ' ' || qf_idx == qi->qf_listcount)
6892 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006893 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006894 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006895 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006896 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006897 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01006898 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006899 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006900 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006901 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006902 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006903 qf_free_items(qfl);
6904 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006905 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006906
6907 for (li = list->lv_first; li != NULL; li = li->li_next)
6908 {
6909 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006910 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006911
6912 d = li->li_tv.vval.v_dict;
6913 if (d == NULL)
6914 continue;
6915
Bram Moolenaar0398e002019-03-21 21:12:49 +01006916 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006917 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02006918 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006919 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006920 }
6921
Bram Moolenaar9752c722018-12-22 16:49:34 +01006922 // Check if any valid error entries are added to the list.
6923 if (valid_entry)
6924 qfl->qf_nonevalid = FALSE;
6925 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006926 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006927 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006928
6929 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006930 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006931 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006932
6933 // Update the current error index if not appending to the list or if the
6934 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006935 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01006936 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006937
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006938 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006939 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006940
6941 return retval;
6942}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006943
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006944/*
6945 * Get the quickfix list index from 'nr' or 'id'
6946 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006947 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006948qf_setprop_get_qfidx(
6949 qf_info_T *qi,
6950 dict_T *what,
6951 int action,
6952 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006953{
6954 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006955 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006956
Bram Moolenaard823fa92016-08-12 16:29:27 +02006957 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6958 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006959 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006960 if (di->di_tv.v_type == VAR_NUMBER)
6961 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006962 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006963 if (di->di_tv.vval.v_number != 0)
6964 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006965
Bram Moolenaar55b69262017-08-13 13:42:01 +02006966 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6967 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006968 // When creating a new list, accept qf_idx pointing to the next
6969 // non-available list and add the new list at the end of the
6970 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006971 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006972 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006973 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006974 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006975 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006976 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006977 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006978 }
6979 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006980 && di->di_tv.vval.v_string != NULL
6981 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006982 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006983 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006984 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006985 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006986 qf_idx = 0;
6987 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006988 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006989 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006990 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006991 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006992 }
6993
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006994 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006995 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006996 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006997 if (di->di_tv.v_type != VAR_NUMBER)
6998 return INVALID_QFIDX;
6999
7000 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007001 }
7002
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007003 return qf_idx;
7004}
7005
7006/*
7007 * Set the quickfix list title.
7008 */
7009 static int
7010qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7011{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007012 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007013
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007014 if (di->di_tv.v_type != VAR_STRING)
7015 return FAIL;
7016
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007017 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01007018 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007019 if (qf_idx == qi->qf_curlist)
7020 qf_update_win_titlevar(qi);
7021
7022 return OK;
7023}
7024
7025/*
7026 * Set quickfix list items/entries.
7027 */
7028 static int
7029qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7030{
7031 int retval = FAIL;
7032 char_u *title_save;
7033
7034 if (di->di_tv.v_type != VAR_LIST)
7035 return FAIL;
7036
7037 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7038 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7039 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007040 vim_free(title_save);
7041
7042 return retval;
7043}
7044
7045/*
7046 * Set quickfix list items/entries from a list of lines.
7047 */
7048 static int
7049qf_setprop_items_from_lines(
7050 qf_info_T *qi,
7051 int qf_idx,
7052 dict_T *what,
7053 dictitem_T *di,
7054 int action)
7055{
7056 char_u *errorformat = p_efm;
7057 dictitem_T *efm_di;
7058 int retval = FAIL;
7059
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007060 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007061 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7062 {
7063 if (efm_di->di_tv.v_type != VAR_STRING ||
7064 efm_di->di_tv.vval.v_string == NULL)
7065 return FAIL;
7066 errorformat = efm_di->di_tv.vval.v_string;
7067 }
7068
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007069 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007070 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7071 return FAIL;
7072
7073 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007074 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007075 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
7076 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
7077 retval = OK;
7078
7079 return retval;
7080}
7081
7082/*
7083 * Set quickfix list context.
7084 */
7085 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007086qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007087{
7088 typval_T *ctx;
7089
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007090 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007091 ctx = alloc_tv();
7092 if (ctx != NULL)
7093 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007094 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007095
7096 return OK;
7097}
7098
7099/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007100 * Set the current index in the specified quickfix list
7101 */
7102 static int
7103qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7104{
7105 int denote = FALSE;
7106 int newidx;
7107 int old_qfidx;
7108 qfline_T *qf_ptr;
7109
7110 // If the specified index is '$', then use the last entry
7111 if (di->di_tv.v_type == VAR_STRING
7112 && di->di_tv.vval.v_string != NULL
7113 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7114 newidx = qfl->qf_count;
7115 else
7116 {
7117 // Otherwise use the specified index
7118 newidx = tv_get_number_chk(&di->di_tv, &denote);
7119 if (denote)
7120 return FAIL;
7121 }
7122
7123 if (newidx < 1) // sanity check
7124 return FAIL;
7125 if (newidx > qfl->qf_count)
7126 newidx = qfl->qf_count;
7127
7128 old_qfidx = qfl->qf_index;
7129 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7130 if (qf_ptr == NULL)
7131 return FAIL;
7132 qfl->qf_ptr = qf_ptr;
7133 qfl->qf_index = newidx;
7134
7135 // If the current list is modified and it is displayed in the quickfix
7136 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007137 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007138 qf_win_pos_update(qi, old_qfidx);
7139
7140 return OK;
7141}
7142
7143/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007144 * Set quickfix/location list properties (title, items, context).
7145 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007146 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007147 */
7148 static int
7149qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7150{
7151 dictitem_T *di;
7152 int retval = FAIL;
7153 int qf_idx;
7154 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007155 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007156
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007157 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007158 newlist = TRUE;
7159
7160 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007161 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007162 return FAIL;
7163
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007164 if (newlist)
7165 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007166 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007167 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007168 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007169 }
7170
Bram Moolenaar0398e002019-03-21 21:12:49 +01007171 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007172 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007173 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007174 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007175 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007176 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007177 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007178 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007179 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007180 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7181 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007182
Bram Moolenaarb254af32017-12-18 19:48:58 +01007183 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007184 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007185
Bram Moolenaard823fa92016-08-12 16:29:27 +02007186 return retval;
7187}
7188
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007189/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007190 * Free the entire quickfix/location list stack.
7191 * If the quickfix/location list window is open, then clear it.
7192 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007193 static void
7194qf_free_stack(win_T *wp, qf_info_T *qi)
7195{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007196 win_T *qfwin = qf_find_win(qi);
7197 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007198
7199 if (qfwin != NULL)
7200 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007201 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007202 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007203 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007204 qf_update_buffer(qi, NULL);
7205 }
7206
7207 if (wp != NULL && IS_LL_WINDOW(wp))
7208 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007209 // If in the location list window, then use the non-location list
7210 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007211 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007212 if (llwin != NULL)
7213 wp = llwin;
7214 }
7215
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007216 qf_free_all(wp);
7217 if (wp == NULL)
7218 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007219 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007220 qi->qf_curlist = 0;
7221 qi->qf_listcount = 0;
7222 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007223 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007224 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007225 // If the location list window is open, then create a new empty
7226 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007227 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007228
Bram Moolenaar95946f12019-03-31 15:31:59 +02007229 if (new_ll != NULL)
7230 {
7231 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007232
Bram Moolenaar95946f12019-03-31 15:31:59 +02007233 // first free the list reference in the location list window
7234 ll_free_all(&qfwin->w_llist_ref);
7235
7236 qfwin->w_llist_ref = new_ll;
7237 if (wp != qfwin)
7238 win_set_loclist(wp, new_ll);
7239 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007240 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007241}
7242
Bram Moolenaard823fa92016-08-12 16:29:27 +02007243/*
7244 * Populate the quickfix list with the items supplied in the list
7245 * of dictionaries. "title" will be copied to w:quickfix_title.
7246 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
7247 */
7248 int
7249set_errorlist(
7250 win_T *wp,
7251 list_T *list,
7252 int action,
7253 char_u *title,
7254 dict_T *what)
7255{
7256 qf_info_T *qi = &ql_info;
7257 int retval = OK;
7258
7259 if (wp != NULL)
7260 {
7261 qi = ll_get_or_alloc_list(wp);
7262 if (qi == NULL)
7263 return FAIL;
7264 }
7265
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007266 if (action == 'f')
7267 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007268 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007269 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007270 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007271 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007272
7273 incr_quickfix_busy();
7274
7275 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007276 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007277 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007278 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007279 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007280 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007281 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007282 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007283
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007284 decr_quickfix_busy();
7285
Bram Moolenaard823fa92016-08-12 16:29:27 +02007286 return retval;
7287}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007288
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007289/*
7290 * Mark the context as in use for all the lists in a quickfix stack.
7291 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007292 static int
7293mark_quickfix_ctx(qf_info_T *qi, int copyID)
7294{
7295 int i;
7296 int abort = FALSE;
7297 typval_T *ctx;
7298
7299 for (i = 0; i < LISTCOUNT && !abort; ++i)
7300 {
7301 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007302 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7303 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007304 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
7305 }
7306
7307 return abort;
7308}
7309
7310/*
7311 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007312 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007313 */
7314 int
7315set_ref_in_quickfix(int copyID)
7316{
7317 int abort = FALSE;
7318 tabpage_T *tp;
7319 win_T *win;
7320
7321 abort = mark_quickfix_ctx(&ql_info, copyID);
7322 if (abort)
7323 return abort;
7324
7325 FOR_ALL_TAB_WINDOWS(tp, win)
7326 {
7327 if (win->w_llist != NULL)
7328 {
7329 abort = mark_quickfix_ctx(win->w_llist, copyID);
7330 if (abort)
7331 return abort;
7332 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007333 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7334 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007335 // In a location list window and none of the other windows is
7336 // referring to this location list. Mark the location list
7337 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007338 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7339 if (abort)
7340 return abort;
7341 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007342 }
7343
7344 return abort;
7345}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007346#endif
7347
Bram Moolenaar81695252004-12-29 20:58:21 +00007348/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007349 * Return the autocmd name for the :cbuffer Ex commands
7350 */
7351 static char_u *
7352cbuffer_get_auname(cmdidx_T cmdidx)
7353{
7354 switch (cmdidx)
7355 {
7356 case CMD_cbuffer: return (char_u *)"cbuffer";
7357 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7358 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7359 case CMD_lbuffer: return (char_u *)"lbuffer";
7360 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7361 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7362 default: return NULL;
7363 }
7364}
7365
7366/*
7367 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7368 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7369 */
7370 static int
7371cbuffer_process_args(
7372 exarg_T *eap,
7373 buf_T **bufp,
7374 linenr_T *line1,
7375 linenr_T *line2)
7376{
7377 buf_T *buf = NULL;
7378
7379 if (*eap->arg == NUL)
7380 buf = curbuf;
7381 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7382 buf = buflist_findnr(atoi((char *)eap->arg));
7383
7384 if (buf == NULL)
7385 {
7386 emsg(_(e_invarg));
7387 return FAIL;
7388 }
7389
7390 if (buf->b_ml.ml_mfp == NULL)
7391 {
7392 emsg(_("E681: Buffer is not loaded"));
7393 return FAIL;
7394 }
7395
7396 if (eap->addr_count == 0)
7397 {
7398 eap->line1 = 1;
7399 eap->line2 = buf->b_ml.ml_line_count;
7400 }
7401
7402 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7403 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7404 {
7405 emsg(_(e_invrange));
7406 return FAIL;
7407 }
7408
7409 *line1 = eap->line1;
7410 *line2 = eap->line2;
7411 *bufp = buf;
7412
7413 return OK;
7414}
7415
7416/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007417 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007418 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007419 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007420 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007421 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007422 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007423 */
7424 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007425ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007426{
7427 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007428 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007429 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007430 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007431 int_u save_qfid;
7432 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007433 char_u *qf_title;
7434 linenr_T line1;
7435 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007436
Bram Moolenaara16123a2019-03-28 20:31:07 +01007437 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007438 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007439 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007440 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007441#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007442 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007443 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007444#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007445 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007446
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007447 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007448 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7449 if (qi == NULL)
7450 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007451
Bram Moolenaara16123a2019-03-28 20:31:07 +01007452 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7453 return;
7454
7455 qf_title = qf_cmdtitle(*eap->cmdlinep);
7456
7457 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007458 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007459 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7460 (char *)qf_title, (char *)buf->b_sfname);
7461 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007462 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007463
7464 incr_quickfix_busy();
7465
7466 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7467 (eap->cmdidx != CMD_caddbuffer
7468 && eap->cmdidx != CMD_laddbuffer),
7469 line1, line2,
7470 qf_title, NULL);
7471 if (qf_stack_empty(qi))
7472 {
7473 decr_quickfix_busy();
7474 return;
7475 }
7476 if (res >= 0)
7477 qf_list_changed(qf_get_curlist(qi));
7478
7479 // Remember the current quickfix list identifier, so that we can
7480 // check for autocommands changing the current quickfix list.
7481 save_qfid = qf_get_curlist(qi)->qf_id;
7482 if (au_name != NULL)
7483 {
7484 buf_T *curbuf_old = curbuf;
7485
7486 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7487 TRUE, curbuf);
7488 if (curbuf != curbuf_old)
7489 // Autocommands changed buffer, don't jump now, "qi" may
7490 // be invalid.
7491 res = 0;
7492 }
7493 // Jump to the first error for a new list and if autocmds didn't
7494 // free the list.
7495 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7496 eap->cmdidx == CMD_lbuffer)
7497 && qflist_valid(wp, save_qfid))
7498 // display the first error
7499 qf_jump_first(qi, save_qfid, eap->forceit);
7500
7501 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007502}
7503
Bram Moolenaar1e015462005-09-25 22:16:38 +00007504#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007505/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007506 * Return the autocmd name for the :cexpr Ex commands.
7507 */
7508 static char_u *
7509cexpr_get_auname(cmdidx_T cmdidx)
7510{
7511 switch (cmdidx)
7512 {
7513 case CMD_cexpr: return (char_u *)"cexpr";
7514 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7515 case CMD_caddexpr: return (char_u *)"caddexpr";
7516 case CMD_lexpr: return (char_u *)"lexpr";
7517 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7518 case CMD_laddexpr: return (char_u *)"laddexpr";
7519 default: return NULL;
7520 }
7521}
7522
7523/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007524 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7525 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007526 */
7527 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007528ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007529{
7530 typval_T *tv;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007531 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007532 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007533 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007534 int_u save_qfid;
7535 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007536
Bram Moolenaara16123a2019-03-28 20:31:07 +01007537 au_name = cexpr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007538 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7539 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007540 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007541#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007542 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007543 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007544#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007545 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007546
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007547 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7548 if (qi == NULL)
7549 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007550
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007551 // Evaluate the expression. When the result is a string or a list we can
7552 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007553 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007554 if (tv != NULL)
7555 {
7556 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7557 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7558 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007559 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007560 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00007561 (eap->cmdidx != CMD_caddexpr
7562 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007563 (linenr_T)0, (linenr_T)0,
7564 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007565 if (qf_stack_empty(qi))
7566 {
7567 decr_quickfix_busy();
7568 goto cleanup;
7569 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01007570 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007571 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007572
7573 // Remember the current quickfix list identifier, so that we can
7574 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007575 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007576 if (au_name != NULL)
7577 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7578 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007579
7580 // Jump to the first error for a new list and if autocmds didn't
7581 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02007582 if (res > 0 && (eap->cmdidx == CMD_cexpr
7583 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007584 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02007585 // display the first error
7586 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007587 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00007588 }
7589 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007590 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007591cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00007592 free_tv(tv);
7593 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007594}
Bram Moolenaar1e015462005-09-25 22:16:38 +00007595#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007596
7597/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007598 * Get the location list for ":lhelpgrep"
7599 */
7600 static qf_info_T *
7601hgr_get_ll(int *new_ll)
7602{
7603 win_T *wp;
7604 qf_info_T *qi;
7605
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007606 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007607 if (bt_help(curwin->w_buffer))
7608 wp = curwin;
7609 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007610 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007611 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007612
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007613 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007614 qi = NULL;
7615 else
7616 qi = wp->w_llist;
7617
7618 if (qi == NULL)
7619 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007620 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007621 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007622 return NULL;
7623 *new_ll = TRUE;
7624 }
7625
7626 return qi;
7627}
7628
7629/*
7630 * Search for a pattern in a help file.
7631 */
7632 static void
7633hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007634 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007635 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007636 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007637 regmatch_T *p_regmatch)
7638{
7639 FILE *fd;
7640 long lnum;
7641
7642 fd = mch_fopen((char *)fname, "r");
7643 if (fd == NULL)
7644 return;
7645
7646 lnum = 1;
7647 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7648 {
7649 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007650
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007651 // Convert a line if 'encoding' is not utf-8 and
7652 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007653 if (p_vc->vc_type != CONV_NONE
7654 && has_non_ascii(IObuff))
7655 {
7656 line = string_convert(p_vc, IObuff, NULL);
7657 if (line == NULL)
7658 line = IObuff;
7659 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007660
7661 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7662 {
7663 int l = (int)STRLEN(line);
7664
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007665 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007666 while (l > 0 && line[l - 1] <= ' ')
7667 line[--l] = NUL;
7668
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007669 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007670 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007671 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007672 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007673 0,
7674 line,
7675 lnum,
7676 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007677 + 1, // col
7678 FALSE, // vis_col
7679 NULL, // search pattern
7680 0, // nr
7681 1, // type
7682 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02007683 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007684 {
7685 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007686 if (line != IObuff)
7687 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007688 break;
7689 }
7690 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007691 if (line != IObuff)
7692 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007693 ++lnum;
7694 line_breakcheck();
7695 }
7696 fclose(fd);
7697}
7698
7699/*
7700 * Search for a pattern in all the help files in the doc directory under
7701 * the given directory.
7702 */
7703 static void
7704hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007705 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007706 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007707 regmatch_T *p_regmatch,
7708 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007709#ifdef FEAT_MULTI_LANG
7710 , char_u *lang
7711#endif
7712 )
7713{
7714 int fcount;
7715 char_u **fnames;
7716 int fi;
7717
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007718 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007719 add_pathsep(dirname);
7720 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7721 if (gen_expand_wildcards(1, &dirname, &fcount,
7722 &fnames, EW_FILE|EW_SILENT) == OK
7723 && fcount > 0)
7724 {
7725 for (fi = 0; fi < fcount && !got_int; ++fi)
7726 {
7727#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007728 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007729 if (lang != NULL
7730 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007731 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007732 && !(STRNICMP(lang, "en", 2) == 0
7733 && STRNICMP("txt", fnames[fi]
7734 + STRLEN(fnames[fi]) - 3, 3) == 0))
7735 continue;
7736#endif
7737
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007738 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007739 }
7740 FreeWild(fcount, fnames);
7741 }
7742}
7743
7744/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007745 * Search for a pattern in all the help files in the 'runtimepath'
7746 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007747 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007748 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007749 */
7750 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007751hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007752{
7753 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007754
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007755 vimconv_T vc;
7756
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007757 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7758 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007759 vc.vc_type = CONV_NONE;
7760 if (!enc_utf8)
7761 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007762
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007763 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007764 p = p_rtp;
7765 while (*p != NUL && !got_int)
7766 {
7767 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7768
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007769 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007770#ifdef FEAT_MULTI_LANG
7771 , lang
7772#endif
7773 );
7774 }
7775
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007776 if (vc.vc_type != CONV_NONE)
7777 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007778}
7779
7780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 * ":helpgrep {pattern}"
7782 */
7783 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007784ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
7786 regmatch_T regmatch;
7787 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007788 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007789 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007790 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007791 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792
Bram Moolenaar73633f82012-01-20 13:39:07 +01007793 switch (eap->cmdidx)
7794 {
7795 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7796 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7797 default: break;
7798 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007799 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7800 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007801 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007802#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007803 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007804 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007805#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007806 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007807
Bram Moolenaar39665952018-08-15 20:59:48 +02007808 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007809 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007810 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007811 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007812 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007813 }
7814
Bram Moolenaara16123a2019-03-28 20:31:07 +01007815 // Make 'cpoptions' empty, the 'l' flag should not be used here.
7816 save_cpo = p_cpo;
7817 p_cpo = empty_option;
7818
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007819 incr_quickfix_busy();
7820
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007821#ifdef FEAT_MULTI_LANG
7822 // Check for a specified language
7823 lang = check_help_lang(eap->arg);
7824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7826 regmatch.rm_ic = FALSE;
7827 if (regmatch.regprog != NULL)
7828 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007829 qf_list_T *qfl;
7830
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007831 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007832 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007833 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007835 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007836
Bram Moolenaar473de612013-06-08 18:19:48 +02007837 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007839 qfl->qf_nonevalid = FALSE;
7840 qfl->qf_ptr = qfl->qf_start;
7841 qfl->qf_index = 1;
7842 qf_list_changed(qfl);
7843 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 }
7845
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007846 if (p_cpo == empty_option)
7847 p_cpo = save_cpo;
7848 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007849 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007850 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851
Bram Moolenaar73633f82012-01-20 13:39:07 +01007852 if (au_name != NULL)
7853 {
7854 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7855 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007856 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007857 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007858 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007859 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007860 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007861 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007862 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007863
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007864 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007865 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007866 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007867 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007868 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007869
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007870 decr_quickfix_busy();
7871
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007872 if (eap->cmdidx == CMD_lhelpgrep)
7873 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007874 // If the help window is not opened or if it already points to the
7875 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007876 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007877 {
7878 if (new_qi)
7879 ll_free_all(&qi);
7880 }
7881 else if (curwin->w_llist == NULL)
7882 curwin->w_llist = qi;
7883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885#endif /* FEAT_QUICKFIX */
Bram Moolenaare677df82019-09-02 22:31:11 +02007886
7887#if defined(FEAT_EVAL) || defined(PROTO)
7888# ifdef FEAT_QUICKFIX
7889 static void
7890get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
7891{
7892 if (what_arg->v_type == VAR_UNKNOWN)
7893 {
7894 if (rettv_list_alloc(rettv) == OK)
7895 if (is_qf || wp != NULL)
7896 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
7897 }
7898 else
7899 {
7900 if (rettv_dict_alloc(rettv) == OK)
7901 if (is_qf || (wp != NULL))
7902 {
7903 if (what_arg->v_type == VAR_DICT)
7904 {
7905 dict_T *d = what_arg->vval.v_dict;
7906
7907 if (d != NULL)
7908 qf_get_properties(wp, d, rettv->vval.v_dict);
7909 }
7910 else
7911 emsg(_(e_dictreq));
7912 }
7913 }
7914}
7915# endif
7916
7917/*
7918 * "getloclist()" function
7919 */
7920 void
7921f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7922{
7923# ifdef FEAT_QUICKFIX
7924 win_T *wp;
7925
7926 wp = find_win_by_nr_or_id(&argvars[0]);
7927 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
7928# endif
7929}
7930
7931/*
7932 * "getqflist()" function
7933 */
7934 void
7935f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7936{
7937# ifdef FEAT_QUICKFIX
7938 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
7939# endif
7940}
7941
7942/*
7943 * Used by "setqflist()" and "setloclist()" functions
7944 */
7945 static void
7946set_qf_ll_list(
7947 win_T *wp UNUSED,
7948 typval_T *list_arg UNUSED,
7949 typval_T *action_arg UNUSED,
7950 typval_T *what_arg UNUSED,
7951 typval_T *rettv)
7952{
7953# ifdef FEAT_QUICKFIX
7954 static char *e_invact = N_("E927: Invalid action: '%s'");
7955 char_u *act;
7956 int action = 0;
7957 static int recursive = 0;
7958# endif
7959
7960 rettv->vval.v_number = -1;
7961
7962# ifdef FEAT_QUICKFIX
7963 if (list_arg->v_type != VAR_LIST)
7964 emsg(_(e_listreq));
7965 else if (recursive != 0)
7966 emsg(_(e_au_recursive));
7967 else
7968 {
7969 list_T *l = list_arg->vval.v_list;
7970 dict_T *d = NULL;
7971 int valid_dict = TRUE;
7972
7973 if (action_arg->v_type == VAR_STRING)
7974 {
7975 act = tv_get_string_chk(action_arg);
7976 if (act == NULL)
7977 return; // type error; errmsg already given
7978 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
7979 act[1] == NUL)
7980 action = *act;
7981 else
7982 semsg(_(e_invact), act);
7983 }
7984 else if (action_arg->v_type == VAR_UNKNOWN)
7985 action = ' ';
7986 else
7987 emsg(_(e_stringreq));
7988
7989 if (action_arg->v_type != VAR_UNKNOWN
7990 && what_arg->v_type != VAR_UNKNOWN)
7991 {
7992 if (what_arg->v_type == VAR_DICT)
7993 d = what_arg->vval.v_dict;
7994 else
7995 {
7996 emsg(_(e_dictreq));
7997 valid_dict = FALSE;
7998 }
7999 }
8000
8001 ++recursive;
8002 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
8003 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
8004 d) == OK)
8005 rettv->vval.v_number = 0;
8006 --recursive;
8007 }
8008# endif
8009}
8010
8011/*
8012 * "setloclist()" function
8013 */
8014 void
8015f_setloclist(typval_T *argvars, typval_T *rettv)
8016{
8017 win_T *win;
8018
8019 rettv->vval.v_number = -1;
8020
8021 win = find_win_by_nr_or_id(&argvars[0]);
8022 if (win != NULL)
8023 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8024}
8025
8026/*
8027 * "setqflist()" function
8028 */
8029 void
8030f_setqflist(typval_T *argvars, typval_T *rettv)
8031{
8032 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8033}
8034#endif