blob: 8c25915326abcfd097063dd4ca202ec27a7448be [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020030 qfline_T *qf_next; // pointer to next error in the list
31 qfline_T *qf_prev; // pointer to previous error in the list
32 linenr_T qf_lnum; // line number where the error occurred
33 int qf_fnum; // file number for the line
34 int qf_col; // column where the error occurred
35 int qf_nr; // error number
36 char_u *qf_module; // module name for this error
37 char_u *qf_pattern; // search pattern for the error
38 char_u *qf_text; // description of the error
39 char_u qf_viscol; // set to TRUE if qf_col is screen column
40 char_u qf_cleared; // set to TRUE if line has been deleted
41 char_u qf_type; // type of the error (mostly 'E'); 1 for
42 // :helpgrep
43 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000044};
45
46/*
47 * There is a stack of error lists.
48 */
49#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020050#define INVALID_QFIDX (-1)
Bram Moolenaaree8188f2019-02-05 21:23:04 +010051#define INVALID_QFBUFNR (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020053/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010054 * Quickfix list type.
55 */
56typedef enum
57{
58 QFLT_QUICKFIX, // Quickfix list - global list
59 QFLT_LOCATION, // Location list - per window list
60 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
61} qfltype_T;
62
63/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020064 * Quickfix/Location list definition
65 * Contains a list of entries (qfline_T). qf_start points to the first entry
66 * and qf_last points to the last entry. qf_count contains the list size.
67 *
68 * Usually the list contains one or more entries. But an empty list can be
69 * created using setqflist()/setloclist() with a title and/or user context
70 * information and entries can be added later using setqflist()/setloclist().
71 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000072typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000073{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020074 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010075 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020076 qfline_T *qf_start; // pointer to the first error
77 qfline_T *qf_last; // pointer to the last error
78 qfline_T *qf_ptr; // pointer to the current error
79 int qf_count; // number of errors (0 means empty list)
80 int qf_index; // current index in the error list
81 int qf_nonevalid; // TRUE if not a single valid entry found
82 char_u *qf_title; // title derived from the command that created
83 // the error list or set by setqflist
84 typval_T *qf_ctx; // context set by setqflist/setloclist
Bram Moolenaara7df8c72017-07-19 13:23:06 +020085
86 struct dir_stack_T *qf_dir_stack;
87 char_u *qf_directory;
88 struct dir_stack_T *qf_file_stack;
89 char_u *qf_currfile;
90 int qf_multiline;
91 int qf_multiignore;
92 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010093 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000094} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000095
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020096/*
97 * Quickfix/Location list stack definition
98 * Contains a list of quickfix/location lists (qf_list_T)
99 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000100struct qf_info_S
101{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200102 // Count of references to this list. Used only for location lists.
103 // When a location list window reference this list, qf_refcount
104 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
105 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000106 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200107 int qf_listcount; // current number of lists
108 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000109 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100110 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100111 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000112};
113
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200114static qf_info_T ql_info; // global quickfix list
115static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200117#define FMT_PATTERNS 11 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
120 * Structure used to hold the info of one part of 'errorformat'
121 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000122typedef struct efm_S efm_T;
123struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200125 regprog_T *prog; // pre-formatted part of 'errorformat'
126 efm_T *next; // pointer to next (NULL if last)
127 char_u addr[FMT_PATTERNS]; // indices of used % patterns
128 char_u prefix; // prefix of this format line:
129 // 'D' enter directory
130 // 'X' leave directory
131 // 'A' start of multi-line message
132 // 'E' error message
133 // 'W' warning message
134 // 'I' informational message
135 // 'C' continuation line
136 // 'Z' end of multi-line message
137 // 'G' general, unspecific message
138 // 'P' push file (partial) message
139 // 'Q' pop/quit file (partial) message
140 // 'O' overread (partial) message
141 char_u flags; // additional flags given in prefix
142 // '-' do not include this line
143 // '+' include whole line in message
144 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145};
146
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200147// List of location lists to be deleted.
148// Used to delay the deletion of locations lists by autocmds.
149typedef struct qf_delq_S
150{
151 struct qf_delq_S *next;
152 qf_info_T *qi;
153} qf_delq_T;
154static qf_delq_T *qf_delq_head = NULL;
155
156// Counter to prevent autocmds from freeing up location lists when they are
157// still being used.
158static int quickfix_busy = 0;
159
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200160static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100161
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100162static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100163static int qf_add_entry(qf_list_T *qfl, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200164static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100166static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200167static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200169static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100172static win_T *qf_find_win(qf_info_T *qi);
173static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200174static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +0100175static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
177static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
178static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
179static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar3ff33112019-05-03 21:56:35 +0200180static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200182// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000183#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200184// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000185#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200186
187// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100188#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
189#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
190#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
191#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200192
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000193/*
194 * Return location list for window 'wp'
195 * For location list window, return the referenced location list
196 */
197#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
198
Bram Moolenaar95946f12019-03-31 15:31:59 +0200199// Macro to loop through all the items in a quickfix list
200// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100201#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
Bram Moolenaar95946f12019-03-31 15:31:59 +0200202 for (i = 1, qfp = qfl->qf_start; \
203 !got_int && i <= qfl->qf_count && qfp != NULL; \
Bram Moolenaara16123a2019-03-28 20:31:07 +0100204 ++i, qfp = qfp->qf_next)
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200207 * Looking up a buffer can be slow if there are many. Remember the last one
208 * to make this a lot faster if there are multiple matches in the same file.
209 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200210static char_u *qf_last_bufname = NULL;
211static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200212
Bram Moolenaar3c097222017-12-21 20:54:49 +0100213static char *e_loc_list_changed =
214 N_("E926: Current location list was changed");
215
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200216/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200217 * Maximum number of bytes allowed per line while reading a errorfile.
218 */
219#define LINE_MAXLEN 4096
220
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200221static struct fmtpattern
222{
223 char_u convchar;
224 char *pattern;
225} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200226 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200227 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200228 {'n', "\\d\\+"},
229 {'l', "\\d\\+"},
230 {'c', "\\d\\+"},
231 {'t', "."},
232 {'m', ".\\+"},
233 {'r', ".*"},
234 {'p', "[- .]*"},
235 {'v', "\\d\\+"},
236 {'s', ".\\+"},
237 {'o', ".\\+"}
238 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200239
240/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200241 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200242 * See fmt_pat definition above for the list of supported patterns. The
243 * pattern specifier is supplied in "efmpat". The converted pattern is stored
244 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200245 */
246 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200247efmpat_to_regpat(
248 char_u *efmpat,
249 char_u *regpat,
250 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200251 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100252 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200253{
254 char_u *srcptr;
255
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200256 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200257 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200258 // Each errorformat pattern can occur only once
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100259 semsg(_("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200260 return NULL;
261 }
262 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200263 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200264 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200265 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100267 semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200268 return NULL;
269 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200270 efminfo->addr[idx] = (char_u)++round;
271 *regpat++ = '\\';
272 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200273#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200274 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200275 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200276 // Also match "c:" in the file name, even when
277 // checking for a colon next: "%f:".
278 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200279 STRCPY(regpat, "\\%(\\a:\\)\\=");
280 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281 }
282#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200283 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200284 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200285 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200286 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200287 // A file name may contain spaces, but this isn't
288 // in "\f". For "%f:%l:%m" there may be a ":" in
289 // the file name. Use ".\{-1,}x" instead (x is
290 // the next character), the requirement that :999:
291 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200292 STRCPY(regpat, ".\\{-1,}");
293 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200294 }
295 else
296 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200297 // File name followed by '\\' or '%': include as
298 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200299 STRCPY(regpat, "\\f\\+");
300 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200301 }
302 }
303 else
304 {
305 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200306 while ((*regpat = *srcptr++) != NUL)
307 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200308 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200309 *regpat++ = '\\';
310 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200311
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200312 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200313}
314
315/*
316 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200317 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200318 */
319 static char_u *
320scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200321 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200322 char_u *efm,
323 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100324 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200325{
326 char_u *efmp = *pefmp;
327
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200328 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200329 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200330 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200331 {
332 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200333 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200334 if (efmp < efm + len)
335 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200336 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200337 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200338 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200339 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200340 if (efmp == efm + len)
341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100342 emsg(_("E374: Missing ] in format string"));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200343 return NULL;
344 }
345 }
346 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200347 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200348 *regpat++ = *++efmp;
349 *regpat++ = '\\';
350 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200351 }
352 else
353 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200354 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100355 semsg(_("E375: Unsupported %%%c in format string"), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200356 return NULL;
357 }
358
359 *pefmp = efmp;
360
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200361 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200362}
363
364/*
365 * Analyze/parse an errorformat prefix.
366 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200367 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100368efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200369{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200370 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200371 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200372 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200373 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200374 else
375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100376 semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200377 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200378 }
379
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200380 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200381}
382
383/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200384 * Converts a 'errorformat' string part in 'efm' to a regular expression
385 * pattern. The resulting regex pattern is returned in "regpat". Additional
386 * information about the 'erroformat' pattern is returned in "fmt_ptr".
387 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200388 */
389 static int
390efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200391 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200392 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200393 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100394 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200395{
396 char_u *ptr;
397 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200398 int round;
399 int idx = 0;
400
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200401 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200402 ptr = regpat;
403 *ptr++ = '^';
404 round = 0;
405 for (efmp = efm; efmp < efm + len; ++efmp)
406 {
407 if (*efmp == '%')
408 {
409 ++efmp;
410 for (idx = 0; idx < FMT_PATTERNS; ++idx)
411 if (fmt_pat[idx].convchar == *efmp)
412 break;
413 if (idx < FMT_PATTERNS)
414 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100415 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200416 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200417 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200418 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200419 }
420 else if (*efmp == '*')
421 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200422 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100423 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200424 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200425 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200426 }
427 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200428 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200429 else if (*efmp == '#')
430 *ptr++ = '*';
431 else if (*efmp == '>')
432 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200433 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200434 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200435 // prefix is allowed only at the beginning of the errorformat
436 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100437 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200438 if (efmp == NULL)
439 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200440 }
441 else
442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100443 semsg(_("E377: Invalid %%%c in format string"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200444 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200445 }
446 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200447 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200448 {
449 if (*efmp == '\\' && efmp + 1 < efm + len)
450 ++efmp;
451 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200452 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453 if (*efmp)
454 *ptr++ = *efmp;
455 }
456 }
457 *ptr++ = '$';
458 *ptr = NUL;
459
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200460 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200461}
462
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200463/*
464 * Free the 'errorformat' information list
465 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200466 static void
467free_efm_list(efm_T **efm_first)
468{
469 efm_T *efm_ptr;
470
471 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
472 {
473 *efm_first = efm_ptr->next;
474 vim_regfree(efm_ptr->prog);
475 vim_free(efm_ptr);
476 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100477 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200478}
479
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200480/*
481 * Compute the size of the buffer used to convert a 'errorformat' pattern into
482 * a regular expression pattern.
483 */
484 static int
485efm_regpat_bufsz(char_u *efm)
486{
487 int sz;
488 int i;
489
490 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
491 for (i = FMT_PATTERNS; i > 0; )
492 sz += (int)STRLEN(fmt_pat[--i].pattern);
493#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200494 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200495#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200496 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200497#endif
498
499 return sz;
500}
501
502/*
503 * Return the length of a 'errorformat' option part (separated by ",").
504 */
505 static int
506efm_option_part_len(char_u *efm)
507{
508 int len;
509
510 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
511 if (efm[len] == '\\' && efm[len + 1] != NUL)
512 ++len;
513
514 return len;
515}
516
517/*
518 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
519 * are parsed and converted to regular expressions. Returns information about
520 * the parsed 'errorformat' option.
521 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200522 static efm_T *
523parse_efm_option(char_u *efm)
524{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200525 efm_T *fmt_ptr = NULL;
526 efm_T *fmt_first = NULL;
527 efm_T *fmt_last = NULL;
528 char_u *fmtstr = NULL;
529 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200530 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200531
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200532 // Each part of the format string is copied and modified from errorformat
533 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200534
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200535 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200536 sz = efm_regpat_bufsz(efm);
537 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200538 goto parse_efm_error;
539
540 while (efm[0] != NUL)
541 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200542 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200543 fmt_ptr = ALLOC_CLEAR_ONE(efm_T);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200544 if (fmt_ptr == NULL)
545 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200546 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200547 fmt_first = fmt_ptr;
548 else
549 fmt_last->next = fmt_ptr;
550 fmt_last = fmt_ptr;
551
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200552 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200553 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200554
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100555 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200556 goto parse_efm_error;
557 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
558 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200559 // Advance to next part
560 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200561 }
562
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200563 if (fmt_first == NULL) // nothing found
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100564 emsg(_("E378: 'errorformat' contains no pattern"));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200565
566 goto parse_efm_end;
567
568parse_efm_error:
569 free_efm_list(&fmt_first);
570
571parse_efm_end:
572 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200573
574 return fmt_first;
575}
576
Bram Moolenaare0d37972016-07-15 22:36:01 +0200577enum {
578 QF_FAIL = 0,
579 QF_OK = 1,
580 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200581 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200582 QF_IGNORE_LINE = 4,
583 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200584};
585
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200586/*
587 * State information used to parse lines and add entries to a quickfix/location
588 * list.
589 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200590typedef struct {
591 char_u *linebuf;
592 int linelen;
593 char_u *growbuf;
594 int growbufsiz;
595 FILE *fd;
596 typval_T *tv;
597 char_u *p_str;
598 listitem_T *p_li;
599 buf_T *buf;
600 linenr_T buflnum;
601 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100602 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200603} qfstate_T;
604
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200605/*
606 * Allocate more memory for the line buffer used for parsing lines.
607 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200608 static char_u *
609qf_grow_linebuf(qfstate_T *state, int newsz)
610{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200611 char_u *p;
612
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200613 // If the line exceeds LINE_MAXLEN exclude the last
614 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200615 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
616 if (state->growbuf == NULL)
617 {
618 state->growbuf = alloc(state->linelen + 1);
619 if (state->growbuf == NULL)
620 return NULL;
621 state->growbufsiz = state->linelen;
622 }
623 else if (state->linelen > state->growbufsiz)
624 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200625 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200626 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200627 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200628 state->growbufsiz = state->linelen;
629 }
630 return state->growbuf;
631}
632
633/*
634 * Get the next string (separated by newline) from state->p_str.
635 */
636 static int
637qf_get_next_str_line(qfstate_T *state)
638{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200639 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200640 char_u *p_str = state->p_str;
641 char_u *p;
642 int len;
643
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200644 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200645 return QF_END_OF_INPUT;
646
647 p = vim_strchr(p_str, '\n');
648 if (p != NULL)
649 len = (int)(p - p_str) + 1;
650 else
651 len = (int)STRLEN(p_str);
652
653 if (len > IOSIZE - 2)
654 {
655 state->linebuf = qf_grow_linebuf(state, len);
656 if (state->linebuf == NULL)
657 return QF_NOMEM;
658 }
659 else
660 {
661 state->linebuf = IObuff;
662 state->linelen = len;
663 }
664 vim_strncpy(state->linebuf, p_str, state->linelen);
665
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200666 // Increment using len in order to discard the rest of the
667 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200668 p_str += len;
669 state->p_str = p_str;
670
671 return QF_OK;
672}
673
674/*
675 * Get the next string from state->p_Li.
676 */
677 static int
678qf_get_next_list_line(qfstate_T *state)
679{
680 listitem_T *p_li = state->p_li;
681 int len;
682
683 while (p_li != NULL
684 && (p_li->li_tv.v_type != VAR_STRING
685 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200686 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200687
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200688 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200689 {
690 state->p_li = NULL;
691 return QF_END_OF_INPUT;
692 }
693
694 len = (int)STRLEN(p_li->li_tv.vval.v_string);
695 if (len > IOSIZE - 2)
696 {
697 state->linebuf = qf_grow_linebuf(state, len);
698 if (state->linebuf == NULL)
699 return QF_NOMEM;
700 }
701 else
702 {
703 state->linebuf = IObuff;
704 state->linelen = len;
705 }
706
707 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
708
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200709 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200710 return QF_OK;
711}
712
713/*
714 * Get the next string from state->buf.
715 */
716 static int
717qf_get_next_buf_line(qfstate_T *state)
718{
719 char_u *p_buf = NULL;
720 int len;
721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200722 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200723 if (state->buflnum > state->lnumlast)
724 return QF_END_OF_INPUT;
725
726 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
727 state->buflnum += 1;
728
729 len = (int)STRLEN(p_buf);
730 if (len > IOSIZE - 2)
731 {
732 state->linebuf = qf_grow_linebuf(state, len);
733 if (state->linebuf == NULL)
734 return QF_NOMEM;
735 }
736 else
737 {
738 state->linebuf = IObuff;
739 state->linelen = len;
740 }
741 vim_strncpy(state->linebuf, p_buf, state->linelen);
742
743 return QF_OK;
744}
745
746/*
747 * Get the next string from file state->fd.
748 */
749 static int
750qf_get_next_file_line(qfstate_T *state)
751{
752 int discard;
753 int growbuflen;
754
755 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
756 return QF_END_OF_INPUT;
757
758 discard = FALSE;
759 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200760 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200761 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200762 // The current line exceeds IObuff, continue reading using
763 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200764 if (state->growbuf == NULL)
765 {
766 state->growbufsiz = 2 * (IOSIZE - 1);
767 state->growbuf = alloc(state->growbufsiz);
768 if (state->growbuf == NULL)
769 return QF_NOMEM;
770 }
771
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200772 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200773 memcpy(state->growbuf, IObuff, IOSIZE - 1);
774 growbuflen = state->linelen;
775
776 for (;;)
777 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200778 char_u *p;
779
Bram Moolenaare0d37972016-07-15 22:36:01 +0200780 if (fgets((char *)state->growbuf + growbuflen,
781 state->growbufsiz - growbuflen, state->fd) == NULL)
782 break;
783 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
784 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200785 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200786 break;
787 if (state->growbufsiz == LINE_MAXLEN)
788 {
789 discard = TRUE;
790 break;
791 }
792
793 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
794 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200795 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200796 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200797 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200798 }
799
800 while (discard)
801 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200802 // The current line is longer than LINE_MAXLEN, continue
803 // reading but discard everything until EOL or EOF is
804 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200805 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
806 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200807 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200808 break;
809 }
810
811 state->linebuf = state->growbuf;
812 state->linelen = growbuflen;
813 }
814 else
815 state->linebuf = IObuff;
816
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200817 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200818 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
819 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100820 char_u *line;
821
822 line = string_convert(&state->vc, state->linebuf, &state->linelen);
823 if (line != NULL)
824 {
825 if (state->linelen < IOSIZE)
826 {
827 STRCPY(state->linebuf, line);
828 vim_free(line);
829 }
830 else
831 {
832 vim_free(state->growbuf);
833 state->linebuf = state->growbuf = line;
834 state->growbufsiz = state->linelen < LINE_MAXLEN
835 ? state->linelen : LINE_MAXLEN;
836 }
837 }
838 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100839
Bram Moolenaare0d37972016-07-15 22:36:01 +0200840 return QF_OK;
841}
842
843/*
844 * Get the next string from a file/buffer/list/string.
845 */
846 static int
847qf_get_nextline(qfstate_T *state)
848{
849 int status = QF_FAIL;
850
851 if (state->fd == NULL)
852 {
853 if (state->tv != NULL)
854 {
855 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200856 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200857 status = qf_get_next_str_line(state);
858 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200859 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200860 status = qf_get_next_list_line(state);
861 }
862 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200863 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200864 status = qf_get_next_buf_line(state);
865 }
866 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200867 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200868 status = qf_get_next_file_line(state);
869
870 if (status != QF_OK)
871 return status;
872
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200873 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200874 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200875 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200876 state->linebuf[state->linelen - 1] = NUL;
877#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200878 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
879 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200880#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200881 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200882
Bram Moolenaare0d37972016-07-15 22:36:01 +0200883 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200884
885 return QF_OK;
886}
887
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200888typedef struct {
889 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200890 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200891 char_u *errmsg;
892 int errmsglen;
893 long lnum;
894 int col;
895 char_u use_viscol;
896 char_u *pattern;
897 int enr;
898 int type;
899 int valid;
900} qffields_T;
901
902/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200903 * Parse the match for filename ('%f') pattern in regmatch.
904 * Return the matched value in "fields->namebuf".
905 */
906 static int
907qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
908{
909 int c;
910
911 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
912 return QF_FAIL;
913
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200914 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200915 c = *rmp->endp[midx];
916 *rmp->endp[midx] = NUL;
917 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
918 *rmp->endp[midx] = c;
919
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200920 // For separate filename patterns (%O, %P and %Q), the specified file
921 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200922 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
923 && mch_getperm(fields->namebuf) == -1)
924 return QF_FAIL;
925
926 return QF_OK;
927}
928
929/*
930 * Parse the match for error number ('%n') pattern in regmatch.
931 * Return the matched value in "fields->enr".
932 */
933 static int
934qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
935{
936 if (rmp->startp[midx] == NULL)
937 return QF_FAIL;
938 fields->enr = (int)atol((char *)rmp->startp[midx]);
939 return QF_OK;
940}
941
942/*
943 * Parse the match for line number (%l') pattern in regmatch.
944 * Return the matched value in "fields->lnum".
945 */
946 static int
947qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
948{
949 if (rmp->startp[midx] == NULL)
950 return QF_FAIL;
951 fields->lnum = atol((char *)rmp->startp[midx]);
952 return QF_OK;
953}
954
955/*
956 * Parse the match for column number ('%c') pattern in regmatch.
957 * Return the matched value in "fields->col".
958 */
959 static int
960qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
961{
962 if (rmp->startp[midx] == NULL)
963 return QF_FAIL;
964 fields->col = (int)atol((char *)rmp->startp[midx]);
965 return QF_OK;
966}
967
968/*
969 * Parse the match for error type ('%t') pattern in regmatch.
970 * Return the matched value in "fields->type".
971 */
972 static int
973qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
974{
975 if (rmp->startp[midx] == NULL)
976 return QF_FAIL;
977 fields->type = *rmp->startp[midx];
978 return QF_OK;
979}
980
981/*
982 * Parse the match for '%+' format pattern. The whole matching line is included
983 * in the error string. Return the matched line in "fields->errmsg".
984 */
985 static int
986qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
987{
988 char_u *p;
989
990 if (linelen >= fields->errmsglen)
991 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200992 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200993 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
994 return QF_NOMEM;
995 fields->errmsg = p;
996 fields->errmsglen = linelen + 1;
997 }
998 vim_strncpy(fields->errmsg, linebuf, linelen);
999 return QF_OK;
1000}
1001
1002/*
1003 * Parse the match for error message ('%m') pattern in regmatch.
1004 * Return the matched value in "fields->errmsg".
1005 */
1006 static int
1007qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1008{
1009 char_u *p;
1010 int len;
1011
1012 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1013 return QF_FAIL;
1014 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1015 if (len >= fields->errmsglen)
1016 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001017 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001018 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1019 return QF_NOMEM;
1020 fields->errmsg = p;
1021 fields->errmsglen = len + 1;
1022 }
1023 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1024 return QF_OK;
1025}
1026
1027/*
1028 * Parse the match for rest of a single-line file message ('%r') pattern.
1029 * Return the matched value in "tail".
1030 */
1031 static int
1032qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1033{
1034 if (rmp->startp[midx] == NULL)
1035 return QF_FAIL;
1036 *tail = rmp->startp[midx];
1037 return QF_OK;
1038}
1039
1040/*
1041 * Parse the match for the pointer line ('%p') pattern in regmatch.
1042 * Return the matched value in "fields->col".
1043 */
1044 static int
1045qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1046{
1047 char_u *match_ptr;
1048
1049 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1050 return QF_FAIL;
1051 fields->col = 0;
1052 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1053 ++match_ptr)
1054 {
1055 ++fields->col;
1056 if (*match_ptr == TAB)
1057 {
1058 fields->col += 7;
1059 fields->col -= fields->col % 8;
1060 }
1061 }
1062 ++fields->col;
1063 fields->use_viscol = TRUE;
1064 return QF_OK;
1065}
1066
1067/*
1068 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1069 * Return the matched value in "fields->col".
1070 */
1071 static int
1072qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1073{
1074 if (rmp->startp[midx] == NULL)
1075 return QF_FAIL;
1076 fields->col = (int)atol((char *)rmp->startp[midx]);
1077 fields->use_viscol = TRUE;
1078 return QF_OK;
1079}
1080
1081/*
1082 * Parse the match for the search text ('%s') pattern in regmatch.
1083 * Return the matched value in "fields->pattern".
1084 */
1085 static int
1086qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1087{
1088 int len;
1089
1090 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1091 return QF_FAIL;
1092 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1093 if (len > CMDBUFFSIZE - 5)
1094 len = CMDBUFFSIZE - 5;
1095 STRCPY(fields->pattern, "^\\V");
1096 STRNCAT(fields->pattern, rmp->startp[midx], len);
1097 fields->pattern[len + 3] = '\\';
1098 fields->pattern[len + 4] = '$';
1099 fields->pattern[len + 5] = NUL;
1100 return QF_OK;
1101}
1102
1103/*
1104 * Parse the match for the module ('%o') pattern in regmatch.
1105 * Return the matched value in "fields->module".
1106 */
1107 static int
1108qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1109{
1110 int len;
1111
1112 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1113 return QF_FAIL;
1114 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1115 if (len > CMDBUFFSIZE)
1116 len = CMDBUFFSIZE;
1117 STRNCAT(fields->module, rmp->startp[midx], len);
1118 return QF_OK;
1119}
1120
1121/*
1122 * 'errorformat' format pattern parser functions.
1123 * The '%f' and '%r' formats are parsed differently from other formats.
1124 * See qf_parse_match() for details.
1125 */
1126static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1127{
1128 NULL,
1129 qf_parse_fmt_n,
1130 qf_parse_fmt_l,
1131 qf_parse_fmt_c,
1132 qf_parse_fmt_t,
1133 qf_parse_fmt_m,
1134 NULL,
1135 qf_parse_fmt_p,
1136 qf_parse_fmt_v,
1137 qf_parse_fmt_s,
1138 qf_parse_fmt_o
1139};
1140
1141/*
1142 * Parse the error format pattern matches in "regmatch" and set the values in
1143 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1144 * match. Returns QF_OK if all the matches are successfully parsed. On
1145 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001146 */
1147 static int
1148qf_parse_match(
1149 char_u *linebuf,
1150 int linelen,
1151 efm_T *fmt_ptr,
1152 regmatch_T *regmatch,
1153 qffields_T *fields,
1154 int qf_multiline,
1155 int qf_multiscan,
1156 char_u **tail)
1157{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001158 int idx = fmt_ptr->prefix;
1159 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001160 int midx;
1161 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001162
1163 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1164 return QF_FAIL;
1165 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1166 fields->type = idx;
1167 else
1168 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001169
1170 // Extract error message data from matched line.
1171 // We check for an actual submatch, because "\[" and "\]" in
1172 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001173 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001174 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001175 status = QF_OK;
1176 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001177 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001178 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1179 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001180 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001181 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001182 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001183 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001184 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001185 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001186 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001187 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001188 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001189 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001190
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001191 if (status != QF_OK)
1192 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001193 }
1194
1195 return QF_OK;
1196}
1197
1198/*
1199 * Parse an error line in 'linebuf' using a single error format string in
1200 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1201 * Returns QF_OK if the efm format matches completely and the fields are
1202 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1203 */
1204 static int
1205qf_parse_get_fields(
1206 char_u *linebuf,
1207 int linelen,
1208 efm_T *fmt_ptr,
1209 qffields_T *fields,
1210 int qf_multiline,
1211 int qf_multiscan,
1212 char_u **tail)
1213{
1214 regmatch_T regmatch;
1215 int status = QF_FAIL;
1216 int r;
1217
1218 if (qf_multiscan &&
1219 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1220 return QF_FAIL;
1221
1222 fields->namebuf[0] = NUL;
1223 fields->module[0] = NUL;
1224 fields->pattern[0] = NUL;
1225 if (!qf_multiscan)
1226 fields->errmsg[0] = NUL;
1227 fields->lnum = 0;
1228 fields->col = 0;
1229 fields->use_viscol = FALSE;
1230 fields->enr = -1;
1231 fields->type = 0;
1232 *tail = NULL;
1233
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001234 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001235 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001236 regmatch.regprog = fmt_ptr->prog;
1237 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1238 fmt_ptr->prog = regmatch.regprog;
1239 if (r)
1240 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1241 fields, qf_multiline, qf_multiscan, tail);
1242
1243 return status;
1244}
1245
1246/*
1247 * Parse directory error format prefixes (%D and %X).
1248 * Push and pop directories from the directory stack when scanning directory
1249 * names.
1250 */
1251 static int
1252qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1253{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001254 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001255 {
1256 if (*fields->namebuf == NUL)
1257 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001258 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001259 return QF_FAIL;
1260 }
1261 qfl->qf_directory =
1262 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1263 if (qfl->qf_directory == NULL)
1264 return QF_FAIL;
1265 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001266 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001267 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1268
1269 return QF_OK;
1270}
1271
1272/*
1273 * Parse global file name error format prefixes (%O, %P and %Q).
1274 */
1275 static int
1276qf_parse_file_pfx(
1277 int idx,
1278 qffields_T *fields,
1279 qf_list_T *qfl,
1280 char_u *tail)
1281{
1282 fields->valid = FALSE;
1283 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1284 {
1285 if (*fields->namebuf && idx == 'P')
1286 qfl->qf_currfile =
1287 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1288 else if (idx == 'Q')
1289 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1290 *fields->namebuf = NUL;
1291 if (tail && *tail)
1292 {
1293 STRMOVE(IObuff, skipwhite(tail));
1294 qfl->qf_multiscan = TRUE;
1295 return QF_MULTISCAN;
1296 }
1297 }
1298
1299 return QF_OK;
1300}
1301
1302/*
1303 * Parse a non-error line (a line which doesn't match any of the error
1304 * format in 'efm').
1305 */
1306 static int
1307qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1308{
1309 char_u *p;
1310
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001311 fields->namebuf[0] = NUL; // no match found, remove file name
1312 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001313 fields->valid = FALSE;
1314 if (linelen >= fields->errmsglen)
1315 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001316 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001317 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1318 return QF_NOMEM;
1319 fields->errmsg = p;
1320 fields->errmsglen = linelen + 1;
1321 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001322 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001323 vim_strncpy(fields->errmsg, linebuf, linelen);
1324
1325 return QF_OK;
1326}
1327
1328/*
1329 * Parse multi-line error format prefixes (%C and %Z)
1330 */
1331 static int
1332qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001333 int idx,
1334 qf_list_T *qfl,
1335 qffields_T *fields)
1336{
1337 char_u *ptr;
1338 int len;
1339
1340 if (!qfl->qf_multiignore)
1341 {
1342 qfline_T *qfprev = qfl->qf_last;
1343
1344 if (qfprev == NULL)
1345 return QF_FAIL;
1346 if (*fields->errmsg && !qfl->qf_multiignore)
1347 {
1348 len = (int)STRLEN(qfprev->qf_text);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001349 if ((ptr = alloc(len + STRLEN(fields->errmsg) + 2))
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001350 == NULL)
1351 return QF_FAIL;
1352 STRCPY(ptr, qfprev->qf_text);
1353 vim_free(qfprev->qf_text);
1354 qfprev->qf_text = ptr;
1355 *(ptr += len) = '\n';
1356 STRCPY(++ptr, fields->errmsg);
1357 }
1358 if (qfprev->qf_nr == -1)
1359 qfprev->qf_nr = fields->enr;
1360 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001361 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001362 qfprev->qf_type = fields->type;
1363
1364 if (!qfprev->qf_lnum)
1365 qfprev->qf_lnum = fields->lnum;
1366 if (!qfprev->qf_col)
1367 qfprev->qf_col = fields->col;
1368 qfprev->qf_viscol = fields->use_viscol;
1369 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001370 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001371 qfl->qf_directory,
1372 *fields->namebuf || qfl->qf_directory != NULL
1373 ? fields->namebuf
1374 : qfl->qf_currfile != NULL && fields->valid
1375 ? qfl->qf_currfile : 0);
1376 }
1377 if (idx == 'Z')
1378 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1379 line_breakcheck();
1380
1381 return QF_IGNORE_LINE;
1382}
1383
1384/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001385 * Parse a line and get the quickfix fields.
1386 * Return the QF_ status.
1387 */
1388 static int
1389qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001390 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001391 char_u *linebuf,
1392 int linelen,
1393 efm_T *fmt_first,
1394 qffields_T *fields)
1395{
1396 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001397 int idx = 0;
1398 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001399 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001400
Bram Moolenaare333e792018-04-08 13:27:39 +02001401restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001402 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001403 if (fmt_start == NULL)
1404 fmt_ptr = fmt_first;
1405 else
1406 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001407 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001408 fmt_ptr = fmt_start;
1409 fmt_start = NULL;
1410 }
1411
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001412 // Try to match each part of 'errorformat' until we find a complete
1413 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001414 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001415 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1416 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001418 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1419 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1420 if (status == QF_NOMEM)
1421 return status;
1422 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001423 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001424 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001425 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001426
1427 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1428 {
1429 if (fmt_ptr != NULL)
1430 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001431 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001432 status = qf_parse_dir_pfx(idx, fields, qfl);
1433 if (status != QF_OK)
1434 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001435 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001436
1437 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1438 if (status != QF_OK)
1439 return status;
1440
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001441 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001442 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001443 }
1444 else if (fmt_ptr != NULL)
1445 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001446 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001447 if (fmt_ptr->conthere)
1448 fmt_start = fmt_ptr;
1449
1450 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1451 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001452 qfl->qf_multiline = TRUE; // start of a multi-line message
1453 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001454 }
1455 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001456 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001457 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001458 if (status != QF_OK)
1459 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 }
1461 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001462 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001463 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1464 if (status == QF_MULTISCAN)
1465 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001467 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001468 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001469 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001470 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001471 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001472 return QF_IGNORE_LINE;
1473 }
1474 }
1475
1476 return QF_OK;
1477}
1478
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001479/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001480 * Returns TRUE if the specified quickfix/location stack is empty
1481 */
1482 static int
1483qf_stack_empty(qf_info_T *qi)
1484{
1485 return qi == NULL || qi->qf_listcount <= 0;
1486}
1487
1488/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001489 * Returns TRUE if the specified quickfix/location list is empty.
1490 */
1491 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001492qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001493{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001494 return qfl == NULL || qfl->qf_count <= 0;
1495}
1496
1497/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001498 * Returns TRUE if the specified quickfix/location list is not empty and
1499 * has valid entries.
1500 */
1501 static int
1502qf_list_has_valid_entries(qf_list_T *qfl)
1503{
1504 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1505}
1506
1507/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001508 * Return a pointer to a list in the specified quickfix stack
1509 */
1510 static qf_list_T *
1511qf_get_list(qf_info_T *qi, int idx)
1512{
1513 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001514}
1515
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001516/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001517 * Allocate the fields used for parsing lines and populating a quickfix list.
1518 */
1519 static int
1520qf_alloc_fields(qffields_T *pfields)
1521{
1522 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1523 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1524 pfields->errmsglen = CMDBUFFSIZE + 1;
1525 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1526 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1527 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1528 || pfields->pattern == NULL || pfields->module == NULL)
1529 return FAIL;
1530
1531 return OK;
1532}
1533
1534/*
1535 * Free the fields used for parsing lines and populating a quickfix list.
1536 */
1537 static void
1538qf_free_fields(qffields_T *pfields)
1539{
1540 vim_free(pfields->namebuf);
1541 vim_free(pfields->module);
1542 vim_free(pfields->errmsg);
1543 vim_free(pfields->pattern);
1544}
1545
1546/*
1547 * Setup the state information used for parsing lines and populating a
1548 * quickfix list.
1549 */
1550 static int
1551qf_setup_state(
1552 qfstate_T *pstate,
1553 char_u *enc,
1554 char_u *efile,
1555 typval_T *tv,
1556 buf_T *buf,
1557 linenr_T lnumfirst,
1558 linenr_T lnumlast)
1559{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001560 pstate->vc.vc_type = CONV_NONE;
1561 if (enc != NULL && *enc != NUL)
1562 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001563
1564 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001566 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001567 return FAIL;
1568 }
1569
1570 if (tv != NULL)
1571 {
1572 if (tv->v_type == VAR_STRING)
1573 pstate->p_str = tv->vval.v_string;
1574 else if (tv->v_type == VAR_LIST)
1575 pstate->p_li = tv->vval.v_list->lv_first;
1576 pstate->tv = tv;
1577 }
1578 pstate->buf = buf;
1579 pstate->buflnum = lnumfirst;
1580 pstate->lnumlast = lnumlast;
1581
1582 return OK;
1583}
1584
1585/*
1586 * Cleanup the state information used for parsing lines and populating a
1587 * quickfix list.
1588 */
1589 static void
1590qf_cleanup_state(qfstate_T *pstate)
1591{
1592 if (pstate->fd != NULL)
1593 fclose(pstate->fd);
1594
1595 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001596 if (pstate->vc.vc_type != CONV_NONE)
1597 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001598}
1599
1600/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001601 * Process the next line from a file/buffer/list/string and add it
1602 * to the quickfix list 'qfl'.
1603 */
1604 static int
1605qf_init_process_nextline(
1606 qf_list_T *qfl,
1607 efm_T *fmt_first,
1608 qfstate_T *state,
1609 qffields_T *fields)
1610{
1611 int status;
1612
1613 // Get the next line from a file/buffer/list/string
1614 status = qf_get_nextline(state);
1615 if (status != QF_OK)
1616 return status;
1617
1618 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1619 fmt_first, fields);
1620 if (status != QF_OK)
1621 return status;
1622
1623 return qf_add_entry(qfl,
1624 qfl->qf_directory,
1625 (*fields->namebuf || qfl->qf_directory != NULL)
1626 ? fields->namebuf
1627 : ((qfl->qf_currfile != NULL && fields->valid)
1628 ? qfl->qf_currfile : (char_u *)NULL),
1629 fields->module,
1630 0,
1631 fields->errmsg,
1632 fields->lnum,
1633 fields->col,
1634 fields->use_viscol,
1635 fields->pattern,
1636 fields->enr,
1637 fields->type,
1638 fields->valid);
1639}
1640
1641/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001642 * Read the errorfile "efile" into memory, line by line, building the error
1643 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001644 * Alternative: when "efile" is NULL read errors from buffer "buf".
1645 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001646 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001647 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1648 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001649 * Return -1 for error, number of errors for success.
1650 */
1651 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001652qf_init_ext(
1653 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001654 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001655 char_u *efile,
1656 buf_T *buf,
1657 typval_T *tv,
1658 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001659 int newlist, // TRUE: start a new error list
1660 linenr_T lnumfirst, // first line number to use
1661 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001662 char_u *qf_title,
1663 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001664{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001665 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001666 qfstate_T state;
1667 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001668 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001669 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001670 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001672 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001673 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001674 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001676 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001677 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001678
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001679 vim_memset(&state, 0, sizeof(state));
1680 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001681 if ((qf_alloc_fields(&fields) == FAIL) ||
1682 (qf_setup_state(&state, enc, efile, tv, buf,
1683 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 goto qf_init_end;
1685
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001686 if (newlist || qf_idx == qi->qf_listcount)
1687 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001688 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001689 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001690 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001691 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001692 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001693 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001694 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001695 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001696 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001697 qfl = qf_get_list(qi, qf_idx);
1698 if (!qf_list_empty(qfl))
1699 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001702 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001703 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001704 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 else
1706 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001708 // If the errorformat didn't change between calls, then reuse the
1709 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001710 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1711 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001712 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001713 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001714 free_efm_list(&fmt_first);
1715
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001716 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001717 fmt_first = parse_efm_option(efm);
1718 if (fmt_first != NULL)
1719 last_efm = vim_strsave(efm);
1720 }
1721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001722 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001725 // got_int is reset here, because it was probably set when killing the
1726 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 got_int = FALSE;
1728
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001729 // Read the lines in the error file one by one.
1730 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001731 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001733 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001734 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001735 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001736 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001737 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001738 if (status == QF_FAIL)
1739 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 line_breakcheck();
1742 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001743 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001745 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001747 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001748 qfl->qf_ptr = qfl->qf_start;
1749 qfl->qf_index = 1;
1750 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752 else
1753 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001754 qfl->qf_nonevalid = FALSE;
1755 if (qfl->qf_ptr == NULL)
1756 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001758 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001759 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001760 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001762 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001764 if (!adding)
1765 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001766 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001767 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001768 qi->qf_listcount--;
1769 if (qi->qf_curlist > 0)
1770 --qi->qf_curlist;
1771 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001772qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001773 if (qf_idx == qi->qf_curlist)
1774 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001775 qf_cleanup_state(&state);
1776 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 return retval;
1779}
1780
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001781/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001782 * Read the errorfile "efile" into memory, line by line, building the error
1783 * list. Set the error list's title to qf_title.
1784 * Return -1 for error, number of errors for success.
1785 */
1786 int
1787qf_init(win_T *wp,
1788 char_u *efile,
1789 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001790 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001791 char_u *qf_title,
1792 char_u *enc)
1793{
1794 qf_info_T *qi = &ql_info;
1795
1796 if (wp != NULL)
1797 {
1798 qi = ll_get_or_alloc_list(wp);
1799 if (qi == NULL)
1800 return FAIL;
1801 }
1802
1803 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1804 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1805}
1806
1807/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001808 * Set the title of the specified quickfix list. Frees the previous title.
1809 * Prepends ':' to the title.
1810 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001811 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001812qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001813{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001814 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001815
Bram Moolenaarfb604092014-07-23 15:55:00 +02001816 if (title != NULL)
1817 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02001818 char_u *p = alloc(STRLEN(title) + 2);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001819
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001820 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001821 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001822 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001823 }
1824}
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001827 * The title of a quickfix/location list is set, by default, to the command
1828 * that created the quickfix list with the ":" prefix.
1829 * Create a quickfix list title string by prepending ":" to a user command.
1830 * Returns a pointer to a static buffer with the title.
1831 */
1832 static char_u *
1833qf_cmdtitle(char_u *cmd)
1834{
1835 static char_u qftitle_str[IOSIZE];
1836
1837 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1838 return qftitle_str;
1839}
1840
1841/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001842 * Return a pointer to the current list in the specified quickfix stack
1843 */
1844 static qf_list_T *
1845qf_get_curlist(qf_info_T *qi)
1846{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001847 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001848}
1849
1850/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001851 * Prepare for adding a new quickfix list. If the current list is in the
1852 * middle of the stack, then all the following lists are freed and then
1853 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 */
1855 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001856qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
1858 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001859 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001861 // If the current entry is not the last entry, delete entries beyond
1862 // the current entry. This makes it possible to browse in a tree-like
1863 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001864 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001865 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001867 // When the stack is full, remove to oldest entry
1868 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001869 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001871 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001873 qi->qf_lists[i - 1] = qi->qf_lists[i];
1874 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001877 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001878 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001879 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1880 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001881 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001882 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883}
1884
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001885/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001886 * Queue location list stack delete request.
1887 */
1888 static void
1889locstack_queue_delreq(qf_info_T *qi)
1890{
1891 qf_delq_T *q;
1892
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001893 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001894 if (q != NULL)
1895 {
1896 q->qi = qi;
1897 q->next = qf_delq_head;
1898 qf_delq_head = q;
1899 }
1900}
1901
1902/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001903 * Return the global quickfix stack window buffer number.
1904 */
1905 int
1906qf_stack_get_bufnr(void)
1907{
1908 return ql_info.qf_bufnr;
1909}
1910
1911/*
1912 * Wipe the quickfix window buffer (if present) for the specified
1913 * quickfix/location list.
1914 */
1915 static void
1916wipe_qf_buffer(qf_info_T *qi)
1917{
1918 buf_T *qfbuf;
1919
1920 if (qi->qf_bufnr != INVALID_QFBUFNR)
1921 {
1922 qfbuf = buflist_findnr(qi->qf_bufnr);
1923 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1924 {
1925 // If the quickfix buffer is not loaded in any window, then
1926 // wipe the buffer.
1927 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE);
1928 qi->qf_bufnr = INVALID_QFBUFNR;
1929 }
1930 }
1931}
1932
1933/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001934 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001935 */
1936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001937ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001938{
1939 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001940 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001941
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001942 qi = *pqi;
1943 if (qi == NULL)
1944 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001945 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001946
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001947 // If the location list is still in use, then queue the delete request
1948 // to be processed later.
1949 if (quickfix_busy > 0)
1950 {
1951 locstack_queue_delreq(qi);
1952 return;
1953 }
1954
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001955 qi->qf_refcount--;
1956 if (qi->qf_refcount < 1)
1957 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001958 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001959 // If the quickfix window buffer is loaded, then wipe it
1960 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001961
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001962 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001963 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001964 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001965 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001966}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001967
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001968/*
1969 * Free all the quickfix/location lists in the stack.
1970 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001971 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001972qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001973{
1974 int i;
1975 qf_info_T *qi = &ql_info;
1976
1977 if (wp != NULL)
1978 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001979 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980 ll_free_all(&wp->w_llist);
1981 ll_free_all(&wp->w_llist_ref);
1982 }
1983 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001984 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001985 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001986 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001987}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001988
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001990 * Delay freeing of location list stacks when the quickfix code is running.
1991 * Used to avoid problems with autocmds freeing location list stacks when the
1992 * quickfix code is still referencing the stack.
1993 * Must always call decr_quickfix_busy() exactly once after this.
1994 */
1995 static void
1996incr_quickfix_busy(void)
1997{
1998 quickfix_busy++;
1999}
2000
2001/*
2002 * Safe to free location list stacks. Process any delayed delete requests.
2003 */
2004 static void
2005decr_quickfix_busy(void)
2006{
2007 if (--quickfix_busy == 0)
2008 {
2009 // No longer referencing the location lists. Process all the pending
2010 // delete requests.
2011 while (qf_delq_head != NULL)
2012 {
2013 qf_delq_T *q = qf_delq_head;
2014
2015 qf_delq_head = q->next;
2016 ll_free_all(&q->qi);
2017 vim_free(q);
2018 }
2019 }
2020#ifdef ABORT_ON_INTERNAL_ERROR
2021 if (quickfix_busy < 0)
2022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002023 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002024 abort();
2025 }
2026#endif
2027}
2028
2029#if defined(EXITFREE) || defined(PROTO)
2030 void
2031check_quickfix_busy(void)
2032{
2033 if (quickfix_busy != 0)
2034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002035 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002036# ifdef ABORT_ON_INTERNAL_ERROR
2037 abort();
2038# endif
2039 }
2040}
2041#endif
2042
2043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 * Add an entry to the end of the list of errors.
Bram Moolenaar95946f12019-03-31 15:31:59 +02002045 * Returns QF_OK or QF_FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 */
2047 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002048qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002049 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002050 char_u *dir, // optional directory name
2051 char_u *fname, // file name or NULL
2052 char_u *module, // module name or NULL
2053 int bufnum, // buffer number or zero
2054 char_u *mesg, // message
2055 long lnum, // line number
2056 int col, // column
2057 int vis_col, // using visual column
2058 char_u *pattern, // search pattern
2059 int nr, // error number
2060 int type, // type character
2061 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002063 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002064 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002066 if ((qfp = ALLOC_ONE(qfline_T)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002067 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002068 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002069 {
2070 buf_T *buf = buflist_findnr(bufnum);
2071
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002072 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002073 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002074 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002075 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002076 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002077 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002078 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2080 {
2081 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002082 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084 qfp->qf_lnum = lnum;
2085 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002086 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002087 if (pattern == NULL || *pattern == NUL)
2088 qfp->qf_pattern = NULL;
2089 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2090 {
2091 vim_free(qfp->qf_text);
2092 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002093 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002094 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002095 if (module == NULL || *module == NUL)
2096 qfp->qf_module = NULL;
2097 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2098 {
2099 vim_free(qfp->qf_text);
2100 vim_free(qfp->qf_pattern);
2101 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002102 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002105 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 type = 0;
2107 qfp->qf_type = type;
2108 qfp->qf_valid = valid;
2109
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002110 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002111 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002113 qfl->qf_start = qfp;
2114 qfl->qf_ptr = qfp;
2115 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002116 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
2118 else
2119 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002120 qfp->qf_prev = *lastp;
2121 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002123 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002125 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002126 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002127 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002129 qfl->qf_index = qfl->qf_count;
2130 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
2132
Bram Moolenaar95946f12019-03-31 15:31:59 +02002133 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134}
2135
2136/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002137 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002138 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002139 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002140qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002141{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002142 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002143
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002144 qi = ALLOC_CLEAR_ONE(qf_info_T);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002145 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002146 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002147 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002148 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002149 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002150 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002151 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002152}
2153
2154/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002155 * Return the location list stack for window 'wp'.
2156 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002157 */
2158 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002159ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002160{
2161 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002162 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002163 return wp->w_llist_ref;
2164
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002165 // For a non-location list window, w_llist_ref should not point to a
2166 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002167 ll_free_all(&wp->w_llist_ref);
2168
2169 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002170 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002171 return wp->w_llist;
2172}
2173
2174/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002175 * Get the quickfix/location list stack to use for the specified Ex command.
2176 * For a location list command, returns the stack for the current window. If
2177 * the location list is not found, then returns NULL and prints an error
2178 * message if 'print_emsg' is TRUE.
2179 */
2180 static qf_info_T *
2181qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2182{
2183 qf_info_T *qi = &ql_info;
2184
2185 if (is_loclist_cmd(eap->cmdidx))
2186 {
2187 qi = GET_LOC_LIST(curwin);
2188 if (qi == NULL)
2189 {
2190 if (print_emsg)
2191 emsg(_(e_loclist));
2192 return NULL;
2193 }
2194 }
2195
2196 return qi;
2197}
2198
2199/*
2200 * Get the quickfix/location list stack to use for the specified Ex command.
2201 * For a location list command, returns the stack for the current window.
2202 * If the location list is not present, then allocates a new one.
2203 * Returns NULL if the allocation fails. For a location list command, sets
2204 * 'pwinp' to curwin.
2205 */
2206 static qf_info_T *
2207qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2208{
2209 qf_info_T *qi = &ql_info;
2210
2211 if (is_loclist_cmd(eap->cmdidx))
2212 {
2213 qi = ll_get_or_alloc_list(curwin);
2214 if (qi == NULL)
2215 return NULL;
2216 *pwinp = curwin;
2217 }
2218
2219 return qi;
2220}
2221
2222/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002223 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2224 */
2225 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002226copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002227{
2228 int i;
2229 qfline_T *from_qfp;
2230 qfline_T *prevp;
2231
2232 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002233 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002234 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002235 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002236 NULL,
2237 NULL,
2238 from_qfp->qf_module,
2239 0,
2240 from_qfp->qf_text,
2241 from_qfp->qf_lnum,
2242 from_qfp->qf_col,
2243 from_qfp->qf_viscol,
2244 from_qfp->qf_pattern,
2245 from_qfp->qf_nr,
2246 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002247 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002248 return FAIL;
2249
2250 // qf_add_entry() will not set the qf_num field, as the
2251 // directory and file names are not supplied. So the qf_fnum
2252 // field is copied here.
2253 prevp = to_qfl->qf_last;
2254 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2255 prevp->qf_type = from_qfp->qf_type; // error type
2256 if (from_qfl->qf_ptr == from_qfp)
2257 to_qfl->qf_ptr = prevp; // current location
2258 }
2259
2260 return OK;
2261}
2262
2263/*
2264 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2265 */
2266 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002267copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002268{
2269 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002270 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002271 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2272 to_qfl->qf_count = 0;
2273 to_qfl->qf_index = 0;
2274 to_qfl->qf_start = NULL;
2275 to_qfl->qf_last = NULL;
2276 to_qfl->qf_ptr = NULL;
2277 if (from_qfl->qf_title != NULL)
2278 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2279 else
2280 to_qfl->qf_title = NULL;
2281 if (from_qfl->qf_ctx != NULL)
2282 {
2283 to_qfl->qf_ctx = alloc_tv();
2284 if (to_qfl->qf_ctx != NULL)
2285 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2286 }
2287 else
2288 to_qfl->qf_ctx = NULL;
2289
2290 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002291 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002292 return FAIL;
2293
2294 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2295
2296 // Assign a new ID for the location list
2297 to_qfl->qf_id = ++last_qf_id;
2298 to_qfl->qf_changedtick = 0L;
2299
2300 // When no valid entries are present in the list, qf_ptr points to
2301 // the first item in the list
2302 if (to_qfl->qf_nonevalid)
2303 {
2304 to_qfl->qf_ptr = to_qfl->qf_start;
2305 to_qfl->qf_index = 1;
2306 }
2307
2308 return OK;
2309}
2310
2311/*
2312 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002313 */
2314 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002315copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002316{
2317 qf_info_T *qi;
2318 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002319
Bram Moolenaar09037502018-09-25 22:08:14 +02002320 // When copying from a location list window, copy the referenced
2321 // location list. For other windows, copy the location list for
2322 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002323 if (IS_LL_WINDOW(from))
2324 qi = from->w_llist_ref;
2325 else
2326 qi = from->w_llist;
2327
Bram Moolenaar09037502018-09-25 22:08:14 +02002328 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002329 return;
2330
Bram Moolenaar09037502018-09-25 22:08:14 +02002331 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002332 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002333 return;
2334
2335 to->w_llist->qf_listcount = qi->qf_listcount;
2336
Bram Moolenaar09037502018-09-25 22:08:14 +02002337 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002338 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002339 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002340 to->w_llist->qf_curlist = idx;
2341
Bram Moolenaar0398e002019-03-21 21:12:49 +01002342 if (copy_loclist(qf_get_list(qi, idx),
2343 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002344 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002345 qf_free_all(to);
2346 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002347 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002348 }
2349
Bram Moolenaar09037502018-09-25 22:08:14 +02002350 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002351}
2352
2353/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002354 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002355 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 */
2357 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002358qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
Bram Moolenaar82404332016-07-10 17:00:38 +02002360 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002361 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002362 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002363
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002364 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
Bram Moolenaare60acc12011-05-10 16:41:25 +02002367#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002368 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002369#endif
2370#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002371 if (directory != NULL)
2372 slash_adjust(directory);
2373 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002374#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002375 if (directory != NULL && !vim_isAbsName(fname)
2376 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2377 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002378 // Here we check if the file really exists.
2379 // This should normally be true, but if make works without
2380 // "leaving directory"-messages we might have missed a
2381 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002382 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002385 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002386 if (directory)
2387 ptr = concat_fnames(directory, fname, TRUE);
2388 else
2389 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002391 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002392 bufname = ptr;
2393 }
2394 else
2395 bufname = fname;
2396
2397 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002398 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002399 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002400 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002401 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002403 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002404 {
2405 vim_free(qf_last_bufname);
2406 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2407 if (bufname == ptr)
2408 qf_last_bufname = bufname;
2409 else
2410 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002411 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002412 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002413 if (buf == NULL)
2414 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002415
Bram Moolenaarc1542742016-07-20 21:44:37 +02002416 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002417 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002418 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419}
2420
2421/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002422 * Push dirbuf onto the directory stack and return pointer to actual dir or
2423 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 */
2425 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002426qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
2428 struct dir_stack_T *ds_new;
2429 struct dir_stack_T *ds_ptr;
2430
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002431 // allocate new stack element and hook it in
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002432 ds_new = ALLOC_ONE(struct dir_stack_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 if (ds_new == NULL)
2434 return NULL;
2435
2436 ds_new->next = *stackptr;
2437 *stackptr = ds_new;
2438
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002439 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 if (vim_isAbsName(dirbuf)
2441 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002442 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 (*stackptr)->dirname = vim_strsave(dirbuf);
2444 else
2445 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002446 // Okay we don't have an absolute path.
2447 // dirbuf must be a subdir of one of the directories on the stack.
2448 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 ds_new = (*stackptr)->next;
2450 (*stackptr)->dirname = NULL;
2451 while (ds_new)
2452 {
2453 vim_free((*stackptr)->dirname);
2454 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2455 TRUE);
2456 if (mch_isdir((*stackptr)->dirname) == TRUE)
2457 break;
2458
2459 ds_new = ds_new->next;
2460 }
2461
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002462 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 while ((*stackptr)->next != ds_new)
2464 {
2465 ds_ptr = (*stackptr)->next;
2466 (*stackptr)->next = (*stackptr)->next->next;
2467 vim_free(ds_ptr->dirname);
2468 vim_free(ds_ptr);
2469 }
2470
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002471 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 if (ds_new == NULL)
2473 {
2474 vim_free((*stackptr)->dirname);
2475 (*stackptr)->dirname = vim_strsave(dirbuf);
2476 }
2477 }
2478
2479 if ((*stackptr)->dirname != NULL)
2480 return (*stackptr)->dirname;
2481 else
2482 {
2483 ds_ptr = *stackptr;
2484 *stackptr = (*stackptr)->next;
2485 vim_free(ds_ptr);
2486 return NULL;
2487 }
2488}
2489
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490/*
2491 * pop dirbuf from the directory stack and return previous directory or NULL if
2492 * stack is empty
2493 */
2494 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002495qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 struct dir_stack_T *ds_ptr;
2498
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002499 // TODO: Should we check if dirbuf is the directory on top of the stack?
2500 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002502 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 if (*stackptr != NULL)
2504 {
2505 ds_ptr = *stackptr;
2506 *stackptr = (*stackptr)->next;
2507 vim_free(ds_ptr->dirname);
2508 vim_free(ds_ptr);
2509 }
2510
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002511 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 return *stackptr ? (*stackptr)->dirname : NULL;
2513}
2514
2515/*
2516 * clean up directory stack
2517 */
2518 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002519qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
2521 struct dir_stack_T *ds_ptr;
2522
2523 while ((ds_ptr = *stackptr) != NULL)
2524 {
2525 *stackptr = (*stackptr)->next;
2526 vim_free(ds_ptr->dirname);
2527 vim_free(ds_ptr);
2528 }
2529}
2530
2531/*
2532 * Check in which directory of the directory stack the given file can be
2533 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002534 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 * Cleans up intermediate directory entries.
2536 *
2537 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002538 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 * ./
2540 * ./aa
2541 * ./aa/bb
2542 * ./bb
2543 * ./bb/x.c
2544 * and make says:
2545 * making all in aa
2546 * making all in bb
2547 * x.c:9: Error
2548 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2549 * qf_guess_filepath will return NULL.
2550 */
2551 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002552qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553{
2554 struct dir_stack_T *ds_ptr;
2555 struct dir_stack_T *ds_tmp;
2556 char_u *fullname;
2557
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002558 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002559 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 return NULL;
2561
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002562 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 fullname = NULL;
2564 while (ds_ptr)
2565 {
2566 vim_free(fullname);
2567 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2568
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002569 // If concat_fnames failed, just go on. The worst thing that can happen
2570 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2572 break;
2573
2574 ds_ptr = ds_ptr->next;
2575 }
2576
2577 vim_free(fullname);
2578
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002579 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002580 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002582 ds_tmp = qfl->qf_dir_stack->next;
2583 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 vim_free(ds_tmp->dirname);
2585 vim_free(ds_tmp);
2586 }
2587
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002588 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589}
2590
2591/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002592 * Returns TRUE if a quickfix/location list with the given identifier exists.
2593 */
2594 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002595qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002596{
2597 qf_info_T *qi = &ql_info;
2598 int i;
2599
2600 if (wp != NULL)
2601 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002602 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002603 if (qi == NULL)
2604 return FALSE;
2605 }
2606
2607 for (i = 0; i < qi->qf_listcount; ++i)
2608 if (qi->qf_lists[i].qf_id == qf_id)
2609 return TRUE;
2610
2611 return FALSE;
2612}
2613
2614/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002615 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002616 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002617 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002618 * Similar to location list.
2619 */
2620 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002621is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002622{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002623 qfline_T *qfp;
2624 int i;
2625
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002626 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002627 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2628 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002629 break;
2630
Bram Moolenaar95946f12019-03-31 15:31:59 +02002631 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002632 return FALSE;
2633
2634 return TRUE;
2635}
2636
2637/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002638 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002639 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002640 */
2641 static qfline_T *
2642get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002643 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002644 qfline_T *qf_ptr,
2645 int *qf_index,
2646 int dir)
2647{
2648 int idx;
2649 int old_qf_fnum;
2650
2651 idx = *qf_index;
2652 old_qf_fnum = qf_ptr->qf_fnum;
2653
2654 do
2655 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002656 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002657 return NULL;
2658 ++idx;
2659 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002660 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002661 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2662
2663 *qf_index = idx;
2664 return qf_ptr;
2665}
2666
2667/*
2668 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002669 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002670 */
2671 static qfline_T *
2672get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002673 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002674 qfline_T *qf_ptr,
2675 int *qf_index,
2676 int dir)
2677{
2678 int idx;
2679 int old_qf_fnum;
2680
2681 idx = *qf_index;
2682 old_qf_fnum = qf_ptr->qf_fnum;
2683
2684 do
2685 {
2686 if (idx == 1 || qf_ptr->qf_prev == NULL)
2687 return NULL;
2688 --idx;
2689 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002690 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002691 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2692
2693 *qf_index = idx;
2694 return qf_ptr;
2695}
2696
2697/*
2698 * Get the n'th (errornr) previous/next valid entry from the current entry in
2699 * the quickfix list.
2700 * dir == FORWARD or FORWARD_FILE: next valid entry
2701 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2702 */
2703 static qfline_T *
2704get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002705 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002706 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002707 int dir,
2708 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002709{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002710 qfline_T *qf_ptr = qfl->qf_ptr;
2711 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002712 qfline_T *prev_qf_ptr;
2713 int prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002714 char_u *err = e_no_more_items;
2715
2716 while (errornr--)
2717 {
2718 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002719 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002720
2721 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002722 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002723 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002724 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002725 if (qf_ptr == NULL)
2726 {
2727 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002728 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002729 if (err != NULL)
2730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002731 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002732 return NULL;
2733 }
2734 break;
2735 }
2736
2737 err = NULL;
2738 }
2739
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002740 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002741 return qf_ptr;
2742}
2743
2744/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002745 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2746 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002747 */
2748 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002749get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002750{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002751 qfline_T *qf_ptr = qfl->qf_ptr;
2752 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002753
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002754 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002755 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2756 {
2757 --qf_idx;
2758 qf_ptr = qf_ptr->qf_prev;
2759 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002760 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002761 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2762 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002763 {
2764 ++qf_idx;
2765 qf_ptr = qf_ptr->qf_next;
2766 }
2767
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002768 *new_qfidx = qf_idx;
2769 return qf_ptr;
2770}
2771
2772/*
2773 * Get a entry specied by 'errornr' and 'dir' from the current
2774 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2775 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2776 * Returns a pointer to the entry and the index of the new entry is stored in
2777 * 'new_qfidx'.
2778 */
2779 static qfline_T *
2780qf_get_entry(
2781 qf_list_T *qfl,
2782 int errornr,
2783 int dir,
2784 int *new_qfidx)
2785{
2786 qfline_T *qf_ptr = qfl->qf_ptr;
2787 int qfidx = qfl->qf_index;
2788
2789 if (dir != 0) // next/prev valid entry
2790 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2791 else if (errornr != 0) // go to specified number
2792 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2793
2794 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002795 return qf_ptr;
2796}
2797
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002798/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002799 * Find a window displaying a Vim help file.
2800 */
2801 static win_T *
2802qf_find_help_win(void)
2803{
2804 win_T *wp;
2805
2806 FOR_ALL_WINDOWS(wp)
2807 if (bt_help(wp->w_buffer))
2808 return wp;
2809
2810 return NULL;
2811}
2812
2813/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002814 * Set the location list for the specified window to 'qi'.
2815 */
2816 static void
2817win_set_loclist(win_T *wp, qf_info_T *qi)
2818{
2819 wp->w_llist = qi;
2820 qi->qf_refcount++;
2821}
2822
2823/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002824 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2825 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002826 */
2827 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002828jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002829{
2830 win_T *wp;
2831 int flags;
2832
Bram Moolenaarb2443732018-11-11 22:50:27 +01002833 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002834 wp = NULL;
2835 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002836 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002837 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2838 win_enter(wp, TRUE);
2839 else
2840 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002841 // Split off help window; put it at far top if no position
2842 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002843 flags = WSP_HELP;
2844 if (cmdmod.split == 0 && curwin->w_width != Columns
2845 && curwin->w_width < 80)
2846 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002847 // If the user asks to open a new window, then copy the location list.
2848 // Otherwise, don't copy the location list.
2849 if (IS_LL_STACK(qi) && !newwin)
2850 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002851
2852 if (win_split(0, flags) == FAIL)
2853 return FAIL;
2854
2855 *opened_window = TRUE;
2856
2857 if (curwin->w_height < p_hh)
2858 win_setheight((int)p_hh);
2859
Bram Moolenaarb2443732018-11-11 22:50:27 +01002860 // When using location list, the new window should use the supplied
2861 // location list. If the user asks to open a new window, then the new
2862 // window will get a copy of the location list.
2863 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002864 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002865 }
2866
2867 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002868 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002869
2870 return OK;
2871}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002872
2873/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002874 * Find a non-quickfix window in the current tabpage using the given location
2875 * list stack.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002876 * Returns NULL if a matching window is not found.
2877 */
2878 static win_T *
2879qf_find_win_with_loclist(qf_info_T *ll)
2880{
2881 win_T *wp;
2882
2883 FOR_ALL_WINDOWS(wp)
2884 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2885 return wp;
2886
2887 return NULL;
2888}
2889
2890/*
2891 * Find a window containing a normal buffer
2892 */
2893 static win_T *
2894qf_find_win_with_normal_buf(void)
2895{
2896 win_T *wp;
2897
2898 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002899 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002900 return wp;
2901
2902 return NULL;
2903}
2904
2905/*
2906 * Go to a window in any tabpage containing the specified file. Returns TRUE
2907 * if successfully jumped to the window. Otherwise returns FALSE.
2908 */
2909 static int
2910qf_goto_tabwin_with_file(int fnum)
2911{
2912 tabpage_T *tp;
2913 win_T *wp;
2914
2915 FOR_ALL_TAB_WINDOWS(tp, wp)
2916 if (wp->w_buffer->b_fnum == fnum)
2917 {
2918 goto_tabpage_win(tp, wp);
2919 return TRUE;
2920 }
2921
2922 return FALSE;
2923}
2924
2925/*
2926 * Create a new window to show a file above the quickfix window. Called when
2927 * only the quickfix window is present.
2928 */
2929 static int
2930qf_open_new_file_win(qf_info_T *ll_ref)
2931{
2932 int flags;
2933
2934 flags = WSP_ABOVE;
2935 if (ll_ref != NULL)
2936 flags |= WSP_NEWLOC;
2937 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002938 return FAIL; // not enough room for window
2939 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002940 swb_flags = 0;
2941 RESET_BINDING(curwin);
2942 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002943 // The new window should use the location list from the
2944 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002945 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002946 return OK;
2947}
2948
2949/*
2950 * Go to a window that shows the right buffer. If the window is not found, go
2951 * to the window just above the location list window. This is used for opening
2952 * a file from a location window and not from a quickfix window. If some usable
2953 * window is previously found, then it is supplied in 'use_win'.
2954 */
2955 static void
2956qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2957{
2958 win_T *win = use_win;
2959
2960 if (win == NULL)
2961 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002962 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002963 FOR_ALL_WINDOWS(win)
2964 if (win->w_buffer->b_fnum == qf_fnum)
2965 break;
2966 if (win == NULL)
2967 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002968 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002969 win = curwin;
2970 do
2971 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002972 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002973 break;
2974 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002975 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002976 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002977 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002978 } while (win != curwin);
2979 }
2980 }
2981 win_goto(win);
2982
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002983 // If the location list for the window is not set, then set it
2984 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01002985 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002986 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002987}
2988
2989/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002990 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2991 * not found, then go to the window just above the quickfix window. This is
2992 * used for opening a file from a quickfix window and not from a location
2993 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002994 */
2995 static void
2996qf_goto_win_with_qfl_file(int qf_fnum)
2997{
2998 win_T *win;
2999 win_T *altwin;
3000
3001 win = curwin;
3002 altwin = NULL;
3003 for (;;)
3004 {
3005 if (win->w_buffer->b_fnum == qf_fnum)
3006 break;
3007 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003008 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003009 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003010 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003011
3012 if (IS_QF_WINDOW(win))
3013 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003014 // Didn't find it, go to the window before the quickfix
3015 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003016 if (altwin != NULL)
3017 win = altwin;
3018 else if (curwin->w_prev != NULL)
3019 win = curwin->w_prev;
3020 else
3021 win = curwin->w_next;
3022 break;
3023 }
3024
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003025 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003026 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003027 altwin = win;
3028 }
3029
3030 win_goto(win);
3031}
3032
3033/*
3034 * Find a suitable window for opening a file (qf_fnum) from the
3035 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003036 * window, jump to it. Otherwise open a new window to display the file. If
3037 * 'newwin' is TRUE, then always open a new window. This is called from either
3038 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003039 */
3040 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003041qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003042{
3043 win_T *usable_win_ptr = NULL;
3044 int usable_win;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003045 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003046 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003047
3048 usable_win = 0;
3049
Bram Moolenaarb2443732018-11-11 22:50:27 +01003050 // If opening a new window, then don't use the location list referred by
3051 // the current window. Otherwise two windows will refer to the same
3052 // location list.
3053 if (!newwin)
3054 ll_ref = curwin->w_llist_ref;
3055
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003056 if (ll_ref != NULL)
3057 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003058 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003059 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
3060 if (usable_win_ptr != NULL)
3061 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003062 }
3063
3064 if (!usable_win)
3065 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003066 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003067 win = qf_find_win_with_normal_buf();
3068 if (win != NULL)
3069 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003070 }
3071
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003072 // If no usable window is found and 'switchbuf' contains "usetab"
3073 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003074 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003075 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003076
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003077 // If there is only one window and it is the quickfix window, create a
3078 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003079 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003080 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003081 if (qf_open_new_file_win(ll_ref) != OK)
3082 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003083 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003084 }
3085 else
3086 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003087 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003088 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003089 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003090 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003091 }
3092
3093 return OK;
3094}
3095
3096/*
3097 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003098 * Returns OK if successfully edited the file, FAIL on failing to open the
3099 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3100 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003101 */
3102 static int
3103qf_jump_edit_buffer(
3104 qf_info_T *qi,
3105 qfline_T *qf_ptr,
3106 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003107 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003108 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003109{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003110 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003111 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003112 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003113 int old_qf_curlist = qi->qf_curlist;
3114 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003115
3116 if (qf_ptr->qf_type == 1)
3117 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003118 // Open help file (do_ecmd() will set b_help flag, readfile() will
3119 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003120 if (!can_abandon(curbuf, forceit))
3121 {
3122 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003123 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003124 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003125
3126 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3127 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003128 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003129 }
3130 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003131 retval = buflist_getfile(qf_ptr->qf_fnum,
3132 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003133
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003134 // If a location list, check whether the associated window is still
3135 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003136 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003137 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003138 win_T *wp = win_id2wp(prev_winid);
3139 if (wp == NULL && curwin->w_llist != qi)
3140 {
3141 emsg(_("E924: Current window was closed"));
3142 *opened_window = FALSE;
3143 return NOTDONE;
3144 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003145 }
3146
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003147 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003149 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003150 return NOTDONE;
3151 }
3152
3153 if (old_qf_curlist != qi->qf_curlist
3154 || !is_qf_entry_present(qfl, qf_ptr))
3155 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003156 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003157 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003158 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003159 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003160 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003161 }
3162
3163 return retval;
3164}
3165
3166/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003167 * Go to the error line in the current file using either line/column number or
3168 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003169 */
3170 static void
3171qf_jump_goto_line(
3172 linenr_T qf_lnum,
3173 int qf_col,
3174 char_u qf_viscol,
3175 char_u *qf_pattern)
3176{
3177 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003178
3179 if (qf_pattern == NULL)
3180 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003181 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003182 i = qf_lnum;
3183 if (i > 0)
3184 {
3185 if (i > curbuf->b_ml.ml_line_count)
3186 i = curbuf->b_ml.ml_line_count;
3187 curwin->w_cursor.lnum = i;
3188 }
3189 if (qf_col > 0)
3190 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003191 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003192 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003193 coladvance(qf_col - 1);
3194 else
3195 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003196 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003197 check_cursor();
3198 }
3199 else
3200 beginline(BL_WHITE | BL_FIX);
3201 }
3202 else
3203 {
3204 pos_T save_cursor;
3205
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003206 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003207 save_cursor = curwin->w_cursor;
3208 curwin->w_cursor.lnum = 0;
3209 if (!do_search(NULL, '/', qf_pattern, (long)1,
3210 SEARCH_KEEP, NULL, NULL))
3211 curwin->w_cursor = save_cursor;
3212 }
3213}
3214
3215/*
3216 * Display quickfix list index and size message
3217 */
3218 static void
3219qf_jump_print_msg(
3220 qf_info_T *qi,
3221 int qf_index,
3222 qfline_T *qf_ptr,
3223 buf_T *old_curbuf,
3224 linenr_T old_lnum)
3225{
3226 linenr_T i;
3227 int len;
3228
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003229 // Update the screen before showing the message, unless the screen
3230 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003231 if (!msg_scrolled)
3232 update_topline_redraw();
3233 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003234 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003235 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3236 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003237 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003238 len = (int)STRLEN(IObuff);
3239 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3240
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003241 // Output the message. Overwrite to avoid scrolling when the 'O'
3242 // flag is present in 'shortmess'; But when not jumping, print the
3243 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003244 i = msg_scroll;
3245 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3246 msg_scroll = TRUE;
3247 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3248 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003249 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003250 msg_scroll = i;
3251}
3252
3253/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003254 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003255 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3256 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003257 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3258 * able to jump/open a window. Returns NOTDONE if a file is not associated
3259 * with the entry.
3260 */
3261 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003262qf_jump_open_window(
3263 qf_info_T *qi,
3264 qfline_T *qf_ptr,
3265 int newwin,
3266 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003267{
3268 // For ":helpgrep" find a help window or open one.
3269 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003270 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003271 return FAIL;
3272
3273 // If currently in the quickfix window, find another window to show the
3274 // file in.
3275 if (bt_quickfix(curbuf) && !*opened_window)
3276 {
3277 // If there is no file specified, we don't know where to go.
3278 // But do advance, otherwise ":cn" gets stuck.
3279 if (qf_ptr->qf_fnum == 0)
3280 return NOTDONE;
3281
Bram Moolenaarb2443732018-11-11 22:50:27 +01003282 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3283 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003284 return FAIL;
3285 }
3286
3287 return OK;
3288}
3289
3290/*
3291 * Edit a selected file from the quickfix/location list and jump to a
3292 * particular line/column, adjust the folds and display a message about the
3293 * jump.
3294 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3295 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3296 * the file.
3297 */
3298 static int
3299qf_jump_to_buffer(
3300 qf_info_T *qi,
3301 int qf_index,
3302 qfline_T *qf_ptr,
3303 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003304 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003305 int *opened_window,
3306 int openfold,
3307 int print_message)
3308{
3309 buf_T *old_curbuf;
3310 linenr_T old_lnum;
3311 int retval = OK;
3312
3313 // If there is a file name, read the wanted file if needed, and check
3314 // autowrite etc.
3315 old_curbuf = curbuf;
3316 old_lnum = curwin->w_cursor.lnum;
3317
3318 if (qf_ptr->qf_fnum != 0)
3319 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003320 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003321 opened_window);
3322 if (retval != OK)
3323 return retval;
3324 }
3325
3326 // When not switched to another buffer, still need to set pc mark
3327 if (curbuf == old_curbuf)
3328 setpcmark();
3329
3330 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3331 qf_ptr->qf_pattern);
3332
3333#ifdef FEAT_FOLDING
3334 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3335 foldOpenCursor();
3336#endif
3337 if (print_message)
3338 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3339
3340 return retval;
3341}
3342
3343/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003344 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 */
3346 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003347qf_jump(qf_info_T *qi,
3348 int dir,
3349 int errornr,
3350 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003352 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3353}
3354
3355/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003356 * Jump to a quickfix line.
3357 * If dir == 0 go to entry "errornr".
3358 * If dir == FORWARD go "errornr" valid entries forward.
3359 * If dir == BACKWARD go "errornr" valid entries backward.
3360 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3361 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3362 * else if "errornr" is zero, redisplay the same line
3363 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003364 * If 'newwin' is TRUE, then open the file in a new window.
3365 */
3366 void
3367qf_jump_newwin(qf_info_T *qi,
3368 int dir,
3369 int errornr,
3370 int forceit,
3371 int newwin)
3372{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003373 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003374 qfline_T *qf_ptr;
3375 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003378 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003379 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003380 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003384 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003386 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003388 if (qi == NULL)
3389 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003390
Bram Moolenaar0398e002019-03-21 21:12:49 +01003391 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003393 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 return;
3395 }
3396
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003397 incr_quickfix_busy();
3398
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003399 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003400
3401 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003403 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003405
3406 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3407 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003409 qf_ptr = old_qf_ptr;
3410 qf_index = old_qf_index;
3411 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003414 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003415 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003416 // No need to print the error message if it's visible in the error
3417 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 print_message = FALSE;
3419
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003420 prev_winid = curwin->w_id;
3421
Bram Moolenaarb2443732018-11-11 22:50:27 +01003422 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003423 if (retval == FAIL)
3424 goto failed;
3425 if (retval == NOTDONE)
3426 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003427
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003428 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003429 &opened_window, old_KeyTyped, print_message);
3430 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003432 // Quickfix/location list is freed by an autocmd
3433 qi = NULL;
3434 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003437 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003440 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003441 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003443 // Couldn't open file, so put index back where it was. This could
3444 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 qf_ptr = old_qf_ptr;
3447 qf_index = old_qf_index;
3448 }
3449 }
3450theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003451 if (qi != NULL)
3452 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003453 qfl->qf_ptr = qf_ptr;
3454 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003455 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003456 if (p_swb != old_swb)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003458 // Restore old 'switchbuf' value, but not when an autocommand or
3459 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003461 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003463 swb_flags = old_swb_flags;
3464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 else
3466 free_string_option(old_swb);
3467 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003468 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469}
3470
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003471// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003472static int qfFileAttr;
3473static int qfSepAttr;
3474static int qfLineAttr;
3475
3476/*
3477 * Display information about a single entry from the quickfix/location list.
3478 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003479 * 'cursel' will be set to TRUE for the currently selected entry in the
3480 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003481 */
3482 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003483qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003484{
3485 char_u *fname;
3486 buf_T *buf;
3487 int filter_entry;
3488
3489 fname = NULL;
3490 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3491 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3492 (char *)qfp->qf_module);
3493 else {
3494 if (qfp->qf_fnum != 0
3495 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3496 {
3497 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003498 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003499 fname = gettail(fname);
3500 }
3501 if (fname == NULL)
3502 sprintf((char *)IObuff, "%2d", qf_idx);
3503 else
3504 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3505 qf_idx, (char *)fname);
3506 }
3507
3508 // Support for filtering entries using :filter /pat/ clist
3509 // Match against the module name, file name, search pattern and
3510 // text of the entry.
3511 filter_entry = TRUE;
3512 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3513 filter_entry &= message_filtered(qfp->qf_module);
3514 if (filter_entry && fname != NULL)
3515 filter_entry &= message_filtered(fname);
3516 if (filter_entry && qfp->qf_pattern != NULL)
3517 filter_entry &= message_filtered(qfp->qf_pattern);
3518 if (filter_entry)
3519 filter_entry &= message_filtered(qfp->qf_text);
3520 if (filter_entry)
3521 return;
3522
3523 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003524 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003525
3526 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003527 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003528 if (qfp->qf_lnum == 0)
3529 IObuff[0] = NUL;
3530 else if (qfp->qf_col == 0)
3531 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3532 else
3533 sprintf((char *)IObuff, "%ld col %d",
3534 qfp->qf_lnum, qfp->qf_col);
3535 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3536 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003537 msg_puts_attr((char *)IObuff, qfLineAttr);
3538 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003539 if (qfp->qf_pattern != NULL)
3540 {
3541 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003542 msg_puts((char *)IObuff);
3543 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003544 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003545 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003546
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003547 // Remove newlines and leading whitespace from the text. For an
3548 // unrecognized line keep the indent, the compiler may mark a word
3549 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003550 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3551 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3552 IObuff, IOSIZE);
3553 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003554 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003555}
3556
3557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003559 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 */
3561 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003562qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003564 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003565 qfline_T *qfp;
3566 int i;
3567 int idx1 = 1;
3568 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003569 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003570 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003571 int all = eap->forceit; // if not :cl!, only show
3572 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003573 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003575 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3576 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003577
Bram Moolenaar0398e002019-03-21 21:12:49 +01003578 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003580 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 return;
3582 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003583 if (*arg == '+')
3584 {
3585 ++arg;
3586 plus = TRUE;
3587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003590 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 return;
3592 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003593 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003594 if (plus)
3595 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003596 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003597 idx2 = i + idx1;
3598 idx1 = i;
3599 }
3600 else
3601 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003602 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003603 if (idx1 < 0)
3604 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3605 if (idx2 < 0)
3606 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003609 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003610 shorten_fnames(FALSE);
3611
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003612 // Get the attributes for the different quickfix highlight items. Note
3613 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003614 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3615 if (qfFileAttr == 0)
3616 qfFileAttr = HL_ATTR(HLF_D);
3617 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3618 if (qfSepAttr == 0)
3619 qfSepAttr = HL_ATTR(HLF_D);
3620 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3621 if (qfLineAttr == 0)
3622 qfLineAttr = HL_ATTR(HLF_N);
3623
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003624 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003626 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
3628 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003629 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003630
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 ui_breakcheck();
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633}
3634
3635/*
3636 * Remove newlines and leading whitespace from an error message.
3637 * Put the result in "buf[bufsize]".
3638 */
3639 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003640qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
3642 int i;
3643 char_u *p = text;
3644
3645 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3646 {
3647 if (*p == '\n')
3648 {
3649 buf[i] = ' ';
3650 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003651 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 break;
3653 }
3654 else
3655 buf[i] = *p++;
3656 }
3657 buf[i] = NUL;
3658}
3659
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003660/*
3661 * Display information (list number, list size and the title) about a
3662 * quickfix/location list.
3663 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003664 static void
3665qf_msg(qf_info_T *qi, int which, char *lead)
3666{
3667 char *title = (char *)qi->qf_lists[which].qf_title;
3668 int count = qi->qf_lists[which].qf_count;
3669 char_u buf[IOSIZE];
3670
3671 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3672 lead,
3673 which + 1,
3674 qi->qf_listcount,
3675 count);
3676
3677 if (title != NULL)
3678 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003679 size_t len = STRLEN(buf);
3680
3681 if (len < 34)
3682 {
3683 vim_memset(buf + len, ' ', 34 - len);
3684 buf[34] = NUL;
3685 }
3686 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003687 }
3688 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003689 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003690}
3691
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692/*
3693 * ":colder [count]": Up in the quickfix stack.
3694 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003695 * ":lolder [count]": Up in the location list stack.
3696 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 */
3698 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003699qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003701 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 int count;
3703
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003704 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3705 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 if (eap->addr_count != 0)
3708 count = eap->line2;
3709 else
3710 count = 1;
3711 while (count--)
3712 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003713 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003715 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003717 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003718 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003720 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722 else
3723 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003724 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003726 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003727 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003729 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
3731 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003732 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003733 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734}
3735
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003736/*
3737 * Display the information about all the quickfix/location lists in the stack
3738 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003739 void
3740qf_history(exarg_T *eap)
3741{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003742 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003743 int i;
3744
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003745 if (eap->addr_count > 0)
3746 {
3747 if (qi == NULL)
3748 {
3749 emsg(_(e_loclist));
3750 return;
3751 }
3752
3753 // Jump to the specified quickfix list
3754 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3755 {
3756 qi->qf_curlist = eap->line2 - 1;
3757 qf_msg(qi, qi->qf_curlist, "");
3758 qf_update_buffer(qi, NULL);
3759 }
3760 else
3761 emsg(_(e_invrange));
3762
3763 return;
3764 }
3765
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003766 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003767 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003768 else
3769 for (i = 0; i < qi->qf_listcount; ++i)
3770 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3771}
3772
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003774 * Free all the entries in the error list "idx". Note that other information
3775 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 */
3777 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003778qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003780 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003781 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003782 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003784 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003786 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003787 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003788 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003789 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003790 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003791 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003792 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003793 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003794 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003795 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003796 // Somehow qf_count may have an incorrect value, set it to 1
3797 // to avoid crashing when it's wrong.
3798 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003799 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003800 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003801 qfl->qf_start = qfpnext;
3802 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003804
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003805 qfl->qf_index = 0;
3806 qfl->qf_start = NULL;
3807 qfl->qf_last = NULL;
3808 qfl->qf_ptr = NULL;
3809 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003810
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003811 qf_clean_dir_stack(&qfl->qf_dir_stack);
3812 qfl->qf_directory = NULL;
3813 qf_clean_dir_stack(&qfl->qf_file_stack);
3814 qfl->qf_currfile = NULL;
3815 qfl->qf_multiline = FALSE;
3816 qfl->qf_multiignore = FALSE;
3817 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818}
3819
3820/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003821 * Free error list "idx". Frees all the entries in the quickfix list,
3822 * associated context information and the title.
3823 */
3824 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003825qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003826{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003827 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003828
Bram Moolenaard23a8232018-02-10 18:45:26 +01003829 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003830 free_tv(qfl->qf_ctx);
3831 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003832 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003833 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003834}
3835
3836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 * qf_mark_adjust: adjust marks
3838 */
3839 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003840qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003841 win_T *wp,
3842 linenr_T line1,
3843 linenr_T line2,
3844 long amount,
3845 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003847 int i;
3848 qfline_T *qfp;
3849 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003850 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003851 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003852 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853
Bram Moolenaarc1542742016-07-20 21:44:37 +02003854 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003855 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003856 if (wp != NULL)
3857 {
3858 if (wp->w_llist == NULL)
3859 return;
3860 qi = wp->w_llist;
3861 }
3862
3863 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003864 {
3865 qf_list_T *qfl = qf_get_list(qi, idx);
3866
3867 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003868 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (qfp->qf_fnum == curbuf->b_fnum)
3870 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003871 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3873 {
3874 if (amount == MAXLNUM)
3875 qfp->qf_cleared = TRUE;
3876 else
3877 qfp->qf_lnum += amount;
3878 }
3879 else if (amount_after && qfp->qf_lnum > line2)
3880 qfp->qf_lnum += amount_after;
3881 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003882 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003883
3884 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003885 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886}
3887
3888/*
3889 * Make a nice message out of the error character and the error number:
3890 * char number message
3891 * e or E 0 " error"
3892 * w or W 0 " warning"
3893 * i or I 0 " info"
3894 * 0 0 ""
3895 * other 0 " c"
3896 * e or E n " error n"
3897 * w or W n " warning n"
3898 * i or I n " info n"
3899 * 0 n " error n"
3900 * other n " c n"
3901 * 1 x "" :helpgrep
3902 */
3903 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003904qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905{
3906 static char_u buf[20];
3907 static char_u cc[3];
3908 char_u *p;
3909
3910 if (c == 'W' || c == 'w')
3911 p = (char_u *)" warning";
3912 else if (c == 'I' || c == 'i')
3913 p = (char_u *)" info";
3914 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3915 p = (char_u *)" error";
3916 else if (c == 0 || c == 1)
3917 p = (char_u *)"";
3918 else
3919 {
3920 cc[0] = ' ';
3921 cc[1] = c;
3922 cc[2] = NUL;
3923 p = cc;
3924 }
3925
3926 if (nr <= 0)
3927 return p;
3928
3929 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3930 return buf;
3931}
3932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003934 * When "split" is FALSE: Open the entry/result under the cursor.
3935 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3936 */
3937 void
3938qf_view_result(int split)
3939{
3940 qf_info_T *qi = &ql_info;
3941
3942 if (!bt_quickfix(curbuf))
3943 return;
3944
3945 if (IS_LL_WINDOW(curwin))
3946 qi = GET_LOC_LIST(curwin);
3947
Bram Moolenaar0398e002019-03-21 21:12:49 +01003948 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003949 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003950 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003951 return;
3952 }
3953
3954 if (split)
3955 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003956 // Open the selected entry in a new window
3957 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3958 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003959 return;
3960 }
3961
3962 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3963}
3964
3965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 * ":cwindow": open the quickfix window if we have errors to display,
3967 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003968 * ":lwindow": open the location list window if we have locations to display,
3969 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 */
3971 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003972ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003974 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003975 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 win_T *win;
3977
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003978 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3979 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003980
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003981 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003982
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003983 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003984 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003986 // If a quickfix window is open but we have no errors to display,
3987 // close the window. If a quickfix window is not open, then open
3988 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003989 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003990 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01003991 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 {
3993 if (win != NULL)
3994 ex_cclose(eap);
3995 }
3996 else if (win == NULL)
3997 ex_copen(eap);
3998}
3999
4000/*
4001 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004002 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004005ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004007 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004008 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004010 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4011 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004013 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004014 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 if (win != NULL)
4016 win_close(win, FALSE);
4017}
4018
4019/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004020 * Set "w:quickfix_title" if "qi" has a title.
4021 */
4022 static void
4023qf_set_title_var(qf_list_T *qfl)
4024{
4025 if (qfl->qf_title != NULL)
4026 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4027}
4028
4029/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004030 * Goto a quickfix or location list window (if present).
4031 * Returns OK if the window is found, FAIL otherwise.
4032 */
4033 static int
4034qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4035{
4036 win_T *win;
4037
4038 win = qf_find_win(qi);
4039 if (win == NULL)
4040 return FAIL;
4041
4042 win_goto(win);
4043 if (resize)
4044 {
4045 if (vertsplit)
4046 {
4047 if (sz != win->w_width)
4048 win_setwidth(sz);
4049 }
Bram Moolenaar36d50222019-05-02 20:17:40 +02004050 else if (sz != win->w_height
4051 && win->w_height + win->w_status_height < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004052 win_setheight(sz);
4053 }
4054
4055 return OK;
4056}
4057
4058/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004059 * Set options for the buffer in the quickfix or location list window.
4060 */
4061 static void
4062qf_set_cwindow_options(void)
4063{
4064 // switch off 'swapfile'
4065 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4066 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4067 OPT_LOCAL);
4068 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4069 RESET_BINDING(curwin);
4070#ifdef FEAT_DIFF
4071 curwin->w_p_diff = FALSE;
4072#endif
4073#ifdef FEAT_FOLDING
4074 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4075 OPT_LOCAL);
4076#endif
4077}
4078
4079/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004080 * Open a new quickfix or location list window, load the quickfix buffer and
4081 * set the appropriate options for the window.
4082 * Returns FAIL if the window could not be opened.
4083 */
4084 static int
4085qf_open_new_cwindow(qf_info_T *qi, int height)
4086{
4087 buf_T *qf_buf;
4088 win_T *oldwin = curwin;
4089 tabpage_T *prevtab = curtab;
4090 int flags = 0;
4091 win_T *win;
4092
4093 qf_buf = qf_find_buf(qi);
4094
4095 // The current window becomes the previous window afterwards.
4096 win = curwin;
4097
4098 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4099 // Create the new quickfix window at the very bottom, except when
4100 // :belowright or :aboveleft is used.
4101 win_goto(lastwin);
4102 // Default is to open the window below the current window
4103 if (cmdmod.split == 0)
4104 flags = WSP_BELOW;
4105 flags |= WSP_NEWLOC;
4106 if (win_split(height, flags) == FAIL)
4107 return FAIL; // not enough room for window
4108 RESET_BINDING(curwin);
4109
4110 if (IS_LL_STACK(qi))
4111 {
4112 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004113 // location list stack from the window 'win'.
4114 curwin->w_llist_ref = qi;
4115 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004116 }
4117
4118 if (oldwin != curwin)
4119 oldwin = NULL; // don't store info when in another window
4120 if (qf_buf != NULL)
4121 {
4122 // Use the existing quickfix buffer
4123 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4124 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4125 }
4126 else
4127 {
4128 // Create a new quickfix buffer
4129 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4130
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004131 // save the number of the new buffer
4132 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004133 }
4134
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004135 // Set the options for the quickfix buffer/window (if not already done)
4136 // Do this even if the quickfix buffer was already present, as an autocmd
4137 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004138 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004139 qf_set_cwindow_options();
4140
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004141 // Only set the height when still in the same tab page and there is no
4142 // window to the side.
4143 if (curtab == prevtab && curwin->w_width == Columns)
4144 win_setheight(height);
4145 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4146 if (win_valid(win))
4147 prevwin = win;
4148
4149 return OK;
4150}
4151
4152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004154 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 */
4156 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004157ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004159 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004160 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004162 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004163 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004165 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4166 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004167
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004168 incr_quickfix_busy();
4169
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 if (eap->addr_count != 0)
4171 height = eap->line2;
4172 else
4173 height = QF_WINHEIGHT;
4174
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004175 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176#ifdef FEAT_GUI
4177 need_mouse_correct = TRUE;
4178#endif
4179
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004180 // Find an existing quickfix window, or open a new one.
4181 if (cmdmod.tab == 0)
4182 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4183 cmdmod.split & WSP_VERT);
4184 if (status == FAIL)
4185 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004186 {
4187 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004188 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004191 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004192 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004193 // Save the current index here, as updating the quickfix buffer may free
4194 // the quickfix list
4195 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004196
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004197 // Fill the buffer with the quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004198 qf_fill_buffer(qfl, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004200 decr_quickfix_busy();
4201
4202 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 curwin->w_cursor.col = 0;
4204 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004205 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206}
4207
4208/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004209 * Move the cursor in the quickfix window to "lnum".
4210 */
4211 static void
4212qf_win_goto(win_T *win, linenr_T lnum)
4213{
4214 win_T *old_curwin = curwin;
4215
4216 curwin = win;
4217 curbuf = win->w_buffer;
4218 curwin->w_cursor.lnum = lnum;
4219 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004220 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004221 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004222 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004223 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004224 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004225 curwin = old_curwin;
4226 curbuf = curwin->w_buffer;
4227}
4228
4229/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004230 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004231 */
4232 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004233ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004234{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004235 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004236 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004237
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004238 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4239 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004240
4241 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004242 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4243 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4244}
4245
4246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 * Return the number of the current entry (line number in the quickfix
4248 * window).
4249 */
4250 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004251qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004253 qf_info_T *qi = &ql_info;
4254
4255 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004256 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004257 qi = wp->w_llist_ref;
4258
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004259 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260}
4261
4262/*
4263 * Update the cursor position in the quickfix window to the current error.
4264 * Return TRUE if there is a quickfix window.
4265 */
4266 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004267qf_win_pos_update(
4268 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004269 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270{
4271 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004272 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004274 // Put the cursor on the current error in the quickfix window, so that
4275 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004276 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if (win != NULL
4278 && qf_index <= win->w_buffer->b_ml.ml_line_count
4279 && old_qf_index != qf_index)
4280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 if (qf_index > old_qf_index)
4282 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004283 win->w_redraw_top = old_qf_index;
4284 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 else
4287 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004288 win->w_redraw_top = qf_index;
4289 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004291 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 return win != NULL;
4294}
4295
4296/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004297 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004298 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004299 */
4300 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004301is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004302{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004303 // A window displaying the quickfix buffer will have the w_llist_ref field
4304 // set to NULL.
4305 // A window displaying a location list buffer will have the w_llist_ref
4306 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004307 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004308 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4309 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004310 return TRUE;
4311
4312 return FALSE;
4313}
4314
4315/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004316 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004317 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004318 */
4319 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004320qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004321{
4322 win_T *win;
4323
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004324 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004325 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004326 return win;
4327 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004328}
4329
4330/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004331 * Find a quickfix buffer.
4332 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 */
4334 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004335qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004337 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004338 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004340 if (qi->qf_bufnr != INVALID_QFBUFNR)
4341 {
4342 buf_T *qfbuf;
4343 qfbuf = buflist_findnr(qi->qf_bufnr);
4344 if (qfbuf != NULL)
4345 return qfbuf;
4346 // buffer is no longer present
4347 qi->qf_bufnr = INVALID_QFBUFNR;
4348 }
4349
Bram Moolenaar9c102382006-05-03 21:26:49 +00004350 FOR_ALL_TAB_WINDOWS(tp, win)
4351 if (is_qf_win(win, qi))
4352 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004353
Bram Moolenaar9c102382006-05-03 21:26:49 +00004354 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355}
4356
4357/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004358 * Update the w:quickfix_title variable in the quickfix/location list window
4359 */
4360 static void
4361qf_update_win_titlevar(qf_info_T *qi)
4362{
4363 win_T *win;
4364 win_T *curwin_save;
4365
4366 if ((win = qf_find_win(qi)) != NULL)
4367 {
4368 curwin_save = curwin;
4369 curwin = win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004370 qf_set_title_var(qf_get_curlist(qi));
Bram Moolenaard823fa92016-08-12 16:29:27 +02004371 curwin = curwin_save;
4372 }
4373}
4374
4375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 * Find the quickfix buffer. If it exists, update the contents.
4377 */
4378 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004379qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380{
4381 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004382 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004385 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004386 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 if (buf != NULL)
4388 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004389 linenr_T old_line_count = buf->b_ml.ml_line_count;
4390
4391 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004392 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004393 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394
Bram Moolenaard823fa92016-08-12 16:29:27 +02004395 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004396
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004397 qf_fill_buffer(qf_get_curlist(qi), buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004398 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004399
Bram Moolenaar864293a2016-06-02 13:40:04 +02004400 if (old_last == NULL)
4401 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004402 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004403
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004404 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004405 aucmd_restbuf(&aco);
4406 }
4407
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004408 // Only redraw when added lines are visible. This avoids flickering
4409 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004410 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4411 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413}
4414
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004415/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004416 * Add an error line to the quickfix buffer.
4417 */
4418 static int
4419qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4420{
4421 int len;
4422 buf_T *errbuf;
4423
4424 if (qfp->qf_module != NULL)
4425 {
4426 STRCPY(IObuff, qfp->qf_module);
4427 len = (int)STRLEN(IObuff);
4428 }
4429 else if (qfp->qf_fnum != 0
4430 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4431 && errbuf->b_fname != NULL)
4432 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004433 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004434 STRCPY(IObuff, gettail(errbuf->b_fname));
4435 else
4436 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004437 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004438 if (errbuf->b_sfname == NULL
4439 || mch_isFullName(errbuf->b_sfname))
4440 {
4441 if (*dirname == NUL)
4442 mch_dirname(dirname, MAXPATHL);
4443 shorten_buf_fname(errbuf, dirname, FALSE);
4444 }
4445 STRCPY(IObuff, errbuf->b_fname);
4446 }
4447 len = (int)STRLEN(IObuff);
4448 }
4449 else
4450 len = 0;
4451 IObuff[len++] = '|';
4452
4453 if (qfp->qf_lnum > 0)
4454 {
4455 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4456 len += (int)STRLEN(IObuff + len);
4457
4458 if (qfp->qf_col > 0)
4459 {
4460 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4461 len += (int)STRLEN(IObuff + len);
4462 }
4463
4464 sprintf((char *)IObuff + len, "%s",
4465 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4466 len += (int)STRLEN(IObuff + len);
4467 }
4468 else if (qfp->qf_pattern != NULL)
4469 {
4470 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4471 len += (int)STRLEN(IObuff + len);
4472 }
4473 IObuff[len++] = '|';
4474 IObuff[len++] = ' ';
4475
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004476 // Remove newlines and leading whitespace from the text.
4477 // For an unrecognized line keep the indent, the compiler may
4478 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004479 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4480 IObuff + len, IOSIZE - len);
4481
4482 if (ml_append_buf(buf, lnum, IObuff,
4483 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4484 return FAIL;
4485
4486 return OK;
4487}
4488
4489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 * Fill current buffer with quickfix errors, replacing any previous contents.
4491 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004492 * If "old_last" is not NULL append the items after this one.
4493 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4494 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 */
4496 static void
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004497qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004499 linenr_T lnum;
4500 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004501 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
Bram Moolenaar864293a2016-06-02 13:40:04 +02004503 if (old_last == NULL)
4504 {
4505 if (buf != curbuf)
4506 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004507 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004508 return;
4509 }
4510
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004511 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004512 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4513 (void)ml_delete((linenr_T)1, FALSE);
4514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004516 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004517 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004519 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004520
4521 *dirname = NUL;
4522
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004523 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004524 if (old_last == NULL)
4525 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004526 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004527 lnum = 0;
4528 }
4529 else
4530 {
4531 qfp = old_last->qf_next;
4532 lnum = buf->b_ml.ml_line_count;
4533 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004534 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004536 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004538
Bram Moolenaar864293a2016-06-02 13:40:04 +02004539 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004541 if (qfp == NULL)
4542 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004544
4545 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004546 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004547 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004550 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 check_lnums(TRUE);
4552
Bram Moolenaar864293a2016-06-02 13:40:04 +02004553 if (old_last == NULL)
4554 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004555 // Set the 'filetype' to "qf" each time after filling the buffer.
4556 // This resembles reading a file into a buffer, it's more logical when
4557 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004558 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004559 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4560 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004562 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004563 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004565 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004567 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004568 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004569
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004570 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004571 redraw_curbuf_later(NOT_VALID);
4572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004574 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 KeyTyped = old_KeyTyped;
4576}
4577
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004578/*
4579 * For every change made to the quickfix list, update the changed tick.
4580 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004581 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004582qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004583{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004584 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004585}
4586
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004588 * Return the quickfix/location list number with the given identifier.
4589 * Returns -1 if list is not found.
4590 */
4591 static int
4592qf_id2nr(qf_info_T *qi, int_u qfid)
4593{
4594 int qf_idx;
4595
4596 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4597 if (qi->qf_lists[qf_idx].qf_id == qfid)
4598 return qf_idx;
4599 return INVALID_QFIDX;
4600}
4601
4602/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004603 * If the current list is not "save_qfid" and we can find the list with that ID
4604 * then make it the current list.
4605 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004606 * Returns OK if successfully restored the list. Returns FAIL if the list with
4607 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004608 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004609 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004610qf_restore_list(qf_info_T *qi, int_u save_qfid)
4611{
4612 int curlist;
4613
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004614 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004615 {
4616 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004617 if (curlist < 0)
4618 // list is not present
4619 return FAIL;
4620 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004621 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004622 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004623}
4624
4625/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004626 * Jump to the first entry if there is one.
4627 */
4628 static void
4629qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4630{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004631 if (qf_restore_list(qi, save_qfid) == FAIL)
4632 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004633
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004634 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004635 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004636 qf_jump(qi, 0, 0, forceit);
4637}
4638
4639/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004640 * Return TRUE when using ":vimgrep" for ":grep".
4641 */
4642 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004643grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004644{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004645 return ((cmdidx == CMD_grep
4646 || cmdidx == CMD_lgrep
4647 || cmdidx == CMD_grepadd
4648 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004649 && STRCMP("internal",
4650 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4651}
4652
4653/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004654 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004656 static char_u *
4657make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004659 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004660 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004661 case CMD_make: return (char_u *)"make";
4662 case CMD_lmake: return (char_u *)"lmake";
4663 case CMD_grep: return (char_u *)"grep";
4664 case CMD_lgrep: return (char_u *)"lgrep";
4665 case CMD_grepadd: return (char_u *)"grepadd";
4666 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4667 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669}
4670
4671/*
4672 * Return the name for the errorfile, in allocated memory.
4673 * Find a new unique name when 'makeef' contains "##".
4674 * Returns NULL for error.
4675 */
4676 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004677get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678{
4679 char_u *p;
4680 char_u *name;
4681 static int start = -1;
4682 static int off = 0;
4683#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004684 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685#endif
4686
4687 if (*p_mef == NUL)
4688 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004689 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004691 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 return name;
4693 }
4694
4695 for (p = p_mef; *p; ++p)
4696 if (p[0] == '#' && p[1] == '#')
4697 break;
4698
4699 if (*p == NUL)
4700 return vim_strsave(p_mef);
4701
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004702 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 for (;;)
4704 {
4705 if (start == -1)
4706 start = mch_get_pid();
4707 else
4708 off += 19;
4709
Bram Moolenaar964b3742019-05-24 18:54:09 +02004710 name = alloc(STRLEN(p_mef) + 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 if (name == NULL)
4712 break;
4713 STRCPY(name, p_mef);
4714 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4715 STRCAT(name, p + 2);
4716 if (mch_getperm(name) < 0
4717#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004718 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 && mch_lstat((char *)name, &sb) < 0
4720#endif
4721 )
4722 break;
4723 vim_free(name);
4724 }
4725 return name;
4726}
4727
4728/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004729 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4730 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4731 */
4732 static char_u *
4733make_get_fullcmd(char_u *makecmd, char_u *fname)
4734{
4735 char_u *cmd;
4736 unsigned len;
4737
4738 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4739 if (*p_sp != NUL)
4740 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4741 cmd = alloc(len);
4742 if (cmd == NULL)
4743 return NULL;
4744 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4745 (char *)p_shq);
4746
4747 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4748 if (*p_sp != NUL)
4749 append_redir(cmd, len, p_sp, fname);
4750
4751 // Display the fully formed command. Output a newline if there's something
4752 // else than the :make command that was typed (in which case the cursor is
4753 // in column 0).
4754 if (msg_col == 0)
4755 msg_didout = FALSE;
4756 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004757 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004758 msg_outtrans(cmd); // show what we are doing
4759
4760 return cmd;
4761}
4762
4763/*
4764 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4765 */
4766 void
4767ex_make(exarg_T *eap)
4768{
4769 char_u *fname;
4770 char_u *cmd;
4771 char_u *enc = NULL;
4772 win_T *wp = NULL;
4773 qf_info_T *qi = &ql_info;
4774 int res;
4775 char_u *au_name = NULL;
4776 int_u save_qfid;
4777
4778 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4779 if (grep_internal(eap->cmdidx))
4780 {
4781 ex_vimgrep(eap);
4782 return;
4783 }
4784
4785 au_name = make_get_auname(eap->cmdidx);
4786 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4787 curbuf->b_fname, TRUE, curbuf))
4788 {
4789#ifdef FEAT_EVAL
4790 if (aborting())
4791 return;
4792#endif
4793 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004794 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004795
4796 if (is_loclist_cmd(eap->cmdidx))
4797 wp = curwin;
4798
4799 autowrite_all();
4800 fname = get_mef_name();
4801 if (fname == NULL)
4802 return;
4803 mch_remove(fname); // in case it's not unique
4804
4805 cmd = make_get_fullcmd(eap->arg, fname);
4806 if (cmd == NULL)
4807 return;
4808
4809 // let the shell know if we are redirecting output or not
4810 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4811
4812#ifdef AMIGA
4813 out_flush();
4814 // read window status report and redraw before message
4815 (void)char_avail();
4816#endif
4817
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004818 incr_quickfix_busy();
4819
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004820 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4821 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4822 (eap->cmdidx != CMD_grepadd
4823 && eap->cmdidx != CMD_lgrepadd),
4824 qf_cmdtitle(*eap->cmdlinep), enc);
4825 if (wp != NULL)
4826 {
4827 qi = GET_LOC_LIST(wp);
4828 if (qi == NULL)
4829 goto cleanup;
4830 }
4831 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004832 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004833
4834 // Remember the current quickfix list identifier, so that we can
4835 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004836 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004837 if (au_name != NULL)
4838 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4839 curbuf->b_fname, TRUE, curbuf);
4840 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4841 // display the first error
4842 qf_jump_first(qi, save_qfid, FALSE);
4843
4844cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004845 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004846 mch_remove(fname);
4847 vim_free(fname);
4848 vim_free(cmd);
4849}
4850
4851/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02004852 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004853 */
4854 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004855qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004856{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004857 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004858
4859 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4860 return 0;
4861 return qf_get_curlist(qi)->qf_count;
4862}
4863
4864/*
4865 * Returns the number of valid entries in the current quickfix/location list.
4866 */
4867 int
4868qf_get_valid_size(exarg_T *eap)
4869{
4870 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004871 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004872 qfline_T *qfp;
4873 int i, sz = 0;
4874 int prev_fnum = 0;
4875
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004876 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4877 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004878
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004879 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01004880 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004881 {
4882 if (qfp->qf_valid)
4883 {
4884 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004885 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004886 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4887 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004888 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004889 sz++;
4890 prev_fnum = qfp->qf_fnum;
4891 }
4892 }
4893 }
4894
4895 return sz;
4896}
4897
4898/*
4899 * Returns the current index of the quickfix/location list.
4900 * Returns 0 if there is an error.
4901 */
4902 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004903qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004904{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004905 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004906
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004907 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4908 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004909
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004910 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004911}
4912
4913/*
4914 * Returns the current index in the quickfix/location list (counting only valid
4915 * entries). If no valid entries are in the list, then returns 1.
4916 */
4917 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004918qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004919{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004920 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004921 qf_list_T *qfl;
4922 qfline_T *qfp;
4923 int i, eidx = 0;
4924 int prev_fnum = 0;
4925
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004926 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4927 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004928
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004929 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004930 qfp = qfl->qf_start;
4931
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004932 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004933 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004934 return 1;
4935
4936 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4937 {
4938 if (qfp->qf_valid)
4939 {
4940 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4941 {
4942 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4943 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004944 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004945 eidx++;
4946 prev_fnum = qfp->qf_fnum;
4947 }
4948 }
4949 else
4950 eidx++;
4951 }
4952 }
4953
4954 return eidx ? eidx : 1;
4955}
4956
4957/*
4958 * Get the 'n'th valid error entry in the quickfix or location list.
4959 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4960 * For :cdo and :ldo returns the 'n'th valid error entry.
4961 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4962 */
4963 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004964qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004965{
Bram Moolenaar95946f12019-03-31 15:31:59 +02004966 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004967 int i, eidx;
4968 int prev_fnum = 0;
4969
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004970 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004971 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004972 return 1;
4973
Bram Moolenaar95946f12019-03-31 15:31:59 +02004974 eidx = 0;
4975 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004976 {
4977 if (qfp->qf_valid)
4978 {
4979 if (fdo)
4980 {
4981 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4982 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004983 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004984 eidx++;
4985 prev_fnum = qfp->qf_fnum;
4986 }
4987 }
4988 else
4989 eidx++;
4990 }
4991
4992 if (eidx == n)
4993 break;
4994 }
4995
4996 if (i <= qfl->qf_count)
4997 return i;
4998 else
4999 return 1;
5000}
5001
5002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005004 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005005 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 */
5007 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005008ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005010 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005011 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005012
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005013 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5014 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005015
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005016 if (eap->addr_count > 0)
5017 errornr = (int)eap->line2;
5018 else
5019 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005020 switch (eap->cmdidx)
5021 {
5022 case CMD_cc: case CMD_ll:
5023 errornr = 0;
5024 break;
5025 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5026 case CMD_lfirst:
5027 errornr = 1;
5028 break;
5029 default:
5030 errornr = 32767;
5031 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005032 }
5033
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005034 // For cdo and ldo commands, jump to the nth valid error.
5035 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005036 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5037 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005038 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005039 eap->addr_count > 0 ? (int)eap->line1 : 1,
5040 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5041
5042 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043}
5044
5045/*
5046 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005047 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005048 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 */
5050 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005051ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005053 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005054 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005055 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005056
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005057 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5058 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005059
Bram Moolenaar55b69262017-08-13 13:42:01 +02005060 if (eap->addr_count > 0
5061 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5062 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005063 errornr = (int)eap->line2;
5064 else
5065 errornr = 1;
5066
Bram Moolenaar39665952018-08-15 20:59:48 +02005067 // Depending on the command jump to either next or previous entry/file.
5068 switch (eap->cmdidx)
5069 {
5070 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5071 dir = FORWARD;
5072 break;
5073 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5074 case CMD_lNext:
5075 dir = BACKWARD;
5076 break;
5077 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5078 dir = FORWARD_FILE;
5079 break;
5080 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5081 dir = BACKWARD_FILE;
5082 break;
5083 default:
5084 dir = FORWARD;
5085 break;
5086 }
5087
5088 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089}
5090
5091/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005092 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5093 * The index of the entry is stored in 'errornr'.
5094 * Returns NULL if an entry is not found.
5095 */
5096 static qfline_T *
5097qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5098{
5099 qfline_T *qfp = NULL;
5100 int idx = 0;
5101
5102 // Find the first entry in this file
5103 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5104 if (qfp->qf_fnum == bnr)
5105 break;
5106
5107 *errornr = idx;
5108 return qfp;
5109}
5110
5111/*
5112 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5113 * with the error number for the first entry. Assumes the entries are sorted in
5114 * the quickfix list by line number.
5115 */
5116 static qfline_T *
5117qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5118{
5119 while (!got_int
5120 && entry->qf_prev != NULL
5121 && entry->qf_fnum == entry->qf_prev->qf_fnum
5122 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5123 {
5124 entry = entry->qf_prev;
5125 --*errornr;
5126 }
5127
5128 return entry;
5129}
5130
5131/*
5132 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5133 * with the error number for the last entry. Assumes the entries are sorted in
5134 * the quickfix list by line number.
5135 */
5136 static qfline_T *
5137qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5138{
5139 while (!got_int &&
5140 entry->qf_next != NULL
5141 && entry->qf_fnum == entry->qf_next->qf_fnum
5142 && entry->qf_lnum == entry->qf_next->qf_lnum)
5143 {
5144 entry = entry->qf_next;
5145 ++*errornr;
5146 }
5147
5148 return entry;
5149}
5150
5151/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005152 * Returns TRUE if the specified quickfix entry is
5153 * after the given line (linewise is TRUE)
5154 * or after the line and column.
5155 */
5156 static int
5157qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5158{
5159 if (linewise)
5160 return qfp->qf_lnum > pos->lnum;
5161 else
5162 return (qfp->qf_lnum > pos->lnum ||
5163 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5164}
5165
5166/*
5167 * Returns TRUE if the specified quickfix entry is
5168 * before the given line (linewise is TRUE)
5169 * or before the line and column.
5170 */
5171 static int
5172qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5173{
5174 if (linewise)
5175 return qfp->qf_lnum < pos->lnum;
5176 else
5177 return (qfp->qf_lnum < pos->lnum ||
5178 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5179}
5180
5181/*
5182 * Returns TRUE if the specified quickfix entry is
5183 * on or after the given line (linewise is TRUE)
5184 * or on or after the line and column.
5185 */
5186 static int
5187qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5188{
5189 if (linewise)
5190 return qfp->qf_lnum >= pos->lnum;
5191 else
5192 return (qfp->qf_lnum > pos->lnum ||
5193 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5194}
5195
5196/*
5197 * Returns TRUE if the specified quickfix entry is
5198 * on or before the given line (linewise is TRUE)
5199 * or on or before the line and column.
5200 */
5201 static int
5202qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5203{
5204 if (linewise)
5205 return qfp->qf_lnum <= pos->lnum;
5206 else
5207 return (qfp->qf_lnum < pos->lnum ||
5208 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5209}
5210
5211/*
5212 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5213 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5214 * multiple entries on a single line as one. Otherwise returns the entry after
5215 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005216 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5217 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005218 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005219 */
5220 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005221qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005222 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005223 pos_T *pos,
5224 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005225 qfline_T *qfp,
5226 int *errornr)
5227{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005228 if (qf_entry_after_pos(qfp, pos, linewise))
5229 // First entry is after postion 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005230 return qfp;
5231
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005232 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005233 while (qfp->qf_next != NULL
5234 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005235 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005236 {
5237 qfp = qfp->qf_next;
5238 ++*errornr;
5239 }
5240
5241 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005242 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005243 return NULL;
5244
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005245 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005246 qfp = qfp->qf_next;
5247 ++*errornr;
5248
5249 return qfp;
5250}
5251
5252/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005253 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5254 * If 'linewise' is TRUE, returns the entry before the specified line and
5255 * treats multiple entries on a single line as one. Otherwise returns the entry
5256 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005257 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5258 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005259 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005260 */
5261 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005262qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005263 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005264 pos_T *pos,
5265 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005266 qfline_T *qfp,
5267 int *errornr)
5268{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005269 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005270 while (qfp->qf_next != NULL
5271 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005272 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005273 {
5274 qfp = qfp->qf_next;
5275 ++*errornr;
5276 }
5277
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005278 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005279 return NULL;
5280
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005281 if (linewise)
5282 // If multiple entries are on the same line, then use the first entry
5283 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005284
5285 return qfp;
5286}
5287
5288/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005289 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005290 * the direction 'dir'.
5291 */
5292 static qfline_T *
5293qf_find_closest_entry(
5294 qf_list_T *qfl,
5295 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005296 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005297 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005298 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005299 int *errornr)
5300{
5301 qfline_T *qfp;
5302
5303 *errornr = 0;
5304
5305 // Find the first entry in this file
5306 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5307 if (qfp == NULL)
5308 return NULL; // no entry in this file
5309
5310 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005311 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005312 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005313 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005314
5315 return qfp;
5316}
5317
5318/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005319 * Get the nth quickfix entry below the specified entry. Searches forward in
5320 * the list. If linewise is TRUE, then treat multiple entries on a single line
5321 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005322 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005323 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005324qf_get_nth_below_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005325{
5326 while (n-- > 0 && !got_int)
5327 {
5328 qfline_T *first_entry = entry;
5329 int first_errornr = *errornr;
5330
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005331 if (linewise)
5332 // Treat all the entries on the same line in this file as one
5333 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005334
5335 if (entry->qf_next == NULL
5336 || entry->qf_next->qf_fnum != entry->qf_fnum)
5337 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005338 if (linewise)
5339 {
5340 // If multiple entries are on the same line, then use the first
5341 // entry
5342 entry = first_entry;
5343 *errornr = first_errornr;
5344 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005345 break;
5346 }
5347
5348 entry = entry->qf_next;
5349 ++*errornr;
5350 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005351}
5352
5353/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005354 * Get the nth quickfix entry above the specified entry. Searches backwards in
5355 * the list. If linewise is TRUE, then treat multiple entries on a single line
5356 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005357 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005358 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005359qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005360{
5361 while (n-- > 0 && !got_int)
5362 {
5363 if (entry->qf_prev == NULL
5364 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5365 break;
5366
5367 entry = entry->qf_prev;
5368 --*errornr;
5369
5370 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005371 if (linewise)
5372 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005373 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005374}
5375
5376/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005377 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5378 * the specified direction. Returns the error number in the quickfix list or 0
5379 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005380 */
5381 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005382qf_find_nth_adj_entry(
5383 qf_list_T *qfl,
5384 int bnr,
5385 pos_T *pos,
5386 int n,
5387 int dir,
5388 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005389{
5390 qfline_T *adj_entry;
5391 int errornr;
5392
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005393 // Find an entry closest to the specified position
5394 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005395 if (adj_entry == NULL)
5396 return 0;
5397
5398 if (--n > 0)
5399 {
5400 // Go to the n'th entry in the current buffer
5401 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005402 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005403 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005404 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005405 }
5406
5407 return errornr;
5408}
5409
5410/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005411 * Jump to a quickfix entry in the current file nearest to the current line or
5412 * current line/col.
5413 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5414 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005415 */
5416 void
5417ex_cbelow(exarg_T *eap)
5418{
5419 qf_info_T *qi;
5420 qf_list_T *qfl;
5421 int dir;
5422 int buf_has_flag;
5423 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005424 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005425
5426 if (eap->addr_count > 0 && eap->line2 <= 0)
5427 {
5428 emsg(_(e_invrange));
5429 return;
5430 }
5431
5432 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005433 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5434 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005435 buf_has_flag = BUF_HAS_QF_ENTRY;
5436 else
5437 buf_has_flag = BUF_HAS_LL_ENTRY;
5438 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5439 {
5440 emsg(_(e_quickfix));
5441 return;
5442 }
5443
5444 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5445 return;
5446
5447 qfl = qf_get_curlist(qi);
5448 // check if the list has valid errors
5449 if (!qf_list_has_valid_entries(qfl))
5450 {
5451 emsg(_(e_quickfix));
5452 return;
5453 }
5454
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005455 if (eap->cmdidx == CMD_cbelow
5456 || eap->cmdidx == CMD_lbelow
5457 || eap->cmdidx == CMD_cafter
5458 || eap->cmdidx == CMD_lafter)
5459 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005460 dir = FORWARD;
5461 else
5462 dir = BACKWARD;
5463
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005464 pos = curwin->w_cursor;
5465 // A quickfix entry column number is 1 based whereas cursor column
5466 // number is 0 based. Adjust the column number.
5467 pos.col++;
5468 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5469 eap->addr_count > 0 ? eap->line2 : 0, dir,
5470 eap->cmdidx == CMD_cbelow
5471 || eap->cmdidx == CMD_lbelow
5472 || eap->cmdidx == CMD_cabove
5473 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005474
5475 if (errornr > 0)
5476 qf_jump(qi, 0, errornr, FALSE);
5477 else
5478 emsg(_(e_no_more_items));
5479}
5480
5481/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005482 * Return the autocmd name for the :cfile Ex commands
5483 */
5484 static char_u *
5485cfile_get_auname(cmdidx_T cmdidx)
5486{
5487 switch (cmdidx)
5488 {
5489 case CMD_cfile: return (char_u *)"cfile";
5490 case CMD_cgetfile: return (char_u *)"cgetfile";
5491 case CMD_caddfile: return (char_u *)"caddfile";
5492 case CMD_lfile: return (char_u *)"lfile";
5493 case CMD_lgetfile: return (char_u *)"lgetfile";
5494 case CMD_laddfile: return (char_u *)"laddfile";
5495 default: return NULL;
5496 }
5497}
5498
5499/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005500 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005501 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 */
5503 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005504ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005506 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005507 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005508 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005509 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005510 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005511 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005512
Bram Moolenaara16123a2019-03-28 20:31:07 +01005513 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005514 if (au_name != NULL)
5515 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005516
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005517 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005518#ifdef FEAT_BROWSE
5519 if (cmdmod.browse)
5520 {
5521 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005522 NULL, NULL,
5523 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005524 if (browse_file == NULL)
5525 return;
5526 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5527 vim_free(browse_file);
5528 }
5529 else
5530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005532 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005533
Bram Moolenaar39665952018-08-15 20:59:48 +02005534 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005535 wp = curwin;
5536
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005537 incr_quickfix_busy();
5538
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005539 // This function is used by the :cfile, :cgetfile and :caddfile
5540 // commands.
5541 // :cfile always creates a new quickfix list and jumps to the
5542 // first error.
5543 // :cgetfile creates a new quickfix list but doesn't jump to the
5544 // first error.
5545 // :caddfile adds to an existing quickfix list. If there is no
5546 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005547 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005548 && eap->cmdidx != CMD_laddfile),
5549 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005550 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005551 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005552 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005553 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005554 {
5555 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005556 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005557 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005558 }
5559 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005560 qf_list_changed(qf_get_curlist(qi));
5561 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005562 if (au_name != NULL)
5563 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005564
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005565 // Jump to the first error for a new list and if autocmds didn't
5566 // free the list.
5567 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5568 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005569 // display the first error
5570 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005571
5572 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005573}
5574
5575/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005576 * Return the vimgrep autocmd name.
5577 */
5578 static char_u *
5579vgr_get_auname(cmdidx_T cmdidx)
5580{
5581 switch (cmdidx)
5582 {
5583 case CMD_vimgrep: return (char_u *)"vimgrep";
5584 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5585 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5586 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5587 case CMD_grep: return (char_u *)"grep";
5588 case CMD_lgrep: return (char_u *)"lgrep";
5589 case CMD_grepadd: return (char_u *)"grepadd";
5590 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5591 default: return NULL;
5592 }
5593}
5594
5595/*
5596 * Initialize the regmatch used by vimgrep for pattern "s".
5597 */
5598 static void
5599vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5600{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005601 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005602 regmatch->regprog = NULL;
5603
5604 if (s == NULL || *s == NUL)
5605 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005606 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005607 if (last_search_pat() == NULL)
5608 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005609 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005610 return;
5611 }
5612 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5613 }
5614 else
5615 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5616
5617 regmatch->rmm_ic = p_ic;
5618 regmatch->rmm_maxcol = 0;
5619}
5620
5621/*
5622 * Display a file name when vimgrep is running.
5623 */
5624 static void
5625vgr_display_fname(char_u *fname)
5626{
5627 char_u *p;
5628
5629 msg_start();
5630 p = msg_strtrunc(fname, TRUE);
5631 if (p == NULL)
5632 msg_outtrans(fname);
5633 else
5634 {
5635 msg_outtrans(p);
5636 vim_free(p);
5637 }
5638 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005639 msg_didout = FALSE; // overwrite this message
5640 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005641 msg_col = 0;
5642 out_flush();
5643}
5644
5645/*
5646 * Load a dummy buffer to search for a pattern using vimgrep.
5647 */
5648 static buf_T *
5649vgr_load_dummy_buf(
5650 char_u *fname,
5651 char_u *dirname_start,
5652 char_u *dirname_now)
5653{
5654 int save_mls;
5655#if defined(FEAT_SYN_HL)
5656 char_u *save_ei = NULL;
5657#endif
5658 buf_T *buf;
5659
5660#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005661 // Don't do Filetype autocommands to avoid loading syntax and
5662 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005663 save_ei = au_event_disable(",Filetype");
5664#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005665 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005666 save_mls = p_mls;
5667 p_mls = 0;
5668
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005669 // Load file into a buffer, so that 'fileencoding' is detected,
5670 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005671 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5672
5673 p_mls = save_mls;
5674#if defined(FEAT_SYN_HL)
5675 au_event_restore(save_ei);
5676#endif
5677
5678 return buf;
5679}
5680
5681/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005682 * Check whether a quickfix/location list is valid. Autocmds may remove or
5683 * change a quickfix list when vimgrep is running. If the list is not found,
5684 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005685 */
5686 static int
5687vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005688 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005689 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005690 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005691 char_u *title)
5692{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005693 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005694 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005695 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005696 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005697 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005698 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005699 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005700 return FALSE;
5701 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005702 else
5703 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005704 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005705 qf_new_list(qi, title);
5706 return TRUE;
5707 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005708 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005709
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005710 if (qf_restore_list(qi, qfid) == FAIL)
5711 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005712
5713 return TRUE;
5714}
5715
5716/*
5717 * Search for a pattern in all the lines in a buffer and add the matching lines
5718 * to a quickfix list.
5719 */
5720 static int
5721vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005722 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005723 char_u *fname,
5724 buf_T *buf,
5725 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005726 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005727 int duplicate_name,
5728 int flags)
5729{
5730 int found_match = FALSE;
5731 long lnum;
5732 colnr_T col;
5733
Bram Moolenaar1c299432018-10-28 14:36:09 +01005734 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005735 {
5736 col = 0;
5737 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5738 col, NULL, NULL) > 0)
5739 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005740 // Pass the buffer number so that it gets used even for a
5741 // dummy buffer, unless duplicate_name is set, then the
5742 // buffer will be wiped out below.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005743 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005744 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005745 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005746 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005747 duplicate_name ? 0 : buf->b_fnum,
5748 ml_get_buf(buf,
5749 regmatch->startpos[0].lnum + lnum, FALSE),
5750 regmatch->startpos[0].lnum + lnum,
5751 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005752 FALSE, // vis_col
5753 NULL, // search pattern
5754 0, // nr
5755 0, // type
5756 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02005757 ) == QF_FAIL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005758 {
5759 got_int = TRUE;
5760 break;
5761 }
5762 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005763 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005764 break;
5765 if ((flags & VGR_GLOBAL) == 0
5766 || regmatch->endpos[0].lnum > 0)
5767 break;
5768 col = regmatch->endpos[0].col
5769 + (col == regmatch->endpos[0].col);
5770 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5771 break;
5772 }
5773 line_breakcheck();
5774 if (got_int)
5775 break;
5776 }
5777
5778 return found_match;
5779}
5780
5781/*
5782 * Jump to the first match and update the directory.
5783 */
5784 static void
5785vgr_jump_to_match(
5786 qf_info_T *qi,
5787 int forceit,
5788 int *redraw_for_dummy,
5789 buf_T *first_match_buf,
5790 char_u *target_dir)
5791{
5792 buf_T *buf;
5793
5794 buf = curbuf;
5795 qf_jump(qi, 0, 0, forceit);
5796 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005797 // If we jumped to another buffer redrawing will already be
5798 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005799 *redraw_for_dummy = FALSE;
5800
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005801 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005802 if (curbuf == first_match_buf && target_dir != NULL)
5803 {
5804 exarg_T ea;
5805
Bram Moolenaar4ca41532019-05-09 21:48:37 +02005806 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005807 ea.arg = target_dir;
5808 ea.cmdidx = CMD_lcd;
5809 ex_cd(&ea);
5810 }
5811}
5812
5813/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005814 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005815 * ":vimgrepadd {pattern} file(s)"
5816 * ":lvimgrep {pattern} file(s)"
5817 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005818 */
5819 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005820ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005821{
Bram Moolenaar81695252004-12-29 20:58:21 +00005822 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005823 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005824 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005825 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005826 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005827 char_u *s;
5828 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005829 int fi;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005830 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005831 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005832 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005833 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005834 buf_T *buf;
5835 int duplicate_name = FALSE;
5836 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005837 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005838 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005839 buf_T *first_match_buf = NULL;
5840 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005841 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005842 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005843 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005844 char_u *dirname_start = NULL;
5845 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005846 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005847 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005848
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005849 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005850 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5851 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005852 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005853#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005854 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005855 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005856#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005857 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005858
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005859 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
5860 if (qi == NULL)
5861 return;
Bram Moolenaara6557602006-02-04 22:43:20 +00005862
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005863 if (eap->addr_count > 0)
5864 tomatch = eap->line2;
5865 else
5866 tomatch = MAXLNUM;
5867
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005868 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005869 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005870 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005871 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005872 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005873 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005874 emsg(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005875 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005876 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005877
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005878 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005879 if (regmatch.regprog == NULL)
5880 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005881
5882 p = skipwhite(p);
5883 if (*p == NUL)
5884 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005885 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005886 goto theend;
5887 }
5888
Bram Moolenaar55b69262017-08-13 13:42:01 +02005889 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005890 && eap->cmdidx != CMD_vimgrepadd
5891 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005892 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005893 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005894 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005895
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005896 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005897 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005898 goto theend;
5899 if (fcount == 0)
5900 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005901 emsg(_(e_nomatch));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005902 goto theend;
5903 }
5904
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005905 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5906 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005907 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005908 {
5909 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005910 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005911 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005912
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005913 // Remember the current directory, because a BufRead autocommand that does
5914 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005915 mch_dirname(dirname_start, MAXPATHL);
5916
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005917 incr_quickfix_busy();
5918
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005919 // Remember the current quickfix list identifier, so that we can check for
5920 // autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005921 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005922
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005923 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005924 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005925 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005926 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005927 if (time(NULL) > seconds)
5928 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005929 // Display the file name every second or so, show the user we are
5930 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005931 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005932 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005933 }
5934
Bram Moolenaar81695252004-12-29 20:58:21 +00005935 buf = buflist_findname_exp(fnames[fi]);
5936 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5937 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005938 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005939 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005940 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005941 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005942
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005943 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005944 }
5945 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005946 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005947 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005948
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005949 // Check whether the quickfix list is still valid. When loading a
5950 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005951 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005952 {
5953 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005954 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005955 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005956 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005957 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005958
Bram Moolenaar81695252004-12-29 20:58:21 +00005959 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005960 {
5961 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005962 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005963 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005964 else
5965 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005966 // Try for a match in all lines of the buffer.
5967 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005968 found_match = vgr_match_buflines(qf_get_curlist(qi),
5969 fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005970 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005971
Bram Moolenaar81695252004-12-29 20:58:21 +00005972 if (using_dummy)
5973 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005974 if (found_match && first_match_buf == NULL)
5975 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005976 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005977 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005978 // Never keep a dummy buffer if there is another buffer
5979 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005980 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005981 buf = NULL;
5982 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005983 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005984 || buf->b_p_bh[0] == 'u' // "unload"
5985 || buf->b_p_bh[0] == 'w' // "wipe"
5986 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005987 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005988 // When no match was found we don't need to remember the
5989 // buffer, wipe it out. If there was a match and it
5990 // wasn't the first one or we won't jump there: only
5991 // unload the buffer.
5992 // Ignore 'hidden' here, because it may lead to having too
5993 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005994 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005995 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005996 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005997 buf = NULL;
5998 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005999 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006000 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006001 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006002 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006003 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006004 buf = NULL;
6005 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006006 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006007
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006008 if (buf != NULL)
6009 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006010 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006011 buf->b_flags &= ~BF_DUMMY;
6012
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006013 // If the buffer is still loaded we need to use the
6014 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006015 if (buf == first_match_buf
6016 && target_dir == NULL
6017 && STRCMP(dirname_start, dirname_now) != 0)
6018 target_dir = vim_strsave(dirname_now);
6019
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006020 // The buffer is still loaded, the Filetype autocommands
6021 // need to be done now, in that buffer. And the modelines
6022 // need to be done (again). But not the window-local
6023 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006024 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006025#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006026 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6027 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006028#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006029 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006030 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006031 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006032 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006033 }
6034 }
6035
6036 FreeWild(fcount, fnames);
6037
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006038 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006039 qfl->qf_nonevalid = FALSE;
6040 qfl->qf_ptr = qfl->qf_start;
6041 qfl->qf_index = 1;
6042 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006043
Bram Moolenaar864293a2016-06-02 13:40:04 +02006044 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006045
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006046 if (au_name != NULL)
6047 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6048 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006049 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6050 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006051 if (!qflist_valid(wp, save_qfid)
6052 || qf_restore_list(qi, save_qfid) == FAIL)
6053 {
6054 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006055 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006056 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006057
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006058 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006059 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006060 {
6061 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006062 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6063 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006064 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006065 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006066 semsg(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006067
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006068 decr_quickfix_busy();
6069
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006070 // If we loaded a dummy buffer into the current window, the autocommands
6071 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006072 if (redraw_for_dummy)
6073 {
6074#ifdef FEAT_FOLDING
6075 foldUpdateAll(curwin);
6076#else
6077 redraw_later(NOT_VALID);
6078#endif
6079 }
6080
Bram Moolenaar86b68352004-12-27 21:59:20 +00006081theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01006082 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006083 vim_free(dirname_now);
6084 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006085 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02006086 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006087}
6088
6089/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006090 * Restore current working directory to "dirname_start" if they differ, taking
6091 * into account whether it is set locally or globally.
6092 */
6093 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006094restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006095{
6096 char_u *dirname_now = alloc(MAXPATHL);
6097
6098 if (NULL != dirname_now)
6099 {
6100 mch_dirname(dirname_now, MAXPATHL);
6101 if (STRCMP(dirname_start, dirname_now) != 0)
6102 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006103 // If the directory has changed, change it back by building up an
6104 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006105 exarg_T ea;
6106
Bram Moolenaar4ca41532019-05-09 21:48:37 +02006107 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006108 ea.arg = dirname_start;
6109 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6110 ex_cd(&ea);
6111 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006112 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006113 }
6114}
6115
6116/*
6117 * Load file "fname" into a dummy buffer and return the buffer pointer,
6118 * placing the directory resulting from the buffer load into the
6119 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6120 * prior to calling this function. Restores directory to "dirname_start" prior
6121 * to returning, if autocmds or the 'autochdir' option have changed it.
6122 *
6123 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6124 * or wipe_dummy_buffer() later!
6125 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006126 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006127 */
6128 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006129load_dummy_buffer(
6130 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006131 char_u *dirname_start, // in: old directory
6132 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006133{
6134 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006135 bufref_T newbufref;
6136 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006137 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006138 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006139 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006140
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006141 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006142 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6143 if (newbuf == NULL)
6144 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006145 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006146
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006147 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006148 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6149
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006150 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006151 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006152 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006153 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006154 ++newbuf->b_locked;
6155
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006156 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006157 aucmd_prepbuf(&aco, newbuf);
6158
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006159 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006160 (void)setfname(curbuf, fname, NULL, FALSE);
6161
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006162 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006163 check_need_swap(TRUE);
6164
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006165 // Remove the "dummy" flag, otherwise autocommands may not
6166 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006167 curbuf->b_flags &= ~BF_DUMMY;
6168
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006169 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006170 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006171 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006172 NULL, READ_NEW | READ_DUMMY);
6173 --newbuf->b_locked;
6174 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006175 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006176 && !(curbuf->b_flags & BF_NEW))
6177 {
6178 failed = FALSE;
6179 if (curbuf != newbuf)
6180 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006181 // Bloody autocommands changed the buffer! Can happen when
6182 // using netrw and editing a remote file. Use the current
6183 // buffer instead, delete the dummy one after restoring the
6184 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006185 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006186 newbuf = curbuf;
6187 }
6188 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006189
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006190 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006191 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006192 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6193 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006194
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006195 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6196 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006197 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006198 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006199
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006200 // When autocommands/'autochdir' option changed directory: go back.
6201 // Let the caller know what the resulting dir was first, in case it is
6202 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006203 mch_dirname(resulting_dir, MAXPATHL);
6204 restore_start_dir(dirname_start);
6205
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006206 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006207 return NULL;
6208 if (failed)
6209 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006210 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006211 return NULL;
6212 }
6213 return newbuf;
6214}
6215
6216/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006217 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6218 * directory to "dirname_start" prior to returning, if autocmds or the
6219 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006220 */
6221 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006222wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006223{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006224 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006225 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006226#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006227 cleanup_T cs;
6228
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006229 // Reset the error/interrupt/exception state here so that aborting()
6230 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6231 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006232 enter_cleanup(&cs);
6233#endif
6234
Bram Moolenaar81695252004-12-29 20:58:21 +00006235 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006236
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006237#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006238 // Restore the error/interrupt/exception state if not discarded by a
6239 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006240 leave_cleanup(&cs);
6241#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006242 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006243 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006244 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006245}
6246
6247/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006248 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6249 * directory to "dirname_start" prior to returning, if autocmds or the
6250 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006251 */
6252 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006253unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006254{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006255 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006256 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006257 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006258
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006259 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006260 restore_start_dir(dirname_start);
6261 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006262}
6263
Bram Moolenaar05159a02005-02-26 23:04:13 +00006264#if defined(FEAT_EVAL) || defined(PROTO)
6265/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01006266 * Copy the specified quickfix entry items into a new dict and appened the dict
6267 * to 'list'. Returns OK on success.
6268 */
6269 static int
6270get_qfline_items(qfline_T *qfp, list_T *list)
6271{
6272 int bufnum;
6273 dict_T *dict;
6274 char_u buf[2];
6275
6276 // Handle entries with a non-existing buffer number.
6277 bufnum = qfp->qf_fnum;
6278 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6279 bufnum = 0;
6280
6281 if ((dict = dict_alloc()) == NULL)
6282 return FAIL;
6283 if (list_append_dict(list, dict) == FAIL)
6284 return FAIL;
6285
6286 buf[0] = qfp->qf_type;
6287 buf[1] = NUL;
6288 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
6289 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6290 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6291 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6292 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
6293 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6294 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6295 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6296 || dict_add_string(dict, "type", buf) == FAIL
6297 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6298 return FAIL;
6299
6300 return OK;
6301}
6302
6303/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006304 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006305 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006306 */
6307 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006308get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006309{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006310 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006311 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006312 qfline_T *qfp;
6313 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006314
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006315 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006316 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006317 qi = &ql_info;
6318 if (wp != NULL)
6319 {
6320 qi = GET_LOC_LIST(wp);
6321 if (qi == NULL)
6322 return FAIL;
6323 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006324 }
6325
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006326 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006327 qf_idx = qi->qf_curlist;
6328
Bram Moolenaar0398e002019-03-21 21:12:49 +01006329 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006330 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006331
Bram Moolenaar0398e002019-03-21 21:12:49 +01006332 qfl = qf_get_list(qi, qf_idx);
6333 if (qf_list_empty(qfl))
6334 return FAIL;
6335
Bram Moolenaara16123a2019-03-28 20:31:07 +01006336 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006337 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01006338 if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006339 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006340 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006341
Bram Moolenaar05159a02005-02-26 23:04:13 +00006342 return OK;
6343}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006344
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006345// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006346enum {
6347 QF_GETLIST_NONE = 0x0,
6348 QF_GETLIST_TITLE = 0x1,
6349 QF_GETLIST_ITEMS = 0x2,
6350 QF_GETLIST_NR = 0x4,
6351 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006352 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006353 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006354 QF_GETLIST_IDX = 0x40,
6355 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006356 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006357 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006358 QF_GETLIST_QFBUFNR = 0x400,
6359 QF_GETLIST_ALL = 0x7FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006360};
6361
6362/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006363 * Parse text from 'di' and return the quickfix list items.
6364 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006365 */
6366 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006367qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006368{
6369 int status = FAIL;
6370 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006371 char_u *errorformat = p_efm;
6372 dictitem_T *efm_di;
6373 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006374
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006375 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006376 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006377 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006378 // If errorformat is supplied then use it, otherwise use the 'efm'
6379 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006380 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6381 {
6382 if (efm_di->di_tv.v_type != VAR_STRING ||
6383 efm_di->di_tv.vval.v_string == NULL)
6384 return FAIL;
6385 errorformat = efm_di->di_tv.vval.v_string;
6386 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006387
Bram Moolenaar36538222017-09-02 19:51:44 +02006388 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006389 if (l == NULL)
6390 return FAIL;
6391
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006392 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006393 if (qi != NULL)
6394 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006395 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006396 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6397 {
Bram Moolenaarda732532017-08-31 20:58:02 +02006398 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006399 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006400 }
6401 free(qi);
6402 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006403 dict_add_list(retdict, "items", l);
6404 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006405 }
6406
6407 return status;
6408}
6409
6410/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006411 * Return the quickfix/location list window identifier in the current tabpage.
6412 */
6413 static int
6414qf_winid(qf_info_T *qi)
6415{
6416 win_T *win;
6417
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006418 // The quickfix window can be opened even if the quickfix list is not set
6419 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006420 if (qi == NULL)
6421 return 0;
6422 win = qf_find_win(qi);
6423 if (win != NULL)
6424 return win->w_id;
6425 return 0;
6426}
6427
6428/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006429 * Returns the number of the buffer displayed in the quickfix/location list
6430 * window. If there is no buffer associated with the list, then returns 0.
6431 */
6432 static int
6433qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6434{
6435 return dict_add_number(retdict, "qfbufnr",
6436 (qi == NULL) ? 0 : qi->qf_bufnr);
6437}
6438
6439/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006440 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006441 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006442 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006443qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006444{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006445 int flags = QF_GETLIST_NONE;
6446
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006447 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006448 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006449 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006450 if (!loclist)
6451 // File window ID is applicable only to location list windows
6452 flags &= ~ QF_GETLIST_FILEWINID;
6453 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006454
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006455 if (dict_find(what, (char_u *)"title", -1) != NULL)
6456 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006457
Bram Moolenaara6d48492017-12-12 22:45:31 +01006458 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6459 flags |= QF_GETLIST_NR;
6460
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006461 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6462 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006463
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006464 if (dict_find(what, (char_u *)"context", -1) != NULL)
6465 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006466
Bram Moolenaara6d48492017-12-12 22:45:31 +01006467 if (dict_find(what, (char_u *)"id", -1) != NULL)
6468 flags |= QF_GETLIST_ID;
6469
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006470 if (dict_find(what, (char_u *)"items", -1) != NULL)
6471 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006472
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006473 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6474 flags |= QF_GETLIST_IDX;
6475
6476 if (dict_find(what, (char_u *)"size", -1) != NULL)
6477 flags |= QF_GETLIST_SIZE;
6478
Bram Moolenaarb254af32017-12-18 19:48:58 +01006479 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6480 flags |= QF_GETLIST_TICK;
6481
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006482 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6483 flags |= QF_GETLIST_FILEWINID;
6484
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006485 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6486 flags |= QF_GETLIST_QFBUFNR;
6487
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006488 return flags;
6489}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006490
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006491/*
6492 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6493 * If 'nr' and 'id' are not present in 'what' then return the current
6494 * quickfix list index.
6495 * If 'nr' is zero then return the current quickfix list index.
6496 * If 'nr' is '$' then return the last quickfix list index.
6497 * If 'id' is present then return the index of the quickfix list with that id.
6498 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6499 * Return -1, if quickfix list is not present or if the stack is empty.
6500 */
6501 static int
6502qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6503{
6504 int qf_idx;
6505 dictitem_T *di;
6506
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006507 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006508 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6509 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006510 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006511 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006512 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006513 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006514 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006515 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006516 qf_idx = di->di_tv.vval.v_number - 1;
6517 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006518 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006519 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006520 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006521 else if (di->di_tv.v_type == VAR_STRING
6522 && di->di_tv.vval.v_string != NULL
6523 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006524 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006525 qf_idx = qi->qf_listcount - 1;
6526 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006527 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006528 }
6529
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006530 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6531 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006532 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006533 if (di->di_tv.v_type == VAR_NUMBER)
6534 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006535 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006536 if (di->di_tv.vval.v_number != 0)
6537 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6538 }
6539 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006540 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006541 }
6542
6543 return qf_idx;
6544}
6545
6546/*
6547 * Return default values for quickfix list properties in retdict.
6548 */
6549 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006550qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006551{
6552 int status = OK;
6553
6554 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006555 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006556 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6557 {
6558 list_T *l = list_alloc();
6559 if (l != NULL)
6560 status = dict_add_list(retdict, "items", l);
6561 else
6562 status = FAIL;
6563 }
6564 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006565 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006566 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006567 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006568 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006569 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006570 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006571 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006572 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006573 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006574 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006575 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006576 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006577 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006578 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006579 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006580 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6581 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006582
6583 return status;
6584}
6585
6586/*
6587 * Return the quickfix list title as 'title' in retdict
6588 */
6589 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006590qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006591{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006592 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006593}
6594
6595/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006596 * Returns the identifier of the window used to display files from a location
6597 * list. If there is no associated window, then returns 0. Useful only when
6598 * called from a location list window.
6599 */
6600 static int
6601qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6602{
6603 int winid = 0;
6604
6605 if (wp != NULL && IS_LL_WINDOW(wp))
6606 {
6607 win_T *ll_wp = qf_find_win_with_loclist(qi);
6608 if (ll_wp != NULL)
6609 winid = ll_wp->w_id;
6610 }
6611
6612 return dict_add_number(retdict, "filewinid", winid);
6613}
6614
6615/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006616 * Return the quickfix list items/entries as 'items' in retdict
6617 */
6618 static int
6619qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6620{
6621 int status = OK;
6622 list_T *l = list_alloc();
6623 if (l != NULL)
6624 {
6625 (void)get_errorlist(qi, NULL, qf_idx, l);
6626 dict_add_list(retdict, "items", l);
6627 }
6628 else
6629 status = FAIL;
6630
6631 return status;
6632}
6633
6634/*
6635 * Return the quickfix list context (if any) as 'context' in retdict.
6636 */
6637 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006638qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006639{
6640 int status;
6641 dictitem_T *di;
6642
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006643 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006644 {
6645 di = dictitem_alloc((char_u *)"context");
6646 if (di != NULL)
6647 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006648 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006649 status = dict_add(retdict, di);
6650 if (status == FAIL)
6651 dictitem_free(di);
6652 }
6653 else
6654 status = FAIL;
6655 }
6656 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006657 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006658
6659 return status;
6660}
6661
6662/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006663 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006664 */
6665 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01006666qf_getprop_idx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006667{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006668 int curidx = qfl->qf_index;
6669 if (qf_list_empty(qfl))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006670 // For empty lists, current index is set to 0
6671 curidx = 0;
6672 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006673}
6674
6675/*
6676 * Return quickfix/location list details (title) as a
6677 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6678 * then current list is used. Otherwise the specified list is used.
6679 */
6680 int
6681qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6682{
6683 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006684 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006685 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006686 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006687 dictitem_T *di;
6688 int flags = QF_GETLIST_NONE;
6689
6690 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6691 return qf_get_list_from_lines(what, di, retdict);
6692
6693 if (wp != NULL)
6694 qi = GET_LOC_LIST(wp);
6695
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006696 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006697
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006698 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006699 qf_idx = qf_getprop_qfidx(qi, what);
6700
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006701 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006702 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006703 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006704
Bram Moolenaar0398e002019-03-21 21:12:49 +01006705 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006706
Bram Moolenaard823fa92016-08-12 16:29:27 +02006707 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006708 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006709 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006710 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006711 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006712 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006713 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006714 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006715 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006716 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006717 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006718 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006719 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar0398e002019-03-21 21:12:49 +01006720 status = qf_getprop_idx(qfl, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006721 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006722 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006723 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006724 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006725 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6726 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006727 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6728 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006729
Bram Moolenaard823fa92016-08-12 16:29:27 +02006730 return status;
6731}
6732
6733/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006734 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006735 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6736 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006737 */
6738 static int
6739qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01006740 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006741 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006742 int first_entry,
6743 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006744{
6745 static int did_bufnr_emsg;
6746 char_u *filename, *module, *pattern, *text, *type;
6747 int bufnum, valid, status, col, vcol, nr;
6748 long lnum;
6749
6750 if (first_entry)
6751 did_bufnr_emsg = FALSE;
6752
Bram Moolenaar8f667172018-12-14 15:38:31 +01006753 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6754 module = dict_get_string(d, (char_u *)"module", TRUE);
6755 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6756 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6757 col = (int)dict_get_number(d, (char_u *)"col");
6758 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6759 nr = (int)dict_get_number(d, (char_u *)"nr");
6760 type = dict_get_string(d, (char_u *)"type", TRUE);
6761 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6762 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006763 if (text == NULL)
6764 text = vim_strsave((char_u *)"");
6765
6766 valid = TRUE;
6767 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6768 valid = FALSE;
6769
6770 // Mark entries with non-existing buffer number as not valid. Give the
6771 // error message only once.
6772 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6773 {
6774 if (!did_bufnr_emsg)
6775 {
6776 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006777 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006778 }
6779 valid = FALSE;
6780 bufnum = 0;
6781 }
6782
6783 // If the 'valid' field is present it overrules the detected value.
6784 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006785 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006786
Bram Moolenaar0398e002019-03-21 21:12:49 +01006787 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006788 NULL, // dir
6789 filename,
6790 module,
6791 bufnum,
6792 text,
6793 lnum,
6794 col,
6795 vcol, // vis_col
6796 pattern, // search pattern
6797 nr,
6798 type == NULL ? NUL : *type,
6799 valid);
6800
6801 vim_free(filename);
6802 vim_free(module);
6803 vim_free(pattern);
6804 vim_free(text);
6805 vim_free(type);
6806
Bram Moolenaar9752c722018-12-22 16:49:34 +01006807 if (valid)
6808 *valid_entry = TRUE;
6809
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006810 return status;
6811}
6812
6813/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006814 * Add list of entries to quickfix/location list. Each list entry is
6815 * a dictionary with item information.
6816 */
6817 static int
6818qf_add_entries(
6819 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006820 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006821 list_T *list,
6822 char_u *title,
6823 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006824{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006825 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006826 listitem_T *li;
6827 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006828 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006829 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006830 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006831
Bram Moolenaara3921f42017-06-04 15:30:34 +02006832 if (action == ' ' || qf_idx == qi->qf_listcount)
6833 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006834 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006835 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006836 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006837 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006838 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01006839 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006840 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006841 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006842 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006843 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006844 qf_free_items(qfl);
6845 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006846 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006847
6848 for (li = list->lv_first; li != NULL; li = li->li_next)
6849 {
6850 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006851 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006852
6853 d = li->li_tv.vval.v_dict;
6854 if (d == NULL)
6855 continue;
6856
Bram Moolenaar0398e002019-03-21 21:12:49 +01006857 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006858 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02006859 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006860 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006861 }
6862
Bram Moolenaar9752c722018-12-22 16:49:34 +01006863 // Check if any valid error entries are added to the list.
6864 if (valid_entry)
6865 qfl->qf_nonevalid = FALSE;
6866 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006867 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006868 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006869
6870 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006871 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006872 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006873
6874 // Update the current error index if not appending to the list or if the
6875 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006876 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01006877 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006878
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006879 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006880 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006881
6882 return retval;
6883}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006884
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006885/*
6886 * Get the quickfix list index from 'nr' or 'id'
6887 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006888 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006889qf_setprop_get_qfidx(
6890 qf_info_T *qi,
6891 dict_T *what,
6892 int action,
6893 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006894{
6895 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006896 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006897
Bram Moolenaard823fa92016-08-12 16:29:27 +02006898 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6899 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006900 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006901 if (di->di_tv.v_type == VAR_NUMBER)
6902 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006903 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006904 if (di->di_tv.vval.v_number != 0)
6905 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006906
Bram Moolenaar55b69262017-08-13 13:42:01 +02006907 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6908 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006909 // When creating a new list, accept qf_idx pointing to the next
6910 // non-available list and add the new list at the end of the
6911 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006912 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006913 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006914 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006915 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006916 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006917 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006918 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006919 }
6920 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006921 && di->di_tv.vval.v_string != NULL
6922 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006923 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006924 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006925 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006926 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006927 qf_idx = 0;
6928 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006929 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006930 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006931 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006932 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006933 }
6934
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006935 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006936 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006937 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006938 if (di->di_tv.v_type != VAR_NUMBER)
6939 return INVALID_QFIDX;
6940
6941 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006942 }
6943
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006944 return qf_idx;
6945}
6946
6947/*
6948 * Set the quickfix list title.
6949 */
6950 static int
6951qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6952{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006953 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006954
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006955 if (di->di_tv.v_type != VAR_STRING)
6956 return FAIL;
6957
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006958 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006959 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006960 if (qf_idx == qi->qf_curlist)
6961 qf_update_win_titlevar(qi);
6962
6963 return OK;
6964}
6965
6966/*
6967 * Set quickfix list items/entries.
6968 */
6969 static int
6970qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6971{
6972 int retval = FAIL;
6973 char_u *title_save;
6974
6975 if (di->di_tv.v_type != VAR_LIST)
6976 return FAIL;
6977
6978 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6979 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6980 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006981 vim_free(title_save);
6982
6983 return retval;
6984}
6985
6986/*
6987 * Set quickfix list items/entries from a list of lines.
6988 */
6989 static int
6990qf_setprop_items_from_lines(
6991 qf_info_T *qi,
6992 int qf_idx,
6993 dict_T *what,
6994 dictitem_T *di,
6995 int action)
6996{
6997 char_u *errorformat = p_efm;
6998 dictitem_T *efm_di;
6999 int retval = FAIL;
7000
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007001 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007002 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7003 {
7004 if (efm_di->di_tv.v_type != VAR_STRING ||
7005 efm_di->di_tv.vval.v_string == NULL)
7006 return FAIL;
7007 errorformat = efm_di->di_tv.vval.v_string;
7008 }
7009
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007010 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007011 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7012 return FAIL;
7013
7014 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007015 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007016 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
7017 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
7018 retval = OK;
7019
7020 return retval;
7021}
7022
7023/*
7024 * Set quickfix list context.
7025 */
7026 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007027qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007028{
7029 typval_T *ctx;
7030
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007031 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007032 ctx = alloc_tv();
7033 if (ctx != NULL)
7034 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007035 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007036
7037 return OK;
7038}
7039
7040/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007041 * Set the current index in the specified quickfix list
7042 */
7043 static int
7044qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7045{
7046 int denote = FALSE;
7047 int newidx;
7048 int old_qfidx;
7049 qfline_T *qf_ptr;
7050
7051 // If the specified index is '$', then use the last entry
7052 if (di->di_tv.v_type == VAR_STRING
7053 && di->di_tv.vval.v_string != NULL
7054 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7055 newidx = qfl->qf_count;
7056 else
7057 {
7058 // Otherwise use the specified index
7059 newidx = tv_get_number_chk(&di->di_tv, &denote);
7060 if (denote)
7061 return FAIL;
7062 }
7063
7064 if (newidx < 1) // sanity check
7065 return FAIL;
7066 if (newidx > qfl->qf_count)
7067 newidx = qfl->qf_count;
7068
7069 old_qfidx = qfl->qf_index;
7070 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7071 if (qf_ptr == NULL)
7072 return FAIL;
7073 qfl->qf_ptr = qf_ptr;
7074 qfl->qf_index = newidx;
7075
7076 // If the current list is modified and it is displayed in the quickfix
7077 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007078 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007079 qf_win_pos_update(qi, old_qfidx);
7080
7081 return OK;
7082}
7083
7084/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007085 * Set quickfix/location list properties (title, items, context).
7086 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007087 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007088 */
7089 static int
7090qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7091{
7092 dictitem_T *di;
7093 int retval = FAIL;
7094 int qf_idx;
7095 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007096 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007097
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007098 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007099 newlist = TRUE;
7100
7101 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007102 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007103 return FAIL;
7104
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007105 if (newlist)
7106 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007107 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007108 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007109 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007110 }
7111
Bram Moolenaar0398e002019-03-21 21:12:49 +01007112 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007113 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007114 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007115 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007116 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007117 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007118 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007119 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007120 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007121 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7122 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007123
Bram Moolenaarb254af32017-12-18 19:48:58 +01007124 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007125 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007126
Bram Moolenaard823fa92016-08-12 16:29:27 +02007127 return retval;
7128}
7129
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007130/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007131 * Free the entire quickfix/location list stack.
7132 * If the quickfix/location list window is open, then clear it.
7133 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007134 static void
7135qf_free_stack(win_T *wp, qf_info_T *qi)
7136{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007137 win_T *qfwin = qf_find_win(qi);
7138 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007139
7140 if (qfwin != NULL)
7141 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007142 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007143 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007144 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007145 qf_update_buffer(qi, NULL);
7146 }
7147
7148 if (wp != NULL && IS_LL_WINDOW(wp))
7149 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007150 // If in the location list window, then use the non-location list
7151 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007152 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007153 if (llwin != NULL)
7154 wp = llwin;
7155 }
7156
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007157 qf_free_all(wp);
7158 if (wp == NULL)
7159 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007160 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007161 qi->qf_curlist = 0;
7162 qi->qf_listcount = 0;
7163 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007164 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007165 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007166 // If the location list window is open, then create a new empty
7167 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007168 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007169
Bram Moolenaar95946f12019-03-31 15:31:59 +02007170 if (new_ll != NULL)
7171 {
7172 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007173
Bram Moolenaar95946f12019-03-31 15:31:59 +02007174 // first free the list reference in the location list window
7175 ll_free_all(&qfwin->w_llist_ref);
7176
7177 qfwin->w_llist_ref = new_ll;
7178 if (wp != qfwin)
7179 win_set_loclist(wp, new_ll);
7180 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007181 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007182}
7183
Bram Moolenaard823fa92016-08-12 16:29:27 +02007184/*
7185 * Populate the quickfix list with the items supplied in the list
7186 * of dictionaries. "title" will be copied to w:quickfix_title.
7187 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
7188 */
7189 int
7190set_errorlist(
7191 win_T *wp,
7192 list_T *list,
7193 int action,
7194 char_u *title,
7195 dict_T *what)
7196{
7197 qf_info_T *qi = &ql_info;
7198 int retval = OK;
7199
7200 if (wp != NULL)
7201 {
7202 qi = ll_get_or_alloc_list(wp);
7203 if (qi == NULL)
7204 return FAIL;
7205 }
7206
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007207 if (action == 'f')
7208 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007209 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007210 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007211 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007212 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007213
7214 incr_quickfix_busy();
7215
7216 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007217 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007218 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007219 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007220 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007221 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007222 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007223 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007224
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007225 decr_quickfix_busy();
7226
Bram Moolenaard823fa92016-08-12 16:29:27 +02007227 return retval;
7228}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007229
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007230/*
7231 * Mark the context as in use for all the lists in a quickfix stack.
7232 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007233 static int
7234mark_quickfix_ctx(qf_info_T *qi, int copyID)
7235{
7236 int i;
7237 int abort = FALSE;
7238 typval_T *ctx;
7239
7240 for (i = 0; i < LISTCOUNT && !abort; ++i)
7241 {
7242 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007243 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7244 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007245 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
7246 }
7247
7248 return abort;
7249}
7250
7251/*
7252 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007253 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007254 */
7255 int
7256set_ref_in_quickfix(int copyID)
7257{
7258 int abort = FALSE;
7259 tabpage_T *tp;
7260 win_T *win;
7261
7262 abort = mark_quickfix_ctx(&ql_info, copyID);
7263 if (abort)
7264 return abort;
7265
7266 FOR_ALL_TAB_WINDOWS(tp, win)
7267 {
7268 if (win->w_llist != NULL)
7269 {
7270 abort = mark_quickfix_ctx(win->w_llist, copyID);
7271 if (abort)
7272 return abort;
7273 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007274 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7275 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007276 // In a location list window and none of the other windows is
7277 // referring to this location list. Mark the location list
7278 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007279 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7280 if (abort)
7281 return abort;
7282 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007283 }
7284
7285 return abort;
7286}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007287#endif
7288
Bram Moolenaar81695252004-12-29 20:58:21 +00007289/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007290 * Return the autocmd name for the :cbuffer Ex commands
7291 */
7292 static char_u *
7293cbuffer_get_auname(cmdidx_T cmdidx)
7294{
7295 switch (cmdidx)
7296 {
7297 case CMD_cbuffer: return (char_u *)"cbuffer";
7298 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7299 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7300 case CMD_lbuffer: return (char_u *)"lbuffer";
7301 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7302 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7303 default: return NULL;
7304 }
7305}
7306
7307/*
7308 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7309 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7310 */
7311 static int
7312cbuffer_process_args(
7313 exarg_T *eap,
7314 buf_T **bufp,
7315 linenr_T *line1,
7316 linenr_T *line2)
7317{
7318 buf_T *buf = NULL;
7319
7320 if (*eap->arg == NUL)
7321 buf = curbuf;
7322 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7323 buf = buflist_findnr(atoi((char *)eap->arg));
7324
7325 if (buf == NULL)
7326 {
7327 emsg(_(e_invarg));
7328 return FAIL;
7329 }
7330
7331 if (buf->b_ml.ml_mfp == NULL)
7332 {
7333 emsg(_("E681: Buffer is not loaded"));
7334 return FAIL;
7335 }
7336
7337 if (eap->addr_count == 0)
7338 {
7339 eap->line1 = 1;
7340 eap->line2 = buf->b_ml.ml_line_count;
7341 }
7342
7343 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7344 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7345 {
7346 emsg(_(e_invrange));
7347 return FAIL;
7348 }
7349
7350 *line1 = eap->line1;
7351 *line2 = eap->line2;
7352 *bufp = buf;
7353
7354 return OK;
7355}
7356
7357/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007358 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007359 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007360 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007361 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007362 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007363 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007364 */
7365 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007366ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007367{
7368 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007369 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007370 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007371 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007372 int_u save_qfid;
7373 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007374 char_u *qf_title;
7375 linenr_T line1;
7376 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007377
Bram Moolenaara16123a2019-03-28 20:31:07 +01007378 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007379 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007380 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007381 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007382#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007383 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007384 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007385#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007386 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007387
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007388 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007389 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7390 if (qi == NULL)
7391 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007392
Bram Moolenaara16123a2019-03-28 20:31:07 +01007393 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7394 return;
7395
7396 qf_title = qf_cmdtitle(*eap->cmdlinep);
7397
7398 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007399 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007400 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7401 (char *)qf_title, (char *)buf->b_sfname);
7402 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007403 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007404
7405 incr_quickfix_busy();
7406
7407 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7408 (eap->cmdidx != CMD_caddbuffer
7409 && eap->cmdidx != CMD_laddbuffer),
7410 line1, line2,
7411 qf_title, NULL);
7412 if (qf_stack_empty(qi))
7413 {
7414 decr_quickfix_busy();
7415 return;
7416 }
7417 if (res >= 0)
7418 qf_list_changed(qf_get_curlist(qi));
7419
7420 // Remember the current quickfix list identifier, so that we can
7421 // check for autocommands changing the current quickfix list.
7422 save_qfid = qf_get_curlist(qi)->qf_id;
7423 if (au_name != NULL)
7424 {
7425 buf_T *curbuf_old = curbuf;
7426
7427 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7428 TRUE, curbuf);
7429 if (curbuf != curbuf_old)
7430 // Autocommands changed buffer, don't jump now, "qi" may
7431 // be invalid.
7432 res = 0;
7433 }
7434 // Jump to the first error for a new list and if autocmds didn't
7435 // free the list.
7436 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7437 eap->cmdidx == CMD_lbuffer)
7438 && qflist_valid(wp, save_qfid))
7439 // display the first error
7440 qf_jump_first(qi, save_qfid, eap->forceit);
7441
7442 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007443}
7444
Bram Moolenaar1e015462005-09-25 22:16:38 +00007445#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007446/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007447 * Return the autocmd name for the :cexpr Ex commands.
7448 */
7449 static char_u *
7450cexpr_get_auname(cmdidx_T cmdidx)
7451{
7452 switch (cmdidx)
7453 {
7454 case CMD_cexpr: return (char_u *)"cexpr";
7455 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7456 case CMD_caddexpr: return (char_u *)"caddexpr";
7457 case CMD_lexpr: return (char_u *)"lexpr";
7458 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7459 case CMD_laddexpr: return (char_u *)"laddexpr";
7460 default: return NULL;
7461 }
7462}
7463
7464/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007465 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7466 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007467 */
7468 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007469ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007470{
7471 typval_T *tv;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007472 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007473 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007474 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007475 int_u save_qfid;
7476 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007477
Bram Moolenaara16123a2019-03-28 20:31:07 +01007478 au_name = cexpr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007479 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7480 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007481 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007482#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007483 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007484 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007485#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007486 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007487
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007488 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7489 if (qi == NULL)
7490 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007491
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007492 // Evaluate the expression. When the result is a string or a list we can
7493 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007494 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007495 if (tv != NULL)
7496 {
7497 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7498 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7499 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007500 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007501 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00007502 (eap->cmdidx != CMD_caddexpr
7503 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007504 (linenr_T)0, (linenr_T)0,
7505 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007506 if (qf_stack_empty(qi))
7507 {
7508 decr_quickfix_busy();
7509 goto cleanup;
7510 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01007511 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007512 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007513
7514 // Remember the current quickfix list identifier, so that we can
7515 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007516 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007517 if (au_name != NULL)
7518 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7519 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007520
7521 // Jump to the first error for a new list and if autocmds didn't
7522 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02007523 if (res > 0 && (eap->cmdidx == CMD_cexpr
7524 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007525 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02007526 // display the first error
7527 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007528 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00007529 }
7530 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007531 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007532cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00007533 free_tv(tv);
7534 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007535}
Bram Moolenaar1e015462005-09-25 22:16:38 +00007536#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007537
7538/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007539 * Get the location list for ":lhelpgrep"
7540 */
7541 static qf_info_T *
7542hgr_get_ll(int *new_ll)
7543{
7544 win_T *wp;
7545 qf_info_T *qi;
7546
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007547 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007548 if (bt_help(curwin->w_buffer))
7549 wp = curwin;
7550 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007551 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007552 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007553
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007554 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007555 qi = NULL;
7556 else
7557 qi = wp->w_llist;
7558
7559 if (qi == NULL)
7560 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007561 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007562 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007563 return NULL;
7564 *new_ll = TRUE;
7565 }
7566
7567 return qi;
7568}
7569
7570/*
7571 * Search for a pattern in a help file.
7572 */
7573 static void
7574hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007575 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007576 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007577 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007578 regmatch_T *p_regmatch)
7579{
7580 FILE *fd;
7581 long lnum;
7582
7583 fd = mch_fopen((char *)fname, "r");
7584 if (fd == NULL)
7585 return;
7586
7587 lnum = 1;
7588 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7589 {
7590 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007591
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007592 // Convert a line if 'encoding' is not utf-8 and
7593 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007594 if (p_vc->vc_type != CONV_NONE
7595 && has_non_ascii(IObuff))
7596 {
7597 line = string_convert(p_vc, IObuff, NULL);
7598 if (line == NULL)
7599 line = IObuff;
7600 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007601
7602 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7603 {
7604 int l = (int)STRLEN(line);
7605
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007606 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007607 while (l > 0 && line[l - 1] <= ' ')
7608 line[--l] = NUL;
7609
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007610 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007611 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007612 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007613 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007614 0,
7615 line,
7616 lnum,
7617 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007618 + 1, // col
7619 FALSE, // vis_col
7620 NULL, // search pattern
7621 0, // nr
7622 1, // type
7623 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02007624 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007625 {
7626 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007627 if (line != IObuff)
7628 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007629 break;
7630 }
7631 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007632 if (line != IObuff)
7633 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007634 ++lnum;
7635 line_breakcheck();
7636 }
7637 fclose(fd);
7638}
7639
7640/*
7641 * Search for a pattern in all the help files in the doc directory under
7642 * the given directory.
7643 */
7644 static void
7645hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007646 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007647 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007648 regmatch_T *p_regmatch,
7649 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007650#ifdef FEAT_MULTI_LANG
7651 , char_u *lang
7652#endif
7653 )
7654{
7655 int fcount;
7656 char_u **fnames;
7657 int fi;
7658
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007659 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007660 add_pathsep(dirname);
7661 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7662 if (gen_expand_wildcards(1, &dirname, &fcount,
7663 &fnames, EW_FILE|EW_SILENT) == OK
7664 && fcount > 0)
7665 {
7666 for (fi = 0; fi < fcount && !got_int; ++fi)
7667 {
7668#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007669 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007670 if (lang != NULL
7671 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007672 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007673 && !(STRNICMP(lang, "en", 2) == 0
7674 && STRNICMP("txt", fnames[fi]
7675 + STRLEN(fnames[fi]) - 3, 3) == 0))
7676 continue;
7677#endif
7678
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007679 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007680 }
7681 FreeWild(fcount, fnames);
7682 }
7683}
7684
7685/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007686 * Search for a pattern in all the help files in the 'runtimepath'
7687 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007688 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007689 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007690 */
7691 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007692hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007693{
7694 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007695
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007696 vimconv_T vc;
7697
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007698 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7699 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007700 vc.vc_type = CONV_NONE;
7701 if (!enc_utf8)
7702 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007703
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007704 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007705 p = p_rtp;
7706 while (*p != NUL && !got_int)
7707 {
7708 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7709
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007710 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007711#ifdef FEAT_MULTI_LANG
7712 , lang
7713#endif
7714 );
7715 }
7716
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007717 if (vc.vc_type != CONV_NONE)
7718 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007719}
7720
7721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 * ":helpgrep {pattern}"
7723 */
7724 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007725ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726{
7727 regmatch_T regmatch;
7728 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007729 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007730 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007731 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007732 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733
Bram Moolenaar73633f82012-01-20 13:39:07 +01007734 switch (eap->cmdidx)
7735 {
7736 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7737 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7738 default: break;
7739 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007740 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7741 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007742 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007743#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007744 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007745 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007746#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007747 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007748
Bram Moolenaar39665952018-08-15 20:59:48 +02007749 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007750 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007751 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007752 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007753 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007754 }
7755
Bram Moolenaara16123a2019-03-28 20:31:07 +01007756 // Make 'cpoptions' empty, the 'l' flag should not be used here.
7757 save_cpo = p_cpo;
7758 p_cpo = empty_option;
7759
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007760 incr_quickfix_busy();
7761
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007762#ifdef FEAT_MULTI_LANG
7763 // Check for a specified language
7764 lang = check_help_lang(eap->arg);
7765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7767 regmatch.rm_ic = FALSE;
7768 if (regmatch.regprog != NULL)
7769 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007770 qf_list_T *qfl;
7771
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007772 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007773 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007774 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007776 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007777
Bram Moolenaar473de612013-06-08 18:19:48 +02007778 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007780 qfl->qf_nonevalid = FALSE;
7781 qfl->qf_ptr = qfl->qf_start;
7782 qfl->qf_index = 1;
7783 qf_list_changed(qfl);
7784 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 }
7786
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007787 if (p_cpo == empty_option)
7788 p_cpo = save_cpo;
7789 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007790 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007791 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792
Bram Moolenaar73633f82012-01-20 13:39:07 +01007793 if (au_name != NULL)
7794 {
7795 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7796 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007797 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007798 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007799 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007800 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007801 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007802 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007803 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007804
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007805 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007806 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007807 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007808 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007809 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007810
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007811 decr_quickfix_busy();
7812
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007813 if (eap->cmdidx == CMD_lhelpgrep)
7814 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007815 // If the help window is not opened or if it already points to the
7816 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007817 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007818 {
7819 if (new_qi)
7820 ll_free_all(&qi);
7821 }
7822 else if (curwin->w_llist == NULL)
7823 curwin->w_llist = qi;
7824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825}
7826
7827#endif /* FEAT_QUICKFIX */