blob: 45354233701451e7cb6618596ee0c675a4a66b45 [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 Moolenaar071d4272004-06-13 20:20:40 +0000180
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200181// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000182#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200183// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000184#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200185
186// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100187#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
188#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
189#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
190#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200191
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000192/*
193 * Return location list for window 'wp'
194 * For location list window, return the referenced location list
195 */
196#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
197
Bram Moolenaara16123a2019-03-28 20:31:07 +0100198#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
199 for (i = 0, qfp = qfl->qf_start; \
200 !got_int && i < qfl->qf_count && qfp != NULL; \
201 ++i, qfp = qfp->qf_next)
202
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200204 * Looking up a buffer can be slow if there are many. Remember the last one
205 * to make this a lot faster if there are multiple matches in the same file.
206 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200207static char_u *qf_last_bufname = NULL;
208static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200209
Bram Moolenaar3c097222017-12-21 20:54:49 +0100210static char *e_loc_list_changed =
211 N_("E926: Current location list was changed");
212
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200213/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200214 * Maximum number of bytes allowed per line while reading a errorfile.
215 */
216#define LINE_MAXLEN 4096
217
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200218static struct fmtpattern
219{
220 char_u convchar;
221 char *pattern;
222} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200223 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200224 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200225 {'n', "\\d\\+"},
226 {'l', "\\d\\+"},
227 {'c', "\\d\\+"},
228 {'t', "."},
229 {'m', ".\\+"},
230 {'r', ".*"},
231 {'p', "[- .]*"},
232 {'v', "\\d\\+"},
233 {'s', ".\\+"},
234 {'o', ".\\+"}
235 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200236
237/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200238 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200239 * See fmt_pat definition above for the list of supported patterns. The
240 * pattern specifier is supplied in "efmpat". The converted pattern is stored
241 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200242 */
243 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200244efmpat_to_regpat(
245 char_u *efmpat,
246 char_u *regpat,
247 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200248 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100249 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200250{
251 char_u *srcptr;
252
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200253 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200255 // Each errorformat pattern can occur only once
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100256 semsg(_("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200257 return NULL;
258 }
259 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200260 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200261 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200262 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200263 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100264 semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200265 return NULL;
266 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200267 efminfo->addr[idx] = (char_u)++round;
268 *regpat++ = '\\';
269 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200270#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200271 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200272 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200273 // Also match "c:" in the file name, even when
274 // checking for a colon next: "%f:".
275 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200276 STRCPY(regpat, "\\%(\\a:\\)\\=");
277 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200278 }
279#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200280 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200282 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200283 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200284 // A file name may contain spaces, but this isn't
285 // in "\f". For "%f:%l:%m" there may be a ":" in
286 // the file name. Use ".\{-1,}x" instead (x is
287 // the next character), the requirement that :999:
288 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200289 STRCPY(regpat, ".\\{-1,}");
290 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200291 }
292 else
293 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200294 // File name followed by '\\' or '%': include as
295 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200296 STRCPY(regpat, "\\f\\+");
297 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200298 }
299 }
300 else
301 {
302 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200303 while ((*regpat = *srcptr++) != NUL)
304 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200305 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200306 *regpat++ = '\\';
307 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200308
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200309 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200310}
311
312/*
313 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200314 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200315 */
316 static char_u *
317scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200318 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200319 char_u *efm,
320 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100321 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200322{
323 char_u *efmp = *pefmp;
324
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200325 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200326 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200327 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200328 {
329 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200330 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200331 if (efmp < efm + len)
332 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200333 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200334 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200335 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200336 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200337 if (efmp == efm + len)
338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100339 emsg(_("E374: Missing ] in format string"));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200340 return NULL;
341 }
342 }
343 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200344 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200345 *regpat++ = *++efmp;
346 *regpat++ = '\\';
347 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200348 }
349 else
350 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200351 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100352 semsg(_("E375: Unsupported %%%c in format string"), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200353 return NULL;
354 }
355
356 *pefmp = efmp;
357
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200358 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200359}
360
361/*
362 * Analyze/parse an errorformat prefix.
363 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200364 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100365efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200366{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200367 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200368 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200369 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200370 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200371 else
372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100373 semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200374 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200375 }
376
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200377 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200378}
379
380/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200381 * Converts a 'errorformat' string part in 'efm' to a regular expression
382 * pattern. The resulting regex pattern is returned in "regpat". Additional
383 * information about the 'erroformat' pattern is returned in "fmt_ptr".
384 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200385 */
386 static int
387efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200388 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200389 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200390 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100391 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200392{
393 char_u *ptr;
394 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200395 int round;
396 int idx = 0;
397
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200398 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200399 ptr = regpat;
400 *ptr++ = '^';
401 round = 0;
402 for (efmp = efm; efmp < efm + len; ++efmp)
403 {
404 if (*efmp == '%')
405 {
406 ++efmp;
407 for (idx = 0; idx < FMT_PATTERNS; ++idx)
408 if (fmt_pat[idx].convchar == *efmp)
409 break;
410 if (idx < FMT_PATTERNS)
411 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100412 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200413 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200414 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200415 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200416 }
417 else if (*efmp == '*')
418 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200419 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100420 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200421 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200422 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200423 }
424 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200425 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200426 else if (*efmp == '#')
427 *ptr++ = '*';
428 else if (*efmp == '>')
429 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200430 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200431 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200432 // prefix is allowed only at the beginning of the errorformat
433 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100434 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200435 if (efmp == NULL)
436 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200437 }
438 else
439 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100440 semsg(_("E377: Invalid %%%c in format string"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200441 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200442 }
443 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200444 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200445 {
446 if (*efmp == '\\' && efmp + 1 < efm + len)
447 ++efmp;
448 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200449 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200450 if (*efmp)
451 *ptr++ = *efmp;
452 }
453 }
454 *ptr++ = '$';
455 *ptr = NUL;
456
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200457 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200458}
459
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200460/*
461 * Free the 'errorformat' information list
462 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200463 static void
464free_efm_list(efm_T **efm_first)
465{
466 efm_T *efm_ptr;
467
468 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
469 {
470 *efm_first = efm_ptr->next;
471 vim_regfree(efm_ptr->prog);
472 vim_free(efm_ptr);
473 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100474 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200475}
476
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200477/*
478 * Compute the size of the buffer used to convert a 'errorformat' pattern into
479 * a regular expression pattern.
480 */
481 static int
482efm_regpat_bufsz(char_u *efm)
483{
484 int sz;
485 int i;
486
487 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
488 for (i = FMT_PATTERNS; i > 0; )
489 sz += (int)STRLEN(fmt_pat[--i].pattern);
490#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200491 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200492#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200493 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200494#endif
495
496 return sz;
497}
498
499/*
500 * Return the length of a 'errorformat' option part (separated by ",").
501 */
502 static int
503efm_option_part_len(char_u *efm)
504{
505 int len;
506
507 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
508 if (efm[len] == '\\' && efm[len + 1] != NUL)
509 ++len;
510
511 return len;
512}
513
514/*
515 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
516 * are parsed and converted to regular expressions. Returns information about
517 * the parsed 'errorformat' option.
518 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200519 static efm_T *
520parse_efm_option(char_u *efm)
521{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200522 efm_T *fmt_ptr = NULL;
523 efm_T *fmt_first = NULL;
524 efm_T *fmt_last = NULL;
525 char_u *fmtstr = NULL;
526 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200527 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200528
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200529 // Each part of the format string is copied and modified from errorformat
530 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200531
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200532 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200533 sz = efm_regpat_bufsz(efm);
534 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200535 goto parse_efm_error;
536
537 while (efm[0] != NUL)
538 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200539 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200540 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
541 if (fmt_ptr == NULL)
542 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200543 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200544 fmt_first = fmt_ptr;
545 else
546 fmt_last->next = fmt_ptr;
547 fmt_last = fmt_ptr;
548
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200549 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200550 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200551
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100552 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200553 goto parse_efm_error;
554 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
555 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200556 // Advance to next part
557 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200558 }
559
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200560 if (fmt_first == NULL) // nothing found
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100561 emsg(_("E378: 'errorformat' contains no pattern"));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200562
563 goto parse_efm_end;
564
565parse_efm_error:
566 free_efm_list(&fmt_first);
567
568parse_efm_end:
569 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200570
571 return fmt_first;
572}
573
Bram Moolenaare0d37972016-07-15 22:36:01 +0200574enum {
575 QF_FAIL = 0,
576 QF_OK = 1,
577 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200578 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200579 QF_IGNORE_LINE = 4,
580 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200581};
582
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200583/*
584 * State information used to parse lines and add entries to a quickfix/location
585 * list.
586 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200587typedef struct {
588 char_u *linebuf;
589 int linelen;
590 char_u *growbuf;
591 int growbufsiz;
592 FILE *fd;
593 typval_T *tv;
594 char_u *p_str;
595 listitem_T *p_li;
596 buf_T *buf;
597 linenr_T buflnum;
598 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100599 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200600} qfstate_T;
601
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200602/*
603 * Allocate more memory for the line buffer used for parsing lines.
604 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200605 static char_u *
606qf_grow_linebuf(qfstate_T *state, int newsz)
607{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200608 char_u *p;
609
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200610 // If the line exceeds LINE_MAXLEN exclude the last
611 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200612 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
613 if (state->growbuf == NULL)
614 {
615 state->growbuf = alloc(state->linelen + 1);
616 if (state->growbuf == NULL)
617 return NULL;
618 state->growbufsiz = state->linelen;
619 }
620 else if (state->linelen > state->growbufsiz)
621 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200622 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200623 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200624 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200625 state->growbufsiz = state->linelen;
626 }
627 return state->growbuf;
628}
629
630/*
631 * Get the next string (separated by newline) from state->p_str.
632 */
633 static int
634qf_get_next_str_line(qfstate_T *state)
635{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200636 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200637 char_u *p_str = state->p_str;
638 char_u *p;
639 int len;
640
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200641 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200642 return QF_END_OF_INPUT;
643
644 p = vim_strchr(p_str, '\n');
645 if (p != NULL)
646 len = (int)(p - p_str) + 1;
647 else
648 len = (int)STRLEN(p_str);
649
650 if (len > IOSIZE - 2)
651 {
652 state->linebuf = qf_grow_linebuf(state, len);
653 if (state->linebuf == NULL)
654 return QF_NOMEM;
655 }
656 else
657 {
658 state->linebuf = IObuff;
659 state->linelen = len;
660 }
661 vim_strncpy(state->linebuf, p_str, state->linelen);
662
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200663 // Increment using len in order to discard the rest of the
664 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200665 p_str += len;
666 state->p_str = p_str;
667
668 return QF_OK;
669}
670
671/*
672 * Get the next string from state->p_Li.
673 */
674 static int
675qf_get_next_list_line(qfstate_T *state)
676{
677 listitem_T *p_li = state->p_li;
678 int len;
679
680 while (p_li != NULL
681 && (p_li->li_tv.v_type != VAR_STRING
682 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200683 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200684
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200685 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200686 {
687 state->p_li = NULL;
688 return QF_END_OF_INPUT;
689 }
690
691 len = (int)STRLEN(p_li->li_tv.vval.v_string);
692 if (len > IOSIZE - 2)
693 {
694 state->linebuf = qf_grow_linebuf(state, len);
695 if (state->linebuf == NULL)
696 return QF_NOMEM;
697 }
698 else
699 {
700 state->linebuf = IObuff;
701 state->linelen = len;
702 }
703
704 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
705
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200706 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200707 return QF_OK;
708}
709
710/*
711 * Get the next string from state->buf.
712 */
713 static int
714qf_get_next_buf_line(qfstate_T *state)
715{
716 char_u *p_buf = NULL;
717 int len;
718
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200719 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200720 if (state->buflnum > state->lnumlast)
721 return QF_END_OF_INPUT;
722
723 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
724 state->buflnum += 1;
725
726 len = (int)STRLEN(p_buf);
727 if (len > IOSIZE - 2)
728 {
729 state->linebuf = qf_grow_linebuf(state, len);
730 if (state->linebuf == NULL)
731 return QF_NOMEM;
732 }
733 else
734 {
735 state->linebuf = IObuff;
736 state->linelen = len;
737 }
738 vim_strncpy(state->linebuf, p_buf, state->linelen);
739
740 return QF_OK;
741}
742
743/*
744 * Get the next string from file state->fd.
745 */
746 static int
747qf_get_next_file_line(qfstate_T *state)
748{
749 int discard;
750 int growbuflen;
751
752 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
753 return QF_END_OF_INPUT;
754
755 discard = FALSE;
756 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200757 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200758 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200759 // The current line exceeds IObuff, continue reading using
760 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200761 if (state->growbuf == NULL)
762 {
763 state->growbufsiz = 2 * (IOSIZE - 1);
764 state->growbuf = alloc(state->growbufsiz);
765 if (state->growbuf == NULL)
766 return QF_NOMEM;
767 }
768
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200769 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200770 memcpy(state->growbuf, IObuff, IOSIZE - 1);
771 growbuflen = state->linelen;
772
773 for (;;)
774 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200775 char_u *p;
776
Bram Moolenaare0d37972016-07-15 22:36:01 +0200777 if (fgets((char *)state->growbuf + growbuflen,
778 state->growbufsiz - growbuflen, state->fd) == NULL)
779 break;
780 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
781 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200782 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200783 break;
784 if (state->growbufsiz == LINE_MAXLEN)
785 {
786 discard = TRUE;
787 break;
788 }
789
790 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
791 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200792 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200793 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200794 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200795 }
796
797 while (discard)
798 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200799 // The current line is longer than LINE_MAXLEN, continue
800 // reading but discard everything until EOL or EOF is
801 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200802 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
803 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200804 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200805 break;
806 }
807
808 state->linebuf = state->growbuf;
809 state->linelen = growbuflen;
810 }
811 else
812 state->linebuf = IObuff;
813
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200814 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200815 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
816 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100817 char_u *line;
818
819 line = string_convert(&state->vc, state->linebuf, &state->linelen);
820 if (line != NULL)
821 {
822 if (state->linelen < IOSIZE)
823 {
824 STRCPY(state->linebuf, line);
825 vim_free(line);
826 }
827 else
828 {
829 vim_free(state->growbuf);
830 state->linebuf = state->growbuf = line;
831 state->growbufsiz = state->linelen < LINE_MAXLEN
832 ? state->linelen : LINE_MAXLEN;
833 }
834 }
835 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100836
Bram Moolenaare0d37972016-07-15 22:36:01 +0200837 return QF_OK;
838}
839
840/*
841 * Get the next string from a file/buffer/list/string.
842 */
843 static int
844qf_get_nextline(qfstate_T *state)
845{
846 int status = QF_FAIL;
847
848 if (state->fd == NULL)
849 {
850 if (state->tv != NULL)
851 {
852 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200853 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200854 status = qf_get_next_str_line(state);
855 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200856 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200857 status = qf_get_next_list_line(state);
858 }
859 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200860 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200861 status = qf_get_next_buf_line(state);
862 }
863 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200864 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200865 status = qf_get_next_file_line(state);
866
867 if (status != QF_OK)
868 return status;
869
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200870 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200871 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200872 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200873 state->linebuf[state->linelen - 1] = NUL;
874#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200875 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
876 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200877#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200878 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200879
Bram Moolenaare0d37972016-07-15 22:36:01 +0200880 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200881
882 return QF_OK;
883}
884
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200885typedef struct {
886 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200887 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200888 char_u *errmsg;
889 int errmsglen;
890 long lnum;
891 int col;
892 char_u use_viscol;
893 char_u *pattern;
894 int enr;
895 int type;
896 int valid;
897} qffields_T;
898
899/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200900 * Parse the match for filename ('%f') pattern in regmatch.
901 * Return the matched value in "fields->namebuf".
902 */
903 static int
904qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
905{
906 int c;
907
908 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
909 return QF_FAIL;
910
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200911 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200912 c = *rmp->endp[midx];
913 *rmp->endp[midx] = NUL;
914 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
915 *rmp->endp[midx] = c;
916
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200917 // For separate filename patterns (%O, %P and %Q), the specified file
918 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200919 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
920 && mch_getperm(fields->namebuf) == -1)
921 return QF_FAIL;
922
923 return QF_OK;
924}
925
926/*
927 * Parse the match for error number ('%n') pattern in regmatch.
928 * Return the matched value in "fields->enr".
929 */
930 static int
931qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
932{
933 if (rmp->startp[midx] == NULL)
934 return QF_FAIL;
935 fields->enr = (int)atol((char *)rmp->startp[midx]);
936 return QF_OK;
937}
938
939/*
940 * Parse the match for line number (%l') pattern in regmatch.
941 * Return the matched value in "fields->lnum".
942 */
943 static int
944qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
945{
946 if (rmp->startp[midx] == NULL)
947 return QF_FAIL;
948 fields->lnum = atol((char *)rmp->startp[midx]);
949 return QF_OK;
950}
951
952/*
953 * Parse the match for column number ('%c') pattern in regmatch.
954 * Return the matched value in "fields->col".
955 */
956 static int
957qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
958{
959 if (rmp->startp[midx] == NULL)
960 return QF_FAIL;
961 fields->col = (int)atol((char *)rmp->startp[midx]);
962 return QF_OK;
963}
964
965/*
966 * Parse the match for error type ('%t') pattern in regmatch.
967 * Return the matched value in "fields->type".
968 */
969 static int
970qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
971{
972 if (rmp->startp[midx] == NULL)
973 return QF_FAIL;
974 fields->type = *rmp->startp[midx];
975 return QF_OK;
976}
977
978/*
979 * Parse the match for '%+' format pattern. The whole matching line is included
980 * in the error string. Return the matched line in "fields->errmsg".
981 */
982 static int
983qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
984{
985 char_u *p;
986
987 if (linelen >= fields->errmsglen)
988 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200989 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200990 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
991 return QF_NOMEM;
992 fields->errmsg = p;
993 fields->errmsglen = linelen + 1;
994 }
995 vim_strncpy(fields->errmsg, linebuf, linelen);
996 return QF_OK;
997}
998
999/*
1000 * Parse the match for error message ('%m') pattern in regmatch.
1001 * Return the matched value in "fields->errmsg".
1002 */
1003 static int
1004qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1005{
1006 char_u *p;
1007 int len;
1008
1009 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1010 return QF_FAIL;
1011 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1012 if (len >= fields->errmsglen)
1013 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001014 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001015 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1016 return QF_NOMEM;
1017 fields->errmsg = p;
1018 fields->errmsglen = len + 1;
1019 }
1020 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1021 return QF_OK;
1022}
1023
1024/*
1025 * Parse the match for rest of a single-line file message ('%r') pattern.
1026 * Return the matched value in "tail".
1027 */
1028 static int
1029qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1030{
1031 if (rmp->startp[midx] == NULL)
1032 return QF_FAIL;
1033 *tail = rmp->startp[midx];
1034 return QF_OK;
1035}
1036
1037/*
1038 * Parse the match for the pointer line ('%p') pattern in regmatch.
1039 * Return the matched value in "fields->col".
1040 */
1041 static int
1042qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1043{
1044 char_u *match_ptr;
1045
1046 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1047 return QF_FAIL;
1048 fields->col = 0;
1049 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1050 ++match_ptr)
1051 {
1052 ++fields->col;
1053 if (*match_ptr == TAB)
1054 {
1055 fields->col += 7;
1056 fields->col -= fields->col % 8;
1057 }
1058 }
1059 ++fields->col;
1060 fields->use_viscol = TRUE;
1061 return QF_OK;
1062}
1063
1064/*
1065 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1066 * Return the matched value in "fields->col".
1067 */
1068 static int
1069qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1070{
1071 if (rmp->startp[midx] == NULL)
1072 return QF_FAIL;
1073 fields->col = (int)atol((char *)rmp->startp[midx]);
1074 fields->use_viscol = TRUE;
1075 return QF_OK;
1076}
1077
1078/*
1079 * Parse the match for the search text ('%s') pattern in regmatch.
1080 * Return the matched value in "fields->pattern".
1081 */
1082 static int
1083qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1084{
1085 int len;
1086
1087 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1088 return QF_FAIL;
1089 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1090 if (len > CMDBUFFSIZE - 5)
1091 len = CMDBUFFSIZE - 5;
1092 STRCPY(fields->pattern, "^\\V");
1093 STRNCAT(fields->pattern, rmp->startp[midx], len);
1094 fields->pattern[len + 3] = '\\';
1095 fields->pattern[len + 4] = '$';
1096 fields->pattern[len + 5] = NUL;
1097 return QF_OK;
1098}
1099
1100/*
1101 * Parse the match for the module ('%o') pattern in regmatch.
1102 * Return the matched value in "fields->module".
1103 */
1104 static int
1105qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1106{
1107 int len;
1108
1109 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1110 return QF_FAIL;
1111 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1112 if (len > CMDBUFFSIZE)
1113 len = CMDBUFFSIZE;
1114 STRNCAT(fields->module, rmp->startp[midx], len);
1115 return QF_OK;
1116}
1117
1118/*
1119 * 'errorformat' format pattern parser functions.
1120 * The '%f' and '%r' formats are parsed differently from other formats.
1121 * See qf_parse_match() for details.
1122 */
1123static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1124{
1125 NULL,
1126 qf_parse_fmt_n,
1127 qf_parse_fmt_l,
1128 qf_parse_fmt_c,
1129 qf_parse_fmt_t,
1130 qf_parse_fmt_m,
1131 NULL,
1132 qf_parse_fmt_p,
1133 qf_parse_fmt_v,
1134 qf_parse_fmt_s,
1135 qf_parse_fmt_o
1136};
1137
1138/*
1139 * Parse the error format pattern matches in "regmatch" and set the values in
1140 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1141 * match. Returns QF_OK if all the matches are successfully parsed. On
1142 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001143 */
1144 static int
1145qf_parse_match(
1146 char_u *linebuf,
1147 int linelen,
1148 efm_T *fmt_ptr,
1149 regmatch_T *regmatch,
1150 qffields_T *fields,
1151 int qf_multiline,
1152 int qf_multiscan,
1153 char_u **tail)
1154{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001155 int idx = fmt_ptr->prefix;
1156 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001157 int midx;
1158 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001159
1160 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1161 return QF_FAIL;
1162 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1163 fields->type = idx;
1164 else
1165 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001166
1167 // Extract error message data from matched line.
1168 // We check for an actual submatch, because "\[" and "\]" in
1169 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001170 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001171 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001172 status = QF_OK;
1173 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001174 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001175 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1176 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001177 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001178 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001179 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001180 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001181 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001182 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001183 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001184 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001185 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001186 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001187
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001188 if (status != QF_OK)
1189 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001190 }
1191
1192 return QF_OK;
1193}
1194
1195/*
1196 * Parse an error line in 'linebuf' using a single error format string in
1197 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1198 * Returns QF_OK if the efm format matches completely and the fields are
1199 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1200 */
1201 static int
1202qf_parse_get_fields(
1203 char_u *linebuf,
1204 int linelen,
1205 efm_T *fmt_ptr,
1206 qffields_T *fields,
1207 int qf_multiline,
1208 int qf_multiscan,
1209 char_u **tail)
1210{
1211 regmatch_T regmatch;
1212 int status = QF_FAIL;
1213 int r;
1214
1215 if (qf_multiscan &&
1216 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1217 return QF_FAIL;
1218
1219 fields->namebuf[0] = NUL;
1220 fields->module[0] = NUL;
1221 fields->pattern[0] = NUL;
1222 if (!qf_multiscan)
1223 fields->errmsg[0] = NUL;
1224 fields->lnum = 0;
1225 fields->col = 0;
1226 fields->use_viscol = FALSE;
1227 fields->enr = -1;
1228 fields->type = 0;
1229 *tail = NULL;
1230
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001231 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001232 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001233 regmatch.regprog = fmt_ptr->prog;
1234 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1235 fmt_ptr->prog = regmatch.regprog;
1236 if (r)
1237 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1238 fields, qf_multiline, qf_multiscan, tail);
1239
1240 return status;
1241}
1242
1243/*
1244 * Parse directory error format prefixes (%D and %X).
1245 * Push and pop directories from the directory stack when scanning directory
1246 * names.
1247 */
1248 static int
1249qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1250{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001251 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001252 {
1253 if (*fields->namebuf == NUL)
1254 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001255 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001256 return QF_FAIL;
1257 }
1258 qfl->qf_directory =
1259 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1260 if (qfl->qf_directory == NULL)
1261 return QF_FAIL;
1262 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001263 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001264 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1265
1266 return QF_OK;
1267}
1268
1269/*
1270 * Parse global file name error format prefixes (%O, %P and %Q).
1271 */
1272 static int
1273qf_parse_file_pfx(
1274 int idx,
1275 qffields_T *fields,
1276 qf_list_T *qfl,
1277 char_u *tail)
1278{
1279 fields->valid = FALSE;
1280 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1281 {
1282 if (*fields->namebuf && idx == 'P')
1283 qfl->qf_currfile =
1284 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1285 else if (idx == 'Q')
1286 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1287 *fields->namebuf = NUL;
1288 if (tail && *tail)
1289 {
1290 STRMOVE(IObuff, skipwhite(tail));
1291 qfl->qf_multiscan = TRUE;
1292 return QF_MULTISCAN;
1293 }
1294 }
1295
1296 return QF_OK;
1297}
1298
1299/*
1300 * Parse a non-error line (a line which doesn't match any of the error
1301 * format in 'efm').
1302 */
1303 static int
1304qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1305{
1306 char_u *p;
1307
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001308 fields->namebuf[0] = NUL; // no match found, remove file name
1309 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001310 fields->valid = FALSE;
1311 if (linelen >= fields->errmsglen)
1312 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001313 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001314 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1315 return QF_NOMEM;
1316 fields->errmsg = p;
1317 fields->errmsglen = linelen + 1;
1318 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001319 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001320 vim_strncpy(fields->errmsg, linebuf, linelen);
1321
1322 return QF_OK;
1323}
1324
1325/*
1326 * Parse multi-line error format prefixes (%C and %Z)
1327 */
1328 static int
1329qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001330 int idx,
1331 qf_list_T *qfl,
1332 qffields_T *fields)
1333{
1334 char_u *ptr;
1335 int len;
1336
1337 if (!qfl->qf_multiignore)
1338 {
1339 qfline_T *qfprev = qfl->qf_last;
1340
1341 if (qfprev == NULL)
1342 return QF_FAIL;
1343 if (*fields->errmsg && !qfl->qf_multiignore)
1344 {
1345 len = (int)STRLEN(qfprev->qf_text);
1346 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1347 == NULL)
1348 return QF_FAIL;
1349 STRCPY(ptr, qfprev->qf_text);
1350 vim_free(qfprev->qf_text);
1351 qfprev->qf_text = ptr;
1352 *(ptr += len) = '\n';
1353 STRCPY(++ptr, fields->errmsg);
1354 }
1355 if (qfprev->qf_nr == -1)
1356 qfprev->qf_nr = fields->enr;
1357 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001358 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001359 qfprev->qf_type = fields->type;
1360
1361 if (!qfprev->qf_lnum)
1362 qfprev->qf_lnum = fields->lnum;
1363 if (!qfprev->qf_col)
1364 qfprev->qf_col = fields->col;
1365 qfprev->qf_viscol = fields->use_viscol;
1366 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001367 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001368 qfl->qf_directory,
1369 *fields->namebuf || qfl->qf_directory != NULL
1370 ? fields->namebuf
1371 : qfl->qf_currfile != NULL && fields->valid
1372 ? qfl->qf_currfile : 0);
1373 }
1374 if (idx == 'Z')
1375 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1376 line_breakcheck();
1377
1378 return QF_IGNORE_LINE;
1379}
1380
1381/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001382 * Parse a line and get the quickfix fields.
1383 * Return the QF_ status.
1384 */
1385 static int
1386qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001387 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001388 char_u *linebuf,
1389 int linelen,
1390 efm_T *fmt_first,
1391 qffields_T *fields)
1392{
1393 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001394 int idx = 0;
1395 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001396 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001397
Bram Moolenaare333e792018-04-08 13:27:39 +02001398restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001399 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001400 if (fmt_start == NULL)
1401 fmt_ptr = fmt_first;
1402 else
1403 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001404 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001405 fmt_ptr = fmt_start;
1406 fmt_start = NULL;
1407 }
1408
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001409 // Try to match each part of 'errorformat' until we find a complete
1410 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001411 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001412 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1413 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001414 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001415 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1416 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1417 if (status == QF_NOMEM)
1418 return status;
1419 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001420 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001421 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001422 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001423
1424 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1425 {
1426 if (fmt_ptr != NULL)
1427 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001428 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001429 status = qf_parse_dir_pfx(idx, fields, qfl);
1430 if (status != QF_OK)
1431 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001432 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001433
1434 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1435 if (status != QF_OK)
1436 return status;
1437
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001438 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001439 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001440 }
1441 else if (fmt_ptr != NULL)
1442 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001443 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001444 if (fmt_ptr->conthere)
1445 fmt_start = fmt_ptr;
1446
1447 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1448 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001449 qfl->qf_multiline = TRUE; // start of a multi-line message
1450 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001451 }
1452 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001453 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001454 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001455 if (status != QF_OK)
1456 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001457 }
1458 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001459 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001460 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1461 if (status == QF_MULTISCAN)
1462 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001463 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001464 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001465 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001466 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001467 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001468 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001469 return QF_IGNORE_LINE;
1470 }
1471 }
1472
1473 return QF_OK;
1474}
1475
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001476/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001477 * Returns TRUE if the specified quickfix/location stack is empty
1478 */
1479 static int
1480qf_stack_empty(qf_info_T *qi)
1481{
1482 return qi == NULL || qi->qf_listcount <= 0;
1483}
1484
1485/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001486 * Returns TRUE if the specified quickfix/location list is empty.
1487 */
1488 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001489qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001490{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001491 return qfl == NULL || qfl->qf_count <= 0;
1492}
1493
1494/*
1495 * Return a pointer to a list in the specified quickfix stack
1496 */
1497 static qf_list_T *
1498qf_get_list(qf_info_T *qi, int idx)
1499{
1500 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001501}
1502
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001503/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001504 * Allocate the fields used for parsing lines and populating a quickfix list.
1505 */
1506 static int
1507qf_alloc_fields(qffields_T *pfields)
1508{
1509 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1510 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1511 pfields->errmsglen = CMDBUFFSIZE + 1;
1512 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1513 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1514 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1515 || pfields->pattern == NULL || pfields->module == NULL)
1516 return FAIL;
1517
1518 return OK;
1519}
1520
1521/*
1522 * Free the fields used for parsing lines and populating a quickfix list.
1523 */
1524 static void
1525qf_free_fields(qffields_T *pfields)
1526{
1527 vim_free(pfields->namebuf);
1528 vim_free(pfields->module);
1529 vim_free(pfields->errmsg);
1530 vim_free(pfields->pattern);
1531}
1532
1533/*
1534 * Setup the state information used for parsing lines and populating a
1535 * quickfix list.
1536 */
1537 static int
1538qf_setup_state(
1539 qfstate_T *pstate,
1540 char_u *enc,
1541 char_u *efile,
1542 typval_T *tv,
1543 buf_T *buf,
1544 linenr_T lnumfirst,
1545 linenr_T lnumlast)
1546{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001547 pstate->vc.vc_type = CONV_NONE;
1548 if (enc != NULL && *enc != NUL)
1549 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001550
1551 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1552 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001553 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001554 return FAIL;
1555 }
1556
1557 if (tv != NULL)
1558 {
1559 if (tv->v_type == VAR_STRING)
1560 pstate->p_str = tv->vval.v_string;
1561 else if (tv->v_type == VAR_LIST)
1562 pstate->p_li = tv->vval.v_list->lv_first;
1563 pstate->tv = tv;
1564 }
1565 pstate->buf = buf;
1566 pstate->buflnum = lnumfirst;
1567 pstate->lnumlast = lnumlast;
1568
1569 return OK;
1570}
1571
1572/*
1573 * Cleanup the state information used for parsing lines and populating a
1574 * quickfix list.
1575 */
1576 static void
1577qf_cleanup_state(qfstate_T *pstate)
1578{
1579 if (pstate->fd != NULL)
1580 fclose(pstate->fd);
1581
1582 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001583 if (pstate->vc.vc_type != CONV_NONE)
1584 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001585}
1586
1587/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001588 * Read the errorfile "efile" into memory, line by line, building the error
1589 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001590 * Alternative: when "efile" is NULL read errors from buffer "buf".
1591 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001592 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001593 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1594 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001595 * Return -1 for error, number of errors for success.
1596 */
1597 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001598qf_init_ext(
1599 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001600 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001601 char_u *efile,
1602 buf_T *buf,
1603 typval_T *tv,
1604 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001605 int newlist, // TRUE: start a new error list
1606 linenr_T lnumfirst, // first line number to use
1607 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001608 char_u *qf_title,
1609 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001610{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001611 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001612 qfstate_T state;
1613 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001614 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001615 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001616 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001618 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001619 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001620 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001622 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001623 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001624
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001625 vim_memset(&state, 0, sizeof(state));
1626 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001627 if ((qf_alloc_fields(&fields) == FAIL) ||
1628 (qf_setup_state(&state, enc, efile, tv, buf,
1629 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 goto qf_init_end;
1631
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001632 if (newlist || qf_idx == qi->qf_listcount)
1633 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001634 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001635 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001636 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001637 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001638 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001639 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001640 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001641 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001642 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001643 qfl = qf_get_list(qi, qf_idx);
1644 if (!qf_list_empty(qfl))
1645 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001648 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001649 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001650 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 else
1652 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001654 // If the errorformat didn't change between calls, then reuse the
1655 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001656 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1657 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001658 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001659 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001660 free_efm_list(&fmt_first);
1661
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001662 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001663 fmt_first = parse_efm_option(efm);
1664 if (fmt_first != NULL)
1665 last_efm = vim_strsave(efm);
1666 }
1667
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001668 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001671 // got_int is reset here, because it was probably set when killing the
1672 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 got_int = FALSE;
1674
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001675 // Read the lines in the error file one by one.
1676 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001677 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001679 // Get the next line from a file/buffer/list/string
Bram Moolenaare0d37972016-07-15 22:36:01 +02001680 status = qf_get_nextline(&state);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001681 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001682 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001683 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001684 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaar0398e002019-03-21 21:12:49 +01001686 status = qf_parse_line(qfl, state.linebuf, state.linelen,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001687 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001688 if (status == QF_FAIL)
1689 goto error2;
1690 if (status == QF_NOMEM)
1691 goto qf_init_end;
1692 if (status == QF_IGNORE_LINE)
1693 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
Bram Moolenaar0398e002019-03-21 21:12:49 +01001695 if (qf_add_entry(qfl,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001696 qfl->qf_directory,
1697 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001698 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001699 : ((qfl->qf_currfile != NULL && fields.valid)
1700 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001701 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001702 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001703 fields.errmsg,
1704 fields.lnum,
1705 fields.col,
1706 fields.use_viscol,
1707 fields.pattern,
1708 fields.enr,
1709 fields.type,
1710 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 goto error2;
1712 line_breakcheck();
1713 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001714 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001716 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001718 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001719 qfl->qf_ptr = qfl->qf_start;
1720 qfl->qf_index = 1;
1721 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 }
1723 else
1724 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001725 qfl->qf_nonevalid = FALSE;
1726 if (qfl->qf_ptr == NULL)
1727 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001729 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001730 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001731 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001733 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001735 if (!adding)
1736 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001737 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001738 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001739 qi->qf_listcount--;
1740 if (qi->qf_curlist > 0)
1741 --qi->qf_curlist;
1742 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001743qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001744 if (qf_idx == qi->qf_curlist)
1745 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001746 qf_cleanup_state(&state);
1747 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
1749 return retval;
1750}
1751
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001752/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001753 * Read the errorfile "efile" into memory, line by line, building the error
1754 * list. Set the error list's title to qf_title.
1755 * Return -1 for error, number of errors for success.
1756 */
1757 int
1758qf_init(win_T *wp,
1759 char_u *efile,
1760 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001761 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001762 char_u *qf_title,
1763 char_u *enc)
1764{
1765 qf_info_T *qi = &ql_info;
1766
1767 if (wp != NULL)
1768 {
1769 qi = ll_get_or_alloc_list(wp);
1770 if (qi == NULL)
1771 return FAIL;
1772 }
1773
1774 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1775 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1776}
1777
1778/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001779 * Set the title of the specified quickfix list. Frees the previous title.
1780 * Prepends ':' to the title.
1781 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001782 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001783qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001784{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001785 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001786
Bram Moolenaarfb604092014-07-23 15:55:00 +02001787 if (title != NULL)
1788 {
1789 char_u *p = alloc((int)STRLEN(title) + 2);
1790
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001791 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001792 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001793 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001794 }
1795}
1796
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001798 * The title of a quickfix/location list is set, by default, to the command
1799 * that created the quickfix list with the ":" prefix.
1800 * Create a quickfix list title string by prepending ":" to a user command.
1801 * Returns a pointer to a static buffer with the title.
1802 */
1803 static char_u *
1804qf_cmdtitle(char_u *cmd)
1805{
1806 static char_u qftitle_str[IOSIZE];
1807
1808 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1809 return qftitle_str;
1810}
1811
1812/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001813 * Return a pointer to the current list in the specified quickfix stack
1814 */
1815 static qf_list_T *
1816qf_get_curlist(qf_info_T *qi)
1817{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001818 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001819}
1820
1821/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001822 * Prepare for adding a new quickfix list. If the current list is in the
1823 * middle of the stack, then all the following lists are freed and then
1824 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 */
1826 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001827qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001830 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001832 // If the current entry is not the last entry, delete entries beyond
1833 // the current entry. This makes it possible to browse in a tree-like
1834 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001835 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001836 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001838 // When the stack is full, remove to oldest entry
1839 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001840 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001842 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001844 qi->qf_lists[i - 1] = qi->qf_lists[i];
1845 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
1847 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001848 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001849 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001850 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1851 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001852 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001853 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854}
1855
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001856/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001857 * Queue location list stack delete request.
1858 */
1859 static void
1860locstack_queue_delreq(qf_info_T *qi)
1861{
1862 qf_delq_T *q;
1863
1864 q = (qf_delq_T *)alloc((unsigned)sizeof(qf_delq_T));
1865 if (q != NULL)
1866 {
1867 q->qi = qi;
1868 q->next = qf_delq_head;
1869 qf_delq_head = q;
1870 }
1871}
1872
1873/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001874 * Return the global quickfix stack window buffer number.
1875 */
1876 int
1877qf_stack_get_bufnr(void)
1878{
1879 return ql_info.qf_bufnr;
1880}
1881
1882/*
1883 * Wipe the quickfix window buffer (if present) for the specified
1884 * quickfix/location list.
1885 */
1886 static void
1887wipe_qf_buffer(qf_info_T *qi)
1888{
1889 buf_T *qfbuf;
1890
1891 if (qi->qf_bufnr != INVALID_QFBUFNR)
1892 {
1893 qfbuf = buflist_findnr(qi->qf_bufnr);
1894 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1895 {
1896 // If the quickfix buffer is not loaded in any window, then
1897 // wipe the buffer.
1898 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE);
1899 qi->qf_bufnr = INVALID_QFBUFNR;
1900 }
1901 }
1902}
1903
1904/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001905 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001906 */
1907 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001908ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001909{
1910 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001911 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001912
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001913 qi = *pqi;
1914 if (qi == NULL)
1915 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001916 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001917
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001918 // If the location list is still in use, then queue the delete request
1919 // to be processed later.
1920 if (quickfix_busy > 0)
1921 {
1922 locstack_queue_delreq(qi);
1923 return;
1924 }
1925
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001926 qi->qf_refcount--;
1927 if (qi->qf_refcount < 1)
1928 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001929 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001930 // If the quickfix window buffer is loaded, then wipe it
1931 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001932
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001933 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001934 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001935 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001936 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001937}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001938
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001939/*
1940 * Free all the quickfix/location lists in the stack.
1941 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001942 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001943qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001944{
1945 int i;
1946 qf_info_T *qi = &ql_info;
1947
1948 if (wp != NULL)
1949 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001950 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001951 ll_free_all(&wp->w_llist);
1952 ll_free_all(&wp->w_llist_ref);
1953 }
1954 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001955 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001956 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001957 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001958}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001959
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001961 * Delay freeing of location list stacks when the quickfix code is running.
1962 * Used to avoid problems with autocmds freeing location list stacks when the
1963 * quickfix code is still referencing the stack.
1964 * Must always call decr_quickfix_busy() exactly once after this.
1965 */
1966 static void
1967incr_quickfix_busy(void)
1968{
1969 quickfix_busy++;
1970}
1971
1972/*
1973 * Safe to free location list stacks. Process any delayed delete requests.
1974 */
1975 static void
1976decr_quickfix_busy(void)
1977{
1978 if (--quickfix_busy == 0)
1979 {
1980 // No longer referencing the location lists. Process all the pending
1981 // delete requests.
1982 while (qf_delq_head != NULL)
1983 {
1984 qf_delq_T *q = qf_delq_head;
1985
1986 qf_delq_head = q->next;
1987 ll_free_all(&q->qi);
1988 vim_free(q);
1989 }
1990 }
1991#ifdef ABORT_ON_INTERNAL_ERROR
1992 if (quickfix_busy < 0)
1993 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001994 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001995 abort();
1996 }
1997#endif
1998}
1999
2000#if defined(EXITFREE) || defined(PROTO)
2001 void
2002check_quickfix_busy(void)
2003{
2004 if (quickfix_busy != 0)
2005 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002006 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002007# ifdef ABORT_ON_INTERNAL_ERROR
2008 abort();
2009# endif
2010 }
2011}
2012#endif
2013
2014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 * Add an entry to the end of the list of errors.
2016 * Returns OK or FAIL.
2017 */
2018 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002019qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002020 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002021 char_u *dir, // optional directory name
2022 char_u *fname, // file name or NULL
2023 char_u *module, // module name or NULL
2024 int bufnum, // buffer number or zero
2025 char_u *mesg, // message
2026 long lnum, // line number
2027 int col, // column
2028 int vis_col, // using visual column
2029 char_u *pattern, // search pattern
2030 int nr, // error number
2031 int type, // type character
2032 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002034 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002035 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002037 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002039 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002040 {
2041 buf_T *buf = buflist_findnr(bufnum);
2042
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002043 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002044 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002045 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002046 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002047 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002048 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002049 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2051 {
2052 vim_free(qfp);
2053 return FAIL;
2054 }
2055 qfp->qf_lnum = lnum;
2056 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002057 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002058 if (pattern == NULL || *pattern == NUL)
2059 qfp->qf_pattern = NULL;
2060 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2061 {
2062 vim_free(qfp->qf_text);
2063 vim_free(qfp);
2064 return FAIL;
2065 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002066 if (module == NULL || *module == NUL)
2067 qfp->qf_module = NULL;
2068 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2069 {
2070 vim_free(qfp->qf_text);
2071 vim_free(qfp->qf_pattern);
2072 vim_free(qfp);
2073 return FAIL;
2074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002076 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 type = 0;
2078 qfp->qf_type = type;
2079 qfp->qf_valid = valid;
2080
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002081 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002082 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002084 qfl->qf_start = qfp;
2085 qfl->qf_ptr = qfp;
2086 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002087 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
2089 else
2090 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002091 qfp->qf_prev = *lastp;
2092 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002094 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002096 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002097 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002098 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002100 qfl->qf_index = qfl->qf_count;
2101 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 }
2103
2104 return OK;
2105}
2106
2107/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002108 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002109 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002110 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002111qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002112{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002113 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002114
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002115 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002116 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002117 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002118 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002119 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002120 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002121 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002122 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002123}
2124
2125/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002126 * Return the location list stack for window 'wp'.
2127 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002128 */
2129 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002130ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002131{
2132 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002133 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002134 return wp->w_llist_ref;
2135
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002136 // For a non-location list window, w_llist_ref should not point to a
2137 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002138 ll_free_all(&wp->w_llist_ref);
2139
2140 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002141 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002142 return wp->w_llist;
2143}
2144
2145/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002146 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2147 */
2148 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002149copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002150{
2151 int i;
2152 qfline_T *from_qfp;
2153 qfline_T *prevp;
2154
2155 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002156 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002157 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002158 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002159 NULL,
2160 NULL,
2161 from_qfp->qf_module,
2162 0,
2163 from_qfp->qf_text,
2164 from_qfp->qf_lnum,
2165 from_qfp->qf_col,
2166 from_qfp->qf_viscol,
2167 from_qfp->qf_pattern,
2168 from_qfp->qf_nr,
2169 0,
2170 from_qfp->qf_valid) == FAIL)
2171 return FAIL;
2172
2173 // qf_add_entry() will not set the qf_num field, as the
2174 // directory and file names are not supplied. So the qf_fnum
2175 // field is copied here.
2176 prevp = to_qfl->qf_last;
2177 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2178 prevp->qf_type = from_qfp->qf_type; // error type
2179 if (from_qfl->qf_ptr == from_qfp)
2180 to_qfl->qf_ptr = prevp; // current location
2181 }
2182
2183 return OK;
2184}
2185
2186/*
2187 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2188 */
2189 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002190copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002191{
2192 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002193 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002194 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2195 to_qfl->qf_count = 0;
2196 to_qfl->qf_index = 0;
2197 to_qfl->qf_start = NULL;
2198 to_qfl->qf_last = NULL;
2199 to_qfl->qf_ptr = NULL;
2200 if (from_qfl->qf_title != NULL)
2201 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2202 else
2203 to_qfl->qf_title = NULL;
2204 if (from_qfl->qf_ctx != NULL)
2205 {
2206 to_qfl->qf_ctx = alloc_tv();
2207 if (to_qfl->qf_ctx != NULL)
2208 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2209 }
2210 else
2211 to_qfl->qf_ctx = NULL;
2212
2213 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002214 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002215 return FAIL;
2216
2217 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2218
2219 // Assign a new ID for the location list
2220 to_qfl->qf_id = ++last_qf_id;
2221 to_qfl->qf_changedtick = 0L;
2222
2223 // When no valid entries are present in the list, qf_ptr points to
2224 // the first item in the list
2225 if (to_qfl->qf_nonevalid)
2226 {
2227 to_qfl->qf_ptr = to_qfl->qf_start;
2228 to_qfl->qf_index = 1;
2229 }
2230
2231 return OK;
2232}
2233
2234/*
2235 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002236 */
2237 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002238copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002239{
2240 qf_info_T *qi;
2241 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002242
Bram Moolenaar09037502018-09-25 22:08:14 +02002243 // When copying from a location list window, copy the referenced
2244 // location list. For other windows, copy the location list for
2245 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002246 if (IS_LL_WINDOW(from))
2247 qi = from->w_llist_ref;
2248 else
2249 qi = from->w_llist;
2250
Bram Moolenaar09037502018-09-25 22:08:14 +02002251 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002252 return;
2253
Bram Moolenaar09037502018-09-25 22:08:14 +02002254 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002255 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002256 return;
2257
2258 to->w_llist->qf_listcount = qi->qf_listcount;
2259
Bram Moolenaar09037502018-09-25 22:08:14 +02002260 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002261 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002262 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002263 to->w_llist->qf_curlist = idx;
2264
Bram Moolenaar0398e002019-03-21 21:12:49 +01002265 if (copy_loclist(qf_get_list(qi, idx),
2266 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002267 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002268 qf_free_all(to);
2269 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002270 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002271 }
2272
Bram Moolenaar09037502018-09-25 22:08:14 +02002273 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002274}
2275
2276/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002277 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002278 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 */
2280 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002281qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282{
Bram Moolenaar82404332016-07-10 17:00:38 +02002283 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002284 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002285 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002286
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002287 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
Bram Moolenaare60acc12011-05-10 16:41:25 +02002290#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002291 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002292#endif
2293#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002294 if (directory != NULL)
2295 slash_adjust(directory);
2296 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002297#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002298 if (directory != NULL && !vim_isAbsName(fname)
2299 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2300 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002301 // Here we check if the file really exists.
2302 // This should normally be true, but if make works without
2303 // "leaving directory"-messages we might have missed a
2304 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002305 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002308 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002309 if (directory)
2310 ptr = concat_fnames(directory, fname, TRUE);
2311 else
2312 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002314 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002315 bufname = ptr;
2316 }
2317 else
2318 bufname = fname;
2319
2320 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002321 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002322 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002323 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002324 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002326 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002327 {
2328 vim_free(qf_last_bufname);
2329 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2330 if (bufname == ptr)
2331 qf_last_bufname = bufname;
2332 else
2333 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002334 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002335 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002336 if (buf == NULL)
2337 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002338
Bram Moolenaarc1542742016-07-20 21:44:37 +02002339 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002340 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002341 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342}
2343
2344/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002345 * Push dirbuf onto the directory stack and return pointer to actual dir or
2346 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 */
2348 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002349qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350{
2351 struct dir_stack_T *ds_new;
2352 struct dir_stack_T *ds_ptr;
2353
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002354 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2356 if (ds_new == NULL)
2357 return NULL;
2358
2359 ds_new->next = *stackptr;
2360 *stackptr = ds_new;
2361
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002362 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (vim_isAbsName(dirbuf)
2364 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002365 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 (*stackptr)->dirname = vim_strsave(dirbuf);
2367 else
2368 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002369 // Okay we don't have an absolute path.
2370 // dirbuf must be a subdir of one of the directories on the stack.
2371 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 ds_new = (*stackptr)->next;
2373 (*stackptr)->dirname = NULL;
2374 while (ds_new)
2375 {
2376 vim_free((*stackptr)->dirname);
2377 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2378 TRUE);
2379 if (mch_isdir((*stackptr)->dirname) == TRUE)
2380 break;
2381
2382 ds_new = ds_new->next;
2383 }
2384
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002385 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 while ((*stackptr)->next != ds_new)
2387 {
2388 ds_ptr = (*stackptr)->next;
2389 (*stackptr)->next = (*stackptr)->next->next;
2390 vim_free(ds_ptr->dirname);
2391 vim_free(ds_ptr);
2392 }
2393
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002394 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 if (ds_new == NULL)
2396 {
2397 vim_free((*stackptr)->dirname);
2398 (*stackptr)->dirname = vim_strsave(dirbuf);
2399 }
2400 }
2401
2402 if ((*stackptr)->dirname != NULL)
2403 return (*stackptr)->dirname;
2404 else
2405 {
2406 ds_ptr = *stackptr;
2407 *stackptr = (*stackptr)->next;
2408 vim_free(ds_ptr);
2409 return NULL;
2410 }
2411}
2412
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413/*
2414 * pop dirbuf from the directory stack and return previous directory or NULL if
2415 * stack is empty
2416 */
2417 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002418qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419{
2420 struct dir_stack_T *ds_ptr;
2421
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002422 // TODO: Should we check if dirbuf is the directory on top of the stack?
2423 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002425 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (*stackptr != NULL)
2427 {
2428 ds_ptr = *stackptr;
2429 *stackptr = (*stackptr)->next;
2430 vim_free(ds_ptr->dirname);
2431 vim_free(ds_ptr);
2432 }
2433
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002434 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 return *stackptr ? (*stackptr)->dirname : NULL;
2436}
2437
2438/*
2439 * clean up directory stack
2440 */
2441 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002442qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443{
2444 struct dir_stack_T *ds_ptr;
2445
2446 while ((ds_ptr = *stackptr) != NULL)
2447 {
2448 *stackptr = (*stackptr)->next;
2449 vim_free(ds_ptr->dirname);
2450 vim_free(ds_ptr);
2451 }
2452}
2453
2454/*
2455 * Check in which directory of the directory stack the given file can be
2456 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002457 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 * Cleans up intermediate directory entries.
2459 *
2460 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002461 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 * ./
2463 * ./aa
2464 * ./aa/bb
2465 * ./bb
2466 * ./bb/x.c
2467 * and make says:
2468 * making all in aa
2469 * making all in bb
2470 * x.c:9: Error
2471 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2472 * qf_guess_filepath will return NULL.
2473 */
2474 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002475qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476{
2477 struct dir_stack_T *ds_ptr;
2478 struct dir_stack_T *ds_tmp;
2479 char_u *fullname;
2480
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002481 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002482 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 return NULL;
2484
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002485 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 fullname = NULL;
2487 while (ds_ptr)
2488 {
2489 vim_free(fullname);
2490 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2491
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002492 // If concat_fnames failed, just go on. The worst thing that can happen
2493 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2495 break;
2496
2497 ds_ptr = ds_ptr->next;
2498 }
2499
2500 vim_free(fullname);
2501
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002502 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002503 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002505 ds_tmp = qfl->qf_dir_stack->next;
2506 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 vim_free(ds_tmp->dirname);
2508 vim_free(ds_tmp);
2509 }
2510
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002511 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512}
2513
2514/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002515 * Returns TRUE if a quickfix/location list with the given identifier exists.
2516 */
2517 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002518qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002519{
2520 qf_info_T *qi = &ql_info;
2521 int i;
2522
2523 if (wp != NULL)
2524 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002525 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002526 if (qi == NULL)
2527 return FALSE;
2528 }
2529
2530 for (i = 0; i < qi->qf_listcount; ++i)
2531 if (qi->qf_lists[i].qf_id == qf_id)
2532 return TRUE;
2533
2534 return FALSE;
2535}
2536
2537/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002538 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002539 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002540 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002541 * Similar to location list.
2542 */
2543 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002544is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002545{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002546 qfline_T *qfp;
2547 int i;
2548
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002549 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002550 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2551 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002552 break;
2553
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002554 if (i == qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002555 return FALSE;
2556
2557 return TRUE;
2558}
2559
2560/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002561 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002562 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002563 */
2564 static qfline_T *
2565get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002566 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002567 qfline_T *qf_ptr,
2568 int *qf_index,
2569 int dir)
2570{
2571 int idx;
2572 int old_qf_fnum;
2573
2574 idx = *qf_index;
2575 old_qf_fnum = qf_ptr->qf_fnum;
2576
2577 do
2578 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002579 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002580 return NULL;
2581 ++idx;
2582 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002583 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002584 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2585
2586 *qf_index = idx;
2587 return qf_ptr;
2588}
2589
2590/*
2591 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002592 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002593 */
2594 static qfline_T *
2595get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002596 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002597 qfline_T *qf_ptr,
2598 int *qf_index,
2599 int dir)
2600{
2601 int idx;
2602 int old_qf_fnum;
2603
2604 idx = *qf_index;
2605 old_qf_fnum = qf_ptr->qf_fnum;
2606
2607 do
2608 {
2609 if (idx == 1 || qf_ptr->qf_prev == NULL)
2610 return NULL;
2611 --idx;
2612 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002613 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002614 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2615
2616 *qf_index = idx;
2617 return qf_ptr;
2618}
2619
2620/*
2621 * Get the n'th (errornr) previous/next valid entry from the current entry in
2622 * the quickfix list.
2623 * dir == FORWARD or FORWARD_FILE: next valid entry
2624 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2625 */
2626 static qfline_T *
2627get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002628 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002629 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002630 int dir,
2631 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002632{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002633 qfline_T *qf_ptr = qfl->qf_ptr;
2634 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002635 qfline_T *prev_qf_ptr;
2636 int prev_index;
2637 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2638 char_u *err = e_no_more_items;
2639
2640 while (errornr--)
2641 {
2642 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002643 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002644
2645 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002646 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002647 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002648 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002649 if (qf_ptr == NULL)
2650 {
2651 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002652 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002653 if (err != NULL)
2654 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002655 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002656 return NULL;
2657 }
2658 break;
2659 }
2660
2661 err = NULL;
2662 }
2663
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002664 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002665 return qf_ptr;
2666}
2667
2668/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002669 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2670 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002671 */
2672 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002673get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002674{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002675 qfline_T *qf_ptr = qfl->qf_ptr;
2676 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002677
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002678 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002679 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2680 {
2681 --qf_idx;
2682 qf_ptr = qf_ptr->qf_prev;
2683 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002684 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002685 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2686 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002687 {
2688 ++qf_idx;
2689 qf_ptr = qf_ptr->qf_next;
2690 }
2691
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002692 *new_qfidx = qf_idx;
2693 return qf_ptr;
2694}
2695
2696/*
2697 * Get a entry specied by 'errornr' and 'dir' from the current
2698 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2699 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2700 * Returns a pointer to the entry and the index of the new entry is stored in
2701 * 'new_qfidx'.
2702 */
2703 static qfline_T *
2704qf_get_entry(
2705 qf_list_T *qfl,
2706 int errornr,
2707 int dir,
2708 int *new_qfidx)
2709{
2710 qfline_T *qf_ptr = qfl->qf_ptr;
2711 int qfidx = qfl->qf_index;
2712
2713 if (dir != 0) // next/prev valid entry
2714 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2715 else if (errornr != 0) // go to specified number
2716 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2717
2718 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002719 return qf_ptr;
2720}
2721
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002722/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002723 * Find a window displaying a Vim help file.
2724 */
2725 static win_T *
2726qf_find_help_win(void)
2727{
2728 win_T *wp;
2729
2730 FOR_ALL_WINDOWS(wp)
2731 if (bt_help(wp->w_buffer))
2732 return wp;
2733
2734 return NULL;
2735}
2736
2737/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002738 * Set the location list for the specified window to 'qi'.
2739 */
2740 static void
2741win_set_loclist(win_T *wp, qf_info_T *qi)
2742{
2743 wp->w_llist = qi;
2744 qi->qf_refcount++;
2745}
2746
2747/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002748 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2749 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002750 */
2751 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002752jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002753{
2754 win_T *wp;
2755 int flags;
2756
Bram Moolenaarb2443732018-11-11 22:50:27 +01002757 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002758 wp = NULL;
2759 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002760 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002761 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2762 win_enter(wp, TRUE);
2763 else
2764 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002765 // Split off help window; put it at far top if no position
2766 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002767 flags = WSP_HELP;
2768 if (cmdmod.split == 0 && curwin->w_width != Columns
2769 && curwin->w_width < 80)
2770 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002771 // If the user asks to open a new window, then copy the location list.
2772 // Otherwise, don't copy the location list.
2773 if (IS_LL_STACK(qi) && !newwin)
2774 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002775
2776 if (win_split(0, flags) == FAIL)
2777 return FAIL;
2778
2779 *opened_window = TRUE;
2780
2781 if (curwin->w_height < p_hh)
2782 win_setheight((int)p_hh);
2783
Bram Moolenaarb2443732018-11-11 22:50:27 +01002784 // When using location list, the new window should use the supplied
2785 // location list. If the user asks to open a new window, then the new
2786 // window will get a copy of the location list.
2787 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002788 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002789 }
2790
2791 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002792 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002793
2794 return OK;
2795}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002796
2797/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002798 * Find a non-quickfix window in the current tabpage using the given location
2799 * list stack.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002800 * Returns NULL if a matching window is not found.
2801 */
2802 static win_T *
2803qf_find_win_with_loclist(qf_info_T *ll)
2804{
2805 win_T *wp;
2806
2807 FOR_ALL_WINDOWS(wp)
2808 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2809 return wp;
2810
2811 return NULL;
2812}
2813
2814/*
2815 * Find a window containing a normal buffer
2816 */
2817 static win_T *
2818qf_find_win_with_normal_buf(void)
2819{
2820 win_T *wp;
2821
2822 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002823 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002824 return wp;
2825
2826 return NULL;
2827}
2828
2829/*
2830 * Go to a window in any tabpage containing the specified file. Returns TRUE
2831 * if successfully jumped to the window. Otherwise returns FALSE.
2832 */
2833 static int
2834qf_goto_tabwin_with_file(int fnum)
2835{
2836 tabpage_T *tp;
2837 win_T *wp;
2838
2839 FOR_ALL_TAB_WINDOWS(tp, wp)
2840 if (wp->w_buffer->b_fnum == fnum)
2841 {
2842 goto_tabpage_win(tp, wp);
2843 return TRUE;
2844 }
2845
2846 return FALSE;
2847}
2848
2849/*
2850 * Create a new window to show a file above the quickfix window. Called when
2851 * only the quickfix window is present.
2852 */
2853 static int
2854qf_open_new_file_win(qf_info_T *ll_ref)
2855{
2856 int flags;
2857
2858 flags = WSP_ABOVE;
2859 if (ll_ref != NULL)
2860 flags |= WSP_NEWLOC;
2861 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002862 return FAIL; // not enough room for window
2863 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002864 swb_flags = 0;
2865 RESET_BINDING(curwin);
2866 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002867 // The new window should use the location list from the
2868 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002869 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002870 return OK;
2871}
2872
2873/*
2874 * Go to a window that shows the right buffer. If the window is not found, go
2875 * to the window just above the location list window. This is used for opening
2876 * a file from a location window and not from a quickfix window. If some usable
2877 * window is previously found, then it is supplied in 'use_win'.
2878 */
2879 static void
2880qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2881{
2882 win_T *win = use_win;
2883
2884 if (win == NULL)
2885 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002886 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002887 FOR_ALL_WINDOWS(win)
2888 if (win->w_buffer->b_fnum == qf_fnum)
2889 break;
2890 if (win == NULL)
2891 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002892 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002893 win = curwin;
2894 do
2895 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002896 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002897 break;
2898 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002899 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002900 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002901 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002902 } while (win != curwin);
2903 }
2904 }
2905 win_goto(win);
2906
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002907 // If the location list for the window is not set, then set it
2908 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01002909 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002910 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002911}
2912
2913/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002914 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2915 * not found, then go to the window just above the quickfix window. This is
2916 * used for opening a file from a quickfix window and not from a location
2917 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002918 */
2919 static void
2920qf_goto_win_with_qfl_file(int qf_fnum)
2921{
2922 win_T *win;
2923 win_T *altwin;
2924
2925 win = curwin;
2926 altwin = NULL;
2927 for (;;)
2928 {
2929 if (win->w_buffer->b_fnum == qf_fnum)
2930 break;
2931 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002932 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002933 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002934 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002935
2936 if (IS_QF_WINDOW(win))
2937 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002938 // Didn't find it, go to the window before the quickfix
2939 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002940 if (altwin != NULL)
2941 win = altwin;
2942 else if (curwin->w_prev != NULL)
2943 win = curwin->w_prev;
2944 else
2945 win = curwin->w_next;
2946 break;
2947 }
2948
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002949 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02002950 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002951 altwin = win;
2952 }
2953
2954 win_goto(win);
2955}
2956
2957/*
2958 * Find a suitable window for opening a file (qf_fnum) from the
2959 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01002960 * window, jump to it. Otherwise open a new window to display the file. If
2961 * 'newwin' is TRUE, then always open a new window. This is called from either
2962 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002963 */
2964 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002965qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002966{
2967 win_T *usable_win_ptr = NULL;
2968 int usable_win;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002969 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002970 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002971
2972 usable_win = 0;
2973
Bram Moolenaarb2443732018-11-11 22:50:27 +01002974 // If opening a new window, then don't use the location list referred by
2975 // the current window. Otherwise two windows will refer to the same
2976 // location list.
2977 if (!newwin)
2978 ll_ref = curwin->w_llist_ref;
2979
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002980 if (ll_ref != NULL)
2981 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002982 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002983 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2984 if (usable_win_ptr != NULL)
2985 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002986 }
2987
2988 if (!usable_win)
2989 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002990 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002991 win = qf_find_win_with_normal_buf();
2992 if (win != NULL)
2993 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002994 }
2995
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002996 // If no usable window is found and 'switchbuf' contains "usetab"
2997 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002998 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002999 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003000
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003001 // If there is only one window and it is the quickfix window, create a
3002 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003003 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003004 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003005 if (qf_open_new_file_win(ll_ref) != OK)
3006 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003007 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003008 }
3009 else
3010 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003011 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003012 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003013 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003014 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003015 }
3016
3017 return OK;
3018}
3019
3020/*
3021 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003022 * Returns OK if successfully edited the file, FAIL on failing to open the
3023 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3024 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003025 */
3026 static int
3027qf_jump_edit_buffer(
3028 qf_info_T *qi,
3029 qfline_T *qf_ptr,
3030 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003031 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003032 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003033{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003034 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003035 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003036 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003037 int old_qf_curlist = qi->qf_curlist;
3038 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003039
3040 if (qf_ptr->qf_type == 1)
3041 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003042 // Open help file (do_ecmd() will set b_help flag, readfile() will
3043 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003044 if (!can_abandon(curbuf, forceit))
3045 {
3046 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003047 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003048 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003049
3050 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3051 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003052 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003053 }
3054 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003055 retval = buflist_getfile(qf_ptr->qf_fnum,
3056 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003057
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003058 // If a location list, check whether the associated window is still
3059 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003060 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003061 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003062 win_T *wp = win_id2wp(prev_winid);
3063 if (wp == NULL && curwin->w_llist != qi)
3064 {
3065 emsg(_("E924: Current window was closed"));
3066 *opened_window = FALSE;
3067 return NOTDONE;
3068 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003069 }
3070
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003071 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003073 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003074 return NOTDONE;
3075 }
3076
3077 if (old_qf_curlist != qi->qf_curlist
3078 || !is_qf_entry_present(qfl, qf_ptr))
3079 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003080 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003081 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003082 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003083 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003084 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003085 }
3086
3087 return retval;
3088}
3089
3090/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003091 * Go to the error line in the current file using either line/column number or
3092 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003093 */
3094 static void
3095qf_jump_goto_line(
3096 linenr_T qf_lnum,
3097 int qf_col,
3098 char_u qf_viscol,
3099 char_u *qf_pattern)
3100{
3101 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003102
3103 if (qf_pattern == NULL)
3104 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003105 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003106 i = qf_lnum;
3107 if (i > 0)
3108 {
3109 if (i > curbuf->b_ml.ml_line_count)
3110 i = curbuf->b_ml.ml_line_count;
3111 curwin->w_cursor.lnum = i;
3112 }
3113 if (qf_col > 0)
3114 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003115 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003116 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003117 coladvance(qf_col - 1);
3118 else
3119 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003120 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003121 check_cursor();
3122 }
3123 else
3124 beginline(BL_WHITE | BL_FIX);
3125 }
3126 else
3127 {
3128 pos_T save_cursor;
3129
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003130 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003131 save_cursor = curwin->w_cursor;
3132 curwin->w_cursor.lnum = 0;
3133 if (!do_search(NULL, '/', qf_pattern, (long)1,
3134 SEARCH_KEEP, NULL, NULL))
3135 curwin->w_cursor = save_cursor;
3136 }
3137}
3138
3139/*
3140 * Display quickfix list index and size message
3141 */
3142 static void
3143qf_jump_print_msg(
3144 qf_info_T *qi,
3145 int qf_index,
3146 qfline_T *qf_ptr,
3147 buf_T *old_curbuf,
3148 linenr_T old_lnum)
3149{
3150 linenr_T i;
3151 int len;
3152
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003153 // Update the screen before showing the message, unless the screen
3154 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003155 if (!msg_scrolled)
3156 update_topline_redraw();
3157 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003158 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003159 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3160 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003161 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003162 len = (int)STRLEN(IObuff);
3163 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3164
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003165 // Output the message. Overwrite to avoid scrolling when the 'O'
3166 // flag is present in 'shortmess'; But when not jumping, print the
3167 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003168 i = msg_scroll;
3169 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3170 msg_scroll = TRUE;
3171 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3172 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003173 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003174 msg_scroll = i;
3175}
3176
3177/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003178 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003179 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3180 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003181 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3182 * able to jump/open a window. Returns NOTDONE if a file is not associated
3183 * with the entry.
3184 */
3185 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003186qf_jump_open_window(
3187 qf_info_T *qi,
3188 qfline_T *qf_ptr,
3189 int newwin,
3190 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003191{
3192 // For ":helpgrep" find a help window or open one.
3193 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003194 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003195 return FAIL;
3196
3197 // If currently in the quickfix window, find another window to show the
3198 // file in.
3199 if (bt_quickfix(curbuf) && !*opened_window)
3200 {
3201 // If there is no file specified, we don't know where to go.
3202 // But do advance, otherwise ":cn" gets stuck.
3203 if (qf_ptr->qf_fnum == 0)
3204 return NOTDONE;
3205
Bram Moolenaarb2443732018-11-11 22:50:27 +01003206 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3207 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003208 return FAIL;
3209 }
3210
3211 return OK;
3212}
3213
3214/*
3215 * Edit a selected file from the quickfix/location list and jump to a
3216 * particular line/column, adjust the folds and display a message about the
3217 * jump.
3218 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3219 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3220 * the file.
3221 */
3222 static int
3223qf_jump_to_buffer(
3224 qf_info_T *qi,
3225 int qf_index,
3226 qfline_T *qf_ptr,
3227 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003228 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003229 int *opened_window,
3230 int openfold,
3231 int print_message)
3232{
3233 buf_T *old_curbuf;
3234 linenr_T old_lnum;
3235 int retval = OK;
3236
3237 // If there is a file name, read the wanted file if needed, and check
3238 // autowrite etc.
3239 old_curbuf = curbuf;
3240 old_lnum = curwin->w_cursor.lnum;
3241
3242 if (qf_ptr->qf_fnum != 0)
3243 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003244 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003245 opened_window);
3246 if (retval != OK)
3247 return retval;
3248 }
3249
3250 // When not switched to another buffer, still need to set pc mark
3251 if (curbuf == old_curbuf)
3252 setpcmark();
3253
3254 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3255 qf_ptr->qf_pattern);
3256
3257#ifdef FEAT_FOLDING
3258 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3259 foldOpenCursor();
3260#endif
3261 if (print_message)
3262 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3263
3264 return retval;
3265}
3266
3267/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003268 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 */
3270 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003271qf_jump(qf_info_T *qi,
3272 int dir,
3273 int errornr,
3274 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003276 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3277}
3278
3279/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003280 * Jump to a quickfix line.
3281 * If dir == 0 go to entry "errornr".
3282 * If dir == FORWARD go "errornr" valid entries forward.
3283 * If dir == BACKWARD go "errornr" valid entries backward.
3284 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3285 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3286 * else if "errornr" is zero, redisplay the same line
3287 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003288 * If 'newwin' is TRUE, then open the file in a new window.
3289 */
3290 void
3291qf_jump_newwin(qf_info_T *qi,
3292 int dir,
3293 int errornr,
3294 int forceit,
3295 int newwin)
3296{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003297 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003298 qfline_T *qf_ptr;
3299 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003302 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003303 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003304 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003308 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003310 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003312 if (qi == NULL)
3313 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003314
Bram Moolenaar0398e002019-03-21 21:12:49 +01003315 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003317 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 return;
3319 }
3320
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003321 incr_quickfix_busy();
3322
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003323 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003324
3325 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003327 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003329
3330 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3331 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003333 qf_ptr = old_qf_ptr;
3334 qf_index = old_qf_index;
3335 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003338 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003339 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003340 // No need to print the error message if it's visible in the error
3341 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 print_message = FALSE;
3343
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003344 prev_winid = curwin->w_id;
3345
Bram Moolenaarb2443732018-11-11 22:50:27 +01003346 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003347 if (retval == FAIL)
3348 goto failed;
3349 if (retval == NOTDONE)
3350 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003351
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003352 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003353 &opened_window, old_KeyTyped, print_message);
3354 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003356 // Quickfix/location list is freed by an autocmd
3357 qi = NULL;
3358 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003361 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003364 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003365 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003367 // Couldn't open file, so put index back where it was. This could
3368 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 qf_ptr = old_qf_ptr;
3371 qf_index = old_qf_index;
3372 }
3373 }
3374theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003375 if (qi != NULL)
3376 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003377 qfl->qf_ptr = qf_ptr;
3378 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003379 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003380 if (p_swb != old_swb)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003382 // Restore old 'switchbuf' value, but not when an autocommand or
3383 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003387 swb_flags = old_swb_flags;
3388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 else
3390 free_string_option(old_swb);
3391 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003392 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393}
3394
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003395// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003396static int qfFileAttr;
3397static int qfSepAttr;
3398static int qfLineAttr;
3399
3400/*
3401 * Display information about a single entry from the quickfix/location list.
3402 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003403 * 'cursel' will be set to TRUE for the currently selected entry in the
3404 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003405 */
3406 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003407qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003408{
3409 char_u *fname;
3410 buf_T *buf;
3411 int filter_entry;
3412
3413 fname = NULL;
3414 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3415 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3416 (char *)qfp->qf_module);
3417 else {
3418 if (qfp->qf_fnum != 0
3419 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3420 {
3421 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003422 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003423 fname = gettail(fname);
3424 }
3425 if (fname == NULL)
3426 sprintf((char *)IObuff, "%2d", qf_idx);
3427 else
3428 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3429 qf_idx, (char *)fname);
3430 }
3431
3432 // Support for filtering entries using :filter /pat/ clist
3433 // Match against the module name, file name, search pattern and
3434 // text of the entry.
3435 filter_entry = TRUE;
3436 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3437 filter_entry &= message_filtered(qfp->qf_module);
3438 if (filter_entry && fname != NULL)
3439 filter_entry &= message_filtered(fname);
3440 if (filter_entry && qfp->qf_pattern != NULL)
3441 filter_entry &= message_filtered(qfp->qf_pattern);
3442 if (filter_entry)
3443 filter_entry &= message_filtered(qfp->qf_text);
3444 if (filter_entry)
3445 return;
3446
3447 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003448 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003449
3450 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003451 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003452 if (qfp->qf_lnum == 0)
3453 IObuff[0] = NUL;
3454 else if (qfp->qf_col == 0)
3455 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3456 else
3457 sprintf((char *)IObuff, "%ld col %d",
3458 qfp->qf_lnum, qfp->qf_col);
3459 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3460 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003461 msg_puts_attr((char *)IObuff, qfLineAttr);
3462 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003463 if (qfp->qf_pattern != NULL)
3464 {
3465 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003466 msg_puts((char *)IObuff);
3467 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003468 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003469 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003470
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003471 // Remove newlines and leading whitespace from the text. For an
3472 // unrecognized line keep the indent, the compiler may mark a word
3473 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003474 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3475 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3476 IObuff, IOSIZE);
3477 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003478 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003479}
3480
3481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003483 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 */
3485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003486qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003488 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003489 qfline_T *qfp;
3490 int i;
3491 int idx1 = 1;
3492 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003493 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003494 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003495 int all = eap->forceit; // if not :cl!, only show
3496 // recognised errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003497 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
Bram Moolenaar39665952018-08-15 20:59:48 +02003499 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003500 {
3501 qi = GET_LOC_LIST(curwin);
3502 if (qi == NULL)
3503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003504 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003505 return;
3506 }
3507 }
3508
Bram Moolenaar0398e002019-03-21 21:12:49 +01003509 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003511 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 return;
3513 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003514 if (*arg == '+')
3515 {
3516 ++arg;
3517 plus = TRUE;
3518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3520 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003521 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 return;
3523 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003524 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003525 if (plus)
3526 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003527 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003528 idx2 = i + idx1;
3529 idx1 = i;
3530 }
3531 else
3532 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003533 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003534 if (idx1 < 0)
3535 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3536 if (idx2 < 0)
3537 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003540 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003541 shorten_fnames(FALSE);
3542
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003543 // Get the attributes for the different quickfix highlight items. Note
3544 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003545 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3546 if (qfFileAttr == 0)
3547 qfFileAttr = HL_ATTR(HLF_D);
3548 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3549 if (qfSepAttr == 0)
3550 qfSepAttr = HL_ATTR(HLF_D);
3551 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3552 if (qfLineAttr == 0)
3553 qfLineAttr = HL_ATTR(HLF_N);
3554
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003555 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003557 qfp = qfl->qf_start;
3558 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
3560 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3561 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003562 if (got_int)
3563 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003564
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003565 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003567
3568 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003569 if (qfp == NULL)
3570 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003571 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 ui_breakcheck();
3573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574}
3575
3576/*
3577 * Remove newlines and leading whitespace from an error message.
3578 * Put the result in "buf[bufsize]".
3579 */
3580 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003581qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582{
3583 int i;
3584 char_u *p = text;
3585
3586 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3587 {
3588 if (*p == '\n')
3589 {
3590 buf[i] = ' ';
3591 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003592 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 break;
3594 }
3595 else
3596 buf[i] = *p++;
3597 }
3598 buf[i] = NUL;
3599}
3600
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003601/*
3602 * Display information (list number, list size and the title) about a
3603 * quickfix/location list.
3604 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003605 static void
3606qf_msg(qf_info_T *qi, int which, char *lead)
3607{
3608 char *title = (char *)qi->qf_lists[which].qf_title;
3609 int count = qi->qf_lists[which].qf_count;
3610 char_u buf[IOSIZE];
3611
3612 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3613 lead,
3614 which + 1,
3615 qi->qf_listcount,
3616 count);
3617
3618 if (title != NULL)
3619 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003620 size_t len = STRLEN(buf);
3621
3622 if (len < 34)
3623 {
3624 vim_memset(buf + len, ' ', 34 - len);
3625 buf[34] = NUL;
3626 }
3627 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003628 }
3629 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003630 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003631}
3632
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633/*
3634 * ":colder [count]": Up in the quickfix stack.
3635 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003636 * ":lolder [count]": Up in the location list stack.
3637 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 */
3639 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003640qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003642 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 int count;
3644
Bram Moolenaar39665952018-08-15 20:59:48 +02003645 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003646 {
3647 qi = GET_LOC_LIST(curwin);
3648 if (qi == NULL)
3649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003650 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003651 return;
3652 }
3653 }
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 if (eap->addr_count != 0)
3656 count = eap->line2;
3657 else
3658 count = 1;
3659 while (count--)
3660 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003661 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003663 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003665 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003666 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003668 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670 else
3671 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003672 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003674 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003675 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003677 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
3679 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003680 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003681 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682}
3683
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003684/*
3685 * Display the information about all the quickfix/location lists in the stack
3686 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003687 void
3688qf_history(exarg_T *eap)
3689{
3690 qf_info_T *qi = &ql_info;
3691 int i;
3692
Bram Moolenaar39665952018-08-15 20:59:48 +02003693 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003694 qi = GET_LOC_LIST(curwin);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003695 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003696 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003697 else
3698 for (i = 0; i < qi->qf_listcount; ++i)
3699 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3700}
3701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003703 * Free all the entries in the error list "idx". Note that other information
3704 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 */
3706 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003707qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003709 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003710 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003711 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003713 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003715 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003716 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003717 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003718 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003719 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003720 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003721 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003722 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003723 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003724 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003725 // Somehow qf_count may have an incorrect value, set it to 1
3726 // to avoid crashing when it's wrong.
3727 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003728 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003729 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003730 qfl->qf_start = qfpnext;
3731 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003733
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003734 qfl->qf_index = 0;
3735 qfl->qf_start = NULL;
3736 qfl->qf_last = NULL;
3737 qfl->qf_ptr = NULL;
3738 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003739
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003740 qf_clean_dir_stack(&qfl->qf_dir_stack);
3741 qfl->qf_directory = NULL;
3742 qf_clean_dir_stack(&qfl->qf_file_stack);
3743 qfl->qf_currfile = NULL;
3744 qfl->qf_multiline = FALSE;
3745 qfl->qf_multiignore = FALSE;
3746 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747}
3748
3749/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003750 * Free error list "idx". Frees all the entries in the quickfix list,
3751 * associated context information and the title.
3752 */
3753 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003754qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003755{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003756 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003757
Bram Moolenaard23a8232018-02-10 18:45:26 +01003758 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003759 free_tv(qfl->qf_ctx);
3760 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003761 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003762 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003763}
3764
3765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 * qf_mark_adjust: adjust marks
3767 */
3768 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003769qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003770 win_T *wp,
3771 linenr_T line1,
3772 linenr_T line2,
3773 long amount,
3774 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003776 int i;
3777 qfline_T *qfp;
3778 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003779 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003780 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003781 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
Bram Moolenaarc1542742016-07-20 21:44:37 +02003783 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003784 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003785 if (wp != NULL)
3786 {
3787 if (wp->w_llist == NULL)
3788 return;
3789 qi = wp->w_llist;
3790 }
3791
3792 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003793 {
3794 qf_list_T *qfl = qf_get_list(qi, idx);
3795
3796 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003797 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (qfp->qf_fnum == curbuf->b_fnum)
3799 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003800 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3802 {
3803 if (amount == MAXLNUM)
3804 qfp->qf_cleared = TRUE;
3805 else
3806 qfp->qf_lnum += amount;
3807 }
3808 else if (amount_after && qfp->qf_lnum > line2)
3809 qfp->qf_lnum += amount_after;
3810 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003811 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003812
3813 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003814 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815}
3816
3817/*
3818 * Make a nice message out of the error character and the error number:
3819 * char number message
3820 * e or E 0 " error"
3821 * w or W 0 " warning"
3822 * i or I 0 " info"
3823 * 0 0 ""
3824 * other 0 " c"
3825 * e or E n " error n"
3826 * w or W n " warning n"
3827 * i or I n " info n"
3828 * 0 n " error n"
3829 * other n " c n"
3830 * 1 x "" :helpgrep
3831 */
3832 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003833qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834{
3835 static char_u buf[20];
3836 static char_u cc[3];
3837 char_u *p;
3838
3839 if (c == 'W' || c == 'w')
3840 p = (char_u *)" warning";
3841 else if (c == 'I' || c == 'i')
3842 p = (char_u *)" info";
3843 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3844 p = (char_u *)" error";
3845 else if (c == 0 || c == 1)
3846 p = (char_u *)"";
3847 else
3848 {
3849 cc[0] = ' ';
3850 cc[1] = c;
3851 cc[2] = NUL;
3852 p = cc;
3853 }
3854
3855 if (nr <= 0)
3856 return p;
3857
3858 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3859 return buf;
3860}
3861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003863 * When "split" is FALSE: Open the entry/result under the cursor.
3864 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3865 */
3866 void
3867qf_view_result(int split)
3868{
3869 qf_info_T *qi = &ql_info;
3870
3871 if (!bt_quickfix(curbuf))
3872 return;
3873
3874 if (IS_LL_WINDOW(curwin))
3875 qi = GET_LOC_LIST(curwin);
3876
Bram Moolenaar0398e002019-03-21 21:12:49 +01003877 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003878 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003879 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003880 return;
3881 }
3882
3883 if (split)
3884 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003885 // Open the selected entry in a new window
3886 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3887 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003888 return;
3889 }
3890
3891 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3892}
3893
3894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 * ":cwindow": open the quickfix window if we have errors to display,
3896 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003897 * ":lwindow": open the location list window if we have locations to display,
3898 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 */
3900 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003901ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003903 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003904 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 win_T *win;
3906
Bram Moolenaar39665952018-08-15 20:59:48 +02003907 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003908 {
3909 qi = GET_LOC_LIST(curwin);
3910 if (qi == NULL)
3911 return;
3912 }
3913
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003914 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003915
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003916 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003917 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003919 // If a quickfix window is open but we have no errors to display,
3920 // close the window. If a quickfix window is not open, then open
3921 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003922 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003923 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01003924 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 {
3926 if (win != NULL)
3927 ex_cclose(eap);
3928 }
3929 else if (win == NULL)
3930 ex_copen(eap);
3931}
3932
3933/*
3934 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003935 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003938ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003940 win_T *win = NULL;
3941 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
Bram Moolenaar39665952018-08-15 20:59:48 +02003943 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003944 {
3945 qi = GET_LOC_LIST(curwin);
3946 if (qi == NULL)
3947 return;
3948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003950 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003951 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 if (win != NULL)
3953 win_close(win, FALSE);
3954}
3955
3956/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003957 * Set "w:quickfix_title" if "qi" has a title.
3958 */
3959 static void
3960qf_set_title_var(qf_list_T *qfl)
3961{
3962 if (qfl->qf_title != NULL)
3963 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3964}
3965
3966/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003967 * Goto a quickfix or location list window (if present).
3968 * Returns OK if the window is found, FAIL otherwise.
3969 */
3970 static int
3971qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3972{
3973 win_T *win;
3974
3975 win = qf_find_win(qi);
3976 if (win == NULL)
3977 return FAIL;
3978
3979 win_goto(win);
3980 if (resize)
3981 {
3982 if (vertsplit)
3983 {
3984 if (sz != win->w_width)
3985 win_setwidth(sz);
3986 }
3987 else if (sz != win->w_height)
3988 win_setheight(sz);
3989 }
3990
3991 return OK;
3992}
3993
3994/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01003995 * Set options for the buffer in the quickfix or location list window.
3996 */
3997 static void
3998qf_set_cwindow_options(void)
3999{
4000 // switch off 'swapfile'
4001 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4002 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4003 OPT_LOCAL);
4004 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4005 RESET_BINDING(curwin);
4006#ifdef FEAT_DIFF
4007 curwin->w_p_diff = FALSE;
4008#endif
4009#ifdef FEAT_FOLDING
4010 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4011 OPT_LOCAL);
4012#endif
4013}
4014
4015/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004016 * Open a new quickfix or location list window, load the quickfix buffer and
4017 * set the appropriate options for the window.
4018 * Returns FAIL if the window could not be opened.
4019 */
4020 static int
4021qf_open_new_cwindow(qf_info_T *qi, int height)
4022{
4023 buf_T *qf_buf;
4024 win_T *oldwin = curwin;
4025 tabpage_T *prevtab = curtab;
4026 int flags = 0;
4027 win_T *win;
4028
4029 qf_buf = qf_find_buf(qi);
4030
4031 // The current window becomes the previous window afterwards.
4032 win = curwin;
4033
4034 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4035 // Create the new quickfix window at the very bottom, except when
4036 // :belowright or :aboveleft is used.
4037 win_goto(lastwin);
4038 // Default is to open the window below the current window
4039 if (cmdmod.split == 0)
4040 flags = WSP_BELOW;
4041 flags |= WSP_NEWLOC;
4042 if (win_split(height, flags) == FAIL)
4043 return FAIL; // not enough room for window
4044 RESET_BINDING(curwin);
4045
4046 if (IS_LL_STACK(qi))
4047 {
4048 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004049 // location list stack from the window 'win'.
4050 curwin->w_llist_ref = qi;
4051 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004052 }
4053
4054 if (oldwin != curwin)
4055 oldwin = NULL; // don't store info when in another window
4056 if (qf_buf != NULL)
4057 {
4058 // Use the existing quickfix buffer
4059 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4060 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4061 }
4062 else
4063 {
4064 // Create a new quickfix buffer
4065 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4066
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004067 // save the number of the new buffer
4068 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004069 }
4070
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004071 // Set the options for the quickfix buffer/window (if not already done)
4072 // Do this even if the quickfix buffer was already present, as an autocmd
4073 // might have previously deleted (:bdelete) the quickfix buffer.
4074 if (curbuf->b_p_bt[0] != 'q')
4075 qf_set_cwindow_options();
4076
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004077 // Only set the height when still in the same tab page and there is no
4078 // window to the side.
4079 if (curtab == prevtab && curwin->w_width == Columns)
4080 win_setheight(height);
4081 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4082 if (win_valid(win))
4083 prevwin = win;
4084
4085 return OK;
4086}
4087
4088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004090 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 */
4092 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004093ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004095 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004096 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004098 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004099 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
Bram Moolenaar39665952018-08-15 20:59:48 +02004101 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004102 {
4103 qi = GET_LOC_LIST(curwin);
4104 if (qi == NULL)
4105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004106 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004107 return;
4108 }
4109 }
4110
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004111 incr_quickfix_busy();
4112
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 if (eap->addr_count != 0)
4114 height = eap->line2;
4115 else
4116 height = QF_WINHEIGHT;
4117
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004118 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119#ifdef FEAT_GUI
4120 need_mouse_correct = TRUE;
4121#endif
4122
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004123 // Find an existing quickfix window, or open a new one.
4124 if (cmdmod.tab == 0)
4125 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4126 cmdmod.split & WSP_VERT);
4127 if (status == FAIL)
4128 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004129 {
4130 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004131 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004134 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004135 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004136 // Save the current index here, as updating the quickfix buffer may free
4137 // the quickfix list
4138 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004139
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004140 // Fill the buffer with the quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004141 qf_fill_buffer(qfl, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004143 decr_quickfix_busy();
4144
4145 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 curwin->w_cursor.col = 0;
4147 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004148 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149}
4150
4151/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004152 * Move the cursor in the quickfix window to "lnum".
4153 */
4154 static void
4155qf_win_goto(win_T *win, linenr_T lnum)
4156{
4157 win_T *old_curwin = curwin;
4158
4159 curwin = win;
4160 curbuf = win->w_buffer;
4161 curwin->w_cursor.lnum = lnum;
4162 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004163 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004164 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004165 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004166 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004167 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004168 curwin = old_curwin;
4169 curbuf = curwin->w_buffer;
4170}
4171
4172/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004173 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004174 */
4175 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004176ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004177{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004178 qf_info_T *qi = &ql_info;
4179 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004180
Bram Moolenaar39665952018-08-15 20:59:48 +02004181 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004182 {
4183 qi = GET_LOC_LIST(curwin);
4184 if (qi == NULL)
4185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004186 emsg(_(e_loclist));
Bram Moolenaar537ef082016-07-09 17:56:19 +02004187 return;
4188 }
4189 }
4190
4191 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004192 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4193 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4194}
4195
4196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 * Return the number of the current entry (line number in the quickfix
4198 * window).
4199 */
4200 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004201qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004203 qf_info_T *qi = &ql_info;
4204
4205 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004206 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004207 qi = wp->w_llist_ref;
4208
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004209 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210}
4211
4212/*
4213 * Update the cursor position in the quickfix window to the current error.
4214 * Return TRUE if there is a quickfix window.
4215 */
4216 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004217qf_win_pos_update(
4218 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004219 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220{
4221 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004222 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004224 // Put the cursor on the current error in the quickfix window, so that
4225 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004226 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 if (win != NULL
4228 && qf_index <= win->w_buffer->b_ml.ml_line_count
4229 && old_qf_index != qf_index)
4230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 if (qf_index > old_qf_index)
4232 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004233 win->w_redraw_top = old_qf_index;
4234 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236 else
4237 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004238 win->w_redraw_top = qf_index;
4239 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004241 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 return win != NULL;
4244}
4245
4246/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004247 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004248 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004249 */
4250 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004251is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004252{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004253 // A window displaying the quickfix buffer will have the w_llist_ref field
4254 // set to NULL.
4255 // A window displaying a location list buffer will have the w_llist_ref
4256 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004257 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004258 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4259 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004260 return TRUE;
4261
4262 return FALSE;
4263}
4264
4265/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004266 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004267 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004268 */
4269 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004270qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004271{
4272 win_T *win;
4273
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004274 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004275 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004276 return win;
4277 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004278}
4279
4280/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004281 * Find a quickfix buffer.
4282 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 */
4284 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004285qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004287 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004288 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004290 if (qi->qf_bufnr != INVALID_QFBUFNR)
4291 {
4292 buf_T *qfbuf;
4293 qfbuf = buflist_findnr(qi->qf_bufnr);
4294 if (qfbuf != NULL)
4295 return qfbuf;
4296 // buffer is no longer present
4297 qi->qf_bufnr = INVALID_QFBUFNR;
4298 }
4299
Bram Moolenaar9c102382006-05-03 21:26:49 +00004300 FOR_ALL_TAB_WINDOWS(tp, win)
4301 if (is_qf_win(win, qi))
4302 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004303
Bram Moolenaar9c102382006-05-03 21:26:49 +00004304 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305}
4306
4307/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004308 * Update the w:quickfix_title variable in the quickfix/location list window
4309 */
4310 static void
4311qf_update_win_titlevar(qf_info_T *qi)
4312{
4313 win_T *win;
4314 win_T *curwin_save;
4315
4316 if ((win = qf_find_win(qi)) != NULL)
4317 {
4318 curwin_save = curwin;
4319 curwin = win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004320 qf_set_title_var(qf_get_curlist(qi));
Bram Moolenaard823fa92016-08-12 16:29:27 +02004321 curwin = curwin_save;
4322 }
4323}
4324
4325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 * Find the quickfix buffer. If it exists, update the contents.
4327 */
4328 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004329qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330{
4331 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004332 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004335 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004336 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 if (buf != NULL)
4338 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004339 linenr_T old_line_count = buf->b_ml.ml_line_count;
4340
4341 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004342 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004343 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
Bram Moolenaard823fa92016-08-12 16:29:27 +02004345 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004346
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004347 qf_fill_buffer(qf_get_curlist(qi), buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004348 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004349
Bram Moolenaar864293a2016-06-02 13:40:04 +02004350 if (old_last == NULL)
4351 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004352 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004353
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004354 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004355 aucmd_restbuf(&aco);
4356 }
4357
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004358 // Only redraw when added lines are visible. This avoids flickering
4359 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004360 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4361 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363}
4364
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004365/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004366 * Add an error line to the quickfix buffer.
4367 */
4368 static int
4369qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4370{
4371 int len;
4372 buf_T *errbuf;
4373
4374 if (qfp->qf_module != NULL)
4375 {
4376 STRCPY(IObuff, qfp->qf_module);
4377 len = (int)STRLEN(IObuff);
4378 }
4379 else if (qfp->qf_fnum != 0
4380 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4381 && errbuf->b_fname != NULL)
4382 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004383 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004384 STRCPY(IObuff, gettail(errbuf->b_fname));
4385 else
4386 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004387 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004388 if (errbuf->b_sfname == NULL
4389 || mch_isFullName(errbuf->b_sfname))
4390 {
4391 if (*dirname == NUL)
4392 mch_dirname(dirname, MAXPATHL);
4393 shorten_buf_fname(errbuf, dirname, FALSE);
4394 }
4395 STRCPY(IObuff, errbuf->b_fname);
4396 }
4397 len = (int)STRLEN(IObuff);
4398 }
4399 else
4400 len = 0;
4401 IObuff[len++] = '|';
4402
4403 if (qfp->qf_lnum > 0)
4404 {
4405 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4406 len += (int)STRLEN(IObuff + len);
4407
4408 if (qfp->qf_col > 0)
4409 {
4410 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4411 len += (int)STRLEN(IObuff + len);
4412 }
4413
4414 sprintf((char *)IObuff + len, "%s",
4415 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4416 len += (int)STRLEN(IObuff + len);
4417 }
4418 else if (qfp->qf_pattern != NULL)
4419 {
4420 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4421 len += (int)STRLEN(IObuff + len);
4422 }
4423 IObuff[len++] = '|';
4424 IObuff[len++] = ' ';
4425
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004426 // Remove newlines and leading whitespace from the text.
4427 // For an unrecognized line keep the indent, the compiler may
4428 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004429 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4430 IObuff + len, IOSIZE - len);
4431
4432 if (ml_append_buf(buf, lnum, IObuff,
4433 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4434 return FAIL;
4435
4436 return OK;
4437}
4438
4439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 * Fill current buffer with quickfix errors, replacing any previous contents.
4441 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004442 * If "old_last" is not NULL append the items after this one.
4443 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4444 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 */
4446 static void
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004447qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004449 linenr_T lnum;
4450 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004451 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
Bram Moolenaar864293a2016-06-02 13:40:04 +02004453 if (old_last == NULL)
4454 {
4455 if (buf != curbuf)
4456 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004457 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004458 return;
4459 }
4460
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004461 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004462 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4463 (void)ml_delete((linenr_T)1, FALSE);
4464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004466 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004467 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004469 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004470
4471 *dirname = NUL;
4472
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004473 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004474 if (old_last == NULL)
4475 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004476 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004477 lnum = 0;
4478 }
4479 else
4480 {
4481 qfp = old_last->qf_next;
4482 lnum = buf->b_ml.ml_line_count;
4483 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004484 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004486 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004488
Bram Moolenaar864293a2016-06-02 13:40:04 +02004489 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004491 if (qfp == NULL)
4492 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004494
4495 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004496 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004497 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004500 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 check_lnums(TRUE);
4502
Bram Moolenaar864293a2016-06-02 13:40:04 +02004503 if (old_last == NULL)
4504 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004505 // Set the 'filetype' to "qf" each time after filling the buffer.
4506 // This resembles reading a file into a buffer, it's more logical when
4507 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004508 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004509 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4510 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004512 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004513 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004515 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004517 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004518 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004519
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004520 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004521 redraw_curbuf_later(NOT_VALID);
4522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004524 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 KeyTyped = old_KeyTyped;
4526}
4527
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004528/*
4529 * For every change made to the quickfix list, update the changed tick.
4530 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004531 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004532qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004533{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004534 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004535}
4536
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004538 * Return the quickfix/location list number with the given identifier.
4539 * Returns -1 if list is not found.
4540 */
4541 static int
4542qf_id2nr(qf_info_T *qi, int_u qfid)
4543{
4544 int qf_idx;
4545
4546 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4547 if (qi->qf_lists[qf_idx].qf_id == qfid)
4548 return qf_idx;
4549 return INVALID_QFIDX;
4550}
4551
4552/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004553 * If the current list is not "save_qfid" and we can find the list with that ID
4554 * then make it the current list.
4555 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004556 * Returns OK if successfully restored the list. Returns FAIL if the list with
4557 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004558 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004559 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004560qf_restore_list(qf_info_T *qi, int_u save_qfid)
4561{
4562 int curlist;
4563
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004564 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004565 {
4566 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004567 if (curlist < 0)
4568 // list is not present
4569 return FAIL;
4570 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004571 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004572 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004573}
4574
4575/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004576 * Jump to the first entry if there is one.
4577 */
4578 static void
4579qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4580{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004581 if (qf_restore_list(qi, save_qfid) == FAIL)
4582 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004583
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004584 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004585 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004586 qf_jump(qi, 0, 0, forceit);
4587}
4588
4589/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004590 * Return TRUE when using ":vimgrep" for ":grep".
4591 */
4592 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004593grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004594{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004595 return ((cmdidx == CMD_grep
4596 || cmdidx == CMD_lgrep
4597 || cmdidx == CMD_grepadd
4598 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004599 && STRCMP("internal",
4600 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4601}
4602
4603/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004604 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004606 static char_u *
4607make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004609 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004610 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004611 case CMD_make: return (char_u *)"make";
4612 case CMD_lmake: return (char_u *)"lmake";
4613 case CMD_grep: return (char_u *)"grep";
4614 case CMD_lgrep: return (char_u *)"lgrep";
4615 case CMD_grepadd: return (char_u *)"grepadd";
4616 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4617 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619}
4620
4621/*
4622 * Return the name for the errorfile, in allocated memory.
4623 * Find a new unique name when 'makeef' contains "##".
4624 * Returns NULL for error.
4625 */
4626 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004627get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628{
4629 char_u *p;
4630 char_u *name;
4631 static int start = -1;
4632 static int off = 0;
4633#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004634 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635#endif
4636
4637 if (*p_mef == NUL)
4638 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004639 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004641 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 return name;
4643 }
4644
4645 for (p = p_mef; *p; ++p)
4646 if (p[0] == '#' && p[1] == '#')
4647 break;
4648
4649 if (*p == NUL)
4650 return vim_strsave(p_mef);
4651
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004652 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 for (;;)
4654 {
4655 if (start == -1)
4656 start = mch_get_pid();
4657 else
4658 off += 19;
4659
4660 name = alloc((unsigned)STRLEN(p_mef) + 30);
4661 if (name == NULL)
4662 break;
4663 STRCPY(name, p_mef);
4664 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4665 STRCAT(name, p + 2);
4666 if (mch_getperm(name) < 0
4667#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004668 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 && mch_lstat((char *)name, &sb) < 0
4670#endif
4671 )
4672 break;
4673 vim_free(name);
4674 }
4675 return name;
4676}
4677
4678/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004679 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4680 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4681 */
4682 static char_u *
4683make_get_fullcmd(char_u *makecmd, char_u *fname)
4684{
4685 char_u *cmd;
4686 unsigned len;
4687
4688 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4689 if (*p_sp != NUL)
4690 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4691 cmd = alloc(len);
4692 if (cmd == NULL)
4693 return NULL;
4694 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4695 (char *)p_shq);
4696
4697 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4698 if (*p_sp != NUL)
4699 append_redir(cmd, len, p_sp, fname);
4700
4701 // Display the fully formed command. Output a newline if there's something
4702 // else than the :make command that was typed (in which case the cursor is
4703 // in column 0).
4704 if (msg_col == 0)
4705 msg_didout = FALSE;
4706 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004707 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004708 msg_outtrans(cmd); // show what we are doing
4709
4710 return cmd;
4711}
4712
4713/*
4714 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4715 */
4716 void
4717ex_make(exarg_T *eap)
4718{
4719 char_u *fname;
4720 char_u *cmd;
4721 char_u *enc = NULL;
4722 win_T *wp = NULL;
4723 qf_info_T *qi = &ql_info;
4724 int res;
4725 char_u *au_name = NULL;
4726 int_u save_qfid;
4727
4728 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4729 if (grep_internal(eap->cmdidx))
4730 {
4731 ex_vimgrep(eap);
4732 return;
4733 }
4734
4735 au_name = make_get_auname(eap->cmdidx);
4736 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4737 curbuf->b_fname, TRUE, curbuf))
4738 {
4739#ifdef FEAT_EVAL
4740 if (aborting())
4741 return;
4742#endif
4743 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004744 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004745
4746 if (is_loclist_cmd(eap->cmdidx))
4747 wp = curwin;
4748
4749 autowrite_all();
4750 fname = get_mef_name();
4751 if (fname == NULL)
4752 return;
4753 mch_remove(fname); // in case it's not unique
4754
4755 cmd = make_get_fullcmd(eap->arg, fname);
4756 if (cmd == NULL)
4757 return;
4758
4759 // let the shell know if we are redirecting output or not
4760 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4761
4762#ifdef AMIGA
4763 out_flush();
4764 // read window status report and redraw before message
4765 (void)char_avail();
4766#endif
4767
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004768 incr_quickfix_busy();
4769
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004770 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4771 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4772 (eap->cmdidx != CMD_grepadd
4773 && eap->cmdidx != CMD_lgrepadd),
4774 qf_cmdtitle(*eap->cmdlinep), enc);
4775 if (wp != NULL)
4776 {
4777 qi = GET_LOC_LIST(wp);
4778 if (qi == NULL)
4779 goto cleanup;
4780 }
4781 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004782 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004783
4784 // Remember the current quickfix list identifier, so that we can
4785 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004786 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004787 if (au_name != NULL)
4788 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4789 curbuf->b_fname, TRUE, curbuf);
4790 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4791 // display the first error
4792 qf_jump_first(qi, save_qfid, FALSE);
4793
4794cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004795 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004796 mch_remove(fname);
4797 vim_free(fname);
4798 vim_free(cmd);
4799}
4800
4801/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004802 * Returns the number of valid entries in the current quickfix/location list.
4803 */
4804 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004805qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004806{
4807 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004808 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004809 qfline_T *qfp;
4810 int i, sz = 0;
4811 int prev_fnum = 0;
4812
Bram Moolenaar39665952018-08-15 20:59:48 +02004813 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004814 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004815 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004816 qi = GET_LOC_LIST(curwin);
4817 if (qi == NULL)
4818 return 0;
4819 }
4820
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004821 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01004822 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004823 {
4824 if (qfp->qf_valid)
4825 {
4826 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004827 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004828 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4829 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004830 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004831 sz++;
4832 prev_fnum = qfp->qf_fnum;
4833 }
4834 }
4835 }
4836
4837 return sz;
4838}
4839
4840/*
4841 * Returns the current index of the quickfix/location list.
4842 * Returns 0 if there is an error.
4843 */
4844 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004845qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004846{
4847 qf_info_T *qi = &ql_info;
4848
Bram Moolenaar39665952018-08-15 20:59:48 +02004849 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004850 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004851 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004852 qi = GET_LOC_LIST(curwin);
4853 if (qi == NULL)
4854 return 0;
4855 }
4856
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004857 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004858}
4859
4860/*
4861 * Returns the current index in the quickfix/location list (counting only valid
4862 * entries). If no valid entries are in the list, then returns 1.
4863 */
4864 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004865qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004866{
4867 qf_info_T *qi = &ql_info;
4868 qf_list_T *qfl;
4869 qfline_T *qfp;
4870 int i, eidx = 0;
4871 int prev_fnum = 0;
4872
Bram Moolenaar39665952018-08-15 20:59:48 +02004873 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004874 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004875 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004876 qi = GET_LOC_LIST(curwin);
4877 if (qi == NULL)
4878 return 1;
4879 }
4880
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004881 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004882 qfp = qfl->qf_start;
4883
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004884 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004885 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4886 return 1;
4887
4888 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4889 {
4890 if (qfp->qf_valid)
4891 {
4892 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4893 {
4894 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4895 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004896 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004897 eidx++;
4898 prev_fnum = qfp->qf_fnum;
4899 }
4900 }
4901 else
4902 eidx++;
4903 }
4904 }
4905
4906 return eidx ? eidx : 1;
4907}
4908
4909/*
4910 * Get the 'n'th valid error entry in the quickfix or location list.
4911 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4912 * For :cdo and :ldo returns the 'n'th valid error entry.
4913 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4914 */
4915 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004916qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004917{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004918 qfline_T *qfp = qfl->qf_start;
4919 int i, eidx;
4920 int prev_fnum = 0;
4921
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004922 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004923 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4924 return 1;
4925
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004926 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004927 i++, qfp = qfp->qf_next)
4928 {
4929 if (qfp->qf_valid)
4930 {
4931 if (fdo)
4932 {
4933 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4934 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004935 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004936 eidx++;
4937 prev_fnum = qfp->qf_fnum;
4938 }
4939 }
4940 else
4941 eidx++;
4942 }
4943
4944 if (eidx == n)
4945 break;
4946 }
4947
4948 if (i <= qfl->qf_count)
4949 return i;
4950 else
4951 return 1;
4952}
4953
4954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004956 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004957 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 */
4959 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004960ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004962 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004963 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004964
Bram Moolenaar39665952018-08-15 20:59:48 +02004965 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004966 {
4967 qi = GET_LOC_LIST(curwin);
4968 if (qi == NULL)
4969 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004970 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004971 return;
4972 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004973 }
4974
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004975 if (eap->addr_count > 0)
4976 errornr = (int)eap->line2;
4977 else
4978 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004979 switch (eap->cmdidx)
4980 {
4981 case CMD_cc: case CMD_ll:
4982 errornr = 0;
4983 break;
4984 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4985 case CMD_lfirst:
4986 errornr = 1;
4987 break;
4988 default:
4989 errornr = 32767;
4990 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004991 }
4992
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004993 // For cdo and ldo commands, jump to the nth valid error.
4994 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004995 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4996 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004997 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004998 eap->addr_count > 0 ? (int)eap->line1 : 1,
4999 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5000
5001 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002}
5003
5004/*
5005 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005006 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005007 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 */
5009 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005010ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005012 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005013 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005014 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005015
Bram Moolenaar39665952018-08-15 20:59:48 +02005016 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005017 {
5018 qi = GET_LOC_LIST(curwin);
5019 if (qi == NULL)
5020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005021 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005022 return;
5023 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005024 }
5025
Bram Moolenaar55b69262017-08-13 13:42:01 +02005026 if (eap->addr_count > 0
5027 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5028 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005029 errornr = (int)eap->line2;
5030 else
5031 errornr = 1;
5032
Bram Moolenaar39665952018-08-15 20:59:48 +02005033 // Depending on the command jump to either next or previous entry/file.
5034 switch (eap->cmdidx)
5035 {
5036 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5037 dir = FORWARD;
5038 break;
5039 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5040 case CMD_lNext:
5041 dir = BACKWARD;
5042 break;
5043 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5044 dir = FORWARD_FILE;
5045 break;
5046 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5047 dir = BACKWARD_FILE;
5048 break;
5049 default:
5050 dir = FORWARD;
5051 break;
5052 }
5053
5054 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055}
5056
5057/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005058 * Return the autocmd name for the :cfile Ex commands
5059 */
5060 static char_u *
5061cfile_get_auname(cmdidx_T cmdidx)
5062{
5063 switch (cmdidx)
5064 {
5065 case CMD_cfile: return (char_u *)"cfile";
5066 case CMD_cgetfile: return (char_u *)"cgetfile";
5067 case CMD_caddfile: return (char_u *)"caddfile";
5068 case CMD_lfile: return (char_u *)"lfile";
5069 case CMD_lgetfile: return (char_u *)"lgetfile";
5070 case CMD_laddfile: return (char_u *)"laddfile";
5071 default: return NULL;
5072 }
5073}
5074
5075/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005076 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005077 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 */
5079 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005080ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005082 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005083 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005084 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005085 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005086 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005087 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005088
Bram Moolenaara16123a2019-03-28 20:31:07 +01005089 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005090 if (au_name != NULL)
5091 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005092
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005093 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005094#ifdef FEAT_BROWSE
5095 if (cmdmod.browse)
5096 {
5097 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005098 NULL, NULL,
5099 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005100 if (browse_file == NULL)
5101 return;
5102 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5103 vim_free(browse_file);
5104 }
5105 else
5106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005108 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005109
Bram Moolenaar39665952018-08-15 20:59:48 +02005110 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005111 wp = curwin;
5112
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005113 incr_quickfix_busy();
5114
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005115 // This function is used by the :cfile, :cgetfile and :caddfile
5116 // commands.
5117 // :cfile always creates a new quickfix list and jumps to the
5118 // first error.
5119 // :cgetfile creates a new quickfix list but doesn't jump to the
5120 // first error.
5121 // :caddfile adds to an existing quickfix list. If there is no
5122 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005123 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005124 && eap->cmdidx != CMD_laddfile),
5125 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005126 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005127 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005128 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005129 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005130 {
5131 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005132 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005133 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005134 }
5135 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005136 qf_list_changed(qf_get_curlist(qi));
5137 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005138 if (au_name != NULL)
5139 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005140
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005141 // Jump to the first error for a new list and if autocmds didn't
5142 // free the list.
5143 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5144 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005145 // display the first error
5146 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005147
5148 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005149}
5150
5151/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005152 * Return the vimgrep autocmd name.
5153 */
5154 static char_u *
5155vgr_get_auname(cmdidx_T cmdidx)
5156{
5157 switch (cmdidx)
5158 {
5159 case CMD_vimgrep: return (char_u *)"vimgrep";
5160 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5161 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5162 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5163 case CMD_grep: return (char_u *)"grep";
5164 case CMD_lgrep: return (char_u *)"lgrep";
5165 case CMD_grepadd: return (char_u *)"grepadd";
5166 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5167 default: return NULL;
5168 }
5169}
5170
5171/*
5172 * Initialize the regmatch used by vimgrep for pattern "s".
5173 */
5174 static void
5175vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5176{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005177 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005178 regmatch->regprog = NULL;
5179
5180 if (s == NULL || *s == NUL)
5181 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005182 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005183 if (last_search_pat() == NULL)
5184 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005185 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005186 return;
5187 }
5188 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5189 }
5190 else
5191 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5192
5193 regmatch->rmm_ic = p_ic;
5194 regmatch->rmm_maxcol = 0;
5195}
5196
5197/*
5198 * Display a file name when vimgrep is running.
5199 */
5200 static void
5201vgr_display_fname(char_u *fname)
5202{
5203 char_u *p;
5204
5205 msg_start();
5206 p = msg_strtrunc(fname, TRUE);
5207 if (p == NULL)
5208 msg_outtrans(fname);
5209 else
5210 {
5211 msg_outtrans(p);
5212 vim_free(p);
5213 }
5214 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005215 msg_didout = FALSE; // overwrite this message
5216 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005217 msg_col = 0;
5218 out_flush();
5219}
5220
5221/*
5222 * Load a dummy buffer to search for a pattern using vimgrep.
5223 */
5224 static buf_T *
5225vgr_load_dummy_buf(
5226 char_u *fname,
5227 char_u *dirname_start,
5228 char_u *dirname_now)
5229{
5230 int save_mls;
5231#if defined(FEAT_SYN_HL)
5232 char_u *save_ei = NULL;
5233#endif
5234 buf_T *buf;
5235
5236#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005237 // Don't do Filetype autocommands to avoid loading syntax and
5238 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005239 save_ei = au_event_disable(",Filetype");
5240#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005241 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005242 save_mls = p_mls;
5243 p_mls = 0;
5244
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005245 // Load file into a buffer, so that 'fileencoding' is detected,
5246 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005247 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5248
5249 p_mls = save_mls;
5250#if defined(FEAT_SYN_HL)
5251 au_event_restore(save_ei);
5252#endif
5253
5254 return buf;
5255}
5256
5257/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005258 * Check whether a quickfix/location list is valid. Autocmds may remove or
5259 * change a quickfix list when vimgrep is running. If the list is not found,
5260 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005261 */
5262 static int
5263vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005264 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005265 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005266 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005267 char_u *title)
5268{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005269 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005270 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005271 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005272 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005273 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005274 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005275 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005276 return FALSE;
5277 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005278 else
5279 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005280 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005281 qf_new_list(qi, title);
5282 return TRUE;
5283 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005284 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005285
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005286 if (qf_restore_list(qi, qfid) == FAIL)
5287 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005288
5289 return TRUE;
5290}
5291
5292/*
5293 * Search for a pattern in all the lines in a buffer and add the matching lines
5294 * to a quickfix list.
5295 */
5296 static int
5297vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005298 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005299 char_u *fname,
5300 buf_T *buf,
5301 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005302 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005303 int duplicate_name,
5304 int flags)
5305{
5306 int found_match = FALSE;
5307 long lnum;
5308 colnr_T col;
5309
Bram Moolenaar1c299432018-10-28 14:36:09 +01005310 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005311 {
5312 col = 0;
5313 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5314 col, NULL, NULL) > 0)
5315 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005316 // Pass the buffer number so that it gets used even for a
5317 // dummy buffer, unless duplicate_name is set, then the
5318 // buffer will be wiped out below.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005319 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005320 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005321 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005322 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005323 duplicate_name ? 0 : buf->b_fnum,
5324 ml_get_buf(buf,
5325 regmatch->startpos[0].lnum + lnum, FALSE),
5326 regmatch->startpos[0].lnum + lnum,
5327 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005328 FALSE, // vis_col
5329 NULL, // search pattern
5330 0, // nr
5331 0, // type
5332 TRUE // valid
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005333 ) == FAIL)
5334 {
5335 got_int = TRUE;
5336 break;
5337 }
5338 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005339 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005340 break;
5341 if ((flags & VGR_GLOBAL) == 0
5342 || regmatch->endpos[0].lnum > 0)
5343 break;
5344 col = regmatch->endpos[0].col
5345 + (col == regmatch->endpos[0].col);
5346 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5347 break;
5348 }
5349 line_breakcheck();
5350 if (got_int)
5351 break;
5352 }
5353
5354 return found_match;
5355}
5356
5357/*
5358 * Jump to the first match and update the directory.
5359 */
5360 static void
5361vgr_jump_to_match(
5362 qf_info_T *qi,
5363 int forceit,
5364 int *redraw_for_dummy,
5365 buf_T *first_match_buf,
5366 char_u *target_dir)
5367{
5368 buf_T *buf;
5369
5370 buf = curbuf;
5371 qf_jump(qi, 0, 0, forceit);
5372 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005373 // If we jumped to another buffer redrawing will already be
5374 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005375 *redraw_for_dummy = FALSE;
5376
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005377 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005378 if (curbuf == first_match_buf && target_dir != NULL)
5379 {
5380 exarg_T ea;
5381
5382 ea.arg = target_dir;
5383 ea.cmdidx = CMD_lcd;
5384 ex_cd(&ea);
5385 }
5386}
5387
5388/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005389 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005390 * ":vimgrepadd {pattern} file(s)"
5391 * ":lvimgrep {pattern} file(s)"
5392 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005393 */
5394 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005395ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005396{
Bram Moolenaar81695252004-12-29 20:58:21 +00005397 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005398 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005399 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005400 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005401 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005402 char_u *s;
5403 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005404 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005405 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005406 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005407 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005408 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005409 buf_T *buf;
5410 int duplicate_name = FALSE;
5411 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005412 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005413 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005414 buf_T *first_match_buf = NULL;
5415 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005416 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005417 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005418 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005419 char_u *dirname_start = NULL;
5420 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005421 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005422 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005423
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005424 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005425 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5426 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005427 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005428#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005429 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005430 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005431#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005432 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005433
Bram Moolenaar39665952018-08-15 20:59:48 +02005434 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005435 {
5436 qi = ll_get_or_alloc_list(curwin);
5437 if (qi == NULL)
5438 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005439 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005440 }
5441
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005442 if (eap->addr_count > 0)
5443 tomatch = eap->line2;
5444 else
5445 tomatch = MAXLNUM;
5446
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005447 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005448 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005449 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005450 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005451 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005452 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005453 emsg(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005454 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005455 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005456
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005457 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005458 if (regmatch.regprog == NULL)
5459 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005460
5461 p = skipwhite(p);
5462 if (*p == NUL)
5463 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005464 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005465 goto theend;
5466 }
5467
Bram Moolenaar55b69262017-08-13 13:42:01 +02005468 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005469 && eap->cmdidx != CMD_vimgrepadd
5470 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005471 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005472 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005473 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005474
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005475 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005476 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005477 goto theend;
5478 if (fcount == 0)
5479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005480 emsg(_(e_nomatch));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005481 goto theend;
5482 }
5483
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005484 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5485 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005486 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005487 {
5488 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005489 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005490 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005491
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005492 // Remember the current directory, because a BufRead autocommand that does
5493 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005494 mch_dirname(dirname_start, MAXPATHL);
5495
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005496 incr_quickfix_busy();
5497
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005498 // Remember the current quickfix list identifier, so that we can check for
5499 // autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005500 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005501
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005502 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005503 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005504 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005505 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005506 if (time(NULL) > seconds)
5507 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005508 // Display the file name every second or so, show the user we are
5509 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005510 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005511 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005512 }
5513
Bram Moolenaar81695252004-12-29 20:58:21 +00005514 buf = buflist_findname_exp(fnames[fi]);
5515 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5516 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005517 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005518 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005519 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005520 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005521
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005522 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005523 }
5524 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005525 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005526 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005527
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005528 // Check whether the quickfix list is still valid. When loading a
5529 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005530 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005531 {
5532 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005533 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005534 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005535 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005536 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005537
Bram Moolenaar81695252004-12-29 20:58:21 +00005538 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005539 {
5540 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005541 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005542 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005543 else
5544 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005545 // Try for a match in all lines of the buffer.
5546 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005547 found_match = vgr_match_buflines(qf_get_curlist(qi),
5548 fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005549 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005550
Bram Moolenaar81695252004-12-29 20:58:21 +00005551 if (using_dummy)
5552 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005553 if (found_match && first_match_buf == NULL)
5554 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005555 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005556 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005557 // Never keep a dummy buffer if there is another buffer
5558 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005559 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005560 buf = NULL;
5561 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005562 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005563 || buf->b_p_bh[0] == 'u' // "unload"
5564 || buf->b_p_bh[0] == 'w' // "wipe"
5565 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005566 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005567 // When no match was found we don't need to remember the
5568 // buffer, wipe it out. If there was a match and it
5569 // wasn't the first one or we won't jump there: only
5570 // unload the buffer.
5571 // Ignore 'hidden' here, because it may lead to having too
5572 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005573 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005574 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005575 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005576 buf = NULL;
5577 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005578 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005579 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005580 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005581 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005582 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005583 buf = NULL;
5584 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005585 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005586
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005587 if (buf != NULL)
5588 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005589 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005590 buf->b_flags &= ~BF_DUMMY;
5591
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005592 // If the buffer is still loaded we need to use the
5593 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005594 if (buf == first_match_buf
5595 && target_dir == NULL
5596 && STRCMP(dirname_start, dirname_now) != 0)
5597 target_dir = vim_strsave(dirname_now);
5598
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005599 // The buffer is still loaded, the Filetype autocommands
5600 // need to be done now, in that buffer. And the modelines
5601 // need to be done (again). But not the window-local
5602 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005603 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005604#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005605 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5606 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005607#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005608 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005609 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005610 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005611 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005612 }
5613 }
5614
5615 FreeWild(fcount, fnames);
5616
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005617 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005618 qfl->qf_nonevalid = FALSE;
5619 qfl->qf_ptr = qfl->qf_start;
5620 qfl->qf_index = 1;
5621 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005622
Bram Moolenaar864293a2016-06-02 13:40:04 +02005623 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005624
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005625 if (au_name != NULL)
5626 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5627 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005628 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5629 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005630 if (!qflist_valid(wp, save_qfid)
5631 || qf_restore_list(qi, save_qfid) == FAIL)
5632 {
5633 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005634 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005635 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005636
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005637 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01005638 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005639 {
5640 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005641 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5642 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005643 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005644 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005645 semsg(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005646
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005647 decr_quickfix_busy();
5648
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005649 // If we loaded a dummy buffer into the current window, the autocommands
5650 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005651 if (redraw_for_dummy)
5652 {
5653#ifdef FEAT_FOLDING
5654 foldUpdateAll(curwin);
5655#else
5656 redraw_later(NOT_VALID);
5657#endif
5658 }
5659
Bram Moolenaar86b68352004-12-27 21:59:20 +00005660theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005661 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005662 vim_free(dirname_now);
5663 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005664 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005665 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005666}
5667
5668/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005669 * Restore current working directory to "dirname_start" if they differ, taking
5670 * into account whether it is set locally or globally.
5671 */
5672 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005673restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005674{
5675 char_u *dirname_now = alloc(MAXPATHL);
5676
5677 if (NULL != dirname_now)
5678 {
5679 mch_dirname(dirname_now, MAXPATHL);
5680 if (STRCMP(dirname_start, dirname_now) != 0)
5681 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005682 // If the directory has changed, change it back by building up an
5683 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005684 exarg_T ea;
5685
5686 ea.arg = dirname_start;
5687 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5688 ex_cd(&ea);
5689 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005690 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005691 }
5692}
5693
5694/*
5695 * Load file "fname" into a dummy buffer and return the buffer pointer,
5696 * placing the directory resulting from the buffer load into the
5697 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5698 * prior to calling this function. Restores directory to "dirname_start" prior
5699 * to returning, if autocmds or the 'autochdir' option have changed it.
5700 *
5701 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5702 * or wipe_dummy_buffer() later!
5703 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005704 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005705 */
5706 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005707load_dummy_buffer(
5708 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005709 char_u *dirname_start, // in: old directory
5710 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005711{
5712 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005713 bufref_T newbufref;
5714 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005715 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005716 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005717 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005718
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005719 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005720 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5721 if (newbuf == NULL)
5722 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005723 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005725 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005726 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5727
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005728 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005729 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005730 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005731 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005732 ++newbuf->b_locked;
5733
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005734 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005735 aucmd_prepbuf(&aco, newbuf);
5736
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005737 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005738 (void)setfname(curbuf, fname, NULL, FALSE);
5739
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005740 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005741 check_need_swap(TRUE);
5742
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005743 // Remove the "dummy" flag, otherwise autocommands may not
5744 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005745 curbuf->b_flags &= ~BF_DUMMY;
5746
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005747 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005748 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005749 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005750 NULL, READ_NEW | READ_DUMMY);
5751 --newbuf->b_locked;
5752 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005753 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005754 && !(curbuf->b_flags & BF_NEW))
5755 {
5756 failed = FALSE;
5757 if (curbuf != newbuf)
5758 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005759 // Bloody autocommands changed the buffer! Can happen when
5760 // using netrw and editing a remote file. Use the current
5761 // buffer instead, delete the dummy one after restoring the
5762 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005763 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005764 newbuf = curbuf;
5765 }
5766 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005767
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005768 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005769 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005770 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5771 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005772
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005773 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5774 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005775 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005776 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005777
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005778 // When autocommands/'autochdir' option changed directory: go back.
5779 // Let the caller know what the resulting dir was first, in case it is
5780 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005781 mch_dirname(resulting_dir, MAXPATHL);
5782 restore_start_dir(dirname_start);
5783
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005784 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005785 return NULL;
5786 if (failed)
5787 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005788 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005789 return NULL;
5790 }
5791 return newbuf;
5792}
5793
5794/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005795 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5796 * directory to "dirname_start" prior to returning, if autocmds or the
5797 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005798 */
5799 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005800wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005801{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005802 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005803 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005804#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005805 cleanup_T cs;
5806
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005807 // Reset the error/interrupt/exception state here so that aborting()
5808 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5809 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005810 enter_cleanup(&cs);
5811#endif
5812
Bram Moolenaar81695252004-12-29 20:58:21 +00005813 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005814
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005815#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005816 // Restore the error/interrupt/exception state if not discarded by a
5817 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005818 leave_cleanup(&cs);
5819#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005820 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005821 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005822 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005823}
5824
5825/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005826 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5827 * directory to "dirname_start" prior to returning, if autocmds or the
5828 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005829 */
5830 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005831unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005832{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005833 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005834 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005835 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005836
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005837 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005838 restore_start_dir(dirname_start);
5839 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005840}
5841
Bram Moolenaar05159a02005-02-26 23:04:13 +00005842#if defined(FEAT_EVAL) || defined(PROTO)
5843/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005844 * Copy the specified quickfix entry items into a new dict and appened the dict
5845 * to 'list'. Returns OK on success.
5846 */
5847 static int
5848get_qfline_items(qfline_T *qfp, list_T *list)
5849{
5850 int bufnum;
5851 dict_T *dict;
5852 char_u buf[2];
5853
5854 // Handle entries with a non-existing buffer number.
5855 bufnum = qfp->qf_fnum;
5856 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5857 bufnum = 0;
5858
5859 if ((dict = dict_alloc()) == NULL)
5860 return FAIL;
5861 if (list_append_dict(list, dict) == FAIL)
5862 return FAIL;
5863
5864 buf[0] = qfp->qf_type;
5865 buf[1] = NUL;
5866 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5867 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5868 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5869 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5870 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5871 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5872 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5873 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5874 || dict_add_string(dict, "type", buf) == FAIL
5875 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
5876 return FAIL;
5877
5878 return OK;
5879}
5880
5881/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005882 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005883 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005884 */
5885 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005886get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005887{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005888 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01005889 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005890 qfline_T *qfp;
5891 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005892
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005893 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005894 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005895 qi = &ql_info;
5896 if (wp != NULL)
5897 {
5898 qi = GET_LOC_LIST(wp);
5899 if (qi == NULL)
5900 return FAIL;
5901 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005902 }
5903
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005904 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005905 qf_idx = qi->qf_curlist;
5906
Bram Moolenaar0398e002019-03-21 21:12:49 +01005907 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005908 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005909
Bram Moolenaar0398e002019-03-21 21:12:49 +01005910 qfl = qf_get_list(qi, qf_idx);
5911 if (qf_list_empty(qfl))
5912 return FAIL;
5913
Bram Moolenaara16123a2019-03-28 20:31:07 +01005914 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005915 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01005916 if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005917 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005918 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005919
Bram Moolenaar05159a02005-02-26 23:04:13 +00005920 return OK;
5921}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005922
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005923// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005924enum {
5925 QF_GETLIST_NONE = 0x0,
5926 QF_GETLIST_TITLE = 0x1,
5927 QF_GETLIST_ITEMS = 0x2,
5928 QF_GETLIST_NR = 0x4,
5929 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005930 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005931 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005932 QF_GETLIST_IDX = 0x40,
5933 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005934 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005935 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005936 QF_GETLIST_QFBUFNR = 0x400,
5937 QF_GETLIST_ALL = 0x7FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005938};
5939
5940/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005941 * Parse text from 'di' and return the quickfix list items.
5942 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005943 */
5944 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005945qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005946{
5947 int status = FAIL;
5948 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005949 char_u *errorformat = p_efm;
5950 dictitem_T *efm_di;
5951 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005952
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005953 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005954 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005955 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005956 // If errorformat is supplied then use it, otherwise use the 'efm'
5957 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005958 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5959 {
5960 if (efm_di->di_tv.v_type != VAR_STRING ||
5961 efm_di->di_tv.vval.v_string == NULL)
5962 return FAIL;
5963 errorformat = efm_di->di_tv.vval.v_string;
5964 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005965
Bram Moolenaar36538222017-09-02 19:51:44 +02005966 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005967 if (l == NULL)
5968 return FAIL;
5969
Bram Moolenaar2d67d302018-11-16 18:46:02 +01005970 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005971 if (qi != NULL)
5972 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005973 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005974 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5975 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005976 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005977 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005978 }
5979 free(qi);
5980 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005981 dict_add_list(retdict, "items", l);
5982 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005983 }
5984
5985 return status;
5986}
5987
5988/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005989 * Return the quickfix/location list window identifier in the current tabpage.
5990 */
5991 static int
5992qf_winid(qf_info_T *qi)
5993{
5994 win_T *win;
5995
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005996 // The quickfix window can be opened even if the quickfix list is not set
5997 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005998 if (qi == NULL)
5999 return 0;
6000 win = qf_find_win(qi);
6001 if (win != NULL)
6002 return win->w_id;
6003 return 0;
6004}
6005
6006/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006007 * Returns the number of the buffer displayed in the quickfix/location list
6008 * window. If there is no buffer associated with the list, then returns 0.
6009 */
6010 static int
6011qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6012{
6013 return dict_add_number(retdict, "qfbufnr",
6014 (qi == NULL) ? 0 : qi->qf_bufnr);
6015}
6016
6017/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006018 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006019 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006020 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006021qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006022{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006023 int flags = QF_GETLIST_NONE;
6024
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006025 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006026 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006027 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006028 if (!loclist)
6029 // File window ID is applicable only to location list windows
6030 flags &= ~ QF_GETLIST_FILEWINID;
6031 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006032
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006033 if (dict_find(what, (char_u *)"title", -1) != NULL)
6034 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006035
Bram Moolenaara6d48492017-12-12 22:45:31 +01006036 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6037 flags |= QF_GETLIST_NR;
6038
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006039 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6040 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006041
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006042 if (dict_find(what, (char_u *)"context", -1) != NULL)
6043 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006044
Bram Moolenaara6d48492017-12-12 22:45:31 +01006045 if (dict_find(what, (char_u *)"id", -1) != NULL)
6046 flags |= QF_GETLIST_ID;
6047
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006048 if (dict_find(what, (char_u *)"items", -1) != NULL)
6049 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006050
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006051 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6052 flags |= QF_GETLIST_IDX;
6053
6054 if (dict_find(what, (char_u *)"size", -1) != NULL)
6055 flags |= QF_GETLIST_SIZE;
6056
Bram Moolenaarb254af32017-12-18 19:48:58 +01006057 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6058 flags |= QF_GETLIST_TICK;
6059
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006060 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6061 flags |= QF_GETLIST_FILEWINID;
6062
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006063 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6064 flags |= QF_GETLIST_QFBUFNR;
6065
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006066 return flags;
6067}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006068
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006069/*
6070 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6071 * If 'nr' and 'id' are not present in 'what' then return the current
6072 * quickfix list index.
6073 * If 'nr' is zero then return the current quickfix list index.
6074 * If 'nr' is '$' then return the last quickfix list index.
6075 * If 'id' is present then return the index of the quickfix list with that id.
6076 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6077 * Return -1, if quickfix list is not present or if the stack is empty.
6078 */
6079 static int
6080qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6081{
6082 int qf_idx;
6083 dictitem_T *di;
6084
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006085 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006086 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6087 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006088 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006089 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006090 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006091 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006092 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006093 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006094 qf_idx = di->di_tv.vval.v_number - 1;
6095 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006096 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006097 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006098 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006099 else if (di->di_tv.v_type == VAR_STRING
6100 && di->di_tv.vval.v_string != NULL
6101 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006102 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006103 qf_idx = qi->qf_listcount - 1;
6104 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006105 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006106 }
6107
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006108 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6109 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006110 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006111 if (di->di_tv.v_type == VAR_NUMBER)
6112 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006113 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006114 if (di->di_tv.vval.v_number != 0)
6115 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6116 }
6117 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006118 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006119 }
6120
6121 return qf_idx;
6122}
6123
6124/*
6125 * Return default values for quickfix list properties in retdict.
6126 */
6127 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006128qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006129{
6130 int status = OK;
6131
6132 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006133 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006134 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6135 {
6136 list_T *l = list_alloc();
6137 if (l != NULL)
6138 status = dict_add_list(retdict, "items", l);
6139 else
6140 status = FAIL;
6141 }
6142 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006143 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006144 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006145 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006146 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006147 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006148 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006149 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006150 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006151 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006152 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006153 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006154 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006155 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006156 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006157 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006158 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6159 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006160
6161 return status;
6162}
6163
6164/*
6165 * Return the quickfix list title as 'title' in retdict
6166 */
6167 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006168qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006169{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006170 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006171}
6172
6173/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006174 * Returns the identifier of the window used to display files from a location
6175 * list. If there is no associated window, then returns 0. Useful only when
6176 * called from a location list window.
6177 */
6178 static int
6179qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6180{
6181 int winid = 0;
6182
6183 if (wp != NULL && IS_LL_WINDOW(wp))
6184 {
6185 win_T *ll_wp = qf_find_win_with_loclist(qi);
6186 if (ll_wp != NULL)
6187 winid = ll_wp->w_id;
6188 }
6189
6190 return dict_add_number(retdict, "filewinid", winid);
6191}
6192
6193/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006194 * Return the quickfix list items/entries as 'items' in retdict
6195 */
6196 static int
6197qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6198{
6199 int status = OK;
6200 list_T *l = list_alloc();
6201 if (l != NULL)
6202 {
6203 (void)get_errorlist(qi, NULL, qf_idx, l);
6204 dict_add_list(retdict, "items", l);
6205 }
6206 else
6207 status = FAIL;
6208
6209 return status;
6210}
6211
6212/*
6213 * Return the quickfix list context (if any) as 'context' in retdict.
6214 */
6215 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006216qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006217{
6218 int status;
6219 dictitem_T *di;
6220
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006221 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006222 {
6223 di = dictitem_alloc((char_u *)"context");
6224 if (di != NULL)
6225 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006226 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006227 status = dict_add(retdict, di);
6228 if (status == FAIL)
6229 dictitem_free(di);
6230 }
6231 else
6232 status = FAIL;
6233 }
6234 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006235 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006236
6237 return status;
6238}
6239
6240/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006241 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006242 */
6243 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01006244qf_getprop_idx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006245{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006246 int curidx = qfl->qf_index;
6247 if (qf_list_empty(qfl))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006248 // For empty lists, current index is set to 0
6249 curidx = 0;
6250 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006251}
6252
6253/*
6254 * Return quickfix/location list details (title) as a
6255 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6256 * then current list is used. Otherwise the specified list is used.
6257 */
6258 int
6259qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6260{
6261 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006262 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006263 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006264 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006265 dictitem_T *di;
6266 int flags = QF_GETLIST_NONE;
6267
6268 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6269 return qf_get_list_from_lines(what, di, retdict);
6270
6271 if (wp != NULL)
6272 qi = GET_LOC_LIST(wp);
6273
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006274 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006275
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006276 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006277 qf_idx = qf_getprop_qfidx(qi, what);
6278
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006279 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006280 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006281 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006282
Bram Moolenaar0398e002019-03-21 21:12:49 +01006283 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006284
Bram Moolenaard823fa92016-08-12 16:29:27 +02006285 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006286 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006287 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006288 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006289 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006290 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006291 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006292 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006293 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006294 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006295 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006296 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006297 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar0398e002019-03-21 21:12:49 +01006298 status = qf_getprop_idx(qfl, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006299 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006300 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006301 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006302 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006303 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6304 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006305 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6306 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006307
Bram Moolenaard823fa92016-08-12 16:29:27 +02006308 return status;
6309}
6310
6311/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006312 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006313 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6314 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006315 */
6316 static int
6317qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01006318 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006319 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006320 int first_entry,
6321 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006322{
6323 static int did_bufnr_emsg;
6324 char_u *filename, *module, *pattern, *text, *type;
6325 int bufnum, valid, status, col, vcol, nr;
6326 long lnum;
6327
6328 if (first_entry)
6329 did_bufnr_emsg = FALSE;
6330
Bram Moolenaar8f667172018-12-14 15:38:31 +01006331 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6332 module = dict_get_string(d, (char_u *)"module", TRUE);
6333 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6334 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6335 col = (int)dict_get_number(d, (char_u *)"col");
6336 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6337 nr = (int)dict_get_number(d, (char_u *)"nr");
6338 type = dict_get_string(d, (char_u *)"type", TRUE);
6339 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6340 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006341 if (text == NULL)
6342 text = vim_strsave((char_u *)"");
6343
6344 valid = TRUE;
6345 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6346 valid = FALSE;
6347
6348 // Mark entries with non-existing buffer number as not valid. Give the
6349 // error message only once.
6350 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6351 {
6352 if (!did_bufnr_emsg)
6353 {
6354 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006355 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006356 }
6357 valid = FALSE;
6358 bufnum = 0;
6359 }
6360
6361 // If the 'valid' field is present it overrules the detected value.
6362 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006363 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006364
Bram Moolenaar0398e002019-03-21 21:12:49 +01006365 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006366 NULL, // dir
6367 filename,
6368 module,
6369 bufnum,
6370 text,
6371 lnum,
6372 col,
6373 vcol, // vis_col
6374 pattern, // search pattern
6375 nr,
6376 type == NULL ? NUL : *type,
6377 valid);
6378
6379 vim_free(filename);
6380 vim_free(module);
6381 vim_free(pattern);
6382 vim_free(text);
6383 vim_free(type);
6384
Bram Moolenaar9752c722018-12-22 16:49:34 +01006385 if (valid)
6386 *valid_entry = TRUE;
6387
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006388 return status;
6389}
6390
6391/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006392 * Add list of entries to quickfix/location list. Each list entry is
6393 * a dictionary with item information.
6394 */
6395 static int
6396qf_add_entries(
6397 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006398 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006399 list_T *list,
6400 char_u *title,
6401 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006402{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006403 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006404 listitem_T *li;
6405 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006406 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006407 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006408 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006409
Bram Moolenaara3921f42017-06-04 15:30:34 +02006410 if (action == ' ' || qf_idx == qi->qf_listcount)
6411 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006412 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006413 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006414 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006415 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006416 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01006417 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006418 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006419 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006420 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006421 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006422 qf_free_items(qfl);
6423 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006424 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006425
6426 for (li = list->lv_first; li != NULL; li = li->li_next)
6427 {
6428 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006429 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006430
6431 d = li->li_tv.vval.v_dict;
6432 if (d == NULL)
6433 continue;
6434
Bram Moolenaar0398e002019-03-21 21:12:49 +01006435 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006436 &valid_entry);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006437 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006438 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006439 }
6440
Bram Moolenaar9752c722018-12-22 16:49:34 +01006441 // Check if any valid error entries are added to the list.
6442 if (valid_entry)
6443 qfl->qf_nonevalid = FALSE;
6444 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006445 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006446 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006447
6448 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006449 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006450 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006451
6452 // Update the current error index if not appending to the list or if the
6453 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006454 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01006455 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006456
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006457 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006458 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006459
6460 return retval;
6461}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006462
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006463/*
6464 * Get the quickfix list index from 'nr' or 'id'
6465 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006466 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006467qf_setprop_get_qfidx(
6468 qf_info_T *qi,
6469 dict_T *what,
6470 int action,
6471 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006472{
6473 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006474 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006475
Bram Moolenaard823fa92016-08-12 16:29:27 +02006476 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6477 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006478 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006479 if (di->di_tv.v_type == VAR_NUMBER)
6480 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006481 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006482 if (di->di_tv.vval.v_number != 0)
6483 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006484
Bram Moolenaar55b69262017-08-13 13:42:01 +02006485 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6486 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006487 // When creating a new list, accept qf_idx pointing to the next
6488 // non-available list and add the new list at the end of the
6489 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006490 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006491 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006492 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006493 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006494 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006495 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006496 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006497 }
6498 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006499 && di->di_tv.vval.v_string != NULL
6500 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006501 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006502 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006503 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006504 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006505 qf_idx = 0;
6506 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006507 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006508 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006509 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006510 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006511 }
6512
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006513 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006514 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006515 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006516 if (di->di_tv.v_type != VAR_NUMBER)
6517 return INVALID_QFIDX;
6518
6519 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006520 }
6521
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006522 return qf_idx;
6523}
6524
6525/*
6526 * Set the quickfix list title.
6527 */
6528 static int
6529qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6530{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006531 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006532
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006533 if (di->di_tv.v_type != VAR_STRING)
6534 return FAIL;
6535
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006536 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006537 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006538 if (qf_idx == qi->qf_curlist)
6539 qf_update_win_titlevar(qi);
6540
6541 return OK;
6542}
6543
6544/*
6545 * Set quickfix list items/entries.
6546 */
6547 static int
6548qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6549{
6550 int retval = FAIL;
6551 char_u *title_save;
6552
6553 if (di->di_tv.v_type != VAR_LIST)
6554 return FAIL;
6555
6556 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6557 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6558 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006559 vim_free(title_save);
6560
6561 return retval;
6562}
6563
6564/*
6565 * Set quickfix list items/entries from a list of lines.
6566 */
6567 static int
6568qf_setprop_items_from_lines(
6569 qf_info_T *qi,
6570 int qf_idx,
6571 dict_T *what,
6572 dictitem_T *di,
6573 int action)
6574{
6575 char_u *errorformat = p_efm;
6576 dictitem_T *efm_di;
6577 int retval = FAIL;
6578
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006579 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006580 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6581 {
6582 if (efm_di->di_tv.v_type != VAR_STRING ||
6583 efm_di->di_tv.vval.v_string == NULL)
6584 return FAIL;
6585 errorformat = efm_di->di_tv.vval.v_string;
6586 }
6587
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006588 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006589 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6590 return FAIL;
6591
6592 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006593 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006594 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6595 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6596 retval = OK;
6597
6598 return retval;
6599}
6600
6601/*
6602 * Set quickfix list context.
6603 */
6604 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006605qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006606{
6607 typval_T *ctx;
6608
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006609 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006610 ctx = alloc_tv();
6611 if (ctx != NULL)
6612 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006613 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006614
6615 return OK;
6616}
6617
6618/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006619 * Set the current index in the specified quickfix list
6620 */
6621 static int
6622qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
6623{
6624 int denote = FALSE;
6625 int newidx;
6626 int old_qfidx;
6627 qfline_T *qf_ptr;
6628
6629 // If the specified index is '$', then use the last entry
6630 if (di->di_tv.v_type == VAR_STRING
6631 && di->di_tv.vval.v_string != NULL
6632 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
6633 newidx = qfl->qf_count;
6634 else
6635 {
6636 // Otherwise use the specified index
6637 newidx = tv_get_number_chk(&di->di_tv, &denote);
6638 if (denote)
6639 return FAIL;
6640 }
6641
6642 if (newidx < 1) // sanity check
6643 return FAIL;
6644 if (newidx > qfl->qf_count)
6645 newidx = qfl->qf_count;
6646
6647 old_qfidx = qfl->qf_index;
6648 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
6649 if (qf_ptr == NULL)
6650 return FAIL;
6651 qfl->qf_ptr = qf_ptr;
6652 qfl->qf_index = newidx;
6653
6654 // If the current list is modified and it is displayed in the quickfix
6655 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006656 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006657 qf_win_pos_update(qi, old_qfidx);
6658
6659 return OK;
6660}
6661
6662/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006663 * Set quickfix/location list properties (title, items, context).
6664 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006665 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006666 */
6667 static int
6668qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6669{
6670 dictitem_T *di;
6671 int retval = FAIL;
6672 int qf_idx;
6673 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006674 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006675
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006676 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006677 newlist = TRUE;
6678
6679 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006680 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006681 return FAIL;
6682
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006683 if (newlist)
6684 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006685 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006686 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006687 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006688 }
6689
Bram Moolenaar0398e002019-03-21 21:12:49 +01006690 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006691 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006692 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006693 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006694 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006695 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006696 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006697 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006698 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006699 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6700 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006701
Bram Moolenaarb254af32017-12-18 19:48:58 +01006702 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006703 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006704
Bram Moolenaard823fa92016-08-12 16:29:27 +02006705 return retval;
6706}
6707
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006708/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006709 * Free the entire quickfix/location list stack.
6710 * If the quickfix/location list window is open, then clear it.
6711 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006712 static void
6713qf_free_stack(win_T *wp, qf_info_T *qi)
6714{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006715 win_T *qfwin = qf_find_win(qi);
6716 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006717
6718 if (qfwin != NULL)
6719 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006720 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006721 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006722 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006723 qf_update_buffer(qi, NULL);
6724 }
6725
6726 if (wp != NULL && IS_LL_WINDOW(wp))
6727 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006728 // If in the location list window, then use the non-location list
6729 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006730 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006731 if (llwin != NULL)
6732 wp = llwin;
6733 }
6734
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006735 qf_free_all(wp);
6736 if (wp == NULL)
6737 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006738 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006739 qi->qf_curlist = 0;
6740 qi->qf_listcount = 0;
6741 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006742 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006743 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006744 // If the location list window is open, then create a new empty
6745 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006746 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006747 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006748
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006749 // first free the list reference in the location list window
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006750 ll_free_all(&qfwin->w_llist_ref);
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006751
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006752 qfwin->w_llist_ref = new_ll;
6753 if (wp != qfwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006754 win_set_loclist(wp, new_ll);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006755 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006756}
6757
Bram Moolenaard823fa92016-08-12 16:29:27 +02006758/*
6759 * Populate the quickfix list with the items supplied in the list
6760 * of dictionaries. "title" will be copied to w:quickfix_title.
6761 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6762 */
6763 int
6764set_errorlist(
6765 win_T *wp,
6766 list_T *list,
6767 int action,
6768 char_u *title,
6769 dict_T *what)
6770{
6771 qf_info_T *qi = &ql_info;
6772 int retval = OK;
6773
6774 if (wp != NULL)
6775 {
6776 qi = ll_get_or_alloc_list(wp);
6777 if (qi == NULL)
6778 return FAIL;
6779 }
6780
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006781 if (action == 'f')
6782 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006783 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006784 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006785 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006786 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006787
6788 incr_quickfix_busy();
6789
6790 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006791 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006792 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006793 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006794 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006795 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006796 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01006797 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006798
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006799 decr_quickfix_busy();
6800
Bram Moolenaard823fa92016-08-12 16:29:27 +02006801 return retval;
6802}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006803
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006804/*
6805 * Mark the context as in use for all the lists in a quickfix stack.
6806 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006807 static int
6808mark_quickfix_ctx(qf_info_T *qi, int copyID)
6809{
6810 int i;
6811 int abort = FALSE;
6812 typval_T *ctx;
6813
6814 for (i = 0; i < LISTCOUNT && !abort; ++i)
6815 {
6816 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006817 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6818 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006819 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6820 }
6821
6822 return abort;
6823}
6824
6825/*
6826 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006827 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006828 */
6829 int
6830set_ref_in_quickfix(int copyID)
6831{
6832 int abort = FALSE;
6833 tabpage_T *tp;
6834 win_T *win;
6835
6836 abort = mark_quickfix_ctx(&ql_info, copyID);
6837 if (abort)
6838 return abort;
6839
6840 FOR_ALL_TAB_WINDOWS(tp, win)
6841 {
6842 if (win->w_llist != NULL)
6843 {
6844 abort = mark_quickfix_ctx(win->w_llist, copyID);
6845 if (abort)
6846 return abort;
6847 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006848 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6849 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006850 // In a location list window and none of the other windows is
6851 // referring to this location list. Mark the location list
6852 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006853 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6854 if (abort)
6855 return abort;
6856 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006857 }
6858
6859 return abort;
6860}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006861#endif
6862
Bram Moolenaar81695252004-12-29 20:58:21 +00006863/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01006864 * Return the autocmd name for the :cbuffer Ex commands
6865 */
6866 static char_u *
6867cbuffer_get_auname(cmdidx_T cmdidx)
6868{
6869 switch (cmdidx)
6870 {
6871 case CMD_cbuffer: return (char_u *)"cbuffer";
6872 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
6873 case CMD_caddbuffer: return (char_u *)"caddbuffer";
6874 case CMD_lbuffer: return (char_u *)"lbuffer";
6875 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
6876 case CMD_laddbuffer: return (char_u *)"laddbuffer";
6877 default: return NULL;
6878 }
6879}
6880
6881/*
6882 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
6883 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
6884 */
6885 static int
6886cbuffer_process_args(
6887 exarg_T *eap,
6888 buf_T **bufp,
6889 linenr_T *line1,
6890 linenr_T *line2)
6891{
6892 buf_T *buf = NULL;
6893
6894 if (*eap->arg == NUL)
6895 buf = curbuf;
6896 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6897 buf = buflist_findnr(atoi((char *)eap->arg));
6898
6899 if (buf == NULL)
6900 {
6901 emsg(_(e_invarg));
6902 return FAIL;
6903 }
6904
6905 if (buf->b_ml.ml_mfp == NULL)
6906 {
6907 emsg(_("E681: Buffer is not loaded"));
6908 return FAIL;
6909 }
6910
6911 if (eap->addr_count == 0)
6912 {
6913 eap->line1 = 1;
6914 eap->line2 = buf->b_ml.ml_line_count;
6915 }
6916
6917 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6918 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6919 {
6920 emsg(_(e_invrange));
6921 return FAIL;
6922 }
6923
6924 *line1 = eap->line1;
6925 *line2 = eap->line2;
6926 *bufp = buf;
6927
6928 return OK;
6929}
6930
6931/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006932 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006933 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006934 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006935 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006936 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006937 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006938 */
6939 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006940ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006941{
6942 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006943 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006944 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006945 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006946 int_u save_qfid;
6947 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01006948 char_u *qf_title;
6949 linenr_T line1;
6950 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006951
Bram Moolenaara16123a2019-03-28 20:31:07 +01006952 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01006953 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01006954 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006955 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006956#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006957 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006958 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006959#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006960 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006961
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006962 // Must come after autocommands.
Bram Moolenaar39665952018-08-15 20:59:48 +02006963 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006964 {
6965 qi = ll_get_or_alloc_list(curwin);
6966 if (qi == NULL)
6967 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006968 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006969 }
6970
Bram Moolenaara16123a2019-03-28 20:31:07 +01006971 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
6972 return;
6973
6974 qf_title = qf_cmdtitle(*eap->cmdlinep);
6975
6976 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006977 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01006978 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6979 (char *)qf_title, (char *)buf->b_sfname);
6980 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006981 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006982
6983 incr_quickfix_busy();
6984
6985 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
6986 (eap->cmdidx != CMD_caddbuffer
6987 && eap->cmdidx != CMD_laddbuffer),
6988 line1, line2,
6989 qf_title, NULL);
6990 if (qf_stack_empty(qi))
6991 {
6992 decr_quickfix_busy();
6993 return;
6994 }
6995 if (res >= 0)
6996 qf_list_changed(qf_get_curlist(qi));
6997
6998 // Remember the current quickfix list identifier, so that we can
6999 // check for autocommands changing the current quickfix list.
7000 save_qfid = qf_get_curlist(qi)->qf_id;
7001 if (au_name != NULL)
7002 {
7003 buf_T *curbuf_old = curbuf;
7004
7005 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7006 TRUE, curbuf);
7007 if (curbuf != curbuf_old)
7008 // Autocommands changed buffer, don't jump now, "qi" may
7009 // be invalid.
7010 res = 0;
7011 }
7012 // Jump to the first error for a new list and if autocmds didn't
7013 // free the list.
7014 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7015 eap->cmdidx == CMD_lbuffer)
7016 && qflist_valid(wp, save_qfid))
7017 // display the first error
7018 qf_jump_first(qi, save_qfid, eap->forceit);
7019
7020 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007021}
7022
Bram Moolenaar1e015462005-09-25 22:16:38 +00007023#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007024/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007025 * Return the autocmd name for the :cexpr Ex commands.
7026 */
7027 static char_u *
7028cexpr_get_auname(cmdidx_T cmdidx)
7029{
7030 switch (cmdidx)
7031 {
7032 case CMD_cexpr: return (char_u *)"cexpr";
7033 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7034 case CMD_caddexpr: return (char_u *)"caddexpr";
7035 case CMD_lexpr: return (char_u *)"lexpr";
7036 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7037 case CMD_laddexpr: return (char_u *)"laddexpr";
7038 default: return NULL;
7039 }
7040}
7041
7042/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007043 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7044 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007045 */
7046 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007047ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007048{
7049 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007050 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007051 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007052 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007053 int_u save_qfid;
7054 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007055
Bram Moolenaara16123a2019-03-28 20:31:07 +01007056 au_name = cexpr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007057 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7058 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007059 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007060#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007061 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007062 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007063#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007064 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007065
Bram Moolenaar39665952018-08-15 20:59:48 +02007066 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01007067 {
7068 qi = ll_get_or_alloc_list(curwin);
7069 if (qi == NULL)
7070 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007071 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007072 }
7073
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007074 // Evaluate the expression. When the result is a string or a list we can
7075 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007076 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007077 if (tv != NULL)
7078 {
7079 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7080 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7081 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007082 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007083 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00007084 (eap->cmdidx != CMD_caddexpr
7085 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007086 (linenr_T)0, (linenr_T)0,
7087 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007088 if (qf_stack_empty(qi))
7089 {
7090 decr_quickfix_busy();
7091 goto cleanup;
7092 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01007093 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007094 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007095
7096 // Remember the current quickfix list identifier, so that we can
7097 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007098 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007099 if (au_name != NULL)
7100 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7101 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007102
7103 // Jump to the first error for a new list and if autocmds didn't
7104 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02007105 if (res > 0 && (eap->cmdidx == CMD_cexpr
7106 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007107 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02007108 // display the first error
7109 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007110 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00007111 }
7112 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007113 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007114cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00007115 free_tv(tv);
7116 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007117}
Bram Moolenaar1e015462005-09-25 22:16:38 +00007118#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007119
7120/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007121 * Get the location list for ":lhelpgrep"
7122 */
7123 static qf_info_T *
7124hgr_get_ll(int *new_ll)
7125{
7126 win_T *wp;
7127 qf_info_T *qi;
7128
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007129 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007130 if (bt_help(curwin->w_buffer))
7131 wp = curwin;
7132 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007133 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007134 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007135
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007136 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007137 qi = NULL;
7138 else
7139 qi = wp->w_llist;
7140
7141 if (qi == NULL)
7142 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007143 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007144 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007145 return NULL;
7146 *new_ll = TRUE;
7147 }
7148
7149 return qi;
7150}
7151
7152/*
7153 * Search for a pattern in a help file.
7154 */
7155 static void
7156hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007157 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007158 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007159 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007160 regmatch_T *p_regmatch)
7161{
7162 FILE *fd;
7163 long lnum;
7164
7165 fd = mch_fopen((char *)fname, "r");
7166 if (fd == NULL)
7167 return;
7168
7169 lnum = 1;
7170 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7171 {
7172 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007173
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007174 // Convert a line if 'encoding' is not utf-8 and
7175 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007176 if (p_vc->vc_type != CONV_NONE
7177 && has_non_ascii(IObuff))
7178 {
7179 line = string_convert(p_vc, IObuff, NULL);
7180 if (line == NULL)
7181 line = IObuff;
7182 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007183
7184 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7185 {
7186 int l = (int)STRLEN(line);
7187
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007188 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007189 while (l > 0 && line[l - 1] <= ' ')
7190 line[--l] = NUL;
7191
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007192 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007193 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007194 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007195 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007196 0,
7197 line,
7198 lnum,
7199 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007200 + 1, // col
7201 FALSE, // vis_col
7202 NULL, // search pattern
7203 0, // nr
7204 1, // type
7205 TRUE // valid
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007206 ) == FAIL)
7207 {
7208 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007209 if (line != IObuff)
7210 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007211 break;
7212 }
7213 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007214 if (line != IObuff)
7215 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007216 ++lnum;
7217 line_breakcheck();
7218 }
7219 fclose(fd);
7220}
7221
7222/*
7223 * Search for a pattern in all the help files in the doc directory under
7224 * the given directory.
7225 */
7226 static void
7227hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007228 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007229 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007230 regmatch_T *p_regmatch,
7231 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007232#ifdef FEAT_MULTI_LANG
7233 , char_u *lang
7234#endif
7235 )
7236{
7237 int fcount;
7238 char_u **fnames;
7239 int fi;
7240
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007241 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007242 add_pathsep(dirname);
7243 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7244 if (gen_expand_wildcards(1, &dirname, &fcount,
7245 &fnames, EW_FILE|EW_SILENT) == OK
7246 && fcount > 0)
7247 {
7248 for (fi = 0; fi < fcount && !got_int; ++fi)
7249 {
7250#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007251 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007252 if (lang != NULL
7253 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007254 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007255 && !(STRNICMP(lang, "en", 2) == 0
7256 && STRNICMP("txt", fnames[fi]
7257 + STRLEN(fnames[fi]) - 3, 3) == 0))
7258 continue;
7259#endif
7260
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007261 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007262 }
7263 FreeWild(fcount, fnames);
7264 }
7265}
7266
7267/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007268 * Search for a pattern in all the help files in the 'runtimepath'
7269 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007270 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007271 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007272 */
7273 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007274hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007275{
7276 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007277
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007278 vimconv_T vc;
7279
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007280 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7281 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007282 vc.vc_type = CONV_NONE;
7283 if (!enc_utf8)
7284 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007285
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007286 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007287 p = p_rtp;
7288 while (*p != NUL && !got_int)
7289 {
7290 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7291
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007292 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007293#ifdef FEAT_MULTI_LANG
7294 , lang
7295#endif
7296 );
7297 }
7298
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007299 if (vc.vc_type != CONV_NONE)
7300 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007301}
7302
7303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 * ":helpgrep {pattern}"
7305 */
7306 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007307ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308{
7309 regmatch_T regmatch;
7310 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007311 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007312 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007313 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007314 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315
Bram Moolenaar73633f82012-01-20 13:39:07 +01007316 switch (eap->cmdidx)
7317 {
7318 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7319 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7320 default: break;
7321 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007322 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7323 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007324 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007325#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007326 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007327 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007328#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007329 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007330
Bram Moolenaar39665952018-08-15 20:59:48 +02007331 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007332 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007333 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007334 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007335 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007336 }
7337
Bram Moolenaara16123a2019-03-28 20:31:07 +01007338 // Make 'cpoptions' empty, the 'l' flag should not be used here.
7339 save_cpo = p_cpo;
7340 p_cpo = empty_option;
7341
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007342 incr_quickfix_busy();
7343
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007344#ifdef FEAT_MULTI_LANG
7345 // Check for a specified language
7346 lang = check_help_lang(eap->arg);
7347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7349 regmatch.rm_ic = FALSE;
7350 if (regmatch.regprog != NULL)
7351 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007352 qf_list_T *qfl;
7353
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007354 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007355 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007356 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007358 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007359
Bram Moolenaar473de612013-06-08 18:19:48 +02007360 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007362 qfl->qf_nonevalid = FALSE;
7363 qfl->qf_ptr = qfl->qf_start;
7364 qfl->qf_index = 1;
7365 qf_list_changed(qfl);
7366 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 }
7368
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007369 if (p_cpo == empty_option)
7370 p_cpo = save_cpo;
7371 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007372 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007373 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374
Bram Moolenaar73633f82012-01-20 13:39:07 +01007375 if (au_name != NULL)
7376 {
7377 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7378 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007379 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007380 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007381 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007382 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007383 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007384 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007385 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007386
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007387 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007388 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007389 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007390 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007391 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007392
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007393 decr_quickfix_busy();
7394
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007395 if (eap->cmdidx == CMD_lhelpgrep)
7396 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007397 // If the help window is not opened or if it already points to the
7398 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007399 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007400 {
7401 if (new_qi)
7402 ll_free_all(&qi);
7403 }
7404 else if (curwin->w_llist == NULL)
7405 curwin->w_llist = qi;
7406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407}
7408
7409#endif /* FEAT_QUICKFIX */