blob: bee56ee7a9a3fd2c904e33bf99ad4685b8a423f9 [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 Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020052/*
53 * Quickfix/Location list definition
54 * Contains a list of entries (qfline_T). qf_start points to the first entry
55 * and qf_last points to the last entry. qf_count contains the list size.
56 *
57 * Usually the list contains one or more entries. But an empty list can be
58 * created using setqflist()/setloclist() with a title and/or user context
59 * information and entries can be added later using setqflist()/setloclist().
60 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000062{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020063 int_u qf_id; // Unique identifier for this list
64 qfline_T *qf_start; // pointer to the first error
65 qfline_T *qf_last; // pointer to the last error
66 qfline_T *qf_ptr; // pointer to the current error
67 int qf_count; // number of errors (0 means empty list)
68 int qf_index; // current index in the error list
69 int qf_nonevalid; // TRUE if not a single valid entry found
70 char_u *qf_title; // title derived from the command that created
71 // the error list or set by setqflist
72 typval_T *qf_ctx; // context set by setqflist/setloclist
Bram Moolenaara7df8c72017-07-19 13:23:06 +020073
74 struct dir_stack_T *qf_dir_stack;
75 char_u *qf_directory;
76 struct dir_stack_T *qf_file_stack;
77 char_u *qf_currfile;
78 int qf_multiline;
79 int qf_multiignore;
80 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010081 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000082} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020084/*
85 * Quickfix/Location list stack definition
86 * Contains a list of quickfix/location lists (qf_list_T)
87 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000088struct qf_info_S
89{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020090 // Count of references to this list. Used only for location lists.
91 // When a location list window reference this list, qf_refcount
92 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
93 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +000094 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020095 int qf_listcount; // current number of lists
96 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +000097 qf_list_T qf_lists[LISTCOUNT];
98};
99
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200100static qf_info_T ql_info; // global quickfix list
101static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200103#define FMT_PATTERNS 11 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
105/*
106 * Structure used to hold the info of one part of 'errorformat'
107 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000108typedef struct efm_S efm_T;
109struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200111 regprog_T *prog; // pre-formatted part of 'errorformat'
112 efm_T *next; // pointer to next (NULL if last)
113 char_u addr[FMT_PATTERNS]; // indices of used % patterns
114 char_u prefix; // prefix of this format line:
115 // 'D' enter directory
116 // 'X' leave directory
117 // 'A' start of multi-line message
118 // 'E' error message
119 // 'W' warning message
120 // 'I' informational message
121 // 'C' continuation line
122 // 'Z' end of multi-line message
123 // 'G' general, unspecific message
124 // 'P' push file (partial) message
125 // 'Q' pop/quit file (partial) message
126 // 'O' overread (partial) message
127 char_u flags; // additional flags given in prefix
128 // '-' do not include this line
129 // '+' include whole line in message
130 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131};
132
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200133// List of location lists to be deleted.
134// Used to delay the deletion of locations lists by autocmds.
135typedef struct qf_delq_S
136{
137 struct qf_delq_S *next;
138 qf_info_T *qi;
139} qf_delq_T;
140static qf_delq_T *qf_delq_head = NULL;
141
142// Counter to prevent autocmds from freeing up location lists when they are
143// still being used.
144static int quickfix_busy = 0;
145
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200146static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100147
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaard76ce852018-05-01 15:02:04 +0200149static int qf_add_entry(qf_info_T *qi, int qf_idx, 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 +0200150static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200152static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200153static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200155static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100158static win_T *qf_find_win(qf_info_T *qi);
159static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200160static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200161static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100162static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
163static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
164static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
165static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200167// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000168#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200169// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000170#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200171
172// Quickfix and location list stack check helper macros
173#define IS_QF_STACK(qi) (qi == &ql_info)
174#define IS_LL_STACK(qi) (qi != &ql_info)
175
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000176/*
177 * Return location list for window 'wp'
178 * For location list window, return the referenced location list
179 */
180#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200183 * Looking up a buffer can be slow if there are many. Remember the last one
184 * to make this a lot faster if there are multiple matches in the same file.
185 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200186static char_u *qf_last_bufname = NULL;
187static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200188
Bram Moolenaar3c097222017-12-21 20:54:49 +0100189static char *e_loc_list_changed =
190 N_("E926: Current location list was changed");
191
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200192/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200193 * Maximum number of bytes allowed per line while reading a errorfile.
194 */
195#define LINE_MAXLEN 4096
196
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200197static struct fmtpattern
198{
199 char_u convchar;
200 char *pattern;
201} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200202 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200203 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200204 {'n', "\\d\\+"},
205 {'l', "\\d\\+"},
206 {'c', "\\d\\+"},
207 {'t', "."},
208 {'m', ".\\+"},
209 {'r', ".*"},
210 {'p', "[- .]*"},
211 {'v', "\\d\\+"},
212 {'s', ".\\+"},
213 {'o', ".\\+"}
214 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200215
216/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200217 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200218 * See fmt_pat definition above for the list of supported patterns. The
219 * pattern specifier is supplied in "efmpat". The converted pattern is stored
220 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200221 */
222 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200223efmpat_to_regpat(
224 char_u *efmpat,
225 char_u *regpat,
226 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200227 int idx,
228 int round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200229 char_u *errmsg)
230{
231 char_u *srcptr;
232
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200233 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200234 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200235 // Each errorformat pattern can occur only once
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200236 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200237 _("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200238 EMSG(errmsg);
239 return NULL;
240 }
241 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200242 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200243 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200244 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200245 {
246 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200247 _("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200248 EMSG(errmsg);
249 return NULL;
250 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200251 efminfo->addr[idx] = (char_u)++round;
252 *regpat++ = '\\';
253 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200255 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200256 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200257 // Also match "c:" in the file name, even when
258 // checking for a colon next: "%f:".
259 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200260 STRCPY(regpat, "\\%(\\a:\\)\\=");
261 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200262 }
263#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200264 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200265 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200266 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200267 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200268 // A file name may contain spaces, but this isn't
269 // in "\f". For "%f:%l:%m" there may be a ":" in
270 // the file name. Use ".\{-1,}x" instead (x is
271 // the next character), the requirement that :999:
272 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200273 STRCPY(regpat, ".\\{-1,}");
274 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200275 }
276 else
277 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200278 // File name followed by '\\' or '%': include as
279 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200280 STRCPY(regpat, "\\f\\+");
281 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200282 }
283 }
284 else
285 {
286 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200287 while ((*regpat = *srcptr++) != NUL)
288 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200289 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200290 *regpat++ = '\\';
291 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200292
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200293 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200294}
295
296/*
297 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200298 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200299 */
300 static char_u *
301scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200302 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200303 char_u *efm,
304 int len,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200305 char_u *regpat,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200306 char_u *errmsg)
307{
308 char_u *efmp = *pefmp;
309
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200310 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200311 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200312 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200313 {
314 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200315 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200316 if (efmp < efm + len)
317 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200318 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200319 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200320 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200321 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200322 if (efmp == efm + len)
323 {
324 EMSG(_("E374: Missing ] in format string"));
325 return NULL;
326 }
327 }
328 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200329 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200330 *regpat++ = *++efmp;
331 *regpat++ = '\\';
332 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200333 }
334 else
335 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200336 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200337 sprintf((char *)errmsg,
338 _("E375: Unsupported %%%c in format string"), *efmp);
339 EMSG(errmsg);
340 return NULL;
341 }
342
343 *pefmp = efmp;
344
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200345 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200346}
347
348/*
349 * Analyze/parse an errorformat prefix.
350 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200351 static char_u *
352efm_analyze_prefix(char_u *efmp, efm_T *efminfo, char_u *errmsg)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200353{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200354 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200355 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200356 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200357 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200358 else
359 {
360 sprintf((char *)errmsg,
361 _("E376: Invalid %%%c in format string prefix"), *efmp);
362 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200363 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364 }
365
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200366 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200367}
368
369/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200370 * Converts a 'errorformat' string part in 'efm' to a regular expression
371 * pattern. The resulting regex pattern is returned in "regpat". Additional
372 * information about the 'erroformat' pattern is returned in "fmt_ptr".
373 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200374 */
375 static int
376efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200377 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200378 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200379 efm_T *fmt_ptr,
380 char_u *regpat,
381 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200382{
383 char_u *ptr;
384 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200385 int round;
386 int idx = 0;
387
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200388 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200389 ptr = regpat;
390 *ptr++ = '^';
391 round = 0;
392 for (efmp = efm; efmp < efm + len; ++efmp)
393 {
394 if (*efmp == '%')
395 {
396 ++efmp;
397 for (idx = 0; idx < FMT_PATTERNS; ++idx)
398 if (fmt_pat[idx].convchar == *efmp)
399 break;
400 if (idx < FMT_PATTERNS)
401 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200402 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200403 errmsg);
404 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200405 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200406 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200407 }
408 else if (*efmp == '*')
409 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200410 ++efmp;
411 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr, errmsg);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200412 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200413 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200414 }
415 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200416 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200417 else if (*efmp == '#')
418 *ptr++ = '*';
419 else if (*efmp == '>')
420 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200421 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200422 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200423 // prefix is allowed only at the beginning of the errorformat
424 // option part
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200425 efmp = efm_analyze_prefix(efmp, fmt_ptr, errmsg);
426 if (efmp == NULL)
427 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200428 }
429 else
430 {
431 sprintf((char *)errmsg,
432 _("E377: Invalid %%%c in format string"), *efmp);
433 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200434 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200435 }
436 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200437 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200438 {
439 if (*efmp == '\\' && efmp + 1 < efm + len)
440 ++efmp;
441 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200442 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200443 if (*efmp)
444 *ptr++ = *efmp;
445 }
446 }
447 *ptr++ = '$';
448 *ptr = NUL;
449
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200450 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200451}
452
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200453/*
454 * Free the 'errorformat' information list
455 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200456 static void
457free_efm_list(efm_T **efm_first)
458{
459 efm_T *efm_ptr;
460
461 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
462 {
463 *efm_first = efm_ptr->next;
464 vim_regfree(efm_ptr->prog);
465 vim_free(efm_ptr);
466 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100467 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200468}
469
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200470/*
471 * Compute the size of the buffer used to convert a 'errorformat' pattern into
472 * a regular expression pattern.
473 */
474 static int
475efm_regpat_bufsz(char_u *efm)
476{
477 int sz;
478 int i;
479
480 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
481 for (i = FMT_PATTERNS; i > 0; )
482 sz += (int)STRLEN(fmt_pat[--i].pattern);
483#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200484 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200485#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200486 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200487#endif
488
489 return sz;
490}
491
492/*
493 * Return the length of a 'errorformat' option part (separated by ",").
494 */
495 static int
496efm_option_part_len(char_u *efm)
497{
498 int len;
499
500 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
501 if (efm[len] == '\\' && efm[len + 1] != NUL)
502 ++len;
503
504 return len;
505}
506
507/*
508 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
509 * are parsed and converted to regular expressions. Returns information about
510 * the parsed 'errorformat' option.
511 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200512 static efm_T *
513parse_efm_option(char_u *efm)
514{
515 char_u *errmsg = NULL;
516 int errmsglen;
517 efm_T *fmt_ptr = NULL;
518 efm_T *fmt_first = NULL;
519 efm_T *fmt_last = NULL;
520 char_u *fmtstr = NULL;
521 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200522 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200523
524 errmsglen = CMDBUFFSIZE + 1;
525 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
526 if (errmsg == NULL)
527 goto parse_efm_end;
528
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 Moolenaarde3b3672018-08-07 21:54:41 +0200552 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == 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 Moolenaar688e3d12016-06-26 22:05:54 +0200561 EMSG(_("E378: 'errorformat' contains no pattern"));
562
563 goto parse_efm_end;
564
565parse_efm_error:
566 free_efm_list(&fmt_first);
567
568parse_efm_end:
569 vim_free(fmtstr);
570 vim_free(errmsg);
571
572 return fmt_first;
573}
574
Bram Moolenaare0d37972016-07-15 22:36:01 +0200575enum {
576 QF_FAIL = 0,
577 QF_OK = 1,
578 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200579 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200580 QF_IGNORE_LINE = 4,
581 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200582};
583
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200584/*
585 * State information used to parse lines and add entries to a quickfix/location
586 * list.
587 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200588typedef struct {
589 char_u *linebuf;
590 int linelen;
591 char_u *growbuf;
592 int growbufsiz;
593 FILE *fd;
594 typval_T *tv;
595 char_u *p_str;
596 listitem_T *p_li;
597 buf_T *buf;
598 linenr_T buflnum;
599 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100600 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200601} qfstate_T;
602
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200603/*
604 * Allocate more memory for the line buffer used for parsing lines.
605 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200606 static char_u *
607qf_grow_linebuf(qfstate_T *state, int newsz)
608{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200609 char_u *p;
610
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200611 // If the line exceeds LINE_MAXLEN exclude the last
612 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200613 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
614 if (state->growbuf == NULL)
615 {
616 state->growbuf = alloc(state->linelen + 1);
617 if (state->growbuf == NULL)
618 return NULL;
619 state->growbufsiz = state->linelen;
620 }
621 else if (state->linelen > state->growbufsiz)
622 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200623 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200624 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200625 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200626 state->growbufsiz = state->linelen;
627 }
628 return state->growbuf;
629}
630
631/*
632 * Get the next string (separated by newline) from state->p_str.
633 */
634 static int
635qf_get_next_str_line(qfstate_T *state)
636{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200637 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200638 char_u *p_str = state->p_str;
639 char_u *p;
640 int len;
641
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200642 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200643 return QF_END_OF_INPUT;
644
645 p = vim_strchr(p_str, '\n');
646 if (p != NULL)
647 len = (int)(p - p_str) + 1;
648 else
649 len = (int)STRLEN(p_str);
650
651 if (len > IOSIZE - 2)
652 {
653 state->linebuf = qf_grow_linebuf(state, len);
654 if (state->linebuf == NULL)
655 return QF_NOMEM;
656 }
657 else
658 {
659 state->linebuf = IObuff;
660 state->linelen = len;
661 }
662 vim_strncpy(state->linebuf, p_str, state->linelen);
663
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200664 // Increment using len in order to discard the rest of the
665 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200666 p_str += len;
667 state->p_str = p_str;
668
669 return QF_OK;
670}
671
672/*
673 * Get the next string from state->p_Li.
674 */
675 static int
676qf_get_next_list_line(qfstate_T *state)
677{
678 listitem_T *p_li = state->p_li;
679 int len;
680
681 while (p_li != NULL
682 && (p_li->li_tv.v_type != VAR_STRING
683 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200684 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200685
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200686 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200687 {
688 state->p_li = NULL;
689 return QF_END_OF_INPUT;
690 }
691
692 len = (int)STRLEN(p_li->li_tv.vval.v_string);
693 if (len > IOSIZE - 2)
694 {
695 state->linebuf = qf_grow_linebuf(state, len);
696 if (state->linebuf == NULL)
697 return QF_NOMEM;
698 }
699 else
700 {
701 state->linebuf = IObuff;
702 state->linelen = len;
703 }
704
705 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
706
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200707 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200708 return QF_OK;
709}
710
711/*
712 * Get the next string from state->buf.
713 */
714 static int
715qf_get_next_buf_line(qfstate_T *state)
716{
717 char_u *p_buf = NULL;
718 int len;
719
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200720 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200721 if (state->buflnum > state->lnumlast)
722 return QF_END_OF_INPUT;
723
724 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
725 state->buflnum += 1;
726
727 len = (int)STRLEN(p_buf);
728 if (len > IOSIZE - 2)
729 {
730 state->linebuf = qf_grow_linebuf(state, len);
731 if (state->linebuf == NULL)
732 return QF_NOMEM;
733 }
734 else
735 {
736 state->linebuf = IObuff;
737 state->linelen = len;
738 }
739 vim_strncpy(state->linebuf, p_buf, state->linelen);
740
741 return QF_OK;
742}
743
744/*
745 * Get the next string from file state->fd.
746 */
747 static int
748qf_get_next_file_line(qfstate_T *state)
749{
750 int discard;
751 int growbuflen;
752
753 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
754 return QF_END_OF_INPUT;
755
756 discard = FALSE;
757 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200758 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200759 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200760 // The current line exceeds IObuff, continue reading using
761 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200762 if (state->growbuf == NULL)
763 {
764 state->growbufsiz = 2 * (IOSIZE - 1);
765 state->growbuf = alloc(state->growbufsiz);
766 if (state->growbuf == NULL)
767 return QF_NOMEM;
768 }
769
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200770 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200771 memcpy(state->growbuf, IObuff, IOSIZE - 1);
772 growbuflen = state->linelen;
773
774 for (;;)
775 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200776 char_u *p;
777
Bram Moolenaare0d37972016-07-15 22:36:01 +0200778 if (fgets((char *)state->growbuf + growbuflen,
779 state->growbufsiz - growbuflen, state->fd) == NULL)
780 break;
781 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
782 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200783 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200784 break;
785 if (state->growbufsiz == LINE_MAXLEN)
786 {
787 discard = TRUE;
788 break;
789 }
790
791 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
792 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200793 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200794 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200795 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200796 }
797
798 while (discard)
799 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200800 // The current line is longer than LINE_MAXLEN, continue
801 // reading but discard everything until EOL or EOF is
802 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200803 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
804 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200805 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200806 break;
807 }
808
809 state->linebuf = state->growbuf;
810 state->linelen = growbuflen;
811 }
812 else
813 state->linebuf = IObuff;
814
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100815#ifdef FEAT_MBYTE
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200816 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200817 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
818 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100819 char_u *line;
820
821 line = string_convert(&state->vc, state->linebuf, &state->linelen);
822 if (line != NULL)
823 {
824 if (state->linelen < IOSIZE)
825 {
826 STRCPY(state->linebuf, line);
827 vim_free(line);
828 }
829 else
830 {
831 vim_free(state->growbuf);
832 state->linebuf = state->growbuf = line;
833 state->growbufsiz = state->linelen < LINE_MAXLEN
834 ? state->linelen : LINE_MAXLEN;
835 }
836 }
837 }
838#endif
839
Bram Moolenaare0d37972016-07-15 22:36:01 +0200840 return QF_OK;
841}
842
843/*
844 * Get the next string from a file/buffer/list/string.
845 */
846 static int
847qf_get_nextline(qfstate_T *state)
848{
849 int status = QF_FAIL;
850
851 if (state->fd == NULL)
852 {
853 if (state->tv != NULL)
854 {
855 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200856 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200857 status = qf_get_next_str_line(state);
858 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200859 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200860 status = qf_get_next_list_line(state);
861 }
862 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200863 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200864 status = qf_get_next_buf_line(state);
865 }
866 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200867 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200868 status = qf_get_next_file_line(state);
869
870 if (status != QF_OK)
871 return status;
872
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200873 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200874 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200875 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200876 state->linebuf[state->linelen - 1] = NUL;
877#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200878 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
879 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200880#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200881 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200882
883#ifdef FEAT_MBYTE
884 remove_bom(state->linebuf);
885#endif
886
887 return QF_OK;
888}
889
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200890typedef struct {
891 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200892 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200893 char_u *errmsg;
894 int errmsglen;
895 long lnum;
896 int col;
897 char_u use_viscol;
898 char_u *pattern;
899 int enr;
900 int type;
901 int valid;
902} qffields_T;
903
904/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200905 * Parse the match for filename ('%f') pattern in regmatch.
906 * Return the matched value in "fields->namebuf".
907 */
908 static int
909qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
910{
911 int c;
912
913 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
914 return QF_FAIL;
915
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200916 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200917 c = *rmp->endp[midx];
918 *rmp->endp[midx] = NUL;
919 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
920 *rmp->endp[midx] = c;
921
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200922 // For separate filename patterns (%O, %P and %Q), the specified file
923 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200924 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
925 && mch_getperm(fields->namebuf) == -1)
926 return QF_FAIL;
927
928 return QF_OK;
929}
930
931/*
932 * Parse the match for error number ('%n') pattern in regmatch.
933 * Return the matched value in "fields->enr".
934 */
935 static int
936qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
937{
938 if (rmp->startp[midx] == NULL)
939 return QF_FAIL;
940 fields->enr = (int)atol((char *)rmp->startp[midx]);
941 return QF_OK;
942}
943
944/*
945 * Parse the match for line number (%l') pattern in regmatch.
946 * Return the matched value in "fields->lnum".
947 */
948 static int
949qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
950{
951 if (rmp->startp[midx] == NULL)
952 return QF_FAIL;
953 fields->lnum = atol((char *)rmp->startp[midx]);
954 return QF_OK;
955}
956
957/*
958 * Parse the match for column number ('%c') pattern in regmatch.
959 * Return the matched value in "fields->col".
960 */
961 static int
962qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
963{
964 if (rmp->startp[midx] == NULL)
965 return QF_FAIL;
966 fields->col = (int)atol((char *)rmp->startp[midx]);
967 return QF_OK;
968}
969
970/*
971 * Parse the match for error type ('%t') pattern in regmatch.
972 * Return the matched value in "fields->type".
973 */
974 static int
975qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
976{
977 if (rmp->startp[midx] == NULL)
978 return QF_FAIL;
979 fields->type = *rmp->startp[midx];
980 return QF_OK;
981}
982
983/*
984 * Parse the match for '%+' format pattern. The whole matching line is included
985 * in the error string. Return the matched line in "fields->errmsg".
986 */
987 static int
988qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
989{
990 char_u *p;
991
992 if (linelen >= fields->errmsglen)
993 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200994 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200995 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
996 return QF_NOMEM;
997 fields->errmsg = p;
998 fields->errmsglen = linelen + 1;
999 }
1000 vim_strncpy(fields->errmsg, linebuf, linelen);
1001 return QF_OK;
1002}
1003
1004/*
1005 * Parse the match for error message ('%m') pattern in regmatch.
1006 * Return the matched value in "fields->errmsg".
1007 */
1008 static int
1009qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1010{
1011 char_u *p;
1012 int len;
1013
1014 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1015 return QF_FAIL;
1016 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1017 if (len >= fields->errmsglen)
1018 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001019 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001020 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1021 return QF_NOMEM;
1022 fields->errmsg = p;
1023 fields->errmsglen = len + 1;
1024 }
1025 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1026 return QF_OK;
1027}
1028
1029/*
1030 * Parse the match for rest of a single-line file message ('%r') pattern.
1031 * Return the matched value in "tail".
1032 */
1033 static int
1034qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1035{
1036 if (rmp->startp[midx] == NULL)
1037 return QF_FAIL;
1038 *tail = rmp->startp[midx];
1039 return QF_OK;
1040}
1041
1042/*
1043 * Parse the match for the pointer line ('%p') pattern in regmatch.
1044 * Return the matched value in "fields->col".
1045 */
1046 static int
1047qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1048{
1049 char_u *match_ptr;
1050
1051 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1052 return QF_FAIL;
1053 fields->col = 0;
1054 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1055 ++match_ptr)
1056 {
1057 ++fields->col;
1058 if (*match_ptr == TAB)
1059 {
1060 fields->col += 7;
1061 fields->col -= fields->col % 8;
1062 }
1063 }
1064 ++fields->col;
1065 fields->use_viscol = TRUE;
1066 return QF_OK;
1067}
1068
1069/*
1070 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1071 * Return the matched value in "fields->col".
1072 */
1073 static int
1074qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1075{
1076 if (rmp->startp[midx] == NULL)
1077 return QF_FAIL;
1078 fields->col = (int)atol((char *)rmp->startp[midx]);
1079 fields->use_viscol = TRUE;
1080 return QF_OK;
1081}
1082
1083/*
1084 * Parse the match for the search text ('%s') pattern in regmatch.
1085 * Return the matched value in "fields->pattern".
1086 */
1087 static int
1088qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1089{
1090 int len;
1091
1092 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1093 return QF_FAIL;
1094 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1095 if (len > CMDBUFFSIZE - 5)
1096 len = CMDBUFFSIZE - 5;
1097 STRCPY(fields->pattern, "^\\V");
1098 STRNCAT(fields->pattern, rmp->startp[midx], len);
1099 fields->pattern[len + 3] = '\\';
1100 fields->pattern[len + 4] = '$';
1101 fields->pattern[len + 5] = NUL;
1102 return QF_OK;
1103}
1104
1105/*
1106 * Parse the match for the module ('%o') pattern in regmatch.
1107 * Return the matched value in "fields->module".
1108 */
1109 static int
1110qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1111{
1112 int len;
1113
1114 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1115 return QF_FAIL;
1116 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1117 if (len > CMDBUFFSIZE)
1118 len = CMDBUFFSIZE;
1119 STRNCAT(fields->module, rmp->startp[midx], len);
1120 return QF_OK;
1121}
1122
1123/*
1124 * 'errorformat' format pattern parser functions.
1125 * The '%f' and '%r' formats are parsed differently from other formats.
1126 * See qf_parse_match() for details.
1127 */
1128static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1129{
1130 NULL,
1131 qf_parse_fmt_n,
1132 qf_parse_fmt_l,
1133 qf_parse_fmt_c,
1134 qf_parse_fmt_t,
1135 qf_parse_fmt_m,
1136 NULL,
1137 qf_parse_fmt_p,
1138 qf_parse_fmt_v,
1139 qf_parse_fmt_s,
1140 qf_parse_fmt_o
1141};
1142
1143/*
1144 * Parse the error format pattern matches in "regmatch" and set the values in
1145 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1146 * match. Returns QF_OK if all the matches are successfully parsed. On
1147 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001148 */
1149 static int
1150qf_parse_match(
1151 char_u *linebuf,
1152 int linelen,
1153 efm_T *fmt_ptr,
1154 regmatch_T *regmatch,
1155 qffields_T *fields,
1156 int qf_multiline,
1157 int qf_multiscan,
1158 char_u **tail)
1159{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001160 int idx = fmt_ptr->prefix;
1161 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001162 int midx;
1163 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001164
1165 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1166 return QF_FAIL;
1167 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1168 fields->type = idx;
1169 else
1170 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001171
1172 // Extract error message data from matched line.
1173 // We check for an actual submatch, because "\[" and "\]" in
1174 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001175 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001176 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001177 status = QF_OK;
1178 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001179 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001180 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1181 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001182 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001183 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001184 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001185 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001186 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001187 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001188 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001189 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001190 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001191 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001192
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001193 if (status != QF_OK)
1194 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001195 }
1196
1197 return QF_OK;
1198}
1199
1200/*
1201 * Parse an error line in 'linebuf' using a single error format string in
1202 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1203 * Returns QF_OK if the efm format matches completely and the fields are
1204 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1205 */
1206 static int
1207qf_parse_get_fields(
1208 char_u *linebuf,
1209 int linelen,
1210 efm_T *fmt_ptr,
1211 qffields_T *fields,
1212 int qf_multiline,
1213 int qf_multiscan,
1214 char_u **tail)
1215{
1216 regmatch_T regmatch;
1217 int status = QF_FAIL;
1218 int r;
1219
1220 if (qf_multiscan &&
1221 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1222 return QF_FAIL;
1223
1224 fields->namebuf[0] = NUL;
1225 fields->module[0] = NUL;
1226 fields->pattern[0] = NUL;
1227 if (!qf_multiscan)
1228 fields->errmsg[0] = NUL;
1229 fields->lnum = 0;
1230 fields->col = 0;
1231 fields->use_viscol = FALSE;
1232 fields->enr = -1;
1233 fields->type = 0;
1234 *tail = NULL;
1235
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001236 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001237 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001238 regmatch.regprog = fmt_ptr->prog;
1239 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1240 fmt_ptr->prog = regmatch.regprog;
1241 if (r)
1242 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1243 fields, qf_multiline, qf_multiscan, tail);
1244
1245 return status;
1246}
1247
1248/*
1249 * Parse directory error format prefixes (%D and %X).
1250 * Push and pop directories from the directory stack when scanning directory
1251 * names.
1252 */
1253 static int
1254qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1255{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001256 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001257 {
1258 if (*fields->namebuf == NUL)
1259 {
1260 EMSG(_("E379: Missing or empty directory name"));
1261 return QF_FAIL;
1262 }
1263 qfl->qf_directory =
1264 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1265 if (qfl->qf_directory == NULL)
1266 return QF_FAIL;
1267 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001268 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001269 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1270
1271 return QF_OK;
1272}
1273
1274/*
1275 * Parse global file name error format prefixes (%O, %P and %Q).
1276 */
1277 static int
1278qf_parse_file_pfx(
1279 int idx,
1280 qffields_T *fields,
1281 qf_list_T *qfl,
1282 char_u *tail)
1283{
1284 fields->valid = FALSE;
1285 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1286 {
1287 if (*fields->namebuf && idx == 'P')
1288 qfl->qf_currfile =
1289 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1290 else if (idx == 'Q')
1291 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1292 *fields->namebuf = NUL;
1293 if (tail && *tail)
1294 {
1295 STRMOVE(IObuff, skipwhite(tail));
1296 qfl->qf_multiscan = TRUE;
1297 return QF_MULTISCAN;
1298 }
1299 }
1300
1301 return QF_OK;
1302}
1303
1304/*
1305 * Parse a non-error line (a line which doesn't match any of the error
1306 * format in 'efm').
1307 */
1308 static int
1309qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1310{
1311 char_u *p;
1312
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001313 fields->namebuf[0] = NUL; // no match found, remove file name
1314 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001315 fields->valid = FALSE;
1316 if (linelen >= fields->errmsglen)
1317 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001318 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001319 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1320 return QF_NOMEM;
1321 fields->errmsg = p;
1322 fields->errmsglen = linelen + 1;
1323 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001324 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001325 vim_strncpy(fields->errmsg, linebuf, linelen);
1326
1327 return QF_OK;
1328}
1329
1330/*
1331 * Parse multi-line error format prefixes (%C and %Z)
1332 */
1333 static int
1334qf_parse_multiline_pfx(
1335 qf_info_T *qi,
1336 int qf_idx,
1337 int idx,
1338 qf_list_T *qfl,
1339 qffields_T *fields)
1340{
1341 char_u *ptr;
1342 int len;
1343
1344 if (!qfl->qf_multiignore)
1345 {
1346 qfline_T *qfprev = qfl->qf_last;
1347
1348 if (qfprev == NULL)
1349 return QF_FAIL;
1350 if (*fields->errmsg && !qfl->qf_multiignore)
1351 {
1352 len = (int)STRLEN(qfprev->qf_text);
1353 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1354 == NULL)
1355 return QF_FAIL;
1356 STRCPY(ptr, qfprev->qf_text);
1357 vim_free(qfprev->qf_text);
1358 qfprev->qf_text = ptr;
1359 *(ptr += len) = '\n';
1360 STRCPY(++ptr, fields->errmsg);
1361 }
1362 if (qfprev->qf_nr == -1)
1363 qfprev->qf_nr = fields->enr;
1364 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001365 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001366 qfprev->qf_type = fields->type;
1367
1368 if (!qfprev->qf_lnum)
1369 qfprev->qf_lnum = fields->lnum;
1370 if (!qfprev->qf_col)
1371 qfprev->qf_col = fields->col;
1372 qfprev->qf_viscol = fields->use_viscol;
1373 if (!qfprev->qf_fnum)
1374 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1375 qfl->qf_directory,
1376 *fields->namebuf || qfl->qf_directory != NULL
1377 ? fields->namebuf
1378 : qfl->qf_currfile != NULL && fields->valid
1379 ? qfl->qf_currfile : 0);
1380 }
1381 if (idx == 'Z')
1382 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1383 line_breakcheck();
1384
1385 return QF_IGNORE_LINE;
1386}
1387
1388/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001389 * Parse a line and get the quickfix fields.
1390 * Return the QF_ status.
1391 */
1392 static int
1393qf_parse_line(
1394 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001395 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001396 char_u *linebuf,
1397 int linelen,
1398 efm_T *fmt_first,
1399 qffields_T *fields)
1400{
1401 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001402 int idx = 0;
1403 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001404 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001405 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001406
Bram Moolenaare333e792018-04-08 13:27:39 +02001407restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001408 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001409 if (fmt_start == NULL)
1410 fmt_ptr = fmt_first;
1411 else
1412 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001413 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001414 fmt_ptr = fmt_start;
1415 fmt_start = NULL;
1416 }
1417
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001418 // Try to match each part of 'errorformat' until we find a complete
1419 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001420 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001421 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1422 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001423 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001424 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1425 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1426 if (status == QF_NOMEM)
1427 return status;
1428 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001429 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001430 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001431 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001432
1433 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1434 {
1435 if (fmt_ptr != NULL)
1436 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001437 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001438 status = qf_parse_dir_pfx(idx, fields, qfl);
1439 if (status != QF_OK)
1440 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001441 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001442
1443 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1444 if (status != QF_OK)
1445 return status;
1446
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001447 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001448 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001449 }
1450 else if (fmt_ptr != NULL)
1451 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001452 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001453 if (fmt_ptr->conthere)
1454 fmt_start = fmt_ptr;
1455
1456 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1457 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001458 qfl->qf_multiline = TRUE; // start of a multi-line message
1459 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 }
1461 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001462 { // continuation of multi-line msg
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001463 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1464 if (status != QF_OK)
1465 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 }
1467 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001468 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001469 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1470 if (status == QF_MULTISCAN)
1471 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001472 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001473 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001474 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001475 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001476 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001477 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001478 return QF_IGNORE_LINE;
1479 }
1480 }
1481
1482 return QF_OK;
1483}
1484
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001485/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001486 * Returns TRUE if the specified quickfix/location stack is empty
1487 */
1488 static int
1489qf_stack_empty(qf_info_T *qi)
1490{
1491 return qi == NULL || qi->qf_listcount <= 0;
1492}
1493
1494/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001495 * Returns TRUE if the specified quickfix/location list is empty.
1496 */
1497 static int
1498qf_list_empty(qf_info_T *qi, int qf_idx)
1499{
1500 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1501 return TRUE;
1502 return qi->qf_lists[qf_idx].qf_count <= 0;
1503}
1504
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001505/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001506 * Allocate the fields used for parsing lines and populating a quickfix list.
1507 */
1508 static int
1509qf_alloc_fields(qffields_T *pfields)
1510{
1511 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1512 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1513 pfields->errmsglen = CMDBUFFSIZE + 1;
1514 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1515 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1516 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1517 || pfields->pattern == NULL || pfields->module == NULL)
1518 return FAIL;
1519
1520 return OK;
1521}
1522
1523/*
1524 * Free the fields used for parsing lines and populating a quickfix list.
1525 */
1526 static void
1527qf_free_fields(qffields_T *pfields)
1528{
1529 vim_free(pfields->namebuf);
1530 vim_free(pfields->module);
1531 vim_free(pfields->errmsg);
1532 vim_free(pfields->pattern);
1533}
1534
1535/*
1536 * Setup the state information used for parsing lines and populating a
1537 * quickfix list.
1538 */
1539 static int
1540qf_setup_state(
1541 qfstate_T *pstate,
1542 char_u *enc,
1543 char_u *efile,
1544 typval_T *tv,
1545 buf_T *buf,
1546 linenr_T lnumfirst,
1547 linenr_T lnumlast)
1548{
1549#ifdef FEAT_MBYTE
1550 pstate->vc.vc_type = CONV_NONE;
1551 if (enc != NULL && *enc != NUL)
1552 convert_setup(&pstate->vc, enc, p_enc);
1553#endif
1554
1555 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1556 {
1557 EMSG2(_(e_openerrf), efile);
1558 return FAIL;
1559 }
1560
1561 if (tv != NULL)
1562 {
1563 if (tv->v_type == VAR_STRING)
1564 pstate->p_str = tv->vval.v_string;
1565 else if (tv->v_type == VAR_LIST)
1566 pstate->p_li = tv->vval.v_list->lv_first;
1567 pstate->tv = tv;
1568 }
1569 pstate->buf = buf;
1570 pstate->buflnum = lnumfirst;
1571 pstate->lnumlast = lnumlast;
1572
1573 return OK;
1574}
1575
1576/*
1577 * Cleanup the state information used for parsing lines and populating a
1578 * quickfix list.
1579 */
1580 static void
1581qf_cleanup_state(qfstate_T *pstate)
1582{
1583 if (pstate->fd != NULL)
1584 fclose(pstate->fd);
1585
1586 vim_free(pstate->growbuf);
1587#ifdef FEAT_MBYTE
1588 if (pstate->vc.vc_type != CONV_NONE)
1589 convert_setup(&pstate->vc, NULL, NULL);
1590#endif
1591}
1592
1593/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001594 * Read the errorfile "efile" into memory, line by line, building the error
1595 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001596 * Alternative: when "efile" is NULL read errors from buffer "buf".
1597 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001598 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001599 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1600 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001601 * Return -1 for error, number of errors for success.
1602 */
1603 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001604qf_init_ext(
1605 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001606 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001607 char_u *efile,
1608 buf_T *buf,
1609 typval_T *tv,
1610 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001611 int newlist, // TRUE: start a new error list
1612 linenr_T lnumfirst, // first line number to use
1613 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001614 char_u *qf_title,
1615 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001616{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001617 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001618 qfstate_T state;
1619 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001620 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001621 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001622 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001624 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001625 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001626 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001628 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001629 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001630
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001631 vim_memset(&state, 0, sizeof(state));
1632 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001633 if ((qf_alloc_fields(&fields) == FAIL) ||
1634 (qf_setup_state(&state, enc, efile, tv, buf,
1635 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 goto qf_init_end;
1637
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001638 if (newlist || qf_idx == qi->qf_listcount)
1639 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001640 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001641 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001642 qf_idx = qi->qf_curlist;
1643 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001644 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001645 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001646 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001647 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001648 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001649 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001652 qfl = &qi->qf_lists[qf_idx];
1653
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001654 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001655 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001656 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 else
1658 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001660 // If the errorformat didn't change between calls, then reuse the
1661 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001662 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1663 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001664 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001665 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001666 free_efm_list(&fmt_first);
1667
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001668 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001669 fmt_first = parse_efm_option(efm);
1670 if (fmt_first != NULL)
1671 last_efm = vim_strsave(efm);
1672 }
1673
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001674 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001677 // got_int is reset here, because it was probably set when killing the
1678 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 got_int = FALSE;
1680
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001681 // Read the lines in the error file one by one.
1682 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001683 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001685 // Get the next line from a file/buffer/list/string
Bram Moolenaare0d37972016-07-15 22:36:01 +02001686 status = qf_get_nextline(&state);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001687 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001688 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001689 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001690 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001692 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1693 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001694 if (status == QF_FAIL)
1695 goto error2;
1696 if (status == QF_NOMEM)
1697 goto qf_init_end;
1698 if (status == QF_IGNORE_LINE)
1699 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001701 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001702 qf_idx,
1703 qfl->qf_directory,
1704 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001705 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001706 : ((qfl->qf_currfile != NULL && fields.valid)
1707 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001708 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001709 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001710 fields.errmsg,
1711 fields.lnum,
1712 fields.col,
1713 fields.use_viscol,
1714 fields.pattern,
1715 fields.enr,
1716 fields.type,
1717 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 goto error2;
1719 line_breakcheck();
1720 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001721 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001723 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001725 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001726 qfl->qf_ptr = qfl->qf_start;
1727 qfl->qf_index = 1;
1728 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 }
1730 else
1731 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001732 qfl->qf_nonevalid = FALSE;
1733 if (qfl->qf_ptr == NULL)
1734 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001736 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001737 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001738 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
1740 EMSG(_(e_readerrf));
1741error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001742 if (!adding)
1743 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001744 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001745 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001746 qi->qf_listcount--;
1747 if (qi->qf_curlist > 0)
1748 --qi->qf_curlist;
1749 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001750qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001751 if (qf_idx == qi->qf_curlist)
1752 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001753 qf_cleanup_state(&state);
1754 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
1756 return retval;
1757}
1758
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001759/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001760 * Read the errorfile "efile" into memory, line by line, building the error
1761 * list. Set the error list's title to qf_title.
1762 * Return -1 for error, number of errors for success.
1763 */
1764 int
1765qf_init(win_T *wp,
1766 char_u *efile,
1767 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001768 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001769 char_u *qf_title,
1770 char_u *enc)
1771{
1772 qf_info_T *qi = &ql_info;
1773
1774 if (wp != NULL)
1775 {
1776 qi = ll_get_or_alloc_list(wp);
1777 if (qi == NULL)
1778 return FAIL;
1779 }
1780
1781 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1782 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1783}
1784
1785/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001786 * Set the title of the specified quickfix list. Frees the previous title.
1787 * Prepends ':' to the title.
1788 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001789 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001790qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001791{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001792 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001793
Bram Moolenaarfb604092014-07-23 15:55:00 +02001794 if (title != NULL)
1795 {
1796 char_u *p = alloc((int)STRLEN(title) + 2);
1797
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001798 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001799 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001800 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001801 }
1802}
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001805 * The title of a quickfix/location list is set, by default, to the command
1806 * that created the quickfix list with the ":" prefix.
1807 * Create a quickfix list title string by prepending ":" to a user command.
1808 * Returns a pointer to a static buffer with the title.
1809 */
1810 static char_u *
1811qf_cmdtitle(char_u *cmd)
1812{
1813 static char_u qftitle_str[IOSIZE];
1814
1815 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1816 return qftitle_str;
1817}
1818
1819/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001820 * Prepare for adding a new quickfix list. If the current list is in the
1821 * middle of the stack, then all the following lists are freed and then
1822 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 */
1824 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001825qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826{
1827 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001828 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001830 // If the current entry is not the last entry, delete entries beyond
1831 // the current entry. This makes it possible to browse in a tree-like
1832 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001833 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001834 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001836 // When the stack is full, remove to oldest entry
1837 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001838 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001840 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001842 qi->qf_lists[i - 1] = qi->qf_lists[i];
1843 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 }
1845 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001846 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001847 qfl = &qi->qf_lists[qi->qf_curlist];
1848 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1849 qf_store_title(qfl, qf_title);
1850 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851}
1852
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001853/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001854 * Queue location list stack delete request.
1855 */
1856 static void
1857locstack_queue_delreq(qf_info_T *qi)
1858{
1859 qf_delq_T *q;
1860
1861 q = (qf_delq_T *)alloc((unsigned)sizeof(qf_delq_T));
1862 if (q != NULL)
1863 {
1864 q->qi = qi;
1865 q->next = qf_delq_head;
1866 qf_delq_head = q;
1867 }
1868}
1869
1870/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001871 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001872 */
1873 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001874ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001875{
1876 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001877 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001878
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001879 qi = *pqi;
1880 if (qi == NULL)
1881 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001882 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001883
1884 qi->qf_refcount--;
1885 if (qi->qf_refcount < 1)
1886 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001887 // No references to this location list.
1888 // If the location list is still in use, then queue the delete request
1889 // to be processed later.
1890 if (quickfix_busy > 0)
1891 locstack_queue_delreq(qi);
1892 else
1893 {
1894 for (i = 0; i < qi->qf_listcount; ++i)
1895 qf_free(&qi->qf_lists[i]);
1896 vim_free(qi);
1897 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001898 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001899}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001900
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001901/*
1902 * Free all the quickfix/location lists in the stack.
1903 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001904 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001905qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001906{
1907 int i;
1908 qf_info_T *qi = &ql_info;
1909
1910 if (wp != NULL)
1911 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001912 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001913 ll_free_all(&wp->w_llist);
1914 ll_free_all(&wp->w_llist_ref);
1915 }
1916 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001917 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001918 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001919 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001920}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001923 * Delay freeing of location list stacks when the quickfix code is running.
1924 * Used to avoid problems with autocmds freeing location list stacks when the
1925 * quickfix code is still referencing the stack.
1926 * Must always call decr_quickfix_busy() exactly once after this.
1927 */
1928 static void
1929incr_quickfix_busy(void)
1930{
1931 quickfix_busy++;
1932}
1933
1934/*
1935 * Safe to free location list stacks. Process any delayed delete requests.
1936 */
1937 static void
1938decr_quickfix_busy(void)
1939{
1940 if (--quickfix_busy == 0)
1941 {
1942 // No longer referencing the location lists. Process all the pending
1943 // delete requests.
1944 while (qf_delq_head != NULL)
1945 {
1946 qf_delq_T *q = qf_delq_head;
1947
1948 qf_delq_head = q->next;
1949 ll_free_all(&q->qi);
1950 vim_free(q);
1951 }
1952 }
1953#ifdef ABORT_ON_INTERNAL_ERROR
1954 if (quickfix_busy < 0)
1955 {
1956 EMSG("quickfix_busy has become negative");
1957 abort();
1958 }
1959#endif
1960}
1961
1962#if defined(EXITFREE) || defined(PROTO)
1963 void
1964check_quickfix_busy(void)
1965{
1966 if (quickfix_busy != 0)
1967 {
1968 EMSGN("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
1969# ifdef ABORT_ON_INTERNAL_ERROR
1970 abort();
1971# endif
1972 }
1973}
1974#endif
1975
1976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 * Add an entry to the end of the list of errors.
1978 * Returns OK or FAIL.
1979 */
1980 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001981qf_add_entry(
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001982 qf_info_T *qi, // quickfix list
1983 int qf_idx, // list index
1984 char_u *dir, // optional directory name
1985 char_u *fname, // file name or NULL
1986 char_u *module, // module name or NULL
1987 int bufnum, // buffer number or zero
1988 char_u *mesg, // message
1989 long lnum, // line number
1990 int col, // column
1991 int vis_col, // using visual column
1992 char_u *pattern, // search pattern
1993 int nr, // error number
1994 int type, // type character
1995 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001997 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001998 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001999 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002001 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002003 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002004 {
2005 buf_T *buf = buflist_findnr(bufnum);
2006
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002007 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002008 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002009 buf->b_has_qf_entry |=
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002010 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002011 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002012 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002013 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2015 {
2016 vim_free(qfp);
2017 return FAIL;
2018 }
2019 qfp->qf_lnum = lnum;
2020 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002021 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002022 if (pattern == NULL || *pattern == NUL)
2023 qfp->qf_pattern = NULL;
2024 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2025 {
2026 vim_free(qfp->qf_text);
2027 vim_free(qfp);
2028 return FAIL;
2029 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002030 if (module == NULL || *module == NUL)
2031 qfp->qf_module = NULL;
2032 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2033 {
2034 vim_free(qfp->qf_text);
2035 vim_free(qfp->qf_pattern);
2036 vim_free(qfp);
2037 return FAIL;
2038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002040 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 type = 0;
2042 qfp->qf_type = type;
2043 qfp->qf_valid = valid;
2044
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002045 lastp = &qfl->qf_last;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002046 if (qf_list_empty(qi, qf_idx)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002048 qfl->qf_start = qfp;
2049 qfl->qf_ptr = qfp;
2050 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002051 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
2053 else
2054 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002055 qfp->qf_prev = *lastp;
2056 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002058 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002060 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002061 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002062 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002064 qfl->qf_index = qfl->qf_count;
2065 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
2067
2068 return OK;
2069}
2070
2071/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002072 * Allocate a new location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002073 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002074 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002075ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002076{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002077 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002078
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002079 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002080 if (qi != NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002081 qi->qf_refcount++;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002082 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002083}
2084
2085/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002086 * Return the location list stack for window 'wp'.
2087 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002088 */
2089 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002090ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002091{
2092 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002093 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002094 return wp->w_llist_ref;
2095
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002096 // For a non-location list window, w_llist_ref should not point to a
2097 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002098 ll_free_all(&wp->w_llist_ref);
2099
2100 if (wp->w_llist == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002101 wp->w_llist = ll_new_list(); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002102 return wp->w_llist;
2103}
2104
2105/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002106 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2107 */
2108 static int
2109copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2110{
2111 int i;
2112 qfline_T *from_qfp;
2113 qfline_T *prevp;
2114
2115 // copy all the location entries in this list
2116 for (i = 0, from_qfp = from_qfl->qf_start;
2117 i < from_qfl->qf_count && from_qfp != NULL;
2118 ++i, from_qfp = from_qfp->qf_next)
2119 {
2120 if (qf_add_entry(to_qi,
2121 to_qi->qf_curlist,
2122 NULL,
2123 NULL,
2124 from_qfp->qf_module,
2125 0,
2126 from_qfp->qf_text,
2127 from_qfp->qf_lnum,
2128 from_qfp->qf_col,
2129 from_qfp->qf_viscol,
2130 from_qfp->qf_pattern,
2131 from_qfp->qf_nr,
2132 0,
2133 from_qfp->qf_valid) == FAIL)
2134 return FAIL;
2135
2136 // qf_add_entry() will not set the qf_num field, as the
2137 // directory and file names are not supplied. So the qf_fnum
2138 // field is copied here.
2139 prevp = to_qfl->qf_last;
2140 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2141 prevp->qf_type = from_qfp->qf_type; // error type
2142 if (from_qfl->qf_ptr == from_qfp)
2143 to_qfl->qf_ptr = prevp; // current location
2144 }
2145
2146 return OK;
2147}
2148
2149/*
2150 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2151 */
2152 static int
2153copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2154{
2155 // Some of the fields are populated by qf_add_entry()
2156 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2157 to_qfl->qf_count = 0;
2158 to_qfl->qf_index = 0;
2159 to_qfl->qf_start = NULL;
2160 to_qfl->qf_last = NULL;
2161 to_qfl->qf_ptr = NULL;
2162 if (from_qfl->qf_title != NULL)
2163 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2164 else
2165 to_qfl->qf_title = NULL;
2166 if (from_qfl->qf_ctx != NULL)
2167 {
2168 to_qfl->qf_ctx = alloc_tv();
2169 if (to_qfl->qf_ctx != NULL)
2170 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2171 }
2172 else
2173 to_qfl->qf_ctx = NULL;
2174
2175 if (from_qfl->qf_count)
2176 if (copy_loclist_entries(from_qfl, to_qfl, to_qi) == FAIL)
2177 return FAIL;
2178
2179 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2180
2181 // Assign a new ID for the location list
2182 to_qfl->qf_id = ++last_qf_id;
2183 to_qfl->qf_changedtick = 0L;
2184
2185 // When no valid entries are present in the list, qf_ptr points to
2186 // the first item in the list
2187 if (to_qfl->qf_nonevalid)
2188 {
2189 to_qfl->qf_ptr = to_qfl->qf_start;
2190 to_qfl->qf_index = 1;
2191 }
2192
2193 return OK;
2194}
2195
2196/*
2197 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002198 */
2199 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002200copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002201{
2202 qf_info_T *qi;
2203 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002204
Bram Moolenaar09037502018-09-25 22:08:14 +02002205 // When copying from a location list window, copy the referenced
2206 // location list. For other windows, copy the location list for
2207 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002208 if (IS_LL_WINDOW(from))
2209 qi = from->w_llist_ref;
2210 else
2211 qi = from->w_llist;
2212
Bram Moolenaar09037502018-09-25 22:08:14 +02002213 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002214 return;
2215
Bram Moolenaar09037502018-09-25 22:08:14 +02002216 // allocate a new location list
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002217 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002218 return;
2219
2220 to->w_llist->qf_listcount = qi->qf_listcount;
2221
Bram Moolenaar09037502018-09-25 22:08:14 +02002222 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002223 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002224 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002225 to->w_llist->qf_curlist = idx;
2226
Bram Moolenaar09037502018-09-25 22:08:14 +02002227 if (copy_loclist(&qi->qf_lists[idx],
2228 &to->w_llist->qf_lists[idx], to->w_llist) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002229 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002230 qf_free_all(to);
2231 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002232 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002233 }
2234
Bram Moolenaar09037502018-09-25 22:08:14 +02002235 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002236}
2237
2238/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002239 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002240 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 */
2242 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002243qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002245 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar82404332016-07-10 17:00:38 +02002246 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002247 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002248 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002249
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002250 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
Bram Moolenaare60acc12011-05-10 16:41:25 +02002253#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002254 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002255#endif
2256#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002257 if (directory != NULL)
2258 slash_adjust(directory);
2259 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002260#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002261 if (directory != NULL && !vim_isAbsName(fname)
2262 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2263 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002264 // Here we check if the file really exists.
2265 // This should normally be true, but if make works without
2266 // "leaving directory"-messages we might have missed a
2267 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002268 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002271 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002272 if (directory)
2273 ptr = concat_fnames(directory, fname, TRUE);
2274 else
2275 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002277 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002278 bufname = ptr;
2279 }
2280 else
2281 bufname = fname;
2282
2283 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002284 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002285 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002286 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002287 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002289 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002290 {
2291 vim_free(qf_last_bufname);
2292 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2293 if (bufname == ptr)
2294 qf_last_bufname = bufname;
2295 else
2296 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002297 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002298 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002299 if (buf == NULL)
2300 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002301
Bram Moolenaarc1542742016-07-20 21:44:37 +02002302 buf->b_has_qf_entry =
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002303 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002304 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305}
2306
2307/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002308 * Push dirbuf onto the directory stack and return pointer to actual dir or
2309 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 */
2311 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002312qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313{
2314 struct dir_stack_T *ds_new;
2315 struct dir_stack_T *ds_ptr;
2316
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002317 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2319 if (ds_new == NULL)
2320 return NULL;
2321
2322 ds_new->next = *stackptr;
2323 *stackptr = ds_new;
2324
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002325 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 if (vim_isAbsName(dirbuf)
2327 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002328 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 (*stackptr)->dirname = vim_strsave(dirbuf);
2330 else
2331 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002332 // Okay we don't have an absolute path.
2333 // dirbuf must be a subdir of one of the directories on the stack.
2334 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 ds_new = (*stackptr)->next;
2336 (*stackptr)->dirname = NULL;
2337 while (ds_new)
2338 {
2339 vim_free((*stackptr)->dirname);
2340 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2341 TRUE);
2342 if (mch_isdir((*stackptr)->dirname) == TRUE)
2343 break;
2344
2345 ds_new = ds_new->next;
2346 }
2347
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002348 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 while ((*stackptr)->next != ds_new)
2350 {
2351 ds_ptr = (*stackptr)->next;
2352 (*stackptr)->next = (*stackptr)->next->next;
2353 vim_free(ds_ptr->dirname);
2354 vim_free(ds_ptr);
2355 }
2356
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002357 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 if (ds_new == NULL)
2359 {
2360 vim_free((*stackptr)->dirname);
2361 (*stackptr)->dirname = vim_strsave(dirbuf);
2362 }
2363 }
2364
2365 if ((*stackptr)->dirname != NULL)
2366 return (*stackptr)->dirname;
2367 else
2368 {
2369 ds_ptr = *stackptr;
2370 *stackptr = (*stackptr)->next;
2371 vim_free(ds_ptr);
2372 return NULL;
2373 }
2374}
2375
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376/*
2377 * pop dirbuf from the directory stack and return previous directory or NULL if
2378 * stack is empty
2379 */
2380 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002381qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382{
2383 struct dir_stack_T *ds_ptr;
2384
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002385 // TODO: Should we check if dirbuf is the directory on top of the stack?
2386 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002388 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (*stackptr != NULL)
2390 {
2391 ds_ptr = *stackptr;
2392 *stackptr = (*stackptr)->next;
2393 vim_free(ds_ptr->dirname);
2394 vim_free(ds_ptr);
2395 }
2396
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002397 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 return *stackptr ? (*stackptr)->dirname : NULL;
2399}
2400
2401/*
2402 * clean up directory stack
2403 */
2404 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002405qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
2407 struct dir_stack_T *ds_ptr;
2408
2409 while ((ds_ptr = *stackptr) != NULL)
2410 {
2411 *stackptr = (*stackptr)->next;
2412 vim_free(ds_ptr->dirname);
2413 vim_free(ds_ptr);
2414 }
2415}
2416
2417/*
2418 * Check in which directory of the directory stack the given file can be
2419 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002420 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 * Cleans up intermediate directory entries.
2422 *
2423 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002424 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 * ./
2426 * ./aa
2427 * ./aa/bb
2428 * ./bb
2429 * ./bb/x.c
2430 * and make says:
2431 * making all in aa
2432 * making all in bb
2433 * x.c:9: Error
2434 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2435 * qf_guess_filepath will return NULL.
2436 */
2437 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002438qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
2440 struct dir_stack_T *ds_ptr;
2441 struct dir_stack_T *ds_tmp;
2442 char_u *fullname;
2443
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002444 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002445 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 return NULL;
2447
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002448 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 fullname = NULL;
2450 while (ds_ptr)
2451 {
2452 vim_free(fullname);
2453 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2454
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002455 // If concat_fnames failed, just go on. The worst thing that can happen
2456 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2458 break;
2459
2460 ds_ptr = ds_ptr->next;
2461 }
2462
2463 vim_free(fullname);
2464
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002465 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002466 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002468 ds_tmp = qfl->qf_dir_stack->next;
2469 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 vim_free(ds_tmp->dirname);
2471 vim_free(ds_tmp);
2472 }
2473
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002474 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475}
2476
2477/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002478 * Returns TRUE if a quickfix/location list with the given identifier exists.
2479 */
2480 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002481qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002482{
2483 qf_info_T *qi = &ql_info;
2484 int i;
2485
2486 if (wp != NULL)
2487 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002488 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002489 if (qi == NULL)
2490 return FALSE;
2491 }
2492
2493 for (i = 0; i < qi->qf_listcount; ++i)
2494 if (qi->qf_lists[i].qf_id == qf_id)
2495 return TRUE;
2496
2497 return FALSE;
2498}
2499
2500/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002501 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002502 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002503 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002504 * Similar to location list.
2505 */
2506 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002507is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002508{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002509 qfline_T *qfp;
2510 int i;
2511
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002512 // Search for the entry in the current list
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002513 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2514 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002515 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002516 break;
2517
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002518 if (i == qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002519 return FALSE;
2520
2521 return TRUE;
2522}
2523
2524/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002525 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002526 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002527 */
2528 static qfline_T *
2529get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002530 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002531 qfline_T *qf_ptr,
2532 int *qf_index,
2533 int dir)
2534{
2535 int idx;
2536 int old_qf_fnum;
2537
2538 idx = *qf_index;
2539 old_qf_fnum = qf_ptr->qf_fnum;
2540
2541 do
2542 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002543 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002544 return NULL;
2545 ++idx;
2546 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002547 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002548 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2549
2550 *qf_index = idx;
2551 return qf_ptr;
2552}
2553
2554/*
2555 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002556 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002557 */
2558 static qfline_T *
2559get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002560 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002561 qfline_T *qf_ptr,
2562 int *qf_index,
2563 int dir)
2564{
2565 int idx;
2566 int old_qf_fnum;
2567
2568 idx = *qf_index;
2569 old_qf_fnum = qf_ptr->qf_fnum;
2570
2571 do
2572 {
2573 if (idx == 1 || qf_ptr->qf_prev == NULL)
2574 return NULL;
2575 --idx;
2576 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002577 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002578 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2579
2580 *qf_index = idx;
2581 return qf_ptr;
2582}
2583
2584/*
2585 * Get the n'th (errornr) previous/next valid entry from the current entry in
2586 * the quickfix list.
2587 * dir == FORWARD or FORWARD_FILE: next valid entry
2588 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2589 */
2590 static qfline_T *
2591get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002592 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002593 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002594 int dir,
2595 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002596{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002597 qfline_T *qf_ptr = qfl->qf_ptr;
2598 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002599 qfline_T *prev_qf_ptr;
2600 int prev_index;
2601 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2602 char_u *err = e_no_more_items;
2603
2604 while (errornr--)
2605 {
2606 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002607 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002608
2609 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002610 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002611 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002612 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002613 if (qf_ptr == NULL)
2614 {
2615 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002616 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002617 if (err != NULL)
2618 {
2619 EMSG(_(err));
2620 return NULL;
2621 }
2622 break;
2623 }
2624
2625 err = NULL;
2626 }
2627
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002628 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002629 return qf_ptr;
2630}
2631
2632/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002633 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2634 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002635 */
2636 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002637get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002638{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002639 qfline_T *qf_ptr = qfl->qf_ptr;
2640 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002641
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002642 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002643 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2644 {
2645 --qf_idx;
2646 qf_ptr = qf_ptr->qf_prev;
2647 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002648 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002649 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2650 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002651 {
2652 ++qf_idx;
2653 qf_ptr = qf_ptr->qf_next;
2654 }
2655
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002656 *new_qfidx = qf_idx;
2657 return qf_ptr;
2658}
2659
2660/*
2661 * Get a entry specied by 'errornr' and 'dir' from the current
2662 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2663 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2664 * Returns a pointer to the entry and the index of the new entry is stored in
2665 * 'new_qfidx'.
2666 */
2667 static qfline_T *
2668qf_get_entry(
2669 qf_list_T *qfl,
2670 int errornr,
2671 int dir,
2672 int *new_qfidx)
2673{
2674 qfline_T *qf_ptr = qfl->qf_ptr;
2675 int qfidx = qfl->qf_index;
2676
2677 if (dir != 0) // next/prev valid entry
2678 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2679 else if (errornr != 0) // go to specified number
2680 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2681
2682 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002683 return qf_ptr;
2684}
2685
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002686/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002687 * Find a window displaying a Vim help file.
2688 */
2689 static win_T *
2690qf_find_help_win(void)
2691{
2692 win_T *wp;
2693
2694 FOR_ALL_WINDOWS(wp)
2695 if (bt_help(wp->w_buffer))
2696 return wp;
2697
2698 return NULL;
2699}
2700
2701/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002702 * Find a help window or open one.
2703 */
2704 static int
2705jump_to_help_window(qf_info_T *qi, int *opened_window)
2706{
2707 win_T *wp;
2708 int flags;
2709
2710 if (cmdmod.tab != 0)
2711 wp = NULL;
2712 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002713 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002714 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2715 win_enter(wp, TRUE);
2716 else
2717 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002718 // Split off help window; put it at far top if no position
2719 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002720 flags = WSP_HELP;
2721 if (cmdmod.split == 0 && curwin->w_width != Columns
2722 && curwin->w_width < 80)
2723 flags |= WSP_TOP;
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002724 if (IS_LL_STACK(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002725 flags |= WSP_NEWLOC; // don't copy the location list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002726
2727 if (win_split(0, flags) == FAIL)
2728 return FAIL;
2729
2730 *opened_window = TRUE;
2731
2732 if (curwin->w_height < p_hh)
2733 win_setheight((int)p_hh);
2734
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002735 if (IS_LL_STACK(qi)) // not a quickfix list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002736 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002737 // The new window should use the supplied location list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002738 curwin->w_llist = qi;
2739 qi->qf_refcount++;
2740 }
2741 }
2742
2743 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002744 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002745
2746 return OK;
2747}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002748
2749/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002750 * Find a non-quickfix window using the given location list.
2751 * Returns NULL if a matching window is not found.
2752 */
2753 static win_T *
2754qf_find_win_with_loclist(qf_info_T *ll)
2755{
2756 win_T *wp;
2757
2758 FOR_ALL_WINDOWS(wp)
2759 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2760 return wp;
2761
2762 return NULL;
2763}
2764
2765/*
2766 * Find a window containing a normal buffer
2767 */
2768 static win_T *
2769qf_find_win_with_normal_buf(void)
2770{
2771 win_T *wp;
2772
2773 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002774 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002775 return wp;
2776
2777 return NULL;
2778}
2779
2780/*
2781 * Go to a window in any tabpage containing the specified file. Returns TRUE
2782 * if successfully jumped to the window. Otherwise returns FALSE.
2783 */
2784 static int
2785qf_goto_tabwin_with_file(int fnum)
2786{
2787 tabpage_T *tp;
2788 win_T *wp;
2789
2790 FOR_ALL_TAB_WINDOWS(tp, wp)
2791 if (wp->w_buffer->b_fnum == fnum)
2792 {
2793 goto_tabpage_win(tp, wp);
2794 return TRUE;
2795 }
2796
2797 return FALSE;
2798}
2799
2800/*
2801 * Create a new window to show a file above the quickfix window. Called when
2802 * only the quickfix window is present.
2803 */
2804 static int
2805qf_open_new_file_win(qf_info_T *ll_ref)
2806{
2807 int flags;
2808
2809 flags = WSP_ABOVE;
2810 if (ll_ref != NULL)
2811 flags |= WSP_NEWLOC;
2812 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002813 return FAIL; // not enough room for window
2814 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002815 swb_flags = 0;
2816 RESET_BINDING(curwin);
2817 if (ll_ref != NULL)
2818 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002819 // The new window should use the location list from the
2820 // location list window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002821 curwin->w_llist = ll_ref;
2822 ll_ref->qf_refcount++;
2823 }
2824 return OK;
2825}
2826
2827/*
2828 * Go to a window that shows the right buffer. If the window is not found, go
2829 * to the window just above the location list window. This is used for opening
2830 * a file from a location window and not from a quickfix window. If some usable
2831 * window is previously found, then it is supplied in 'use_win'.
2832 */
2833 static void
2834qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2835{
2836 win_T *win = use_win;
2837
2838 if (win == NULL)
2839 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002840 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002841 FOR_ALL_WINDOWS(win)
2842 if (win->w_buffer->b_fnum == qf_fnum)
2843 break;
2844 if (win == NULL)
2845 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002846 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002847 win = curwin;
2848 do
2849 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002850 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002851 break;
2852 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002853 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002854 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002855 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002856 } while (win != curwin);
2857 }
2858 }
2859 win_goto(win);
2860
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002861 // If the location list for the window is not set, then set it
2862 // to the location list from the location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002863 if (win->w_llist == NULL)
2864 {
2865 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002866 if (ll_ref != NULL)
2867 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002868 }
2869}
2870
2871/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002872 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2873 * not found, then go to the window just above the quickfix window. This is
2874 * used for opening a file from a quickfix window and not from a location
2875 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002876 */
2877 static void
2878qf_goto_win_with_qfl_file(int qf_fnum)
2879{
2880 win_T *win;
2881 win_T *altwin;
2882
2883 win = curwin;
2884 altwin = NULL;
2885 for (;;)
2886 {
2887 if (win->w_buffer->b_fnum == qf_fnum)
2888 break;
2889 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002890 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002891 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002892 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002893
2894 if (IS_QF_WINDOW(win))
2895 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002896 // Didn't find it, go to the window before the quickfix
2897 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002898 if (altwin != NULL)
2899 win = altwin;
2900 else if (curwin->w_prev != NULL)
2901 win = curwin->w_prev;
2902 else
2903 win = curwin->w_next;
2904 break;
2905 }
2906
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002907 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02002908 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002909 altwin = win;
2910 }
2911
2912 win_goto(win);
2913}
2914
2915/*
2916 * Find a suitable window for opening a file (qf_fnum) from the
2917 * quickfix/location list and jump to it. If the file is already opened in a
2918 * window, jump to it. Otherwise open a new window to display the file. This is
2919 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002920 */
2921 static int
2922qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2923{
2924 win_T *usable_win_ptr = NULL;
2925 int usable_win;
2926 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002927 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002928
2929 usable_win = 0;
2930
2931 ll_ref = curwin->w_llist_ref;
2932 if (ll_ref != NULL)
2933 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002934 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002935 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2936 if (usable_win_ptr != NULL)
2937 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002938 }
2939
2940 if (!usable_win)
2941 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002942 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002943 win = qf_find_win_with_normal_buf();
2944 if (win != NULL)
2945 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002946 }
2947
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002948 // If no usable window is found and 'switchbuf' contains "usetab"
2949 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002950 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002951 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002952
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002953 // If there is only one window and it is the quickfix window, create a
2954 // new one above the quickfix window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002955 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2956 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002957 if (qf_open_new_file_win(ll_ref) != OK)
2958 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002959 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002960 }
2961 else
2962 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002963 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002964 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002965 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002966 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002967 }
2968
2969 return OK;
2970}
2971
2972/*
2973 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002974 * Returns OK if successfully edited the file, FAIL on failing to open the
2975 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
2976 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002977 */
2978 static int
2979qf_jump_edit_buffer(
2980 qf_info_T *qi,
2981 qfline_T *qf_ptr,
2982 int forceit,
2983 win_T *oldwin,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002984 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002985{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002986 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002987 int retval = OK;
2988
2989 if (qf_ptr->qf_type == 1)
2990 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002991 // Open help file (do_ecmd() will set b_help flag, readfile() will
2992 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002993 if (!can_abandon(curbuf, forceit))
2994 {
2995 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002996 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002997 }
2998 else
2999 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3000 ECMD_HIDE + ECMD_SET_HELP,
3001 oldwin == curwin ? curwin : NULL);
3002 }
3003 else
3004 {
3005 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003006 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003007
3008 retval = buflist_getfile(qf_ptr->qf_fnum,
3009 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003010
Bram Moolenaar4d77c652018-08-18 19:59:54 +02003011 if (IS_LL_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003012 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003013 // Location list. Check whether the associated window is still
3014 // present and the list is still valid.
Bram Moolenaar3c097222017-12-21 20:54:49 +01003015 if (!win_valid_any_tab(oldwin))
3016 {
3017 EMSG(_("E924: Current window was closed"));
Bram Moolenaar3c097222017-12-21 20:54:49 +01003018 *opened_window = FALSE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003019 return NOTDONE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01003020 }
3021 else if (!qflist_valid(oldwin, save_qfid))
3022 {
3023 EMSG(_(e_loc_list_changed));
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003024 return NOTDONE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01003025 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003026 }
3027 else if (old_qf_curlist != qi->qf_curlist
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003028 || !is_qf_entry_present(qfl, qf_ptr))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003029 {
Bram Moolenaar4d77c652018-08-18 19:59:54 +02003030 if (IS_QF_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003031 EMSG(_("E925: Current quickfix was changed"));
3032 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01003033 EMSG(_(e_loc_list_changed));
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003034 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003035 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003036 }
3037
3038 return retval;
3039}
3040
3041/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003042 * Go to the error line in the current file using either line/column number or
3043 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003044 */
3045 static void
3046qf_jump_goto_line(
3047 linenr_T qf_lnum,
3048 int qf_col,
3049 char_u qf_viscol,
3050 char_u *qf_pattern)
3051{
3052 linenr_T i;
3053 char_u *line;
3054 colnr_T screen_col;
3055 colnr_T char_col;
3056
3057 if (qf_pattern == NULL)
3058 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003059 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003060 i = qf_lnum;
3061 if (i > 0)
3062 {
3063 if (i > curbuf->b_ml.ml_line_count)
3064 i = curbuf->b_ml.ml_line_count;
3065 curwin->w_cursor.lnum = i;
3066 }
3067 if (qf_col > 0)
3068 {
3069 curwin->w_cursor.col = qf_col - 1;
3070#ifdef FEAT_VIRTUALEDIT
3071 curwin->w_cursor.coladd = 0;
3072#endif
3073 if (qf_viscol == TRUE)
3074 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003075 // Check each character from the beginning of the error
3076 // line up to the error column. For each tab character
3077 // found, reduce the error column value by the length of
3078 // a tab character.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003079 line = ml_get_curline();
3080 screen_col = 0;
3081 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3082 {
3083 if (*line == NUL)
3084 break;
3085 if (*line++ == '\t')
3086 {
3087 curwin->w_cursor.col -= 7 - (screen_col % 8);
3088 screen_col += 8 - (screen_col % 8);
3089 }
3090 else
3091 ++screen_col;
3092 }
3093 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003094 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003095 check_cursor();
3096 }
3097 else
3098 beginline(BL_WHITE | BL_FIX);
3099 }
3100 else
3101 {
3102 pos_T save_cursor;
3103
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003104 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003105 save_cursor = curwin->w_cursor;
3106 curwin->w_cursor.lnum = 0;
3107 if (!do_search(NULL, '/', qf_pattern, (long)1,
3108 SEARCH_KEEP, NULL, NULL))
3109 curwin->w_cursor = save_cursor;
3110 }
3111}
3112
3113/*
3114 * Display quickfix list index and size message
3115 */
3116 static void
3117qf_jump_print_msg(
3118 qf_info_T *qi,
3119 int qf_index,
3120 qfline_T *qf_ptr,
3121 buf_T *old_curbuf,
3122 linenr_T old_lnum)
3123{
3124 linenr_T i;
3125 int len;
3126
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003127 // Update the screen before showing the message, unless the screen
3128 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003129 if (!msg_scrolled)
3130 update_topline_redraw();
3131 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3132 qi->qf_lists[qi->qf_curlist].qf_count,
3133 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3134 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003135 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003136 len = (int)STRLEN(IObuff);
3137 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3138
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003139 // Output the message. Overwrite to avoid scrolling when the 'O'
3140 // flag is present in 'shortmess'; But when not jumping, print the
3141 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003142 i = msg_scroll;
3143 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3144 msg_scroll = TRUE;
3145 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3146 msg_scroll = FALSE;
3147 msg_attr_keep(IObuff, 0, TRUE);
3148 msg_scroll = i;
3149}
3150
3151/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003152 * Find a usable window for opening a file from the quickfix/location list. If
3153 * a window is not found then open a new window.
3154 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3155 * able to jump/open a window. Returns NOTDONE if a file is not associated
3156 * with the entry.
3157 */
3158 static int
3159qf_jump_open_window(qf_info_T *qi, qfline_T *qf_ptr, int *opened_window)
3160{
3161 // For ":helpgrep" find a help window or open one.
3162 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
3163 if (jump_to_help_window(qi, opened_window) == FAIL)
3164 return FAIL;
3165
3166 // If currently in the quickfix window, find another window to show the
3167 // file in.
3168 if (bt_quickfix(curbuf) && !*opened_window)
3169 {
3170 // If there is no file specified, we don't know where to go.
3171 // But do advance, otherwise ":cn" gets stuck.
3172 if (qf_ptr->qf_fnum == 0)
3173 return NOTDONE;
3174
3175 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, opened_window) == FAIL)
3176 return FAIL;
3177 }
3178
3179 return OK;
3180}
3181
3182/*
3183 * Edit a selected file from the quickfix/location list and jump to a
3184 * particular line/column, adjust the folds and display a message about the
3185 * jump.
3186 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3187 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3188 * the file.
3189 */
3190 static int
3191qf_jump_to_buffer(
3192 qf_info_T *qi,
3193 int qf_index,
3194 qfline_T *qf_ptr,
3195 int forceit,
3196 win_T *oldwin,
3197 int *opened_window,
3198 int openfold,
3199 int print_message)
3200{
3201 buf_T *old_curbuf;
3202 linenr_T old_lnum;
3203 int retval = OK;
3204
3205 // If there is a file name, read the wanted file if needed, and check
3206 // autowrite etc.
3207 old_curbuf = curbuf;
3208 old_lnum = curwin->w_cursor.lnum;
3209
3210 if (qf_ptr->qf_fnum != 0)
3211 {
3212 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3213 opened_window);
3214 if (retval != OK)
3215 return retval;
3216 }
3217
3218 // When not switched to another buffer, still need to set pc mark
3219 if (curbuf == old_curbuf)
3220 setpcmark();
3221
3222 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3223 qf_ptr->qf_pattern);
3224
3225#ifdef FEAT_FOLDING
3226 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3227 foldOpenCursor();
3228#endif
3229 if (print_message)
3230 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3231
3232 return retval;
3233}
3234
3235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 * jump to a quickfix line
3237 * if dir == FORWARD go "errornr" valid entries forward
3238 * if dir == BACKWARD go "errornr" valid entries backward
3239 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3240 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3241 * else if "errornr" is zero, redisplay the same line
3242 * else go to entry "errornr"
3243 */
3244 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003245qf_jump(qf_info_T *qi,
3246 int dir,
3247 int errornr,
3248 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003250 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003251 qfline_T *qf_ptr;
3252 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003255 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003256 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003258 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003261 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003263 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003265 if (qi == NULL)
3266 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003267
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003268 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 {
3270 EMSG(_(e_quickfix));
3271 return;
3272 }
3273
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003274 qfl = &qi->qf_lists[qi->qf_curlist];
3275
3276 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003278 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003280
3281 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3282 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003284 qf_ptr = old_qf_ptr;
3285 qf_index = old_qf_index;
3286 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003289 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003290 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003291 // No need to print the error message if it's visible in the error
3292 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 print_message = FALSE;
3294
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003295 retval = qf_jump_open_window(qi, qf_ptr, &opened_window);
3296 if (retval == FAIL)
3297 goto failed;
3298 if (retval == NOTDONE)
3299 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003300
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003301 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3302 &opened_window, old_KeyTyped, print_message);
3303 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003305 // Quickfix/location list is freed by an autocmd
3306 qi = NULL;
3307 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003310 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003313 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003314 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003316 // Couldn't open file, so put index back where it was. This could
3317 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 qf_ptr = old_qf_ptr;
3320 qf_index = old_qf_index;
3321 }
3322 }
3323theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003324 if (qi != NULL)
3325 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003326 qfl->qf_ptr = qf_ptr;
3327 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 if (p_swb != old_swb && opened_window)
3330 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003331 // Restore old 'switchbuf' value, but not when an autocommand or
3332 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003336 swb_flags = old_swb_flags;
3337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 else
3339 free_string_option(old_swb);
3340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341}
3342
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003343// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003344static int qfFileAttr;
3345static int qfSepAttr;
3346static int qfLineAttr;
3347
3348/*
3349 * Display information about a single entry from the quickfix/location list.
3350 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003351 * 'cursel' will be set to TRUE for the currently selected entry in the
3352 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003353 */
3354 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003355qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003356{
3357 char_u *fname;
3358 buf_T *buf;
3359 int filter_entry;
3360
3361 fname = NULL;
3362 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3363 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3364 (char *)qfp->qf_module);
3365 else {
3366 if (qfp->qf_fnum != 0
3367 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3368 {
3369 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003370 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003371 fname = gettail(fname);
3372 }
3373 if (fname == NULL)
3374 sprintf((char *)IObuff, "%2d", qf_idx);
3375 else
3376 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3377 qf_idx, (char *)fname);
3378 }
3379
3380 // Support for filtering entries using :filter /pat/ clist
3381 // Match against the module name, file name, search pattern and
3382 // text of the entry.
3383 filter_entry = TRUE;
3384 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3385 filter_entry &= message_filtered(qfp->qf_module);
3386 if (filter_entry && fname != NULL)
3387 filter_entry &= message_filtered(fname);
3388 if (filter_entry && qfp->qf_pattern != NULL)
3389 filter_entry &= message_filtered(qfp->qf_pattern);
3390 if (filter_entry)
3391 filter_entry &= message_filtered(qfp->qf_text);
3392 if (filter_entry)
3393 return;
3394
3395 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003396 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003397
3398 if (qfp->qf_lnum != 0)
3399 msg_puts_attr((char_u *)":", qfSepAttr);
3400 if (qfp->qf_lnum == 0)
3401 IObuff[0] = NUL;
3402 else if (qfp->qf_col == 0)
3403 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3404 else
3405 sprintf((char *)IObuff, "%ld col %d",
3406 qfp->qf_lnum, qfp->qf_col);
3407 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3408 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3409 msg_puts_attr(IObuff, qfLineAttr);
3410 msg_puts_attr((char_u *)":", qfSepAttr);
3411 if (qfp->qf_pattern != NULL)
3412 {
3413 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3414 msg_puts(IObuff);
3415 msg_puts_attr((char_u *)":", qfSepAttr);
3416 }
3417 msg_puts((char_u *)" ");
3418
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003419 // Remove newlines and leading whitespace from the text. For an
3420 // unrecognized line keep the indent, the compiler may mark a word
3421 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003422 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3423 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3424 IObuff, IOSIZE);
3425 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003426 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003427}
3428
3429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003431 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
3433 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003434qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003436 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003437 qfline_T *qfp;
3438 int i;
3439 int idx1 = 1;
3440 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003441 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003442 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003443 int all = eap->forceit; // if not :cl!, only show
3444 // recognised errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003445 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
Bram Moolenaar39665952018-08-15 20:59:48 +02003447 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003448 {
3449 qi = GET_LOC_LIST(curwin);
3450 if (qi == NULL)
3451 {
3452 EMSG(_(e_loclist));
3453 return;
3454 }
3455 }
3456
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003457 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
3459 EMSG(_(e_quickfix));
3460 return;
3461 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003462 if (*arg == '+')
3463 {
3464 ++arg;
3465 plus = TRUE;
3466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3468 {
3469 EMSG(_(e_trailing));
3470 return;
3471 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003472 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003473 if (plus)
3474 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003475 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003476 idx2 = i + idx1;
3477 idx1 = i;
3478 }
3479 else
3480 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003481 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003482 if (idx1 < 0)
3483 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3484 if (idx2 < 0)
3485 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003488 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003489 shorten_fnames(FALSE);
3490
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003491 // Get the attributes for the different quickfix highlight items. Note
3492 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003493 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3494 if (qfFileAttr == 0)
3495 qfFileAttr = HL_ATTR(HLF_D);
3496 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3497 if (qfSepAttr == 0)
3498 qfSepAttr = HL_ATTR(HLF_D);
3499 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3500 if (qfLineAttr == 0)
3501 qfLineAttr = HL_ATTR(HLF_N);
3502
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003503 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003505 qfp = qfl->qf_start;
3506 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
3508 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3509 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003510 if (got_int)
3511 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003512
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003513 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003515
3516 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003517 if (qfp == NULL)
3518 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003519 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 ui_breakcheck();
3521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522}
3523
3524/*
3525 * Remove newlines and leading whitespace from an error message.
3526 * Put the result in "buf[bufsize]".
3527 */
3528 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003529qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530{
3531 int i;
3532 char_u *p = text;
3533
3534 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3535 {
3536 if (*p == '\n')
3537 {
3538 buf[i] = ' ';
3539 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003540 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 break;
3542 }
3543 else
3544 buf[i] = *p++;
3545 }
3546 buf[i] = NUL;
3547}
3548
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003549/*
3550 * Display information (list number, list size and the title) about a
3551 * quickfix/location list.
3552 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003553 static void
3554qf_msg(qf_info_T *qi, int which, char *lead)
3555{
3556 char *title = (char *)qi->qf_lists[which].qf_title;
3557 int count = qi->qf_lists[which].qf_count;
3558 char_u buf[IOSIZE];
3559
3560 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3561 lead,
3562 which + 1,
3563 qi->qf_listcount,
3564 count);
3565
3566 if (title != NULL)
3567 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003568 size_t len = STRLEN(buf);
3569
3570 if (len < 34)
3571 {
3572 vim_memset(buf + len, ' ', 34 - len);
3573 buf[34] = NUL;
3574 }
3575 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003576 }
3577 trunc_string(buf, buf, Columns - 1, IOSIZE);
3578 msg(buf);
3579}
3580
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581/*
3582 * ":colder [count]": Up in the quickfix stack.
3583 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003584 * ":lolder [count]": Up in the location list stack.
3585 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 */
3587 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003588qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003590 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 int count;
3592
Bram Moolenaar39665952018-08-15 20:59:48 +02003593 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003594 {
3595 qi = GET_LOC_LIST(curwin);
3596 if (qi == NULL)
3597 {
3598 EMSG(_(e_loclist));
3599 return;
3600 }
3601 }
3602
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 if (eap->addr_count != 0)
3604 count = eap->line2;
3605 else
3606 count = 1;
3607 while (count--)
3608 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003609 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003611 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
3613 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003614 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003616 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 }
3618 else
3619 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003620 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003623 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003625 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 }
3627 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003628 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003629 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630}
3631
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003632/*
3633 * Display the information about all the quickfix/location lists in the stack
3634 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003635 void
3636qf_history(exarg_T *eap)
3637{
3638 qf_info_T *qi = &ql_info;
3639 int i;
3640
Bram Moolenaar39665952018-08-15 20:59:48 +02003641 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003642 qi = GET_LOC_LIST(curwin);
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003643 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003644 MSG(_("No entries"));
3645 else
3646 for (i = 0; i < qi->qf_listcount; ++i)
3647 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3648}
3649
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003651 * Free all the entries in the error list "idx". Note that other information
3652 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 */
3654 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003655qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003657 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003658 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003659 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003661 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003663 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003664 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003665 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003666 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003667 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003668 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003669 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003670 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003671 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003672 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003673 // Somehow qf_count may have an incorrect value, set it to 1
3674 // to avoid crashing when it's wrong.
3675 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003676 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003677 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003678 qfl->qf_start = qfpnext;
3679 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003681
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003682 qfl->qf_index = 0;
3683 qfl->qf_start = NULL;
3684 qfl->qf_last = NULL;
3685 qfl->qf_ptr = NULL;
3686 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003687
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003688 qf_clean_dir_stack(&qfl->qf_dir_stack);
3689 qfl->qf_directory = NULL;
3690 qf_clean_dir_stack(&qfl->qf_file_stack);
3691 qfl->qf_currfile = NULL;
3692 qfl->qf_multiline = FALSE;
3693 qfl->qf_multiignore = FALSE;
3694 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695}
3696
3697/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003698 * Free error list "idx". Frees all the entries in the quickfix list,
3699 * associated context information and the title.
3700 */
3701 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003702qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003703{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003704 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003705
Bram Moolenaard23a8232018-02-10 18:45:26 +01003706 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003707 free_tv(qfl->qf_ctx);
3708 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003709 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003710 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003711}
3712
3713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 * qf_mark_adjust: adjust marks
3715 */
3716 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003717qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003718 win_T *wp,
3719 linenr_T line1,
3720 linenr_T line2,
3721 long amount,
3722 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003724 int i;
3725 qfline_T *qfp;
3726 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003727 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003728 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003729 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
Bram Moolenaarc1542742016-07-20 21:44:37 +02003731 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003732 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003733 if (wp != NULL)
3734 {
3735 if (wp->w_llist == NULL)
3736 return;
3737 qi = wp->w_llist;
3738 }
3739
3740 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003741 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003742 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003743 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3744 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 if (qfp->qf_fnum == curbuf->b_fnum)
3746 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003747 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3749 {
3750 if (amount == MAXLNUM)
3751 qfp->qf_cleared = TRUE;
3752 else
3753 qfp->qf_lnum += amount;
3754 }
3755 else if (amount_after && qfp->qf_lnum > line2)
3756 qfp->qf_lnum += amount_after;
3757 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003758
3759 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003760 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761}
3762
3763/*
3764 * Make a nice message out of the error character and the error number:
3765 * char number message
3766 * e or E 0 " error"
3767 * w or W 0 " warning"
3768 * i or I 0 " info"
3769 * 0 0 ""
3770 * other 0 " c"
3771 * e or E n " error n"
3772 * w or W n " warning n"
3773 * i or I n " info n"
3774 * 0 n " error n"
3775 * other n " c n"
3776 * 1 x "" :helpgrep
3777 */
3778 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003779qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780{
3781 static char_u buf[20];
3782 static char_u cc[3];
3783 char_u *p;
3784
3785 if (c == 'W' || c == 'w')
3786 p = (char_u *)" warning";
3787 else if (c == 'I' || c == 'i')
3788 p = (char_u *)" info";
3789 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3790 p = (char_u *)" error";
3791 else if (c == 0 || c == 1)
3792 p = (char_u *)"";
3793 else
3794 {
3795 cc[0] = ' ';
3796 cc[1] = c;
3797 cc[2] = NUL;
3798 p = cc;
3799 }
3800
3801 if (nr <= 0)
3802 return p;
3803
3804 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3805 return buf;
3806}
3807
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003809 * When "split" is FALSE: Open the entry/result under the cursor.
3810 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3811 */
3812 void
3813qf_view_result(int split)
3814{
3815 qf_info_T *qi = &ql_info;
3816
3817 if (!bt_quickfix(curbuf))
3818 return;
3819
3820 if (IS_LL_WINDOW(curwin))
3821 qi = GET_LOC_LIST(curwin);
3822
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003823 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003824 {
3825 EMSG(_(e_quickfix));
3826 return;
3827 }
3828
3829 if (split)
3830 {
3831 char_u cmd[32];
3832
3833 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3834 (long)curwin->w_cursor.lnum,
3835 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3836 if (do_cmdline_cmd(cmd) == OK)
3837 do_cmdline_cmd((char_u *) "clearjumps");
3838 return;
3839 }
3840
3841 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3842}
3843
3844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 * ":cwindow": open the quickfix window if we have errors to display,
3846 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003847 * ":lwindow": open the location list window if we have locations to display,
3848 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 */
3850 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003851ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003853 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003854 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 win_T *win;
3856
Bram Moolenaar39665952018-08-15 20:59:48 +02003857 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003858 {
3859 qi = GET_LOC_LIST(curwin);
3860 if (qi == NULL)
3861 return;
3862 }
3863
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003864 qfl = &qi->qf_lists[qi->qf_curlist];
3865
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003866 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003867 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003869 // If a quickfix window is open but we have no errors to display,
3870 // close the window. If a quickfix window is not open, then open
3871 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003872 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003873 || qfl->qf_nonevalid
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003874 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
3876 if (win != NULL)
3877 ex_cclose(eap);
3878 }
3879 else if (win == NULL)
3880 ex_copen(eap);
3881}
3882
3883/*
3884 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003885 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003888ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003890 win_T *win = NULL;
3891 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
Bram Moolenaar39665952018-08-15 20:59:48 +02003893 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003894 {
3895 qi = GET_LOC_LIST(curwin);
3896 if (qi == NULL)
3897 return;
3898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003900 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003901 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 if (win != NULL)
3903 win_close(win, FALSE);
3904}
3905
3906/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003907 * Set "w:quickfix_title" if "qi" has a title.
3908 */
3909 static void
3910qf_set_title_var(qf_list_T *qfl)
3911{
3912 if (qfl->qf_title != NULL)
3913 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3914}
3915
3916/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003917 * Goto a quickfix or location list window (if present).
3918 * Returns OK if the window is found, FAIL otherwise.
3919 */
3920 static int
3921qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3922{
3923 win_T *win;
3924
3925 win = qf_find_win(qi);
3926 if (win == NULL)
3927 return FAIL;
3928
3929 win_goto(win);
3930 if (resize)
3931 {
3932 if (vertsplit)
3933 {
3934 if (sz != win->w_width)
3935 win_setwidth(sz);
3936 }
3937 else if (sz != win->w_height)
3938 win_setheight(sz);
3939 }
3940
3941 return OK;
3942}
3943
3944/*
3945 * Open a new quickfix or location list window, load the quickfix buffer and
3946 * set the appropriate options for the window.
3947 * Returns FAIL if the window could not be opened.
3948 */
3949 static int
3950qf_open_new_cwindow(qf_info_T *qi, int height)
3951{
3952 buf_T *qf_buf;
3953 win_T *oldwin = curwin;
3954 tabpage_T *prevtab = curtab;
3955 int flags = 0;
3956 win_T *win;
3957
3958 qf_buf = qf_find_buf(qi);
3959
3960 // The current window becomes the previous window afterwards.
3961 win = curwin;
3962
3963 if (IS_QF_STACK(qi) && cmdmod.split == 0)
3964 // Create the new quickfix window at the very bottom, except when
3965 // :belowright or :aboveleft is used.
3966 win_goto(lastwin);
3967 // Default is to open the window below the current window
3968 if (cmdmod.split == 0)
3969 flags = WSP_BELOW;
3970 flags |= WSP_NEWLOC;
3971 if (win_split(height, flags) == FAIL)
3972 return FAIL; // not enough room for window
3973 RESET_BINDING(curwin);
3974
3975 if (IS_LL_STACK(qi))
3976 {
3977 // For the location list window, create a reference to the
3978 // location list from the window 'win'.
3979 curwin->w_llist_ref = win->w_llist;
3980 win->w_llist->qf_refcount++;
3981 }
3982
3983 if (oldwin != curwin)
3984 oldwin = NULL; // don't store info when in another window
3985 if (qf_buf != NULL)
3986 {
3987 // Use the existing quickfix buffer
3988 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
3989 ECMD_HIDE + ECMD_OLDBUF, oldwin);
3990 }
3991 else
3992 {
3993 // Create a new quickfix buffer
3994 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
3995
3996 // switch off 'swapfile'
3997 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3998 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
3999 OPT_LOCAL);
4000 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
4001 RESET_BINDING(curwin);
4002#ifdef FEAT_DIFF
4003 curwin->w_p_diff = FALSE;
4004#endif
4005#ifdef FEAT_FOLDING
4006 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4007 OPT_LOCAL);
4008#endif
4009 }
4010
4011 // Only set the height when still in the same tab page and there is no
4012 // window to the side.
4013 if (curtab == prevtab && curwin->w_width == Columns)
4014 win_setheight(height);
4015 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4016 if (win_valid(win))
4017 prevwin = win;
4018
4019 return OK;
4020}
4021
4022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004024 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 */
4026 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004027ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004029 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004030 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004032 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004033 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034
Bram Moolenaar39665952018-08-15 20:59:48 +02004035 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004036 {
4037 qi = GET_LOC_LIST(curwin);
4038 if (qi == NULL)
4039 {
4040 EMSG(_(e_loclist));
4041 return;
4042 }
4043 }
4044
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004045 incr_quickfix_busy();
4046
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 if (eap->addr_count != 0)
4048 height = eap->line2;
4049 else
4050 height = QF_WINHEIGHT;
4051
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004052 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053#ifdef FEAT_GUI
4054 need_mouse_correct = TRUE;
4055#endif
4056
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004057 // Find an existing quickfix window, or open a new one.
4058 if (cmdmod.tab == 0)
4059 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4060 cmdmod.split & WSP_VERT);
4061 if (status == FAIL)
4062 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004063 {
4064 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004065 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004068 qfl = &qi->qf_lists[qi->qf_curlist];
4069 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004070 // Save the current index here, as updating the quickfix buffer may free
4071 // the quickfix list
4072 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004073
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004074 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004075 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004077 decr_quickfix_busy();
4078
4079 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 curwin->w_cursor.col = 0;
4081 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004082 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083}
4084
4085/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004086 * Move the cursor in the quickfix window to "lnum".
4087 */
4088 static void
4089qf_win_goto(win_T *win, linenr_T lnum)
4090{
4091 win_T *old_curwin = curwin;
4092
4093 curwin = win;
4094 curbuf = win->w_buffer;
4095 curwin->w_cursor.lnum = lnum;
4096 curwin->w_cursor.col = 0;
4097#ifdef FEAT_VIRTUALEDIT
4098 curwin->w_cursor.coladd = 0;
4099#endif
4100 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004101 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004102 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004103 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004104 curwin = old_curwin;
4105 curbuf = curwin->w_buffer;
4106}
4107
4108/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004109 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004110 */
4111 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004112ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004113{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004114 qf_info_T *qi = &ql_info;
4115 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004116
Bram Moolenaar39665952018-08-15 20:59:48 +02004117 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004118 {
4119 qi = GET_LOC_LIST(curwin);
4120 if (qi == NULL)
4121 {
4122 EMSG(_(e_loclist));
4123 return;
4124 }
4125 }
4126
4127 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004128 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4129 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4130}
4131
4132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 * Return the number of the current entry (line number in the quickfix
4134 * window).
4135 */
4136 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004137qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004139 qf_info_T *qi = &ql_info;
4140
4141 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004142 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004143 qi = wp->w_llist_ref;
4144
4145 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146}
4147
4148/*
4149 * Update the cursor position in the quickfix window to the current error.
4150 * Return TRUE if there is a quickfix window.
4151 */
4152 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004153qf_win_pos_update(
4154 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004155 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156{
4157 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004158 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004160 // Put the cursor on the current error in the quickfix window, so that
4161 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004162 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 if (win != NULL
4164 && qf_index <= win->w_buffer->b_ml.ml_line_count
4165 && old_qf_index != qf_index)
4166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 if (qf_index > old_qf_index)
4168 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004169 win->w_redraw_top = old_qf_index;
4170 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
4172 else
4173 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004174 win->w_redraw_top = qf_index;
4175 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004177 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 return win != NULL;
4180}
4181
4182/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004183 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004184 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004185 */
4186 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004187is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004188{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004189 // A window displaying the quickfix buffer will have the w_llist_ref field
4190 // set to NULL.
4191 // A window displaying a location list buffer will have the w_llist_ref
4192 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004193 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004194 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4195 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004196 return TRUE;
4197
4198 return FALSE;
4199}
4200
4201/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004202 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004203 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004204 */
4205 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004206qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004207{
4208 win_T *win;
4209
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004210 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004211 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004212 return win;
4213 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004214}
4215
4216/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004217 * Find a quickfix buffer.
4218 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 */
4220 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004221qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004223 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004224 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
Bram Moolenaar9c102382006-05-03 21:26:49 +00004226 FOR_ALL_TAB_WINDOWS(tp, win)
4227 if (is_qf_win(win, qi))
4228 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004229
Bram Moolenaar9c102382006-05-03 21:26:49 +00004230 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231}
4232
4233/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004234 * Update the w:quickfix_title variable in the quickfix/location list window
4235 */
4236 static void
4237qf_update_win_titlevar(qf_info_T *qi)
4238{
4239 win_T *win;
4240 win_T *curwin_save;
4241
4242 if ((win = qf_find_win(qi)) != NULL)
4243 {
4244 curwin_save = curwin;
4245 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004246 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004247 curwin = curwin_save;
4248 }
4249}
4250
4251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 * Find the quickfix buffer. If it exists, update the contents.
4253 */
4254 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004255qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256{
4257 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004258 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004261 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004262 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (buf != NULL)
4264 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004265 linenr_T old_line_count = buf->b_ml.ml_line_count;
4266
4267 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004268 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004269 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
Bram Moolenaard823fa92016-08-12 16:29:27 +02004271 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004272
Bram Moolenaar864293a2016-06-02 13:40:04 +02004273 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004274 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004275
Bram Moolenaar864293a2016-06-02 13:40:04 +02004276 if (old_last == NULL)
4277 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004278 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004279
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004280 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004281 aucmd_restbuf(&aco);
4282 }
4283
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004284 // Only redraw when added lines are visible. This avoids flickering
4285 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004286 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4287 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
4289}
4290
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004291/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004292 * Add an error line to the quickfix buffer.
4293 */
4294 static int
4295qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4296{
4297 int len;
4298 buf_T *errbuf;
4299
4300 if (qfp->qf_module != NULL)
4301 {
4302 STRCPY(IObuff, qfp->qf_module);
4303 len = (int)STRLEN(IObuff);
4304 }
4305 else if (qfp->qf_fnum != 0
4306 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4307 && errbuf->b_fname != NULL)
4308 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004309 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004310 STRCPY(IObuff, gettail(errbuf->b_fname));
4311 else
4312 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004313 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004314 if (errbuf->b_sfname == NULL
4315 || mch_isFullName(errbuf->b_sfname))
4316 {
4317 if (*dirname == NUL)
4318 mch_dirname(dirname, MAXPATHL);
4319 shorten_buf_fname(errbuf, dirname, FALSE);
4320 }
4321 STRCPY(IObuff, errbuf->b_fname);
4322 }
4323 len = (int)STRLEN(IObuff);
4324 }
4325 else
4326 len = 0;
4327 IObuff[len++] = '|';
4328
4329 if (qfp->qf_lnum > 0)
4330 {
4331 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4332 len += (int)STRLEN(IObuff + len);
4333
4334 if (qfp->qf_col > 0)
4335 {
4336 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4337 len += (int)STRLEN(IObuff + len);
4338 }
4339
4340 sprintf((char *)IObuff + len, "%s",
4341 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4342 len += (int)STRLEN(IObuff + len);
4343 }
4344 else if (qfp->qf_pattern != NULL)
4345 {
4346 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4347 len += (int)STRLEN(IObuff + len);
4348 }
4349 IObuff[len++] = '|';
4350 IObuff[len++] = ' ';
4351
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004352 // Remove newlines and leading whitespace from the text.
4353 // For an unrecognized line keep the indent, the compiler may
4354 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004355 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4356 IObuff + len, IOSIZE - len);
4357
4358 if (ml_append_buf(buf, lnum, IObuff,
4359 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4360 return FAIL;
4361
4362 return OK;
4363}
4364
4365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 * Fill current buffer with quickfix errors, replacing any previous contents.
4367 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004368 * If "old_last" is not NULL append the items after this one.
4369 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4370 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 */
4372 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004373qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004375 linenr_T lnum;
4376 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004377 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
Bram Moolenaar864293a2016-06-02 13:40:04 +02004379 if (old_last == NULL)
4380 {
4381 if (buf != curbuf)
4382 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004383 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004384 return;
4385 }
4386
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004387 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004388 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4389 (void)ml_delete((linenr_T)1, FALSE);
4390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004392 // Check if there is anything to display
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004393 if (!qf_stack_empty(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004395 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4396 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004397
4398 *dirname = NUL;
4399
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004400 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004401 if (old_last == NULL)
4402 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004403 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004404 lnum = 0;
4405 }
4406 else
4407 {
4408 qfp = old_last->qf_next;
4409 lnum = buf->b_ml.ml_line_count;
4410 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004411 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004413 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004415
Bram Moolenaar864293a2016-06-02 13:40:04 +02004416 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004418 if (qfp == NULL)
4419 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004421
4422 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004423 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004424 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004427 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 check_lnums(TRUE);
4429
Bram Moolenaar864293a2016-06-02 13:40:04 +02004430 if (old_last == NULL)
4431 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004432 // Set the 'filetype' to "qf" each time after filling the buffer.
4433 // This resembles reading a file into a buffer, it's more logical when
4434 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004435 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004436 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4437 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004439 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004440 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004442 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004444 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004445 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004446
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004447 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004448 redraw_curbuf_later(NOT_VALID);
4449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004451 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 KeyTyped = old_KeyTyped;
4453}
4454
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004455/*
4456 * For every change made to the quickfix list, update the changed tick.
4457 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004458 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004459qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004460{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004461 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004462}
4463
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004465 * Return the quickfix/location list number with the given identifier.
4466 * Returns -1 if list is not found.
4467 */
4468 static int
4469qf_id2nr(qf_info_T *qi, int_u qfid)
4470{
4471 int qf_idx;
4472
4473 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4474 if (qi->qf_lists[qf_idx].qf_id == qfid)
4475 return qf_idx;
4476 return INVALID_QFIDX;
4477}
4478
4479/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004480 * If the current list is not "save_qfid" and we can find the list with that ID
4481 * then make it the current list.
4482 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004483 * Returns OK if successfully restored the list. Returns FAIL if the list with
4484 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004485 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004486 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004487qf_restore_list(qf_info_T *qi, int_u save_qfid)
4488{
4489 int curlist;
4490
4491 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4492 {
4493 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004494 if (curlist < 0)
4495 // list is not present
4496 return FAIL;
4497 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004498 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004499 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004500}
4501
4502/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004503 * Jump to the first entry if there is one.
4504 */
4505 static void
4506qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4507{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004508 if (qf_restore_list(qi, save_qfid) == FAIL)
4509 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004510
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004511 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004512 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004513 qf_jump(qi, 0, 0, forceit);
4514}
4515
4516/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004517 * Return TRUE when using ":vimgrep" for ":grep".
4518 */
4519 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004520grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004521{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004522 return ((cmdidx == CMD_grep
4523 || cmdidx == CMD_lgrep
4524 || cmdidx == CMD_grepadd
4525 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004526 && STRCMP("internal",
4527 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4528}
4529
4530/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004531 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004533 static char_u *
4534make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004536 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004537 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004538 case CMD_make: return (char_u *)"make";
4539 case CMD_lmake: return (char_u *)"lmake";
4540 case CMD_grep: return (char_u *)"grep";
4541 case CMD_lgrep: return (char_u *)"lgrep";
4542 case CMD_grepadd: return (char_u *)"grepadd";
4543 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4544 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546}
4547
4548/*
4549 * Return the name for the errorfile, in allocated memory.
4550 * Find a new unique name when 'makeef' contains "##".
4551 * Returns NULL for error.
4552 */
4553 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004554get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555{
4556 char_u *p;
4557 char_u *name;
4558 static int start = -1;
4559 static int off = 0;
4560#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004561 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562#endif
4563
4564 if (*p_mef == NUL)
4565 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004566 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (name == NULL)
4568 EMSG(_(e_notmp));
4569 return name;
4570 }
4571
4572 for (p = p_mef; *p; ++p)
4573 if (p[0] == '#' && p[1] == '#')
4574 break;
4575
4576 if (*p == NUL)
4577 return vim_strsave(p_mef);
4578
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004579 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 for (;;)
4581 {
4582 if (start == -1)
4583 start = mch_get_pid();
4584 else
4585 off += 19;
4586
4587 name = alloc((unsigned)STRLEN(p_mef) + 30);
4588 if (name == NULL)
4589 break;
4590 STRCPY(name, p_mef);
4591 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4592 STRCAT(name, p + 2);
4593 if (mch_getperm(name) < 0
4594#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004595 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 && mch_lstat((char *)name, &sb) < 0
4597#endif
4598 )
4599 break;
4600 vim_free(name);
4601 }
4602 return name;
4603}
4604
4605/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004606 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4607 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4608 */
4609 static char_u *
4610make_get_fullcmd(char_u *makecmd, char_u *fname)
4611{
4612 char_u *cmd;
4613 unsigned len;
4614
4615 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4616 if (*p_sp != NUL)
4617 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4618 cmd = alloc(len);
4619 if (cmd == NULL)
4620 return NULL;
4621 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4622 (char *)p_shq);
4623
4624 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4625 if (*p_sp != NUL)
4626 append_redir(cmd, len, p_sp, fname);
4627
4628 // Display the fully formed command. Output a newline if there's something
4629 // else than the :make command that was typed (in which case the cursor is
4630 // in column 0).
4631 if (msg_col == 0)
4632 msg_didout = FALSE;
4633 msg_start();
4634 MSG_PUTS(":!");
4635 msg_outtrans(cmd); // show what we are doing
4636
4637 return cmd;
4638}
4639
4640/*
4641 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4642 */
4643 void
4644ex_make(exarg_T *eap)
4645{
4646 char_u *fname;
4647 char_u *cmd;
4648 char_u *enc = NULL;
4649 win_T *wp = NULL;
4650 qf_info_T *qi = &ql_info;
4651 int res;
4652 char_u *au_name = NULL;
4653 int_u save_qfid;
4654
4655 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4656 if (grep_internal(eap->cmdidx))
4657 {
4658 ex_vimgrep(eap);
4659 return;
4660 }
4661
4662 au_name = make_get_auname(eap->cmdidx);
4663 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4664 curbuf->b_fname, TRUE, curbuf))
4665 {
4666#ifdef FEAT_EVAL
4667 if (aborting())
4668 return;
4669#endif
4670 }
4671#ifdef FEAT_MBYTE
4672 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4673#endif
4674
4675 if (is_loclist_cmd(eap->cmdidx))
4676 wp = curwin;
4677
4678 autowrite_all();
4679 fname = get_mef_name();
4680 if (fname == NULL)
4681 return;
4682 mch_remove(fname); // in case it's not unique
4683
4684 cmd = make_get_fullcmd(eap->arg, fname);
4685 if (cmd == NULL)
4686 return;
4687
4688 // let the shell know if we are redirecting output or not
4689 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4690
4691#ifdef AMIGA
4692 out_flush();
4693 // read window status report and redraw before message
4694 (void)char_avail();
4695#endif
4696
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004697 incr_quickfix_busy();
4698
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004699 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4700 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4701 (eap->cmdidx != CMD_grepadd
4702 && eap->cmdidx != CMD_lgrepadd),
4703 qf_cmdtitle(*eap->cmdlinep), enc);
4704 if (wp != NULL)
4705 {
4706 qi = GET_LOC_LIST(wp);
4707 if (qi == NULL)
4708 goto cleanup;
4709 }
4710 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004711 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004712
4713 // Remember the current quickfix list identifier, so that we can
4714 // check for autocommands changing the current quickfix list.
4715 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4716 if (au_name != NULL)
4717 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4718 curbuf->b_fname, TRUE, curbuf);
4719 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4720 // display the first error
4721 qf_jump_first(qi, save_qfid, FALSE);
4722
4723cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004724 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004725 mch_remove(fname);
4726 vim_free(fname);
4727 vim_free(cmd);
4728}
4729
4730/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004731 * Returns the number of valid entries in the current quickfix/location list.
4732 */
4733 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004734qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004735{
4736 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004737 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004738 qfline_T *qfp;
4739 int i, sz = 0;
4740 int prev_fnum = 0;
4741
Bram Moolenaar39665952018-08-15 20:59:48 +02004742 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004743 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004744 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004745 qi = GET_LOC_LIST(curwin);
4746 if (qi == NULL)
4747 return 0;
4748 }
4749
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004750 qfl = &qi->qf_lists[qi->qf_curlist];
4751 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004752 ++i, qfp = qfp->qf_next)
4753 {
4754 if (qfp->qf_valid)
4755 {
4756 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004757 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004758 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4759 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004760 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004761 sz++;
4762 prev_fnum = qfp->qf_fnum;
4763 }
4764 }
4765 }
4766
4767 return sz;
4768}
4769
4770/*
4771 * Returns the current index of the quickfix/location list.
4772 * Returns 0 if there is an error.
4773 */
4774 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004775qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004776{
4777 qf_info_T *qi = &ql_info;
4778
Bram Moolenaar39665952018-08-15 20:59:48 +02004779 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004780 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004781 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004782 qi = GET_LOC_LIST(curwin);
4783 if (qi == NULL)
4784 return 0;
4785 }
4786
4787 return qi->qf_lists[qi->qf_curlist].qf_index;
4788}
4789
4790/*
4791 * Returns the current index in the quickfix/location list (counting only valid
4792 * entries). If no valid entries are in the list, then returns 1.
4793 */
4794 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004795qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004796{
4797 qf_info_T *qi = &ql_info;
4798 qf_list_T *qfl;
4799 qfline_T *qfp;
4800 int i, eidx = 0;
4801 int prev_fnum = 0;
4802
Bram Moolenaar39665952018-08-15 20:59:48 +02004803 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004804 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004805 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004806 qi = GET_LOC_LIST(curwin);
4807 if (qi == NULL)
4808 return 1;
4809 }
4810
4811 qfl = &qi->qf_lists[qi->qf_curlist];
4812 qfp = qfl->qf_start;
4813
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004814 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004815 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4816 return 1;
4817
4818 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4819 {
4820 if (qfp->qf_valid)
4821 {
4822 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4823 {
4824 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4825 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004826 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004827 eidx++;
4828 prev_fnum = qfp->qf_fnum;
4829 }
4830 }
4831 else
4832 eidx++;
4833 }
4834 }
4835
4836 return eidx ? eidx : 1;
4837}
4838
4839/*
4840 * Get the 'n'th valid error entry in the quickfix or location list.
4841 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4842 * For :cdo and :ldo returns the 'n'th valid error entry.
4843 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4844 */
4845 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004846qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004847{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004848 qfline_T *qfp = qfl->qf_start;
4849 int i, eidx;
4850 int prev_fnum = 0;
4851
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004852 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004853 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4854 return 1;
4855
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004856 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004857 i++, qfp = qfp->qf_next)
4858 {
4859 if (qfp->qf_valid)
4860 {
4861 if (fdo)
4862 {
4863 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4864 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004865 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004866 eidx++;
4867 prev_fnum = qfp->qf_fnum;
4868 }
4869 }
4870 else
4871 eidx++;
4872 }
4873
4874 if (eidx == n)
4875 break;
4876 }
4877
4878 if (i <= qfl->qf_count)
4879 return i;
4880 else
4881 return 1;
4882}
4883
4884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004886 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004887 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 */
4889 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004890ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004892 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004893 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004894
Bram Moolenaar39665952018-08-15 20:59:48 +02004895 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004896 {
4897 qi = GET_LOC_LIST(curwin);
4898 if (qi == NULL)
4899 {
4900 EMSG(_(e_loclist));
4901 return;
4902 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004903 }
4904
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004905 if (eap->addr_count > 0)
4906 errornr = (int)eap->line2;
4907 else
4908 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004909 switch (eap->cmdidx)
4910 {
4911 case CMD_cc: case CMD_ll:
4912 errornr = 0;
4913 break;
4914 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4915 case CMD_lfirst:
4916 errornr = 1;
4917 break;
4918 default:
4919 errornr = 32767;
4920 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004921 }
4922
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004923 // For cdo and ldo commands, jump to the nth valid error.
4924 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004925 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4926 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004927 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004928 eap->addr_count > 0 ? (int)eap->line1 : 1,
4929 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4930
4931 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932}
4933
4934/*
4935 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004936 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004937 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 */
4939 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004940ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004942 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004943 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004944 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004945
Bram Moolenaar39665952018-08-15 20:59:48 +02004946 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004947 {
4948 qi = GET_LOC_LIST(curwin);
4949 if (qi == NULL)
4950 {
4951 EMSG(_(e_loclist));
4952 return;
4953 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004954 }
4955
Bram Moolenaar55b69262017-08-13 13:42:01 +02004956 if (eap->addr_count > 0
4957 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4958 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004959 errornr = (int)eap->line2;
4960 else
4961 errornr = 1;
4962
Bram Moolenaar39665952018-08-15 20:59:48 +02004963 // Depending on the command jump to either next or previous entry/file.
4964 switch (eap->cmdidx)
4965 {
4966 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4967 dir = FORWARD;
4968 break;
4969 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4970 case CMD_lNext:
4971 dir = BACKWARD;
4972 break;
4973 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4974 dir = FORWARD_FILE;
4975 break;
4976 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4977 dir = BACKWARD_FILE;
4978 break;
4979 default:
4980 dir = FORWARD;
4981 break;
4982 }
4983
4984 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985}
4986
4987/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004988 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004989 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 */
4991 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004992ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004994 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004995 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004996 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004997 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004998 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004999 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005000
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005001 switch (eap->cmdidx)
5002 {
5003 case CMD_cfile: au_name = (char_u *)"cfile"; break;
5004 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
5005 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
5006 case CMD_lfile: au_name = (char_u *)"lfile"; break;
5007 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
5008 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
5009 default: break;
5010 }
5011 if (au_name != NULL)
5012 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005013#ifdef FEAT_MBYTE
5014 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
5015#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02005016#ifdef FEAT_BROWSE
5017 if (cmdmod.browse)
5018 {
5019 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005020 NULL, NULL,
5021 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005022 if (browse_file == NULL)
5023 return;
5024 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5025 vim_free(browse_file);
5026 }
5027 else
5028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005030 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005031
Bram Moolenaar39665952018-08-15 20:59:48 +02005032 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005033 wp = curwin;
5034
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005035 incr_quickfix_busy();
5036
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005037 // This function is used by the :cfile, :cgetfile and :caddfile
5038 // commands.
5039 // :cfile always creates a new quickfix list and jumps to the
5040 // first error.
5041 // :cgetfile creates a new quickfix list but doesn't jump to the
5042 // first error.
5043 // :caddfile adds to an existing quickfix list. If there is no
5044 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005045 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005046 && eap->cmdidx != CMD_laddfile),
5047 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005048 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005049 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005050 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005051 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005052 {
5053 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005054 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005055 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005056 }
5057 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005058 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005059 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005060 if (au_name != NULL)
5061 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005062
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005063 // Jump to the first error for a new list and if autocmds didn't
5064 // free the list.
5065 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5066 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005067 // display the first error
5068 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005069
5070 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005071}
5072
5073/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005074 * Return the vimgrep autocmd name.
5075 */
5076 static char_u *
5077vgr_get_auname(cmdidx_T cmdidx)
5078{
5079 switch (cmdidx)
5080 {
5081 case CMD_vimgrep: return (char_u *)"vimgrep";
5082 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5083 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5084 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5085 case CMD_grep: return (char_u *)"grep";
5086 case CMD_lgrep: return (char_u *)"lgrep";
5087 case CMD_grepadd: return (char_u *)"grepadd";
5088 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5089 default: return NULL;
5090 }
5091}
5092
5093/*
5094 * Initialize the regmatch used by vimgrep for pattern "s".
5095 */
5096 static void
5097vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5098{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005099 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005100 regmatch->regprog = NULL;
5101
5102 if (s == NULL || *s == NUL)
5103 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005104 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005105 if (last_search_pat() == NULL)
5106 {
5107 EMSG(_(e_noprevre));
5108 return;
5109 }
5110 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5111 }
5112 else
5113 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5114
5115 regmatch->rmm_ic = p_ic;
5116 regmatch->rmm_maxcol = 0;
5117}
5118
5119/*
5120 * Display a file name when vimgrep is running.
5121 */
5122 static void
5123vgr_display_fname(char_u *fname)
5124{
5125 char_u *p;
5126
5127 msg_start();
5128 p = msg_strtrunc(fname, TRUE);
5129 if (p == NULL)
5130 msg_outtrans(fname);
5131 else
5132 {
5133 msg_outtrans(p);
5134 vim_free(p);
5135 }
5136 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005137 msg_didout = FALSE; // overwrite this message
5138 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005139 msg_col = 0;
5140 out_flush();
5141}
5142
5143/*
5144 * Load a dummy buffer to search for a pattern using vimgrep.
5145 */
5146 static buf_T *
5147vgr_load_dummy_buf(
5148 char_u *fname,
5149 char_u *dirname_start,
5150 char_u *dirname_now)
5151{
5152 int save_mls;
5153#if defined(FEAT_SYN_HL)
5154 char_u *save_ei = NULL;
5155#endif
5156 buf_T *buf;
5157
5158#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005159 // Don't do Filetype autocommands to avoid loading syntax and
5160 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005161 save_ei = au_event_disable(",Filetype");
5162#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005163 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005164 save_mls = p_mls;
5165 p_mls = 0;
5166
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005167 // Load file into a buffer, so that 'fileencoding' is detected,
5168 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005169 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5170
5171 p_mls = save_mls;
5172#if defined(FEAT_SYN_HL)
5173 au_event_restore(save_ei);
5174#endif
5175
5176 return buf;
5177}
5178
5179/*
5180 * Check whether a quickfix/location list valid. Autocmds may remove or change
5181 * a quickfix list when vimgrep is running. If the list is not found, create a
5182 * new list.
5183 */
5184 static int
5185vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005186 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005187 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005188 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005189 char_u *title)
5190{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005191 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005192 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005193 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005194 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005195 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005196 // An autocmd has freed the location list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005197 EMSG(_(e_loc_list_changed));
5198 return FALSE;
5199 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005200 else
5201 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005202 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005203 qf_new_list(qi, title);
5204 return TRUE;
5205 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005206 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005207
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005208 if (qf_restore_list(qi, qfid) == FAIL)
5209 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005210
5211 return TRUE;
5212}
5213
5214/*
5215 * Search for a pattern in all the lines in a buffer and add the matching lines
5216 * to a quickfix list.
5217 */
5218 static int
5219vgr_match_buflines(
5220 qf_info_T *qi,
5221 char_u *fname,
5222 buf_T *buf,
5223 regmmatch_T *regmatch,
5224 long tomatch,
5225 int duplicate_name,
5226 int flags)
5227{
5228 int found_match = FALSE;
5229 long lnum;
5230 colnr_T col;
5231
5232 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5233 {
5234 col = 0;
5235 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5236 col, NULL, NULL) > 0)
5237 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005238 // Pass the buffer number so that it gets used even for a
5239 // dummy buffer, unless duplicate_name is set, then the
5240 // buffer will be wiped out below.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005241 if (qf_add_entry(qi,
5242 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005243 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005244 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005245 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005246 duplicate_name ? 0 : buf->b_fnum,
5247 ml_get_buf(buf,
5248 regmatch->startpos[0].lnum + lnum, FALSE),
5249 regmatch->startpos[0].lnum + lnum,
5250 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005251 FALSE, // vis_col
5252 NULL, // search pattern
5253 0, // nr
5254 0, // type
5255 TRUE // valid
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005256 ) == FAIL)
5257 {
5258 got_int = TRUE;
5259 break;
5260 }
5261 found_match = TRUE;
5262 if (--tomatch == 0)
5263 break;
5264 if ((flags & VGR_GLOBAL) == 0
5265 || regmatch->endpos[0].lnum > 0)
5266 break;
5267 col = regmatch->endpos[0].col
5268 + (col == regmatch->endpos[0].col);
5269 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5270 break;
5271 }
5272 line_breakcheck();
5273 if (got_int)
5274 break;
5275 }
5276
5277 return found_match;
5278}
5279
5280/*
5281 * Jump to the first match and update the directory.
5282 */
5283 static void
5284vgr_jump_to_match(
5285 qf_info_T *qi,
5286 int forceit,
5287 int *redraw_for_dummy,
5288 buf_T *first_match_buf,
5289 char_u *target_dir)
5290{
5291 buf_T *buf;
5292
5293 buf = curbuf;
5294 qf_jump(qi, 0, 0, forceit);
5295 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005296 // If we jumped to another buffer redrawing will already be
5297 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005298 *redraw_for_dummy = FALSE;
5299
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005300 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005301 if (curbuf == first_match_buf && target_dir != NULL)
5302 {
5303 exarg_T ea;
5304
5305 ea.arg = target_dir;
5306 ea.cmdidx = CMD_lcd;
5307 ex_cd(&ea);
5308 }
5309}
5310
5311/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005312 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005313 * ":vimgrepadd {pattern} file(s)"
5314 * ":lvimgrep {pattern} file(s)"
5315 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005316 */
5317 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005318ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005319{
Bram Moolenaar81695252004-12-29 20:58:21 +00005320 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005321 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005322 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005323 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005324 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005325 char_u *s;
5326 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005327 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005328 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005329 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005330 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005331 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005332 buf_T *buf;
5333 int duplicate_name = FALSE;
5334 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005335 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005336 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005337 buf_T *first_match_buf = NULL;
5338 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005339 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005340 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005341 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005342 char_u *dirname_start = NULL;
5343 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005344 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005345 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005346
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005347 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005348 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5349 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005350 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005351#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005352 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005353 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005354#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005355 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005356
Bram Moolenaar39665952018-08-15 20:59:48 +02005357 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005358 {
5359 qi = ll_get_or_alloc_list(curwin);
5360 if (qi == NULL)
5361 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005362 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005363 }
5364
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005365 if (eap->addr_count > 0)
5366 tomatch = eap->line2;
5367 else
5368 tomatch = MAXLNUM;
5369
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005370 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005371 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005372 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005373 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005374 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005375 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005376 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005377 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005378 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005379
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005380 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005381 if (regmatch.regprog == NULL)
5382 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005383
5384 p = skipwhite(p);
5385 if (*p == NUL)
5386 {
5387 EMSG(_("E683: File name missing or invalid pattern"));
5388 goto theend;
5389 }
5390
Bram Moolenaar55b69262017-08-13 13:42:01 +02005391 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005392 && eap->cmdidx != CMD_vimgrepadd
5393 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005394 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005395 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005396 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005397
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005398 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005399 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005400 goto theend;
5401 if (fcount == 0)
5402 {
5403 EMSG(_(e_nomatch));
5404 goto theend;
5405 }
5406
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005407 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5408 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005409 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005410 {
5411 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005412 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005413 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005414
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005415 // Remember the current directory, because a BufRead autocommand that does
5416 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005417 mch_dirname(dirname_start, MAXPATHL);
5418
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005419 incr_quickfix_busy();
5420
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005421 // Remember the current quickfix list identifier, so that we can check for
5422 // autocommands changing the current quickfix list.
Bram Moolenaar3c097222017-12-21 20:54:49 +01005423 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005424
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005425 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005426 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005427 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005428 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005429 if (time(NULL) > seconds)
5430 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005431 // Display the file name every second or so, show the user we are
5432 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005433 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005434 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005435 }
5436
Bram Moolenaar81695252004-12-29 20:58:21 +00005437 buf = buflist_findname_exp(fnames[fi]);
5438 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5439 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005440 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005441 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005442 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005443 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005444
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005445 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005446 }
5447 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005448 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005449 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005450
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005451 // Check whether the quickfix list is still valid. When loading a
5452 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005453 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005454 {
5455 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005456 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005457 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005458 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005459 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005460
Bram Moolenaar81695252004-12-29 20:58:21 +00005461 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005462 {
5463 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005464 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005465 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005466 else
5467 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005468 // Try for a match in all lines of the buffer.
5469 // For ":1vimgrep" look for first match only.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005470 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5471 tomatch, duplicate_name, flags);
5472
Bram Moolenaar81695252004-12-29 20:58:21 +00005473 if (using_dummy)
5474 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005475 if (found_match && first_match_buf == NULL)
5476 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005477 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005478 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005479 // Never keep a dummy buffer if there is another buffer
5480 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005481 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005482 buf = NULL;
5483 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005484 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005485 || buf->b_p_bh[0] == 'u' // "unload"
5486 || buf->b_p_bh[0] == 'w' // "wipe"
5487 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005488 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005489 // When no match was found we don't need to remember the
5490 // buffer, wipe it out. If there was a match and it
5491 // wasn't the first one or we won't jump there: only
5492 // unload the buffer.
5493 // Ignore 'hidden' here, because it may lead to having too
5494 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005495 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005496 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005497 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005498 buf = NULL;
5499 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005500 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005501 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005502 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005503 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005504 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005505 buf = NULL;
5506 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005507 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005508
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005509 if (buf != NULL)
5510 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005511 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005512 buf->b_flags &= ~BF_DUMMY;
5513
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005514 // If the buffer is still loaded we need to use the
5515 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005516 if (buf == first_match_buf
5517 && target_dir == NULL
5518 && STRCMP(dirname_start, dirname_now) != 0)
5519 target_dir = vim_strsave(dirname_now);
5520
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005521 // The buffer is still loaded, the Filetype autocommands
5522 // need to be done now, in that buffer. And the modelines
5523 // need to be done (again). But not the window-local
5524 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005525 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005526#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005527 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5528 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005529#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005530 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005531 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005532 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005533 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005534 }
5535 }
5536
5537 FreeWild(fcount, fnames);
5538
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005539 qfl = &qi->qf_lists[qi->qf_curlist];
5540 qfl->qf_nonevalid = FALSE;
5541 qfl->qf_ptr = qfl->qf_start;
5542 qfl->qf_index = 1;
5543 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005544
Bram Moolenaar864293a2016-06-02 13:40:04 +02005545 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005546
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005547 if (au_name != NULL)
5548 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5549 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005550 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5551 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005552 if (!qflist_valid(wp, save_qfid)
5553 || qf_restore_list(qi, save_qfid) == FAIL)
5554 {
5555 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005556 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005557 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005558
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005559 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005560 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005561 {
5562 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005563 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5564 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005565 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005566 else
5567 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005568
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005569 decr_quickfix_busy();
5570
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005571 // If we loaded a dummy buffer into the current window, the autocommands
5572 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005573 if (redraw_for_dummy)
5574 {
5575#ifdef FEAT_FOLDING
5576 foldUpdateAll(curwin);
5577#else
5578 redraw_later(NOT_VALID);
5579#endif
5580 }
5581
Bram Moolenaar86b68352004-12-27 21:59:20 +00005582theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005583 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005584 vim_free(dirname_now);
5585 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005586 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005587 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005588}
5589
5590/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005591 * Restore current working directory to "dirname_start" if they differ, taking
5592 * into account whether it is set locally or globally.
5593 */
5594 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005595restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005596{
5597 char_u *dirname_now = alloc(MAXPATHL);
5598
5599 if (NULL != dirname_now)
5600 {
5601 mch_dirname(dirname_now, MAXPATHL);
5602 if (STRCMP(dirname_start, dirname_now) != 0)
5603 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005604 // If the directory has changed, change it back by building up an
5605 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005606 exarg_T ea;
5607
5608 ea.arg = dirname_start;
5609 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5610 ex_cd(&ea);
5611 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005612 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005613 }
5614}
5615
5616/*
5617 * Load file "fname" into a dummy buffer and return the buffer pointer,
5618 * placing the directory resulting from the buffer load into the
5619 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5620 * prior to calling this function. Restores directory to "dirname_start" prior
5621 * to returning, if autocmds or the 'autochdir' option have changed it.
5622 *
5623 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5624 * or wipe_dummy_buffer() later!
5625 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005626 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005627 */
5628 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005629load_dummy_buffer(
5630 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005631 char_u *dirname_start, // in: old directory
5632 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005633{
5634 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005635 bufref_T newbufref;
5636 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005637 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005638 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005639 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005640
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005641 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005642 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5643 if (newbuf == NULL)
5644 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005645 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005646
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005647 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005648 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5649
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005650 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005651 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005652 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005653 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005654 ++newbuf->b_locked;
5655
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005656 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005657 aucmd_prepbuf(&aco, newbuf);
5658
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005659 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005660 (void)setfname(curbuf, fname, NULL, FALSE);
5661
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005662 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005663 check_need_swap(TRUE);
5664
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005665 // Remove the "dummy" flag, otherwise autocommands may not
5666 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005667 curbuf->b_flags &= ~BF_DUMMY;
5668
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005669 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005670 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005671 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005672 NULL, READ_NEW | READ_DUMMY);
5673 --newbuf->b_locked;
5674 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005675 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005676 && !(curbuf->b_flags & BF_NEW))
5677 {
5678 failed = FALSE;
5679 if (curbuf != newbuf)
5680 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005681 // Bloody autocommands changed the buffer! Can happen when
5682 // using netrw and editing a remote file. Use the current
5683 // buffer instead, delete the dummy one after restoring the
5684 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005685 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005686 newbuf = curbuf;
5687 }
5688 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005689
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005690 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005691 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005692 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5693 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005694
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005695 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5696 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005697 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005698 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005699
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005700 // When autocommands/'autochdir' option changed directory: go back.
5701 // Let the caller know what the resulting dir was first, in case it is
5702 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005703 mch_dirname(resulting_dir, MAXPATHL);
5704 restore_start_dir(dirname_start);
5705
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005706 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005707 return NULL;
5708 if (failed)
5709 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005710 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005711 return NULL;
5712 }
5713 return newbuf;
5714}
5715
5716/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005717 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5718 * directory to "dirname_start" prior to returning, if autocmds or the
5719 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005720 */
5721 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005722wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005723{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005724 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005725 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005726#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005727 cleanup_T cs;
5728
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005729 // Reset the error/interrupt/exception state here so that aborting()
5730 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5731 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005732 enter_cleanup(&cs);
5733#endif
5734
Bram Moolenaar81695252004-12-29 20:58:21 +00005735 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005736
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005737#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005738 // Restore the error/interrupt/exception state if not discarded by a
5739 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005740 leave_cleanup(&cs);
5741#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005742 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005743 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005744 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005745}
5746
5747/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005748 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5749 * directory to "dirname_start" prior to returning, if autocmds or the
5750 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005751 */
5752 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005753unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005754{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005755 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005756 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005757 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005758
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005759 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005760 restore_start_dir(dirname_start);
5761 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005762}
5763
Bram Moolenaar05159a02005-02-26 23:04:13 +00005764#if defined(FEAT_EVAL) || defined(PROTO)
5765/*
5766 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005767 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005768 */
5769 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005770get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005771{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005772 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005773 dict_T *dict;
5774 char_u buf[2];
5775 qfline_T *qfp;
5776 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005777 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005778
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005779 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005780 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005781 qi = &ql_info;
5782 if (wp != NULL)
5783 {
5784 qi = GET_LOC_LIST(wp);
5785 if (qi == NULL)
5786 return FAIL;
5787 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005788 }
5789
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005790 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005791 qf_idx = qi->qf_curlist;
5792
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005793 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005794 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005795
Bram Moolenaard823fa92016-08-12 16:29:27 +02005796 qfp = qi->qf_lists[qf_idx].qf_start;
5797 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005798 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005799 // Handle entries with a non-existing buffer number.
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005800 bufnum = qfp->qf_fnum;
5801 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5802 bufnum = 0;
5803
Bram Moolenaar05159a02005-02-26 23:04:13 +00005804 if ((dict = dict_alloc()) == NULL)
5805 return FAIL;
5806 if (list_append_dict(list, dict) == FAIL)
5807 return FAIL;
5808
5809 buf[0] = qfp->qf_type;
5810 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005811 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5812 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5813 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5814 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5815 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5816 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5817 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5818 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5819 || dict_add_string(dict, "type", buf) == FAIL
5820 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005821 return FAIL;
5822
5823 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005824 if (qfp == NULL)
5825 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005826 }
5827 return OK;
5828}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005829
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005830// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005831enum {
5832 QF_GETLIST_NONE = 0x0,
5833 QF_GETLIST_TITLE = 0x1,
5834 QF_GETLIST_ITEMS = 0x2,
5835 QF_GETLIST_NR = 0x4,
5836 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005837 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005838 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005839 QF_GETLIST_IDX = 0x40,
5840 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005841 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005842 QF_GETLIST_FILEWINID = 0x200,
5843 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005844};
5845
5846/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005847 * Parse text from 'di' and return the quickfix list items.
5848 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005849 */
5850 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005851qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005852{
5853 int status = FAIL;
5854 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005855 char_u *errorformat = p_efm;
5856 dictitem_T *efm_di;
5857 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005858
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005859 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005860 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005861 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005862 // If errorformat is supplied then use it, otherwise use the 'efm'
5863 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005864 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5865 {
5866 if (efm_di->di_tv.v_type != VAR_STRING ||
5867 efm_di->di_tv.vval.v_string == NULL)
5868 return FAIL;
5869 errorformat = efm_di->di_tv.vval.v_string;
5870 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005871
Bram Moolenaar36538222017-09-02 19:51:44 +02005872 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005873 if (l == NULL)
5874 return FAIL;
5875
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005876 qi = ll_new_list();
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005877 if (qi != NULL)
5878 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005879 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005880 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5881 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005882 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005883 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005884 }
5885 free(qi);
5886 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005887 dict_add_list(retdict, "items", l);
5888 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005889 }
5890
5891 return status;
5892}
5893
5894/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005895 * Return the quickfix/location list window identifier in the current tabpage.
5896 */
5897 static int
5898qf_winid(qf_info_T *qi)
5899{
5900 win_T *win;
5901
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005902 // The quickfix window can be opened even if the quickfix list is not set
5903 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005904 if (qi == NULL)
5905 return 0;
5906 win = qf_find_win(qi);
5907 if (win != NULL)
5908 return win->w_id;
5909 return 0;
5910}
5911
5912/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005913 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005914 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005915 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005916qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005917{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005918 int flags = QF_GETLIST_NONE;
5919
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005920 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005921 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005922 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005923 if (!loclist)
5924 // File window ID is applicable only to location list windows
5925 flags &= ~ QF_GETLIST_FILEWINID;
5926 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005927
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005928 if (dict_find(what, (char_u *)"title", -1) != NULL)
5929 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005930
Bram Moolenaara6d48492017-12-12 22:45:31 +01005931 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5932 flags |= QF_GETLIST_NR;
5933
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005934 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5935 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005936
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005937 if (dict_find(what, (char_u *)"context", -1) != NULL)
5938 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005939
Bram Moolenaara6d48492017-12-12 22:45:31 +01005940 if (dict_find(what, (char_u *)"id", -1) != NULL)
5941 flags |= QF_GETLIST_ID;
5942
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005943 if (dict_find(what, (char_u *)"items", -1) != NULL)
5944 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005945
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005946 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5947 flags |= QF_GETLIST_IDX;
5948
5949 if (dict_find(what, (char_u *)"size", -1) != NULL)
5950 flags |= QF_GETLIST_SIZE;
5951
Bram Moolenaarb254af32017-12-18 19:48:58 +01005952 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5953 flags |= QF_GETLIST_TICK;
5954
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005955 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5956 flags |= QF_GETLIST_FILEWINID;
5957
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005958 return flags;
5959}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005960
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005961/*
5962 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5963 * If 'nr' and 'id' are not present in 'what' then return the current
5964 * quickfix list index.
5965 * If 'nr' is zero then return the current quickfix list index.
5966 * If 'nr' is '$' then return the last quickfix list index.
5967 * If 'id' is present then return the index of the quickfix list with that id.
5968 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5969 * Return -1, if quickfix list is not present or if the stack is empty.
5970 */
5971 static int
5972qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5973{
5974 int qf_idx;
5975 dictitem_T *di;
5976
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005977 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005978 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5979 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005980 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005981 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005982 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005983 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005984 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005985 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005986 qf_idx = di->di_tv.vval.v_number - 1;
5987 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005988 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005989 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005990 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005991 else if (di->di_tv.v_type == VAR_STRING
5992 && di->di_tv.vval.v_string != NULL
5993 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005994 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005995 qf_idx = qi->qf_listcount - 1;
5996 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005997 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005998 }
5999
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006000 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6001 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006002 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006003 if (di->di_tv.v_type == VAR_NUMBER)
6004 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006005 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006006 if (di->di_tv.vval.v_number != 0)
6007 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6008 }
6009 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006010 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006011 }
6012
6013 return qf_idx;
6014}
6015
6016/*
6017 * Return default values for quickfix list properties in retdict.
6018 */
6019 static int
6020qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
6021{
6022 int status = OK;
6023
6024 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006025 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006026 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6027 {
6028 list_T *l = list_alloc();
6029 if (l != NULL)
6030 status = dict_add_list(retdict, "items", l);
6031 else
6032 status = FAIL;
6033 }
6034 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006035 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006036 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006037 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006038 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006039 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006040 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006041 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006042 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006043 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006044 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006045 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006046 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006047 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006048 if ((status == OK) && IS_LL_STACK(qi) && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006049 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006050
6051 return status;
6052}
6053
6054/*
6055 * Return the quickfix list title as 'title' in retdict
6056 */
6057 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006058qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006059{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006060 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006061}
6062
6063/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006064 * Returns the identifier of the window used to display files from a location
6065 * list. If there is no associated window, then returns 0. Useful only when
6066 * called from a location list window.
6067 */
6068 static int
6069qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6070{
6071 int winid = 0;
6072
6073 if (wp != NULL && IS_LL_WINDOW(wp))
6074 {
6075 win_T *ll_wp = qf_find_win_with_loclist(qi);
6076 if (ll_wp != NULL)
6077 winid = ll_wp->w_id;
6078 }
6079
6080 return dict_add_number(retdict, "filewinid", winid);
6081}
6082
6083/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006084 * Return the quickfix list items/entries as 'items' in retdict
6085 */
6086 static int
6087qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6088{
6089 int status = OK;
6090 list_T *l = list_alloc();
6091 if (l != NULL)
6092 {
6093 (void)get_errorlist(qi, NULL, qf_idx, l);
6094 dict_add_list(retdict, "items", l);
6095 }
6096 else
6097 status = FAIL;
6098
6099 return status;
6100}
6101
6102/*
6103 * Return the quickfix list context (if any) as 'context' in retdict.
6104 */
6105 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006106qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006107{
6108 int status;
6109 dictitem_T *di;
6110
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006111 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006112 {
6113 di = dictitem_alloc((char_u *)"context");
6114 if (di != NULL)
6115 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006116 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006117 status = dict_add(retdict, di);
6118 if (status == FAIL)
6119 dictitem_free(di);
6120 }
6121 else
6122 status = FAIL;
6123 }
6124 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006125 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006126
6127 return status;
6128}
6129
6130/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006131 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006132 */
6133 static int
6134qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6135{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006136 int curidx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006137 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006138 // For empty lists, current index is set to 0
6139 curidx = 0;
6140 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006141}
6142
6143/*
6144 * Return quickfix/location list details (title) as a
6145 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6146 * then current list is used. Otherwise the specified list is used.
6147 */
6148 int
6149qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6150{
6151 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006152 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006153 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006154 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006155 dictitem_T *di;
6156 int flags = QF_GETLIST_NONE;
6157
6158 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6159 return qf_get_list_from_lines(what, di, retdict);
6160
6161 if (wp != NULL)
6162 qi = GET_LOC_LIST(wp);
6163
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006164 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006165
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006166 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006167 qf_idx = qf_getprop_qfidx(qi, what);
6168
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006169 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006170 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006171 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006172
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006173 qfl = &qi->qf_lists[qf_idx];
6174
Bram Moolenaard823fa92016-08-12 16:29:27 +02006175 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006176 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006177 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006178 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006179 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006180 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006181 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006182 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006183 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006184 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006185 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006186 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006187 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006188 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006189 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006190 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006191 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006192 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006193 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6194 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006195
Bram Moolenaard823fa92016-08-12 16:29:27 +02006196 return status;
6197}
6198
6199/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006200 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
6201 * items in the dict 'd'.
6202 */
6203 static int
6204qf_add_entry_from_dict(
6205 qf_info_T *qi,
6206 int qf_idx,
6207 dict_T *d,
6208 int first_entry)
6209{
6210 static int did_bufnr_emsg;
6211 char_u *filename, *module, *pattern, *text, *type;
6212 int bufnum, valid, status, col, vcol, nr;
6213 long lnum;
6214
6215 if (first_entry)
6216 did_bufnr_emsg = FALSE;
6217
6218 filename = get_dict_string(d, (char_u *)"filename", TRUE);
6219 module = get_dict_string(d, (char_u *)"module", TRUE);
6220 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6221 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6222 col = (int)get_dict_number(d, (char_u *)"col");
6223 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6224 nr = (int)get_dict_number(d, (char_u *)"nr");
6225 type = get_dict_string(d, (char_u *)"type", TRUE);
6226 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6227 text = get_dict_string(d, (char_u *)"text", TRUE);
6228 if (text == NULL)
6229 text = vim_strsave((char_u *)"");
6230
6231 valid = TRUE;
6232 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6233 valid = FALSE;
6234
6235 // Mark entries with non-existing buffer number as not valid. Give the
6236 // error message only once.
6237 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6238 {
6239 if (!did_bufnr_emsg)
6240 {
6241 did_bufnr_emsg = TRUE;
6242 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6243 }
6244 valid = FALSE;
6245 bufnum = 0;
6246 }
6247
6248 // If the 'valid' field is present it overrules the detected value.
6249 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6250 valid = (int)get_dict_number(d, (char_u *)"valid");
6251
6252 status = qf_add_entry(qi,
6253 qf_idx,
6254 NULL, // dir
6255 filename,
6256 module,
6257 bufnum,
6258 text,
6259 lnum,
6260 col,
6261 vcol, // vis_col
6262 pattern, // search pattern
6263 nr,
6264 type == NULL ? NUL : *type,
6265 valid);
6266
6267 vim_free(filename);
6268 vim_free(module);
6269 vim_free(pattern);
6270 vim_free(text);
6271 vim_free(type);
6272
6273 return status;
6274}
6275
6276/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006277 * Add list of entries to quickfix/location list. Each list entry is
6278 * a dictionary with item information.
6279 */
6280 static int
6281qf_add_entries(
6282 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006283 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006284 list_T *list,
6285 char_u *title,
6286 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006287{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006288 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006289 listitem_T *li;
6290 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006291 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006292 int retval = OK;
6293
Bram Moolenaara3921f42017-06-04 15:30:34 +02006294 if (action == ' ' || qf_idx == qi->qf_listcount)
6295 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006296 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006297 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006298 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006299 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006300 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006301 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006302 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006303 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006304 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006305 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006306 qf_free_items(qfl);
6307 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006308 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006309
6310 for (li = list->lv_first; li != NULL; li = li->li_next)
6311 {
6312 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006313 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006314
6315 d = li->li_tv.vval.v_dict;
6316 if (d == NULL)
6317 continue;
6318
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006319 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first);
6320 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006321 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006322 }
6323
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006324 if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006325 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006326 qfl->qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006327 else
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006328 qfl->qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006329 if (action != 'a')
6330 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006331 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006332 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006333 qfl->qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006334 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006335
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006336 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006337 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006338
6339 return retval;
6340}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006341
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006342/*
6343 * Get the quickfix list index from 'nr' or 'id'
6344 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006345 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006346qf_setprop_get_qfidx(
6347 qf_info_T *qi,
6348 dict_T *what,
6349 int action,
6350 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006351{
6352 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006353 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006354
Bram Moolenaard823fa92016-08-12 16:29:27 +02006355 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6356 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006357 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006358 if (di->di_tv.v_type == VAR_NUMBER)
6359 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006360 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006361 if (di->di_tv.vval.v_number != 0)
6362 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006363
Bram Moolenaar55b69262017-08-13 13:42:01 +02006364 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6365 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006366 // When creating a new list, accept qf_idx pointing to the next
6367 // non-available list and add the new list at the end of the
6368 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006369 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006370 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006371 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006372 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006373 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006374 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006375 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006376 }
6377 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006378 && di->di_tv.vval.v_string != NULL
6379 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006380 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006381 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006382 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006383 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006384 qf_idx = 0;
6385 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006386 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006387 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006388 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006389 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006390 }
6391
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006392 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006393 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006394 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006395 if (di->di_tv.v_type != VAR_NUMBER)
6396 return INVALID_QFIDX;
6397
6398 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006399 }
6400
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006401 return qf_idx;
6402}
6403
6404/*
6405 * Set the quickfix list title.
6406 */
6407 static int
6408qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6409{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006410 qf_list_T *qfl = &qi->qf_lists[qf_idx];
6411
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006412 if (di->di_tv.v_type != VAR_STRING)
6413 return FAIL;
6414
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006415 vim_free(qfl->qf_title);
6416 qfl->qf_title = get_dict_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006417 if (qf_idx == qi->qf_curlist)
6418 qf_update_win_titlevar(qi);
6419
6420 return OK;
6421}
6422
6423/*
6424 * Set quickfix list items/entries.
6425 */
6426 static int
6427qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6428{
6429 int retval = FAIL;
6430 char_u *title_save;
6431
6432 if (di->di_tv.v_type != VAR_LIST)
6433 return FAIL;
6434
6435 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6436 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6437 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006438 vim_free(title_save);
6439
6440 return retval;
6441}
6442
6443/*
6444 * Set quickfix list items/entries from a list of lines.
6445 */
6446 static int
6447qf_setprop_items_from_lines(
6448 qf_info_T *qi,
6449 int qf_idx,
6450 dict_T *what,
6451 dictitem_T *di,
6452 int action)
6453{
6454 char_u *errorformat = p_efm;
6455 dictitem_T *efm_di;
6456 int retval = FAIL;
6457
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006458 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006459 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6460 {
6461 if (efm_di->di_tv.v_type != VAR_STRING ||
6462 efm_di->di_tv.vval.v_string == NULL)
6463 return FAIL;
6464 errorformat = efm_di->di_tv.vval.v_string;
6465 }
6466
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006467 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006468 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6469 return FAIL;
6470
6471 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006472 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006473 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6474 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6475 retval = OK;
6476
6477 return retval;
6478}
6479
6480/*
6481 * Set quickfix list context.
6482 */
6483 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006484qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006485{
6486 typval_T *ctx;
6487
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006488 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006489 ctx = alloc_tv();
6490 if (ctx != NULL)
6491 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006492 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006493
6494 return OK;
6495}
6496
6497/*
6498 * Set quickfix/location list properties (title, items, context).
6499 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006500 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006501 */
6502 static int
6503qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6504{
6505 dictitem_T *di;
6506 int retval = FAIL;
6507 int qf_idx;
6508 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006509 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006510
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006511 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006512 newlist = TRUE;
6513
6514 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006515 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006516 return FAIL;
6517
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006518 if (newlist)
6519 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006520 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006521 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006522 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006523 }
6524
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006525 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaard823fa92016-08-12 16:29:27 +02006526 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006527 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006528 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006529 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006530 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006531 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006532 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006533 retval = qf_setprop_context(qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006534
Bram Moolenaarb254af32017-12-18 19:48:58 +01006535 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006536 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006537
Bram Moolenaard823fa92016-08-12 16:29:27 +02006538 return retval;
6539}
6540
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006541/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006542 * Find the non-location list window with the specified location list stack in
6543 * the current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006544 */
6545 static win_T *
6546find_win_with_ll(qf_info_T *qi)
6547{
6548 win_T *wp = NULL;
6549
6550 FOR_ALL_WINDOWS(wp)
6551 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6552 return wp;
6553
6554 return NULL;
6555}
6556
6557/*
6558 * Free the entire quickfix/location list stack.
6559 * If the quickfix/location list window is open, then clear it.
6560 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006561 static void
6562qf_free_stack(win_T *wp, qf_info_T *qi)
6563{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006564 win_T *qfwin = qf_find_win(qi);
6565 win_T *llwin = NULL;
6566 win_T *orig_wp = wp;
6567
6568 if (qfwin != NULL)
6569 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006570 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006571 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006572 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006573 qf_update_buffer(qi, NULL);
6574 }
6575
6576 if (wp != NULL && IS_LL_WINDOW(wp))
6577 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006578 // If in the location list window, then use the non-location list
6579 // window with this location list (if present)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006580 llwin = find_win_with_ll(qi);
6581 if (llwin != NULL)
6582 wp = llwin;
6583 }
6584
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006585 qf_free_all(wp);
6586 if (wp == NULL)
6587 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006588 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006589 qi->qf_curlist = 0;
6590 qi->qf_listcount = 0;
6591 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006592 else if (IS_LL_WINDOW(orig_wp))
6593 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006594 // If the location list window is open, then create a new empty
6595 // location list
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006596 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006597
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006598 // first free the list reference in the location list window
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006599 ll_free_all(&orig_wp->w_llist_ref);
6600
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006601 orig_wp->w_llist_ref = new_ll;
6602 if (llwin != NULL)
6603 {
6604 llwin->w_llist = new_ll;
6605 new_ll->qf_refcount++;
6606 }
6607 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006608}
6609
Bram Moolenaard823fa92016-08-12 16:29:27 +02006610/*
6611 * Populate the quickfix list with the items supplied in the list
6612 * of dictionaries. "title" will be copied to w:quickfix_title.
6613 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6614 */
6615 int
6616set_errorlist(
6617 win_T *wp,
6618 list_T *list,
6619 int action,
6620 char_u *title,
6621 dict_T *what)
6622{
6623 qf_info_T *qi = &ql_info;
6624 int retval = OK;
6625
6626 if (wp != NULL)
6627 {
6628 qi = ll_get_or_alloc_list(wp);
6629 if (qi == NULL)
6630 return FAIL;
6631 }
6632
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006633 if (action == 'f')
6634 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006635 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006636 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006637 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006638 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006639
6640 incr_quickfix_busy();
6641
6642 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006643 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006644 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006645 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006646 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006647 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006648 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006649 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006650
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006651 decr_quickfix_busy();
6652
Bram Moolenaard823fa92016-08-12 16:29:27 +02006653 return retval;
6654}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006655
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006656/*
6657 * Mark the context as in use for all the lists in a quickfix stack.
6658 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006659 static int
6660mark_quickfix_ctx(qf_info_T *qi, int copyID)
6661{
6662 int i;
6663 int abort = FALSE;
6664 typval_T *ctx;
6665
6666 for (i = 0; i < LISTCOUNT && !abort; ++i)
6667 {
6668 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006669 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6670 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006671 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6672 }
6673
6674 return abort;
6675}
6676
6677/*
6678 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006679 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006680 */
6681 int
6682set_ref_in_quickfix(int copyID)
6683{
6684 int abort = FALSE;
6685 tabpage_T *tp;
6686 win_T *win;
6687
6688 abort = mark_quickfix_ctx(&ql_info, copyID);
6689 if (abort)
6690 return abort;
6691
6692 FOR_ALL_TAB_WINDOWS(tp, win)
6693 {
6694 if (win->w_llist != NULL)
6695 {
6696 abort = mark_quickfix_ctx(win->w_llist, copyID);
6697 if (abort)
6698 return abort;
6699 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006700 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6701 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006702 // In a location list window and none of the other windows is
6703 // referring to this location list. Mark the location list
6704 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006705 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6706 if (abort)
6707 return abort;
6708 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006709 }
6710
6711 return abort;
6712}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006713#endif
6714
Bram Moolenaar81695252004-12-29 20:58:21 +00006715/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006716 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006717 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006718 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006719 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006720 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006721 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006722 */
6723 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006724ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006725{
6726 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006727 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006728 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006729 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006730 int_u save_qfid;
6731 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006732
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006733 switch (eap->cmdidx)
6734 {
6735 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6736 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6737 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6738 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6739 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6740 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6741 default: break;
6742 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006743 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6744 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006745 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006746#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006747 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006748 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006749#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006750 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006751
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006752 // Must come after autocommands.
Bram Moolenaar39665952018-08-15 20:59:48 +02006753 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006754 {
6755 qi = ll_get_or_alloc_list(curwin);
6756 if (qi == NULL)
6757 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006758 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006759 }
6760
Bram Moolenaar86b68352004-12-27 21:59:20 +00006761 if (*eap->arg == NUL)
6762 buf = curbuf;
6763 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6764 buf = buflist_findnr(atoi((char *)eap->arg));
6765 if (buf == NULL)
6766 EMSG(_(e_invarg));
6767 else if (buf->b_ml.ml_mfp == NULL)
6768 EMSG(_("E681: Buffer is not loaded"));
6769 else
6770 {
6771 if (eap->addr_count == 0)
6772 {
6773 eap->line1 = 1;
6774 eap->line2 = buf->b_ml.ml_line_count;
6775 }
6776 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6777 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6778 EMSG(_(e_invrange));
6779 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006780 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006781 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006782
6783 if (buf->b_sfname)
6784 {
6785 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6786 (char *)qf_title, (char *)buf->b_sfname);
6787 qf_title = IObuff;
6788 }
6789
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006790 incr_quickfix_busy();
6791
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006792 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006793 (eap->cmdidx != CMD_caddbuffer
6794 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006795 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006796 qf_title, NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006797 if (qf_stack_empty(qi))
6798 {
6799 decr_quickfix_busy();
6800 return;
6801 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006802 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006803 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006804
6805 // Remember the current quickfix list identifier, so that we can
6806 // check for autocommands changing the current quickfix list.
6807 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006808 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006809 {
6810 buf_T *curbuf_old = curbuf;
6811
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006812 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6813 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006814 if (curbuf != curbuf_old)
6815 // Autocommands changed buffer, don't jump now, "qi" may
6816 // be invalid.
6817 res = 0;
6818 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006819 // Jump to the first error for a new list and if autocmds didn't
6820 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006821 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006822 eap->cmdidx == CMD_lbuffer)
6823 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006824 // display the first error
6825 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006826
6827 decr_quickfix_busy();
Bram Moolenaar754b5602006-02-09 23:53:20 +00006828 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006829 }
6830}
6831
Bram Moolenaar1e015462005-09-25 22:16:38 +00006832#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006833/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006834 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6835 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006836 */
6837 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006838ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006839{
6840 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006841 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006842 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006843 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006844 int_u save_qfid;
6845 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006846
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006847 switch (eap->cmdidx)
6848 {
6849 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6850 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6851 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6852 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6853 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6854 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6855 default: break;
6856 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006857 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6858 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006859 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006860#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006861 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006862 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006863#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006864 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006865
Bram Moolenaar39665952018-08-15 20:59:48 +02006866 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006867 {
6868 qi = ll_get_or_alloc_list(curwin);
6869 if (qi == NULL)
6870 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006871 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006872 }
6873
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006874 // Evaluate the expression. When the result is a string or a list we can
6875 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006876 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006877 if (tv != NULL)
6878 {
6879 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6880 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6881 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006882 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006883 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006884 (eap->cmdidx != CMD_caddexpr
6885 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006886 (linenr_T)0, (linenr_T)0,
6887 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006888 if (qf_stack_empty(qi))
6889 {
6890 decr_quickfix_busy();
6891 goto cleanup;
6892 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006893 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006894 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006895
6896 // Remember the current quickfix list identifier, so that we can
6897 // check for autocommands changing the current quickfix list.
6898 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006899 if (au_name != NULL)
6900 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6901 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006902
6903 // Jump to the first error for a new list and if autocmds didn't
6904 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006905 if (res > 0 && (eap->cmdidx == CMD_cexpr
6906 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006907 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006908 // display the first error
6909 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006910 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006911 }
6912 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006913 EMSG(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006914cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00006915 free_tv(tv);
6916 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006917}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006918#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006919
6920/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006921 * Get the location list for ":lhelpgrep"
6922 */
6923 static qf_info_T *
6924hgr_get_ll(int *new_ll)
6925{
6926 win_T *wp;
6927 qf_info_T *qi;
6928
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006929 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006930 if (bt_help(curwin->w_buffer))
6931 wp = curwin;
6932 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006933 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006934 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006935
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006936 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006937 qi = NULL;
6938 else
6939 qi = wp->w_llist;
6940
6941 if (qi == NULL)
6942 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006943 // Allocate a new location list for help text matches
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006944 if ((qi = ll_new_list()) == NULL)
6945 return NULL;
6946 *new_ll = TRUE;
6947 }
6948
6949 return qi;
6950}
6951
6952/*
6953 * Search for a pattern in a help file.
6954 */
6955 static void
6956hgr_search_file(
6957 qf_info_T *qi,
6958 char_u *fname,
6959#ifdef FEAT_MBYTE
6960 vimconv_T *p_vc,
6961#endif
6962 regmatch_T *p_regmatch)
6963{
6964 FILE *fd;
6965 long lnum;
6966
6967 fd = mch_fopen((char *)fname, "r");
6968 if (fd == NULL)
6969 return;
6970
6971 lnum = 1;
6972 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6973 {
6974 char_u *line = IObuff;
6975#ifdef FEAT_MBYTE
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006976 // Convert a line if 'encoding' is not utf-8 and
6977 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006978 if (p_vc->vc_type != CONV_NONE
6979 && has_non_ascii(IObuff))
6980 {
6981 line = string_convert(p_vc, IObuff, NULL);
6982 if (line == NULL)
6983 line = IObuff;
6984 }
6985#endif
6986
6987 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6988 {
6989 int l = (int)STRLEN(line);
6990
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006991 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006992 while (l > 0 && line[l - 1] <= ' ')
6993 line[--l] = NUL;
6994
6995 if (qf_add_entry(qi,
6996 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006997 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006998 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006999 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007000 0,
7001 line,
7002 lnum,
7003 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007004 + 1, // col
7005 FALSE, // vis_col
7006 NULL, // search pattern
7007 0, // nr
7008 1, // type
7009 TRUE // valid
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007010 ) == FAIL)
7011 {
7012 got_int = TRUE;
7013#ifdef FEAT_MBYTE
7014 if (line != IObuff)
7015 vim_free(line);
7016#endif
7017 break;
7018 }
7019 }
7020#ifdef FEAT_MBYTE
7021 if (line != IObuff)
7022 vim_free(line);
7023#endif
7024 ++lnum;
7025 line_breakcheck();
7026 }
7027 fclose(fd);
7028}
7029
7030/*
7031 * Search for a pattern in all the help files in the doc directory under
7032 * the given directory.
7033 */
7034 static void
7035hgr_search_files_in_dir(
7036 qf_info_T *qi,
7037 char_u *dirname,
7038 regmatch_T *p_regmatch
7039#ifdef FEAT_MBYTE
7040 , vimconv_T *p_vc
7041#endif
7042#ifdef FEAT_MULTI_LANG
7043 , char_u *lang
7044#endif
7045 )
7046{
7047 int fcount;
7048 char_u **fnames;
7049 int fi;
7050
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007051 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007052 add_pathsep(dirname);
7053 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7054 if (gen_expand_wildcards(1, &dirname, &fcount,
7055 &fnames, EW_FILE|EW_SILENT) == OK
7056 && fcount > 0)
7057 {
7058 for (fi = 0; fi < fcount && !got_int; ++fi)
7059 {
7060#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007061 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007062 if (lang != NULL
7063 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007064 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007065 && !(STRNICMP(lang, "en", 2) == 0
7066 && STRNICMP("txt", fnames[fi]
7067 + STRLEN(fnames[fi]) - 3, 3) == 0))
7068 continue;
7069#endif
7070
7071 hgr_search_file(qi, fnames[fi],
7072#ifdef FEAT_MBYTE
7073 p_vc,
7074#endif
7075 p_regmatch);
7076 }
7077 FreeWild(fcount, fnames);
7078 }
7079}
7080
7081/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007082 * Search for a pattern in all the help files in the 'runtimepath'
7083 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007084 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007085 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007086 */
7087 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007088hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007089{
7090 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007091
7092#ifdef FEAT_MBYTE
7093 vimconv_T vc;
7094
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007095 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7096 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007097 vc.vc_type = CONV_NONE;
7098 if (!enc_utf8)
7099 convert_setup(&vc, (char_u *)"utf-8", p_enc);
7100#endif
7101
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007102 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007103 p = p_rtp;
7104 while (*p != NUL && !got_int)
7105 {
7106 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7107
7108 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
7109#ifdef FEAT_MBYTE
7110 , &vc
7111#endif
7112#ifdef FEAT_MULTI_LANG
7113 , lang
7114#endif
7115 );
7116 }
7117
7118#ifdef FEAT_MBYTE
7119 if (vc.vc_type != CONV_NONE)
7120 convert_setup(&vc, NULL, NULL);
7121#endif
7122}
7123
7124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 * ":helpgrep {pattern}"
7126 */
7127 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007128ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129{
7130 regmatch_T regmatch;
7131 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007132 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007133 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007134 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007135 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136
Bram Moolenaar73633f82012-01-20 13:39:07 +01007137 switch (eap->cmdidx)
7138 {
7139 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7140 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7141 default: break;
7142 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007143 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7144 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007145 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007146#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007147 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007148 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007149#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007150 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007151
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007152 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007153 save_cpo = p_cpo;
7154 p_cpo = empty_option;
7155
Bram Moolenaar39665952018-08-15 20:59:48 +02007156 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007157 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007158 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007159 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007160 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007161 }
7162
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007163 incr_quickfix_busy();
7164
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007165#ifdef FEAT_MULTI_LANG
7166 // Check for a specified language
7167 lang = check_help_lang(eap->arg);
7168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7170 regmatch.rm_ic = FALSE;
7171 if (regmatch.regprog != NULL)
7172 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007173 qf_list_T *qfl;
7174
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007175 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007176 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007178 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007179
Bram Moolenaar473de612013-06-08 18:19:48 +02007180 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007182 qfl = &qi->qf_lists[qi->qf_curlist];
7183 qfl->qf_nonevalid = FALSE;
7184 qfl->qf_ptr = qfl->qf_start;
7185 qfl->qf_index = 1;
7186 qf_list_changed(qfl);
7187 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 }
7189
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007190 if (p_cpo == empty_option)
7191 p_cpo = save_cpo;
7192 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007193 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007194 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195
Bram Moolenaar73633f82012-01-20 13:39:07 +01007196 if (au_name != NULL)
7197 {
7198 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7199 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007200 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007201 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007202 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007203 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007204 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007205 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007206 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007207
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007208 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007209 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007210 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007211 else
7212 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007213
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007214 decr_quickfix_busy();
7215
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007216 if (eap->cmdidx == CMD_lhelpgrep)
7217 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007218 // If the help window is not opened or if it already points to the
7219 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007220 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007221 {
7222 if (new_qi)
7223 ll_free_all(&qi);
7224 }
7225 else if (curwin->w_llist == NULL)
7226 curwin->w_llist = qi;
7227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228}
7229
7230#endif /* FEAT_QUICKFIX */